Add 'free' module.
[gnulib.git] / lib / hash.c
index df8f4bd..3948ce4 100644 (file)
@@ -1,5 +1,8 @@
 /* hash - hashing table processing.
-   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
+   Foundation, Inc.
+
    Written by Jim Meyering, 1992.
 
    This program is free software; you can redistribute it and/or modify
 #if HAVE_CONFIG_H
 # include <config.h>
 #endif
-#if HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#if HAVE_STDBOOL_H
-# include <stdbool.h>
-#else
-typedef enum {false = 0, true = 1} bool;
-#endif
-#include <stdio.h>
-#include <assert.h>
 
-#ifndef HAVE_DECL_FREE
-"this configure-time declaration test was not run"
-#endif
-#if !HAVE_DECL_FREE
-void free ();
-#endif
-
-#ifndef HAVE_DECL_MALLOC
-"this configure-time declaration test was not run"
-#endif
-#if !HAVE_DECL_MALLOC
-char *malloc ();
-#endif
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
 
 #if USE_OBSTACK
 # include "obstack.h"
@@ -263,7 +247,8 @@ hash_lookup (const Hash_table *table, const void *entry)
     = table->bucket + table->hasher (entry, table->n_buckets);
   struct hash_entry *cursor;
 
-  assert (bucket < table->bucket_limit);
+  if (! (bucket < table->bucket_limit))
+    abort ();
 
   if (bucket->data == NULL)
     return NULL;
@@ -292,12 +277,11 @@ hash_get_first (const Hash_table *table)
   if (table->n_entries == 0)
     return NULL;
 
-  for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
-    if (bucket->data)
+  for (bucket = table->bucket; ; bucket++)
+    if (! (bucket < table->bucket_limit))
+      abort ();
+    else if (bucket->data)
       return bucket->data;
-
-  assert (0);
-  return NULL;
 }
 
 /* Return the user data for the entry following ENTRY, where ENTRY has been
@@ -311,7 +295,8 @@ hash_get_next (const Hash_table *table, const void *entry)
     = table->bucket + table->hasher (entry, table->n_buckets);
   struct hash_entry *cursor;
 
-  assert (bucket < table->bucket_limit);
+  if (! (bucket < table->bucket_limit))
+    abort ();
 
   /* Find next entry in the same bucket.  */
   for (cursor = bucket; cursor; cursor = cursor->next)
@@ -403,9 +388,6 @@ hash_do_for_each (const Hash_table *table, Hash_processor processor,
 unsigned
 hash_string (const char *string, unsigned n_buckets)
 {
-# ifndef CHAR_BIT
-#  define CHAR_BIT 8
-# endif
 # define ROTATE_LEFT(Value, Shift) \
   ((Value) << (Shift) | (Value) >> ((sizeof (unsigned) * CHAR_BIT) - (Shift)))
 # define HASH_ONE_CHAR(Value, Byte) \
@@ -754,7 +736,9 @@ hash_find_entry (Hash_table *table, const void *entry,
     = table->bucket + table->hasher (entry, table->n_buckets);
   struct hash_entry *cursor;
 
-  assert (bucket < table->bucket_limit);
+  if (! (bucket < table->bucket_limit))
+    abort ();
+
   *bucket_head = bucket;
 
   /* Test for empty bucket.  */
@@ -848,7 +832,9 @@ hash_rehash (Hash_table *table, unsigned candidate)
            = (new_table->bucket
               + new_table->hasher (data, new_table->n_buckets));
 
-         assert (new_bucket < new_table->bucket_limit);
+         if (! (new_bucket < new_table->bucket_limit))
+           abort ();
+
          next = cursor->next;
 
          if (new_bucket->data)
@@ -912,7 +898,9 @@ hash_insert (Hash_table *table, const void *entry)
   void *data;
   struct hash_entry *bucket;
 
-  assert (entry);              /* cannot insert a NULL entry */
+  /* The caller cannot insert a NULL entry.  */
+  if (! entry)
+    abort ();
 
   /* If there's a matching entry already in the table, return that.  */
   if ((data = hash_find_entry (table, entry, &bucket, false)) != NULL)