libiberty: hashtab allocation functions with an extra argument

Daniel Jacobowitz drow@mvista.com
Wed Jan 22 23:36:00 GMT 2003


On Wed, Jan 22, 2003 at 05:45:11PM -0500, DJ Delorie wrote:
> 
> > Callback has an opaque pointer already.  I added one to alloc/free. 
> > That leaves hash, compare-equal, and delete.  Do you think there's a
> > use for extra opaque pointers to those?  I've no idea.  Nor do I know
> > if they should come from an argument to the function which calls the
> > delete callback (like htab_trav) or when the htab is created (like
> > alloc/free).
> 
> I meant, are there other parameters we might want to specify when
> creating a hash table, but I guess we don't.  Ok, post an updated
> patch summarizing what we've discussed and let's see if it's a go.

Let's see how this one looks.  The create function becomes _ex, but I
left the pointers as alloc_with_arg, since that's what they are.  Do
you want _ex there too?

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer

2003-01-22  Daniel Jacobowitz  <drow@mvista.com>

	* hashtab.h (htab_alloc_with_arg, htab_free_with_arg): Add new types.
	(struct htab): Add alloc_arg, alloc_with_arg_f, free_with_arg_f.
	(htab_create_alloc_ex): New prototype.
	(htab_set_functions_ex): New prototype.

2003-01-22  Daniel Jacobowitz  <drow@mvista.com>

	* hashtab.c (htab_create_alloc_ex): New function.
	(hatab_set_functions_ex): New function.
	(htab_delete, htab_expand): Support alternate allocation functions.

Index: include/hashtab.h
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/include/hashtab.h,v
retrieving revision 1.10
diff -u -p -r1.10 hashtab.h
--- include/hashtab.h	2002/06/25 01:03:52	1.10
+++ include/hashtab.h	2003/01/22 23:35:41
@@ -76,10 +76,16 @@ typedef PTR (*htab_alloc) PARAMS ((size_
 /* We also need a free() routine.  */
 typedef void (*htab_free) PARAMS ((PTR));
 
+/* Memory allocation and deallocation; variants which take an extra
+   argument.  */
+typedef PTR (*htab_alloc_with_arg) PARAMS ((void *, size_t, size_t));
+typedef void (*htab_free_with_arg) PARAMS ((void *, void *));
+
 /* Hash tables are of the following type.  The structure
    (implementation) of this type is not needed for using the hash
    tables.  All work with hash table should be executed only through
-   functions mentioned below. */
+   functions mentioned below.  The size of this structure is subject to
+   change.  */
 
 struct htab GTY(())
 {
@@ -115,6 +121,11 @@ struct htab GTY(())
   /* Pointers to allocate/free functions.  */
   htab_alloc alloc_f;
   htab_free free_f;
+
+  /* Alternate allocate/free functions, which take an extra argument.  */
+  PTR alloc_arg;
+  htab_alloc_with_arg alloc_with_arg_f;
+  htab_free_with_arg free_with_arg_f;
 };
 
 typedef struct htab *htab_t;
@@ -128,9 +139,19 @@ extern htab_t	htab_create_alloc	PARAMS (
 						 htab_eq, htab_del,
 						 htab_alloc, htab_free));
 
+extern htab_t	htab_create_alloc_ex	PARAMS ((size_t, htab_hash,
+						    htab_eq, htab_del,
+						    PTR, htab_alloc_with_arg,
+						    htab_free_with_arg));
+
 /* Backward-compatibility functions.  */
 extern htab_t htab_create PARAMS ((size_t, htab_hash, htab_eq, htab_del));
 extern htab_t htab_try_create PARAMS ((size_t, htab_hash, htab_eq, htab_del));
+
+extern void	htab_set_functions_ex	PARAMS ((htab_t, htab_hash,
+						 htab_eq, htab_del,
+						 PTR, htab_alloc_with_arg,
+						 htab_free_with_arg));
 
 extern void	htab_delete	PARAMS ((htab_t));
 extern void	htab_empty	PARAMS ((htab_t));
Index: libiberty/hashtab.c
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/libiberty/hashtab.c,v
retrieving revision 1.15
diff -u -p -r1.15 hashtab.c
--- libiberty/hashtab.c	2003/01/20 19:03:56	1.15
+++ libiberty/hashtab.c	2003/01/22 23:35:12
@@ -191,6 +191,63 @@ htab_create_alloc (size, hash_f, eq_f, d
   return result;
 }
 
+/* As above, but use the variants of alloc_f and free_f which accept
+   an extra argument.  */
+
+htab_t
+htab_create_alloc_ex (size, hash_f, eq_f, del_f, alloc_arg, alloc_f,
+		      free_f)
+     size_t size;
+     htab_hash hash_f;
+     htab_eq eq_f;
+     htab_del del_f;
+     PTR alloc_arg;
+     htab_alloc_with_arg alloc_f;
+     htab_free_with_arg free_f;
+{
+  htab_t result;
+
+  size = higher_prime_number (size);
+  result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab));
+  if (result == NULL)
+    return NULL;
+  result->entries = (PTR *) (*alloc_f) (alloc_arg, size, sizeof (PTR));
+  if (result->entries == NULL)
+    {
+      if (free_f != NULL)
+	(*free_f) (alloc_arg, result);
+      return NULL;
+    }
+  result->size = size;
+  result->hash_f = hash_f;
+  result->eq_f = eq_f;
+  result->del_f = del_f;
+  result->alloc_arg = alloc_arg;
+  result->alloc_with_arg_f = alloc_f;
+  result->free_with_arg_f = free_f;
+  return result;
+}
+
+/* Update the function pointers and allocation parameter in the htab_t.  */
+
+void
+htab_set_functions_ex (htab, hash_f, eq_f, del_f, alloc_arg, alloc_f, free_f)
+     htab_t htab;
+     htab_hash hash_f;
+     htab_eq eq_f;
+     htab_del del_f;
+     PTR alloc_arg;
+     htab_alloc_with_arg alloc_f;
+     htab_free_with_arg free_f;
+{
+  htab->hash_f = hash_f;
+  htab->eq_f = eq_f;
+  htab->del_f = del_f;
+  htab->alloc_arg = alloc_arg;
+  htab->alloc_with_arg_f = alloc_f;
+  htab->free_with_arg_f = free_f;
+}
+
 /* These functions exist solely for backward compatibility.  */
 
 #undef htab_create
@@ -234,6 +291,11 @@ htab_delete (htab)
       (*htab->free_f) (htab->entries);
       (*htab->free_f) (htab);
     }
+  else if (htab->free_with_arg_f != NULL)
+    {
+      (*htab->free_with_arg_f) (htab->alloc_arg, htab->entries);
+      (*htab->free_with_arg_f) (htab->alloc_arg, htab);
+    }
 }
 
 /* This function clears all entries in the given hash table.  */
@@ -313,7 +375,11 @@ htab_expand (htab)
 
   nsize = higher_prime_number (htab->size * 2);
 
-  nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR));
+  if (htab->alloc_with_arg_f != NULL)
+    nentries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
+						  sizeof (PTR *));
+  else
+    nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
   if (nentries == NULL)
     return 0;
   htab->entries = nentries;
@@ -340,6 +406,8 @@ htab_expand (htab)
 
   if (htab->free_f != NULL)
     (*htab->free_f) (oentries);
+  else if (htab->free_with_arg_f != NULL)
+    (*htab->free_with_arg_f) (htab->alloc_arg, oentries);
   return 1;
 }
 



More information about the Gcc-patches mailing list