- minor: hash_insert returns an error in case of memory allocation failure
This commit is contained in:
parent
c3229723ab
commit
570dc16f70
2 changed files with 10 additions and 3 deletions
|
@ -82,21 +82,28 @@ hash_lookup (struct hash_table *h, unsigned long key)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
hash_insert (struct hash_table *h, unsigned long key, unsigned long val)
|
hash_insert (struct hash_table *h, unsigned long key, unsigned long val)
|
||||||
{
|
{
|
||||||
unsigned long bucket_mask = h->bucket_mask;
|
unsigned long bucket_mask = h->bucket_mask;
|
||||||
int bucket_index = (int) (key & bucket_mask);
|
int bucket_index = (int) (key & bucket_mask);
|
||||||
struct hash_item *new;
|
struct hash_item *new;
|
||||||
|
|
||||||
|
new = (struct hash_item *) XMALLOC (sizeof (struct hash_item));
|
||||||
|
if (! new) {
|
||||||
|
debug ("allocation failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
debug ("inserting %lu->%lu (%p->%p) to bucket %d",
|
debug ("inserting %lu->%lu (%p->%p) to bucket %d",
|
||||||
key, val, (void *) key, (void *) val, bucket_index);
|
key, val, (void *) key, (void *) val, bucket_index);
|
||||||
|
|
||||||
new = (struct hash_item *) XMALLOC (sizeof (struct hash_item));
|
|
||||||
new->key = key;
|
new->key = key;
|
||||||
new->val = val;
|
new->val = val;
|
||||||
new->next = h->table[bucket_index];
|
new->next = h->table[bucket_index];
|
||||||
h->table[bucket_index] = new;
|
h->table[bucket_index] = new;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long
|
unsigned long
|
||||||
|
|
|
@ -6,7 +6,7 @@ struct hash_table;
|
||||||
struct hash_table *hash_new (int);
|
struct hash_table *hash_new (int);
|
||||||
void hash_free (struct hash_table *);
|
void hash_free (struct hash_table *);
|
||||||
unsigned long hash_lookup (struct hash_table *, unsigned long);
|
unsigned long hash_lookup (struct hash_table *, unsigned long);
|
||||||
void hash_insert (struct hash_table *, unsigned long, unsigned long);
|
int hash_insert (struct hash_table *, unsigned long, unsigned long);
|
||||||
unsigned long hash_delete (struct hash_table *, unsigned long);
|
unsigned long hash_delete (struct hash_table *, unsigned long);
|
||||||
|
|
||||||
#endif /* __HASH_H */
|
#endif /* __HASH_H */
|
||||||
|
|
Loading…
Reference in a new issue