]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
Let ifmgmt.c take care of calling efree(), since it's the once which
authorMichael Brown <mcb30@etherboot.org>
Fri, 12 Jan 2007 09:46:10 +0000 (09:46 +0000)
committerMichael Brown <mcb30@etherboot.org>
Fri, 12 Jan 2007 09:46:10 +0000 (09:46 +0000)
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.

src/core/image.c
src/include/gpxe/image.h
src/usr/imgmgmt.c

index be0eecf98e78b76264284b4b9c594fa02884d127..6f54be115d62943fb7ade1fed7d5806b3667ebdb 100644 (file)
@@ -23,6 +23,7 @@
 #include <assert.h>
 #include <vsprintf.h>
 #include <gpxe/list.h>
+#include <gpxe/emalloc.h>
 #include <gpxe/image.h>
 
 /** @file
@@ -72,6 +73,20 @@ void unregister_image ( struct image *image ) {
        DBGC ( image, "IMAGE %p unregistered\n", image );
 }
 
+/**
+ * Move image to start of list of registered images
+ *
+ * @v image            Executable/loadable image
+ *
+ * Move the image to the start of the image list.  This makes it
+ * easier to keep track of which of the images marked as loaded is
+ * likely to still be valid.
+ */
+void promote_image ( struct image *image ) {
+       list_del ( &image->list );
+       list_add ( &image->list, &images );
+}
+
 /**
  * Find image by name
  *
@@ -90,18 +105,24 @@ struct image * find_image ( const char *name ) {
 }
 
 /**
- * Free loaded image
+ * Load executable/loadable image into memory
  *
  * @v image            Executable/loadable image
- *
- * This releases the memory being used to store the image; it does not
- * release the @c struct @c image itself, nor does it unregister the
- * image.
+ * @v type             Executable/loadable image type
+ * @ret rc             Return status code
  */
-void free_image ( struct image *image ) {
-       efree ( image->data );
-       image->data = UNULL;
-       image->len = 0;
+static int image_load_type ( struct image *image, struct image_type *type ) {
+       int rc;
+
+       if ( ( rc = type->load ( image ) ) != 0 ) {
+               DBGC ( image, "IMAGE %p could not load as %s: %s\n",
+                      image, type->name, strerror ( rc ) );
+               return rc;
+       }
+
+       /* Flag as loaded */
+       image->flags |= IMAGE_LOADED;
+       return 0;
 }
 
 /**
@@ -111,18 +132,10 @@ void free_image ( struct image *image ) {
  * @ret rc             Return status code
  */
 int image_load ( struct image *image ) {
-       int rc;
 
        assert ( image->type != NULL );
 
-       if ( ( rc = image->type->load ( image ) ) != 0 ) {
-               DBGC ( image, "IMAGE %p could not load: %s\n",
-                      image, strerror ( rc ) );
-               return rc;
-       }
-
-       image->flags |= IMAGE_LOADED;
-       return 0;
+       return image_load_type ( image, image->type );
 }
 
 /**
@@ -137,16 +150,10 @@ int image_autoload ( struct image *image ) {
 
        for ( type = image_types ; type < image_types_end ; type++ ) {
                DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
-               rc = type->load ( image );
+               rc = image_load_type ( image, type );
                if ( image->type == NULL )
                        continue;
-               if ( rc != 0 ) {
-                       DBGC ( image, "IMAGE %p (%s) could not load: %s\n",
-                              image, image->type->name, strerror ( rc ) );
-                       return rc;
-               }
-               image->flags |= IMAGE_LOADED;
-               return 0;
+               return rc;
        }
 
        DBGC ( image, "IMAGE %p format not recognised\n", image );
index e00771fffa66db50567c9f78610792cd31355a64..de10c369e6deaab363d0e0deccba437fe41a23fe 100644 (file)
@@ -107,8 +107,8 @@ extern struct list_head images;
 
 extern int register_image ( struct image *image );
 extern void unregister_image ( struct image *image );
+extern void promote_image ( struct image *image );
 struct image * find_image ( const char *name );
-extern void free_image ( struct image *image );
 extern int image_load ( struct image *image );
 extern int image_autoload ( struct image *image );
 extern int image_exec ( struct image *image );
index 6a2599d533c83e77f1697faa6027d382a54eef3b..eb2330a33138ebac59082868d9254f07bcab0cf4 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <vsprintf.h>
 #include <gpxe/image.h>
+#include <gpxe/emalloc.h>
 #include <usr/fetch.h>
 #include <usr/imgmgmt.h>
 
@@ -65,7 +66,7 @@ int imgfetch ( const char *filename, const char *name,
        return 0;
 
  err:
-       free_image ( image );
+       efree ( image->data );
        free ( image );
        return rc;
 }
@@ -77,7 +78,16 @@ int imgfetch ( const char *filename, const char *name,
  * @ret rc             Return status code
  */
 int imgload ( struct image *image ) {
-       return image_autoload ( image );
+       int rc;
+
+       /* Try to load image */
+       if ( ( rc = image_autoload ( image ) ) != 0 )
+               return rc;
+
+       /* If we succeed, move the image to the start of the list */
+       promote_image ( image );
+
+       return 0;
 }
 
 /**
@@ -129,6 +139,6 @@ void imgstat ( struct image *image ) {
  */
 void imgfree ( struct image *image ) {
        unregister_image ( image );
-       free_image ( image );
+       efree ( image->data );
        free ( image );
 }