浏览代码

Let ifmgmt.c take care of calling efree(), since it's the once which

took out the contract to eventually call efree() when it called fetch().

Maintain the most recently loaded image at the start of the list, so that
imgautoselect() will pick it.
tags/v0.9.3
Michael Brown 18 年前
父节点
当前提交
e2c0055e23
共有 3 个文件被更改,包括 47 次插入30 次删除
  1. 33
    26
      src/core/image.c
  2. 1
    1
      src/include/gpxe/image.h
  3. 13
    3
      src/usr/imgmgmt.c

+ 33
- 26
src/core/image.c 查看文件

23
 #include <assert.h>
23
 #include <assert.h>
24
 #include <vsprintf.h>
24
 #include <vsprintf.h>
25
 #include <gpxe/list.h>
25
 #include <gpxe/list.h>
26
+#include <gpxe/emalloc.h>
26
 #include <gpxe/image.h>
27
 #include <gpxe/image.h>
27
 
28
 
28
 /** @file
29
 /** @file
72
 	DBGC ( image, "IMAGE %p unregistered\n", image );
73
 	DBGC ( image, "IMAGE %p unregistered\n", image );
73
 }
74
 }
74
 
75
 
76
+/**
77
+ * Move image to start of list of registered images
78
+ *
79
+ * @v image		Executable/loadable image
80
+ *
81
+ * Move the image to the start of the image list.  This makes it
82
+ * easier to keep track of which of the images marked as loaded is
83
+ * likely to still be valid.
84
+ */
85
+void promote_image ( struct image *image ) {
86
+	list_del ( &image->list );
87
+	list_add ( &image->list, &images );
88
+}
89
+
75
 /**
90
 /**
76
  * Find image by name
91
  * Find image by name
77
  *
92
  *
90
 }
105
 }
91
 
106
 
92
 /**
107
 /**
93
- * Free loaded image
108
+ * Load executable/loadable image into memory
94
  *
109
  *
95
  * @v image		Executable/loadable image
110
  * @v image		Executable/loadable image
96
- *
97
- * This releases the memory being used to store the image; it does not
98
- * release the @c struct @c image itself, nor does it unregister the
99
- * image.
111
+ * @v type		Executable/loadable image type
112
+ * @ret rc		Return status code
100
  */
113
  */
101
-void free_image ( struct image *image ) {
102
-	efree ( image->data );
103
-	image->data = UNULL;
104
-	image->len = 0;
114
+static int image_load_type ( struct image *image, struct image_type *type ) {
115
+	int rc;
116
+
117
+	if ( ( rc = type->load ( image ) ) != 0 ) {
118
+		DBGC ( image, "IMAGE %p could not load as %s: %s\n",
119
+		       image, type->name, strerror ( rc ) );
120
+		return rc;
121
+	}
122
+
123
+	/* Flag as loaded */
124
+	image->flags |= IMAGE_LOADED;
125
+	return 0;
105
 }
126
 }
106
 
127
 
107
 /**
128
 /**
111
  * @ret rc		Return status code
132
  * @ret rc		Return status code
112
  */
133
  */
113
 int image_load ( struct image *image ) {
134
 int image_load ( struct image *image ) {
114
-	int rc;
115
 
135
 
116
 	assert ( image->type != NULL );
136
 	assert ( image->type != NULL );
117
 
137
 
118
-	if ( ( rc = image->type->load ( image ) ) != 0 ) {
119
-		DBGC ( image, "IMAGE %p could not load: %s\n",
120
-		       image, strerror ( rc ) );
121
-		return rc;
122
-	}
123
-
124
-	image->flags |= IMAGE_LOADED;
125
-	return 0;
138
+	return image_load_type ( image, image->type );
126
 }
139
 }
127
 
140
 
128
 /**
141
 /**
137
 
150
 
138
 	for ( type = image_types ; type < image_types_end ; type++ ) {
151
 	for ( type = image_types ; type < image_types_end ; type++ ) {
139
 		DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
152
 		DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
140
-		rc = type->load ( image );
153
+		rc = image_load_type ( image, type );
141
 		if ( image->type == NULL )
154
 		if ( image->type == NULL )
142
 			continue;
155
 			continue;
143
-		if ( rc != 0 ) {
144
-			DBGC ( image, "IMAGE %p (%s) could not load: %s\n",
145
-			       image, image->type->name, strerror ( rc ) );
146
-			return rc;
147
-		}
148
-		image->flags |= IMAGE_LOADED;
149
-		return 0;
156
+		return rc;
150
 	}
157
 	}
151
 
158
 
152
 	DBGC ( image, "IMAGE %p format not recognised\n", image );
159
 	DBGC ( image, "IMAGE %p format not recognised\n", image );

+ 1
- 1
src/include/gpxe/image.h 查看文件

107
 
107
 
108
 extern int register_image ( struct image *image );
108
 extern int register_image ( struct image *image );
109
 extern void unregister_image ( struct image *image );
109
 extern void unregister_image ( struct image *image );
110
+extern void promote_image ( struct image *image );
110
 struct image * find_image ( const char *name );
111
 struct image * find_image ( const char *name );
111
-extern void free_image ( struct image *image );
112
 extern int image_load ( struct image *image );
112
 extern int image_load ( struct image *image );
113
 extern int image_autoload ( struct image *image );
113
 extern int image_autoload ( struct image *image );
114
 extern int image_exec ( struct image *image );
114
 extern int image_exec ( struct image *image );

+ 13
- 3
src/usr/imgmgmt.c 查看文件

21
 #include <errno.h>
21
 #include <errno.h>
22
 #include <vsprintf.h>
22
 #include <vsprintf.h>
23
 #include <gpxe/image.h>
23
 #include <gpxe/image.h>
24
+#include <gpxe/emalloc.h>
24
 #include <usr/fetch.h>
25
 #include <usr/fetch.h>
25
 #include <usr/imgmgmt.h>
26
 #include <usr/imgmgmt.h>
26
 
27
 
65
 	return 0;
66
 	return 0;
66
 
67
 
67
  err:
68
  err:
68
-	free_image ( image );
69
+	efree ( image->data );
69
 	free ( image );
70
 	free ( image );
70
 	return rc;
71
 	return rc;
71
 }
72
 }
77
  * @ret rc		Return status code
78
  * @ret rc		Return status code
78
  */
79
  */
79
 int imgload ( struct image *image ) {
80
 int imgload ( struct image *image ) {
80
-	return image_autoload ( image );
81
+	int rc;
82
+
83
+	/* Try to load image */
84
+	if ( ( rc = image_autoload ( image ) ) != 0 )
85
+		return rc;
86
+
87
+	/* If we succeed, move the image to the start of the list */
88
+	promote_image ( image );
89
+
90
+	return 0;
81
 }
91
 }
82
 
92
 
83
 /**
93
 /**
129
  */
139
  */
130
 void imgfree ( struct image *image ) {
140
 void imgfree ( struct image *image ) {
131
 	unregister_image ( image );
141
 	unregister_image ( image );
132
-	free_image ( image );
142
+	efree ( image->data );
133
 	free ( image );
143
 	free ( image );
134
 }
144
 }

正在加载...
取消
保存