hashtable v0.1.1
A lightweight separate-chaining arena-backed hashtable in C
|
See API documentation for hashtable_api.h .
This module implements a lightweight hashtable that uses separate chaining to resolve collisions.
This hashtable is designed to be flexible enough for use on embedded systems that have no dynamic memory, and/or limited memory in general.
hashtable.c
along with the rest of the files in your projecthashtable_api.h
in files where you want to use hashtableshashtable_api.h
to create and interact with hashtablesThe following sample program creates a hashtable instance, inserts a single key/value pair, and the retrieves the stored value with the same key, and prints it to stdout.
See API documentation for hashtable_api.h for comprehensive details about all available functions.
The following graph shows the results from a test program which creates a hashtable instance with a 32MB buffer, and inserts items until the buffer is full (each key is a 32-bit unsigned integer, and all values are NULL / 0 bytes).
After every 2,000 items inserted, the test program performs the following checks;
hashtable_has_key
. Divide the time taken for checking all keys by 1000 to get the average time to check for a bad key.Test was compiled with "-O2" optimization level and executed on a system with an Intel Core-i7 running Windows 10.
stdint.h
and string.h
.There are a number of preprocessor symbols you can define to change certain things, here are the details about those symbols.
By default, size_t
is used to hold the sizes of keys/values. If you know that all your keys/values are below a certain size, however, and you want to save some memory, then you can define one of the following options to set the datatype used to hold key/value sizes:
Symbol name | Effect |
---|---|
HASHTABLE_SIZE_T_UINT16 | hashtable_size_t is uint16_t |
HASHTABLE_SIZE_T_UINT32 | hashtable_size_t is uint32_t |
HASHTABLE_SIZE_T_SYS | hashtable_size_t is size_t (default) |
By default, all function parameters are checked for validity (e.g. no NULL pointers, no size values of 0). However, if you have a stable system and have worked most of those types of bugs out, then you may want to compile these checks out for a performance gain. Define the following option to compile out all function parameter validation checks:
Symbol name | Effect |
---|---|
HASHTABLE_DISABLE_PARAM_VALIDATION | All function param. validation checks compiled out |
Use __attribute__((packed))
for key/value pair struct definition, may save some extra space for stored key/value pair data:
Symbol name | Effect |
---|---|
HASHTABLE_PACKED_STRUCT | Key/value pair struct uses __attribute__((packed)) |