summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2014-02-25 15:03:40 +0100
committerMichael Brown2014-02-27 00:34:07 +0100
commitc7b69ac793b7704c09560eb6850e4ec2bd66e575 (patch)
tree26a3a7bc1f4195ee5c39bf098bc44be43c3c5bf6
parent[settings] Remove "uristring" setting type (diff)
downloadipxe-c7b69ac793b7704c09560eb6850e4ec2bd66e575.tar.gz
ipxe-c7b69ac793b7704c09560eb6850e4ec2bd66e575.tar.xz
ipxe-c7b69ac793b7704c09560eb6850e4ec2bd66e575.zip
[params] Use reference counters for form parameter lists
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/core/params.c68
-rw-r--r--src/core/uri.c10
-rw-r--r--src/hci/commands/param_cmd.c6
-rw-r--r--src/include/ipxe/params.h41
4 files changed, 76 insertions, 49 deletions
diff --git a/src/core/params.c b/src/core/params.c
index 21361c1f..93b83441 100644
--- a/src/core/params.c
+++ b/src/core/params.c
@@ -33,6 +33,29 @@ FILE_LICENCE ( GPL2_OR_LATER );
static LIST_HEAD ( parameters );
/**
+ * Free form parameter list
+ *
+ * @v refcnt Reference count
+ */
+static void free_parameters ( struct refcnt *refcnt ) {
+ struct parameters *params =
+ container_of ( refcnt, struct parameters, refcnt );
+ struct parameter *param;
+ struct parameter *tmp;
+
+ DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
+
+ /* Free all parameters */
+ list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
+ list_del ( &param->list );
+ free ( param );
+ }
+
+ /* Free parameter list */
+ free ( params );
+}
+
+/**
* Find form parameter list by name
*
* @v name Parameter list name (may be NULL)
@@ -63,14 +86,17 @@ struct parameters * create_parameters ( const char *name ) {
/* Destroy any existing parameter list of this name */
params = find_parameters ( name );
- if ( params )
- destroy_parameters ( params );
+ if ( params ) {
+ claim_parameters ( params );
+ params_put ( params );
+ }
/* Allocate parameter list */
name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
params = zalloc ( sizeof ( *params ) + name_len );
if ( ! params )
return NULL;
+ ref_init ( &params->refcnt, free_parameters );
name_copy = ( ( void * ) ( params + 1 ) );
/* Populate parameter list */
@@ -125,41 +151,3 @@ struct parameter * add_parameter ( struct parameters *params,
params->name, param->key, param->value );
return param;
}
-
-/**
- * Destroy form parameter list
- *
- * @v params Parameter list
- */
-void destroy_parameters ( struct parameters *params ) {
- struct parameter *param;
- struct parameter *tmp;
-
- DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
-
- /* Free all parameters */
- list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
- list_del ( &param->list );
- free ( param );
- }
-
- /* Free parameter list */
- list_del ( &params->list );
- free ( params );
-}
-
-/**
- * Claim ownership of form parameter list
- *
- * @v params Parameter list
- */
-void claim_parameters ( struct parameters *params ) {
-
- DBGC ( params, "PARAMS \"%s\" claimed\n", params->name );
-
- /* Remove from list of parameter lists */
- list_del ( &params->list );
-
- /* Reinitialise list to allow for subsequent destroy_parameters() */
- INIT_LIST_HEAD ( &params->list );
-}
diff --git a/src/core/uri.c b/src/core/uri.c
index bc55e4d8..499fb975 100644
--- a/src/core/uri.c
+++ b/src/core/uri.c
@@ -72,8 +72,7 @@ static void dump_uri ( struct uri *uri ) {
static void free_uri ( struct refcnt *refcnt ) {
struct uri *uri = container_of ( refcnt, struct uri, refcnt );
- if ( uri->params )
- destroy_parameters ( uri->params );
+ params_put ( uri->params );
free ( uri );
}
@@ -89,6 +88,7 @@ static void free_uri ( struct refcnt *refcnt ) {
*/
struct uri * parse_uri ( const char *uri_string ) {
struct uri *uri;
+ struct parameters *params;
char *raw;
char *tmp;
char *path;
@@ -111,9 +111,9 @@ struct uri * parse_uri ( const char *uri_string ) {
if ( ( tmp = strstr ( raw, "##params" ) ) ) {
*tmp = '\0';
tmp += 8 /* "##params" */;
- uri->params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
- if ( uri->params ) {
- claim_parameters ( uri->params );
+ params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
+ if ( params ) {
+ uri->params = claim_parameters ( params );
} else {
/* Ignore non-existent submission blocks */
}
diff --git a/src/hci/commands/param_cmd.c b/src/hci/commands/param_cmd.c
index a30c62af..6cf096d0 100644
--- a/src/hci/commands/param_cmd.c
+++ b/src/hci/commands/param_cmd.c
@@ -74,8 +74,10 @@ static int params_exec ( int argc, char **argv ) {
return -ENOMEM;
/* Destroy parameter list, if applicable */
- if ( opts.delete )
- destroy_parameters ( params );
+ if ( opts.delete ) {
+ claim_parameters ( params );
+ params_put ( params );
+ }
return 0;
}
diff --git a/src/include/ipxe/params.h b/src/include/ipxe/params.h
index d78adf56..c2d82d9c 100644
--- a/src/include/ipxe/params.h
+++ b/src/include/ipxe/params.h
@@ -14,6 +14,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
/** A form parameter list */
struct parameters {
+ /** Reference count */
+ struct refcnt refcnt;
/** List of all parameter lists */
struct list_head list;
/** Name */
@@ -32,6 +34,43 @@ struct parameter {
const char *value;
};
+/**
+ * Increment form parameter list reference count
+ *
+ * @v params Parameter list, or NULL
+ * @ret params Parameter list as passed in
+ */
+static inline __attribute__ (( always_inline )) struct parameters *
+params_get ( struct parameters *params ) {
+ ref_get ( &params->refcnt );
+ return params;
+}
+
+/**
+ * Decrement form parameter list reference count
+ *
+ * @v params Parameter list, or NULL
+ */
+static inline __attribute__ (( always_inline )) void
+params_put ( struct parameters *params ) {
+ ref_put ( &params->refcnt );
+}
+
+/**
+ * Claim ownership of form parameter list
+ *
+ * @v params Parameter list
+ * @ret params Parameter list
+ */
+static inline __attribute__ (( always_inline )) struct parameters *
+claim_parameters ( struct parameters *params ) {
+
+ /* Remove from list of parameter lists */
+ list_del ( &params->list );
+
+ return params;
+}
+
/** Iterate over all form parameters in a list */
#define for_each_param( param, params ) \
list_for_each_entry ( (param), &(params)->entries, list )
@@ -40,7 +79,5 @@ extern struct parameters * find_parameters ( const char *name );
extern struct parameters * create_parameters ( const char *name );
extern struct parameter * add_parameter ( struct parameters *params,
const char *key, const char *value );
-extern void destroy_parameters ( struct parameters *params );
-extern void claim_parameters ( struct parameters *params );
#endif /* _IPXE_PARAMS_H */