Browse Source

[params] Use reference counters for form parameter lists

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
c7b69ac793
4 changed files with 76 additions and 49 deletions
  1. 28
    40
      src/core/params.c
  2. 5
    5
      src/core/uri.c
  3. 4
    2
      src/hci/commands/param_cmd.c
  4. 39
    2
      src/include/ipxe/params.h

+ 28
- 40
src/core/params.c View File

@@ -32,6 +32,29 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
 /** List of all parameter lists */
33 33
 static LIST_HEAD ( parameters );
34 34
 
35
+/**
36
+ * Free form parameter list
37
+ *
38
+ * @v refcnt		Reference count
39
+ */
40
+static void free_parameters ( struct refcnt *refcnt ) {
41
+	struct parameters *params =
42
+		container_of ( refcnt, struct parameters, refcnt );
43
+	struct parameter *param;
44
+	struct parameter *tmp;
45
+
46
+	DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
47
+
48
+	/* Free all parameters */
49
+	list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
50
+		list_del ( &param->list );
51
+		free ( param );
52
+	}
53
+
54
+	/* Free parameter list */
55
+	free ( params );
56
+}
57
+
35 58
 /**
36 59
  * Find form parameter list by name
37 60
  *
@@ -63,14 +86,17 @@ struct parameters * create_parameters ( const char *name ) {
63 86
 
64 87
 	/* Destroy any existing parameter list of this name */
65 88
 	params = find_parameters ( name );
66
-	if ( params )
67
-		destroy_parameters ( params );
89
+	if ( params ) {
90
+		claim_parameters ( params );
91
+		params_put ( params );
92
+	}
68 93
 
69 94
 	/* Allocate parameter list */
70 95
 	name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
71 96
 	params = zalloc ( sizeof ( *params ) + name_len );
72 97
 	if ( ! params )
73 98
 		return NULL;
99
+	ref_init ( &params->refcnt, free_parameters );
74 100
 	name_copy = ( ( void * ) ( params + 1 ) );
75 101
 
76 102
 	/* Populate parameter list */
@@ -125,41 +151,3 @@ struct parameter * add_parameter ( struct parameters *params,
125 151
 	       params->name, param->key, param->value );
126 152
 	return param;
127 153
 }
128
-
129
-/**
130
- * Destroy form parameter list
131
- *
132
- * @v params		Parameter list
133
- */
134
-void destroy_parameters ( struct parameters *params ) {
135
-	struct parameter *param;
136
-	struct parameter *tmp;
137
-
138
-	DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
139
-
140
-	/* Free all parameters */
141
-	list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
142
-		list_del ( &param->list );
143
-		free ( param );
144
-	}
145
-
146
-	/* Free parameter list */
147
-	list_del ( &params->list );
148
-	free ( params );
149
-}
150
-
151
-/**
152
- * Claim ownership of form parameter list
153
- *
154
- * @v params		Parameter list
155
- */
156
-void claim_parameters ( struct parameters *params ) {
157
-
158
-	DBGC ( params, "PARAMS \"%s\" claimed\n", params->name );
159
-
160
-	/* Remove from list of parameter lists */
161
-	list_del ( &params->list );
162
-
163
-	/* Reinitialise list to allow for subsequent destroy_parameters() */
164
-	INIT_LIST_HEAD ( &params->list );
165
-}

+ 5
- 5
src/core/uri.c View File

@@ -72,8 +72,7 @@ static void dump_uri ( struct uri *uri ) {
72 72
 static void free_uri ( struct refcnt *refcnt ) {
73 73
 	struct uri *uri = container_of ( refcnt, struct uri, refcnt );
74 74
 
75
-	if ( uri->params )
76
-		destroy_parameters ( uri->params );
75
+	params_put ( uri->params );
77 76
 	free ( uri );
78 77
 }
79 78
 
@@ -89,6 +88,7 @@ static void free_uri ( struct refcnt *refcnt ) {
89 88
  */
90 89
 struct uri * parse_uri ( const char *uri_string ) {
91 90
 	struct uri *uri;
91
+	struct parameters *params;
92 92
 	char *raw;
93 93
 	char *tmp;
94 94
 	char *path;
@@ -111,9 +111,9 @@ struct uri * parse_uri ( const char *uri_string ) {
111 111
 	if ( ( tmp = strstr ( raw, "##params" ) ) ) {
112 112
 		*tmp = '\0';
113 113
 		tmp += 8 /* "##params" */;
114
-		uri->params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
115
-		if ( uri->params ) {
116
-			claim_parameters ( uri->params );
114
+		params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
115
+		if ( params ) {
116
+			uri->params = claim_parameters ( params );
117 117
 		} else {
118 118
 			/* Ignore non-existent submission blocks */
119 119
 		}

+ 4
- 2
src/hci/commands/param_cmd.c View File

@@ -74,8 +74,10 @@ static int params_exec ( int argc, char **argv ) {
74 74
 		return -ENOMEM;
75 75
 
76 76
 	/* Destroy parameter list, if applicable */
77
-	if ( opts.delete )
78
-		destroy_parameters ( params );
77
+	if ( opts.delete ) {
78
+		claim_parameters ( params );
79
+		params_put ( params );
80
+	}
79 81
 
80 82
 	return 0;
81 83
 }

+ 39
- 2
src/include/ipxe/params.h View File

@@ -14,6 +14,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
14 14
 
15 15
 /** A form parameter list */
16 16
 struct parameters {
17
+	/** Reference count */
18
+	struct refcnt refcnt;
17 19
 	/** List of all parameter lists */
18 20
 	struct list_head list;
19 21
 	/** Name */
@@ -32,6 +34,43 @@ struct parameter {
32 34
 	const char *value;
33 35
 };
34 36
 
37
+/**
38
+ * Increment form parameter list reference count
39
+ *
40
+ * @v params		Parameter list, or NULL
41
+ * @ret params		Parameter list as passed in
42
+ */
43
+static inline __attribute__ (( always_inline )) struct parameters *
44
+params_get ( struct parameters *params ) {
45
+	ref_get ( &params->refcnt );
46
+	return params;
47
+}
48
+
49
+/**
50
+ * Decrement form parameter list reference count
51
+ *
52
+ * @v params		Parameter list, or NULL
53
+ */
54
+static inline __attribute__ (( always_inline )) void
55
+params_put ( struct parameters *params ) {
56
+	ref_put ( &params->refcnt );
57
+}
58
+
59
+/**
60
+ * Claim ownership of form parameter list
61
+ *
62
+ * @v params		Parameter list
63
+ * @ret params		Parameter list
64
+ */
65
+static inline __attribute__ (( always_inline )) struct parameters *
66
+claim_parameters ( struct parameters *params ) {
67
+
68
+	/* Remove from list of parameter lists */
69
+	list_del ( &params->list );
70
+
71
+	return params;
72
+}
73
+
35 74
 /** Iterate over all form parameters in a list */
36 75
 #define for_each_param( param, params )				\
37 76
 	list_for_each_entry ( (param), &(params)->entries, list )
@@ -40,7 +79,5 @@ extern struct parameters * find_parameters ( const char *name );
40 79
 extern struct parameters * create_parameters ( const char *name );
41 80
 extern struct parameter * add_parameter ( struct parameters *params,
42 81
 					  const char *key, const char *value );
43
-extern void destroy_parameters ( struct parameters *params );
44
-extern void claim_parameters ( struct parameters *params );
45 82
 
46 83
 #endif /* _IPXE_PARAMS_H */

Loading…
Cancel
Save