Last active
December 17, 2015 12:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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