[ccan] hash: Fix hash_pointer example code

Anton Blanchard anton at samba.org
Mon May 2 20:37:40 EST 2011


The hash_pointer example wasn't applying the mask when indexing into
the hash table array.
---

diff --git a/ccan/hash/hash.h b/ccan/hash/hash.h
index 0400e6a..e066c3d 100644
--- a/ccan/hash/hash.h
+++ b/ccan/hash/hash.h
@@ -268,6 +268,7 @@ uint64_t hash64_stable_8(const void *key, size_t n, uint64_t base);
  *
  * Example:
  *	#include <ccan/hash/hash.h>
+ *	#define HASH_SIZE 128
  *
  *	// Code to keep track of memory regions.
  *	struct region {
@@ -276,11 +277,11 @@ uint64_t hash64_stable_8(const void *key, size_t n, uint64_t base);
  *		unsigned int size;
  *	};
  *	// We keep a simple hash table.
- *	static struct region *region_hash[128];
+ *	static struct region *region_hash[HASH_SIZE];
  *
  *	static void add_region(struct region *r)
  *	{
- *		unsigned int h = hash_pointer(r->start, 0);
+ *		unsigned int h = hash_pointer(r->start, 0) % HASH_SIZE;
  *
  *		r->chain = region_hash[h];
  *		region_hash[h] = r->chain;
@@ -290,7 +291,8 @@ uint64_t hash64_stable_8(const void *key, size_t n, uint64_t base);
  *	{
  *		struct region *r;
  *
- *		for (r = region_hash[hash_pointer(start, 0)]; r; r = r->chain)
+ *		for (r = region_hash[hash_pointer(start, 0) % HASH_SIZE]; r;
+ *		     r = r->chain)
  *			if (r->start == start)
  *				return r;
  *		return NULL;


More information about the ccan mailing list