From: uz Date: Fri, 12 Aug 2011 16:18:56 +0000 (+0000) Subject: Do also remove the Owner pointer from the HashNode making it ~50% of its X-Git-Tag: V2.13.3~303 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6b79f5bbb11b27561ab02b51749332a122a56321;p=cc65 Do also remove the Owner pointer from the HashNode making it ~50% of its original size. git-svn-id: svn://svn.cc65.org/cc65/trunk@5159 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/common/hashtab.c b/src/common/hashtab.c index 18ccedaf2..ada8948ae 100644 --- a/src/common/hashtab.c +++ b/src/common/hashtab.c @@ -46,6 +46,32 @@ +HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func) +/* Initialize a hash table and return it */ +{ + /* Initialize the fields */ + T->Slots = Slots; + T->Count = 0; + T->Table = 0; + T->Func = Func; + + /* Return the initialized table */ + return T; +} + + + +void DoneHashTable (HashTable* T) +/* Destroy the contents of a hash table. Note: This will not free the entries + * in the table! + */ +{ + /* Just free the array with the table pointers */ + xfree (T->Table); +} + + + void FreeHashTable (HashTable* T) /* Free a hash table. Note: This will not free the entries in the table! */ { @@ -145,7 +171,7 @@ void HT_Insert (HashTable* T, HashNode* N) /* Generate the hash over the node key. */ N->Hash = T->Func->GenHash (T->Func->GetKey (N)); - + /* Calculate the reduced hash */ RHash = N->Hash % T->Slots; @@ -153,21 +179,15 @@ void HT_Insert (HashTable* T, HashNode* N) N->Next = T->Table[RHash]; T->Table[RHash] = N; - /* Set the owner */ - N->Owner = T; - /* One more entry */ ++T->Count; } -void HT_Remove (HashNode* N) +void HT_Remove (HashTable* T, HashNode* N) /* Remove a node from a hash table. */ { - /* Get the table from the node */ - HashTable* T = N->Owner; - /* Calculate the reduced hash, which is also the slot number */ unsigned Slot = N->Hash % T->Slots; @@ -205,13 +225,7 @@ void HT_RemoveEntry (HashTable* T, void* Entry) /* Remove an entry from the given hash table */ { /* The entry is the first member, so we can just convert the pointer */ - HashNode* N = Entry; - - /* Make sure the entry is actually in the given table */ - CHECK (N->Owner == T); - - /* Remove the node */ - HT_Remove (N); + HT_Remove (T, Entry); } diff --git a/src/common/hashtab.h b/src/common/hashtab.h index fc5698391..f8a2466c3 100644 --- a/src/common/hashtab.h +++ b/src/common/hashtab.h @@ -50,15 +50,14 @@ -/* Hash table node. NOTE: This structure must be the first member of a struct - * that is hashed by the module. Having it first allows to omit a pointer to - * the entry itself, because the C standard guarantees that a pointer to a +/* Hash table node. NOTE: This structure must be the first member of a struct + * that is hashed by the module. Having it first allows to omit a pointer to + * the entry itself, because the C standard guarantees that a pointer to a * struct can be converted to its first member. */ typedef struct HashNode HashNode; struct HashNode { HashNode* Next; /* Next entry in hash list */ - struct HashTable* Owner; /* Owner table */ unsigned Hash; /* The full hash value */ }; @@ -105,12 +104,9 @@ INLINE void InitHashNode (HashNode* N) /* Initialize a hash node. */ { N->Next = 0; - N->Owner = 0; } #else -#define InitHashNode(N) \ - (N)->Next = 0, \ - (N)->Owner = 0 +#define InitHashNode(N) do { (N)->Next = 0; } while (0) #endif @@ -121,40 +117,13 @@ INLINE void InitHashNode (HashNode* N) -#if defined(HAVE_INLINE) -INLINE HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func) +HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func); /* Initialize a hash table and return it */ -{ - /* Initialize the fields */ - T->Slots = Slots; - T->Count = 0; - T->Table = 0; - T->Func = Func; - - /* Return the initialized table */ - return T; -} -#else -#define InitHashTable(T, Slots, Func) \ - (T)->Slots = (Slots), \ - (T)->Count = 0, \ - (T)->Table = 0, \ - (T)->Func = (Func), \ - (T) -#endif -#if defined(HAVE_INLINE) -INLINE void DoneHashTable (HashTable* T) +void DoneHashTable (HashTable* T); /* Destroy the contents of a hash table. Note: This will not free the entries * in the table! */ -{ - /* Just free the array with the table pointers */ - xfree (T->Table); -} -#else -#define DoneHashTable(T) xfree ((T)->Table) -#endif #if defined(HAVE_INLINE) INLINE HashTable* NewHashTable (unsigned Slots, const HashFunctions* Func) @@ -184,7 +153,7 @@ void* HT_FindEntry (const HashTable* T, const void* Key); void HT_Insert (HashTable* T, HashNode* N); /* Insert a node into the given hash table */ -void HT_Remove (HashNode* N); +void HT_Remove (HashTable* T, HashNode* N); /* Remove a node from its hash table */ void HT_InsertEntry (HashTable* T, void* Entry);