netatalk 4.5.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
hash.c File Reference
`#include <assert.h>`
`#include <stddef.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include "hash.h"`

## Macros
#define | NDEBUG
#define | HASH_IMPLEMENTATION
#define | INIT_BITS 10
#define | INIT_SIZE (1UL << (INIT_BITS))
#define | INIT_MASK ((INIT_SIZE) - 1)
#define | next hash_next
#define | key hash_key
#define | data hash_data
#define | hkey hash_hkey
#define | table hash_table
#define | nchains hash_nchains
#define | nodecount hash_nodecount
#define | maxcount hash_maxcount
#define | highmark hash_highmark
#define | lowmark hash_lowmark
#define | compare hash_compare
#define | function hash_function
#define | allocnode hash_allocnode
#define | freenode hash_freenode
#define | context hash_context
#define | mask hash_mask
#define | dynamic hash_dynamic
#define | table hash_table
#define | chain hash_chain
#define | get16bits(d)

## Functions
static hnode_t * | hnode_alloc (void *context)
static void | hnode_free (hnode_t *nodenode, void *context)
static hash_val_t | hash_fun_default (const void *key)
static int | hash_comp_default (const void *key1, const void *key2)
static void | compute_bits (void)
| Compute the number of bits in the hash_val_t type.
static int | is_power_of_two (hash_val_t arg)
static void | clear_table (hash_t *hash)
static void | grow_table (hash_t *hash)
| Double the size of a dynamic table.
static void | shrink_table (hash_t *hash)
| Cut a table size in half.
hash_t * | hash_create (hashcount_t maxcount, hash_comp_t compfun, hash_fun_t hashfun)
| Create a dynamic hash table.
void | hash_set_allocator (hash_t *hash, hnode_alloc_t al, hnode_free_t fr, void *context)
void | hash_free_nodes (hash_t *hash)
void | hash_destroy (hash_t *hash)
void | hash_scan_begin (hscan_t *scan, hash_t *hash)
| Initialize a hash scanner.
hnode_t * | hash_scan_next (hscan_t *scan)
| Retrieve the next node from the hash table, and update the pointer for the next invocation of hash_scan_next().
void | hash_insert (hash_t *hash, hnode_t *nodenode, const void *key)
| Insert a node into the hash table.
hnode_t * | hash_lookup (hash_t *hash, const void *key)
| Find a node in the hash table and return a pointer to it.
hnode_t * | hash_delete (hash_t *hash, hnode_t *nodenode)
| Delete the given node from the hash table. Since the chains are singly linked, we must locate the start of the node's chain and traverse.
int | hash_alloc_insert (hash_t *hash, const void *key, void *data)
void | hash_delete_free (hash_t *hash, hnode_t *nodenode)
hnode_t * | hash_scan_delete (hash_t *hash, hnode_t *nodenode)
void | hash_scan_delfree (hash_t *hash, hnode_t *nodenode)
int | hash_verify (hash_t *hash)
| Verify whether the given object is a valid hash table.
hnode_t * | hnode_init (hnode_t *hnode, void *data)
void * | hnode_get (hnode_t *nodenode)
const void * | hnode_getkey (hnode_t *nodenode)
hashcount_t | hash_count (hash_t *hash)
hashcount_t | hash_size (hash_t *hash)

## Variables
int | hash_val_t_bit

## Macro Definition Documentation

## ◆ allocnode

#define allocnode hash_allocnode

## ◆ chain

#define chain hash_chain

## ◆ compare

#define compare hash_compare

## ◆ context

#define context hash_context

## ◆ data

#define data hash_data

## ◆ dynamic

#define dynamic hash_dynamic

## ◆ freenode

#define freenode hash_freenode

## ◆ function

#define function hash_function

## ◆ get16bits

#define get16bits | ( |  | d | ) |

Value:
((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \
+(uint32_t)(((const uint8_t *)(d))[0]) )

## ◆ HASH_IMPLEMENTATION

#define HASH_IMPLEMENTATION

## ◆ highmark

#define highmark hash_highmark

## ◆ hkey

#define hkey hash_hkey

## ◆ INIT_BITS

#define INIT_BITS 10

## ◆ INIT_MASK

#define INIT_MASK ((INIT_SIZE) - 1)

## ◆ INIT_SIZE

#define INIT_SIZE (1UL << (INIT_BITS))

## ◆ key

#define key hash_key

## ◆ lowmark

#define lowmark hash_lowmark

## ◆ mask

#define mask hash_mask

## ◆ maxcount

#define maxcount hash_maxcount

## ◆ nchains

#define nchains hash_nchains

## ◆ NDEBUG

#define NDEBUG

## ◆ next

#define next hash_next

## ◆ nodecount

#define nodecount hash_nodecount

## ◆ table [1/2]

#define table hash_table

## ◆ table [2/2]

#define table hash_table

## Function Documentation

## ◆ clear_table()

void clear_table | ( | hash_t * | hash | ) |
| static

Initialize the table of pointers to null.

## ◆ compute_bits()

void compute_bits | ( | void |  | ) |
| static

Compute the number of bits in the hash_val_t type.

We know that hash_val_t is an unsigned integral type. Thus the highest value it can hold is a Mersenne number (power of two, less one). We initialize a hash_val_t object with this value and then shift bits out one by one while counting.

Note
  1. HASH_VAL_T_MAX is a Mersenne number—one that is one less than a power of two. This means that its binary representation consists of all one bits, and hence 'val' is initialized to all one bits.
  2. While bits remain in val, we increment the bit count and shift it to the right, replacing the topmost bit by zero.

## ◆ grow_table()

void grow_table | ( | hash_t * | hash | ) |
| static

Double the size of a dynamic table.

This works as follows. Each chain splits into two adjacent chains. The shift amount increases by one, exposing an additional bit of each hashed key. For each node in the original chain, the value of this newly exposed bit will decide which of the two new chains will receive the node: if the bit is 1, the chain with the higher index will have the node, otherwise the lower chain will receive the node. In this manner, the hash table will continue to function exactly as before without having to rehash any of the keys.

Note
  1. Overflow check.
  2. The new number of chains is twice the old number of chains.
  3. The new mask is one bit wider than the previous, revealing a new bit in all hashed keys.
  4. Allocate a new table of chain pointers that is twice as large as the previous one.
  5. If the reallocation was successful, we perform the rest of the growth algorithm, otherwise we do nothing.
  6. The exposed_bit variable holds a mask with which each hashed key can be AND-ed to test the value of its newly exposed bit.
  7. Now loop over each chain in the table and sort its nodes into two chains based on the value of each node's newly exposed hash bit.
  8. The low chain replaces the current chain. The high chain goes into the corresponding sister chain in the upper half of the table.
  9. We have finished dealing with the chains and nodes. We now update the various bookeeping fields of the hash structure.

## ◆ hash_alloc_insert()

int hash_alloc_insert | ( | hash_t * | hash,
|  | const void * | key,
|  | void * | data )

## ◆ hash_comp_default()

int hash_comp_default | ( | const void * | key1,
|  | const void * | key2 )
| static

## ◆ hash_count()

hashcount_t hash_count | ( | hash_t * | hash | ) |

## ◆ hash_create()

hash_t * hash_create | ( | hashcount_t | maxcount,
|  | hash_comp_t | compfun,
|  | hash_fun_t | hashfun )

Create a dynamic hash table.

Both the hash table structure and the table itself are dynamically allocated. Furthermore, the table is extendible in that it will automatically grow as its load factor increases beyond a certain threshold.

Note
  1. If the number of bits in the hash_val_t type has not been computed yet, we do so here, because this is likely to be the first function that the user calls.
  2. Allocate a hash table control structure.
  3. If a hash table control structure is successfully allocated, we proceed to initialize it. Otherwise we return a null pointer.
  4. We try to allocate the table of hash chains.
  5. If we were able to allocate the hash chain table, we can finish initializing the hash structure and the table. Otherwise, we must backtrack by freeing the hash structure.
  6. INIT_SIZE should be a power of two. The high and low marks are always set to be twice the table size and half the table size respectively. When the number of nodes in the table grows beyond the high size (beyond load factor 2), it will double in size to cut the load factor down to about about 1. If the table shrinks down to or beneath load factor 0.5, it will shrink, bringing the load up to about 1. However, the table will never shrink beneath INIT_SIZE even if it's emptied.
  7. This indicates that the table is dynamically allocated and dynamically resized on the fly. A table that has this value set to zero is assumed to be statically allocated and will not be resized.
  8. The table of chains must be properly reset to all null pointers.

## ◆ hash_delete()

hnode_t * hash_delete | ( | hash_t * | hash,
|  | hnode_t * | node )

Delete the given node from the hash table. Since the chains are singly linked, we must locate the start of the node's chain and traverse.

Note
  1. The node must belong to this hash table, and its key must not have been tampered with.
  2. If this deletion will take the node count below the low mark, we shrink the table now.
  3. Determine which chain the node belongs to, and fetch the pointer to the first node in this chain.
  4. If the node being deleted is the first node in the chain, then simply update the chain head pointer.
  5. Otherwise advance to the node's predecessor, and splice out by updating the predecessor's next pointer.
  6. Indicate that the node is no longer in a hash table.

## ◆ hash_delete_free()

void hash_delete_free | ( | hash_t * | hash,
|  | hnode_t * | node )

## ◆ hash_destroy()

void hash_destroy | ( | hash_t * | hash | ) |

Free a dynamic hash table structure.

## ◆ hash_free_nodes()

void hash_free_nodes | ( | hash_t * | hash | ) |

Free every node in the hash using the hash->freenode() function pointer, and cause the hash to become empty.

## ◆ hash_fun_default()

hash_val_t hash_fun_default | ( | const void * | key | ) |
| static

## ◆ hash_insert()

void hash_insert | ( | hash_t * | hash,
|  | hnode_t * | node,
|  | const void * | key )

Insert a node into the hash table.

Note
  1. It's illegal to insert more than the maximum number of nodes. The client should verify that the hash table is not full before attempting an insertion.
  2. The same key may not be inserted into a table twice.
  3. If the table is dynamic and the load factor is already at >= 2, grow the table.
  4. We take the bottom N bits of the hash value to derive the chain index, where N is the base 2 logarithm of the size of the hash table.

## ◆ hash_lookup()

hnode_t * hash_lookup | ( | hash_t * | hash,
|  | const void * | key )
| inline

Find a node in the hash table and return a pointer to it.

Note
  1. We hash the key and keep the entire hash value. As an optimization, when we descend down the chain, we can compare hash values first and only if hash values match do we perform a full key comparison.
  2. To locate the chain from among 2^N chains, we look at the lower N bits of the hash value by anding them with the current mask.
  3. Looping through the chain, we compare the stored hash value inside each node against our computed hash. If they match, then we do a full comparison between the unhashed keys. If these match, we have located the entry.

## ◆ hash_scan_begin()

void hash_scan_begin | ( | hscan_t * | scan,
|  | hash_t * | hash )

Initialize a hash scanner.

Reset the hash scanner so that the next element retrieved by hash_scan_next() shall be the first element on the first non-empty chain.

Note
  1. Locate the first non empty chain.
  2. If an empty chain is found, remember which one it is and set the next pointer to refer to its first element.
  3. Otherwise if a chain is not found, set the next pointer to NULL so that hash_scan_next() shall indicate failure.

## ◆ hash_scan_delete()

hnode_t * hash_scan_delete | ( | hash_t * | hash,
|  | hnode_t * | node )

Exactly like hash_delete, except does not trigger table shrinkage. This is to be used from within a hash table scan operation. See notes for hash_delete.

## ◆ hash_scan_delfree()

void hash_scan_delfree | ( | hash_t * | hash,
|  | hnode_t * | node )

Like hash_delete_free but based on hash_scan_delete.

## ◆ hash_scan_next()

hnode_t * hash_scan_next | ( | hscan_t * | scan | ) |

Retrieve the next node from the hash table, and update the pointer for the next invocation of hash_scan_next().

Note
  1. Remember the next pointer in a temporary value so that it can be returned.
  2. This assertion essentially checks whether the module has been properly initialized. The first point of interaction with the module should be either hash_create() or hash_init(), both of which set hash_val_t_bit to a non zero value.
  3. If the next pointer we are returning is not NULL, then the user is allowed to call hash_scan_next() again. We prepare the new next pointer for that call right now. That way the user is allowed to delete the node we are about to return, since we will no longer be needing it to locate the next node.
  4. If there is a next node in the chain (next->next), then that becomes the new next node, otherwise ...
  5. We have exhausted the current chain, and must locate the next subsequent non-empty chain in the table.
  6. If a non-empty chain is found, the first element of that chain becomes the new next node. Otherwise there is no new next node and we set the pointer to NULL so that the next time hash_scan_next() is called, a null pointer shall be immediately returned.

## ◆ hash_set_allocator()

void hash_set_allocator | ( | hash_t * | hash,
|  | hnode_alloc_t | al,
|  | hnode_free_t | fr,
|  | void * | context )

Select a different set of node allocator routines.

## ◆ hash_size()

hashcount_t hash_size | ( | hash_t * | hash | ) |

## ◆ hash_verify()

int hash_verify | ( | hash_t * | hash | ) |

Verify whether the given object is a valid hash table.

Note
  1. If the hash table is dynamic, verify whether the high and low expansion/shrinkage thresholds are powers of two.
  2. Count all nodes in the table, and test each hash value to see whether it is correct for the node's chain.

## ◆ hnode_alloc()

hnode_t * hnode_alloc | ( | void * | context | ) |
| static

## ◆ hnode_free()

void hnode_free | ( | hnode_t * | node,
|  | void * | context )
| static

## ◆ hnode_get()

void * hnode_get | ( | hnode_t * | node | ) |

## ◆ hnode_getkey()

const void * hnode_getkey | ( | hnode_t * | node | ) |

## ◆ hnode_init()

hnode_t * hnode_init | ( | hnode_t * | hnode,
|  | void * | data )

Initialize a client-supplied node

## ◆ is_power_of_two()

int is_power_of_two | ( | hash_val_t | arg | ) |
| static

Verify whether the given argument is a power of two.

## ◆ shrink_table()

void shrink_table | ( | hash_t * | hash | ) |
| static

Cut a table size in half.

This is done by folding together adjacent chains and populating the lower half of the table with these chains. The chains are simply spliced together. Once this is done, the whole table is reallocated to a smaller object.

Note
  1. It is illegal to have a hash table with one slot. This would mean that hash->shift is equal to hash_val_t_bit, an illegal shift value. Also, other things could go wrong, such as hash->lowmark becoming zero.
  2. Looping over each pair of sister chains, the low_chain is set to point to the head node of the chain in the lower half of the table, and high_chain points to the head node of the sister in the upper half.
  3. The intent here is to compute a pointer to the last node of the lower chain into the low_tail variable. If this chain is empty, low_tail ends up with a null value.
  4. If the lower chain is not empty, we simply tack the upper chain onto it. If the upper chain is a null pointer, nothing happens.
  5. Otherwise if the lower chain is empty but the upper one is not, If the low chain is empty, but the high chain is not, then the high chain is simply transferred to the lower half of the table.
  6. Otherwise if both chains are empty, there is nothing to do.
  7. All the chain pointers are in the lower half of the table now, so we reallocate it to a smaller object. This, of course, invalidates all pointer-to-pointers which reference into the table from the first node of each chain.
  8. Though it's unlikely, the reallocation may fail. In this case we pretend that the table was reallocated to a smaller object.
  9. Finally, update the various table parameters to reflect the new size.

## Variable Documentation

## ◆ hash_val_t_bit

int hash_val_t_bit