Browse Source

Add reference counting to register/unregister procedure.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
08e286714f
1 changed files with 32 additions and 0 deletions
  1. 32
    0
      src/core/image.c

+ 32
- 0
src/core/image.c View File

23
 #include <errno.h>
23
 #include <errno.h>
24
 #include <assert.h>
24
 #include <assert.h>
25
 #include <gpxe/list.h>
25
 #include <gpxe/list.h>
26
+#include <gpxe/umalloc.h>
26
 #include <gpxe/image.h>
27
 #include <gpxe/image.h>
27
 
28
 
28
 /** @file
29
 /** @file
40
 static struct image_type image_types_end[0]
41
 static struct image_type image_types_end[0]
41
 	__table_end ( struct image_type, image_types );
42
 	__table_end ( struct image_type, image_types );
42
 
43
 
44
+/**
45
+ * Free executable/loadable image
46
+ *
47
+ * @v refcnt		Reference counter
48
+ */
49
+static void free_image ( struct refcnt *refcnt ) {
50
+	struct image *image = container_of ( refcnt, struct image, refcnt );
51
+
52
+	ufree ( image->data );
53
+	free ( image );
54
+	DBGC ( image, "IMAGE %p freed\n", image );
55
+}
56
+
57
+/**
58
+ * Allocate executable/loadable image
59
+ *
60
+ * @ret image		Executable/loadable image
61
+ */
62
+struct image * alloc_image ( void ) {
63
+	struct image *image;
64
+
65
+	image = malloc ( sizeof ( *image ) );
66
+	if ( image ) {
67
+		memset ( image, 0, sizeof ( *image ) );
68
+		image->refcnt.free = free_image;
69
+	}
70
+	return image;
71
+}
72
+
43
 /**
73
 /**
44
  * Register executable/loadable image
74
  * Register executable/loadable image
45
  *
75
  *
56
 	}
86
 	}
57
 
87
 
58
 	/* Add to image list */
88
 	/* Add to image list */
89
+	image_get ( image );
59
 	list_add_tail ( &image->list, &images );
90
 	list_add_tail ( &image->list, &images );
60
 	DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
91
 	DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
61
 	       image, user_to_phys ( image->data, 0 ),
92
 	       image, user_to_phys ( image->data, 0 ),
71
  */
102
  */
72
 void unregister_image ( struct image *image ) {
103
 void unregister_image ( struct image *image ) {
73
 	list_del ( &image->list );
104
 	list_del ( &image->list );
105
+	image_put ( image );
74
 	DBGC ( image, "IMAGE %p unregistered\n", image );
106
 	DBGC ( image, "IMAGE %p unregistered\n", image );
75
 }
107
 }
76
 
108
 

Loading…
Cancel
Save