X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/07d9123cf8f28a777794f567b36490841300b6a6..fc2f4a27ed42bcff7c3c5ce00573210110b45b21:/lib/hash.c diff --git a/lib/hash.c b/lib/hash.c index 1d61cbb3..e34e289f 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -1,12 +1,21 @@ /* $Header$ * * Generic hash table routines. Uses integer keys to store char * values. + * + * (c) Copyright 1988 by the Massachusetts Institute of Technology. + * For copying and distribution information, please see the file + * . */ +#include #include -#include "sms_app.h" -#define NULL 0 +#include +#include +extern char *malloc(); + +#define NULL 0 +#define hash_func(h, key) (key >= 0 ? (key % h->size) : (-key % h->size)) /* Create a hash table. The size is just a hint, not a maximum. */ @@ -16,9 +25,15 @@ int size; struct hash *h; h = (struct hash *) malloc(sizeof(struct hash)); + if (h == (struct hash *) NULL) + return((struct hash *) NULL); h->size = size; h->data = (struct bucket **) malloc(size * sizeof(char *)); - bzero(h->data, size * sizeof(char *)); + if (h->data == (struct bucket **) NULL) { + free(h); + return((struct hash *) NULL); + } + memset(h->data, 0, size * sizeof(char *)); return(h); } @@ -32,7 +47,7 @@ register int key; { register struct bucket *b; - b = h->data[key % h->size]; + b = h->data[hash_func(h, key)]; while (b && b->key != key) b = b->next; if (b && b->key == key) @@ -53,7 +68,7 @@ char *value; { register struct bucket *b; - b = h->data[key % h->size]; + b = h->data[hash_func(h, key)]; while (b && b->key != key) b = b->next; if (b && b->key == key) { @@ -65,7 +80,7 @@ char *value; /* Store an item in the hash table. Returns 0 if the key was not previously - * there, or 1 if it was. + * there, 1 if it was, or -1 if we ran out of memory. */ int hash_store(h, key, value) @@ -75,9 +90,11 @@ char *value; { register struct bucket *b, **p; - p = &(h->data[key % h->size]); + p = &(h->data[hash_func(h, key)]); if (*p == NULL) { b = *p = (struct bucket *) malloc(sizeof(struct bucket)); + if (b == (struct bucket *) NULL) + return(-1); b->next = NULL; b->key = key; b->data = value; @@ -91,6 +108,8 @@ char *value; return(1); } b = *p = (struct bucket *) malloc(sizeof(struct bucket)); + if (b == (struct bucket *) NULL) + return(-1); b->next = NULL; b->key = key; b->data = value;