Skip to content

Instantly share code, notes, and snippets.

@cwacek
Last active December 17, 2015 12:49
Show Gist options
  • Save cwacek/5612951 to your computer and use it in GitHub Desktop.
Save cwacek/5612951 to your computer and use it in GitHub Desktop.
An example of how to use uthash for generalized pointer storage with a string key.
typedef struct {
uint32_t intkey;
char strkey[64];
void * data;
UT_hash_handle hh;
} ptrmap_t;
#define str_ptrmap_insert(map,key,insval) \
do { \
ptrmap_t *elem; \
HASH_FIND_STR(map,key,elem); \
if (!elem) { \
elem = (ptrmap_t *) malloc(sizeof(ptrmap_t)); \
memcpy(elem->strkey,key,strnlen(key,63)); \
elem->strkey[strnlen(key,63)] = '\0'; \
elem->data = insval; \
HASH_ADD_STR( map, strkey, elem); \
} \
} while (0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment