Sfoglia il codice sorgente

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 anni fa
parent
commit
e2c0055e23
3 ha cambiato i file con 47 aggiunte e 30 eliminazioni
  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 Vedi File

@@ -23,6 +23,7 @@
23 23
 #include <assert.h>
24 24
 #include <vsprintf.h>
25 25
 #include <gpxe/list.h>
26
+#include <gpxe/emalloc.h>
26 27
 #include <gpxe/image.h>
27 28
 
28 29
 /** @file
@@ -72,6 +73,20 @@ void unregister_image ( struct image *image ) {
72 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 91
  * Find image by name
77 92
  *
@@ -90,18 +105,24 @@ struct image * find_image ( const char *name ) {
90 105
 }
91 106
 
92 107
 /**
93
- * Free loaded image
108
+ * Load executable/loadable image into memory
94 109
  *
95 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,18 +132,10 @@ void free_image ( struct image *image ) {
111 132
  * @ret rc		Return status code
112 133
  */
113 134
 int image_load ( struct image *image ) {
114
-	int rc;
115 135
 
116 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,16 +150,10 @@ int image_autoload ( struct image *image ) {
137 150
 
138 151
 	for ( type = image_types ; type < image_types_end ; type++ ) {
139 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 154
 		if ( image->type == NULL )
142 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 159
 	DBGC ( image, "IMAGE %p format not recognised\n", image );

+ 1
- 1
src/include/gpxe/image.h Vedi File

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

+ 13
- 3
src/usr/imgmgmt.c Vedi File

@@ -21,6 +21,7 @@
21 21
 #include <errno.h>
22 22
 #include <vsprintf.h>
23 23
 #include <gpxe/image.h>
24
+#include <gpxe/emalloc.h>
24 25
 #include <usr/fetch.h>
25 26
 #include <usr/imgmgmt.h>
26 27
 
@@ -65,7 +66,7 @@ int imgfetch ( const char *filename, const char *name,
65 66
 	return 0;
66 67
 
67 68
  err:
68
-	free_image ( image );
69
+	efree ( image->data );
69 70
 	free ( image );
70 71
 	return rc;
71 72
 }
@@ -77,7 +78,16 @@ int imgfetch ( const char *filename, const char *name,
77 78
  * @ret rc		Return status code
78 79
  */
79 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,6 +139,6 @@ void imgstat ( struct image *image ) {
129 139
  */
130 140
 void imgfree ( struct image *image ) {
131 141
 	unregister_image ( image );
132
-	free_image ( image );
142
+	efree ( image->data );
133 143
 	free ( image );
134 144
 }

Loading…
Annulla
Salva