![]() |
Functions | |
size_t | qHasharrSize (int max) |
Get how much memory is needed for N entries. | |
int | qHasharr (Q_HASHARR *tbl, size_t memsize) |
Initialize array-hash table. | |
static bool | _put (Q_HASHARR *tbl, const char *key, const void *value, int size) |
Q_HASHARR->put(): Put object into hash table. | |
static bool | _putStr (Q_HASHARR *tbl, const char *key, const char *str) |
Q_HASHARR->putStr(): Put string into hash table. | |
static bool | _putInt (Q_HASHARR *tbl, const char *key, int num) |
Q_HASHARR->putInt(): Put integer into hash table. | |
static void * | _get (Q_HASHARR *tbl, const char *key, int *size) |
Q_HASHARR->get(): Get object from hash table. | |
static char * | _getStr (Q_HASHARR *tbl, const char *key) |
Q_HASHARR->getStr(): Get string from hash table. | |
static int | _getInt (Q_HASHARR *tbl, const char *key) |
Q_HASHARR->getInt(): Get integer from hash table. | |
static const char * | _getNext (Q_HASHARR *tbl, int *idx) |
Q_HASHARR->getNext(): Get next key name. | |
static bool | _remove (Q_HASHARR *tbl, const char *key) |
Q_HASHARR->remove(): Remove key from hash table. | |
static bool | _truncate (Q_HASHARR *tbl) |
Q_HASHARR->truncate(): Truncate array-hash table. | |
static bool | _print (Q_HASHARR *tbl, FILE *out) |
Q_HASHARR->print(): Print hash table for debugging purpose. | |
static int | _getNum (Q_HASHARR *tbl) |
Q_HASHARR->getNum(): Get number of objects stored. | |
static int | _getMax (Q_HASHARR *tbl) |
Q_HASHARR->getMax(): Get number of object slots. |
Basically this hash-table based on array defines small size slot then it can links several slot for one data. This mechanism can save some wastes of memory. You can adjust default slot size to modify _Q_HASHARR_DEF_VALUESIZE.
int maxkeys = 1000; // calculate how many memory do we need size_t memsize = qHasharrSize(maxkeys * 2); // generally allocate double size of max to decrease hash collision // allocate memory Q_HASHARR *hasharr = (Q_HASHARR *)malloc(memsize); // initialize hash-table if(qHasharr(hasharr, memsize) == 0) return -1; // put some sample data if(hasharr->put(hasharr, "sample1", "binary", 6) == false) return -1; // hash-table full if(hasharr->putStr(hasharr, "sample2", "string") == false) return -1; // hash-table full if(hasharr->putInt(hasharr, "sample3", 3) == false) return -1; // hash-table full // fetch data int size; char *sample_bin = hasharr->get(hasharr, "sample1", &size); char *sample_str = hasharr->getStr(hasharr, "sample2"); int sample_int = hasharr->getInt(hasharr, "sample3");
Another simple way to initialize hash-table.
// define data memory as much as you needed. char datamem[10 * 1024]; // just set the Q_HASHARR points to data memory. Q_HASHARR *hasharr = (Q_HASHARR *)datamem; // initialize hash-table. if(qHasharr(hasharr, sizeof(datamem)) == 0) return -1; (...your codes here...) // no need to free unless you use malloc()
You can create hash table on shared memory like below.
int maxkeys = 1000; int memsize = qHasharrSize(maxkeys * 2); // create shared memory int shmid = qShmInit(g_conf.szEgisdavdPidfile, 's', memsize, true); if(shmid < 0) return -1; // creation failed Q_HASHARR *tbl = (Q_HASHARR *)qShmGet(shmid); // initialize hash-table if(qHasharr(tbl, memsize) == 0) return -1; (...your codes here...) // destroy shared memory qShmFree(shmid);
size_t qHasharrSize | ( | int | max | ) |
Get how much memory is needed for N entries.
max | a number of maximum internal slots |
int qHasharr | ( | Q_HASHARR * | tbl, | |
size_t | memsize | |||
) |
Initialize array-hash table.
tbl | a pointer of Q_HASHARR | |
memsize | actual size of Q_HASHARR |
// allocate memory size_t memsize = qHasharrSize(100); Q_HASHARR *hasharr = (Q_HASHARR *)malloc(memsize); // initialize hash-table if(qHasharr(hasharr, memsize) == 0) return -1;
int maxkeys = 1000; int memsize = qHasharrSize(maxkeys * 2); // create shared memory int shmid = qShmInit(g_conf.szEgisdavdPidfile, 's', memsize, true); if(shmid < 0) return -1; // creation failed Q_HASHARR *tbl = (Q_HASHARR *)qShmGet(shmid); // initialize hash-table if(qHasharr(tbl, memsize) == 0) return -1; (...your codes here...) // destroy shared memory qShmFree(shmid);
static bool _put | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
const void * | value, | |||
int | size | |||
) | [static] |
Q_HASHARR->put(): Put object into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value object data | |
size | size of value |
static bool _putStr | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
const char * | str | |||
) | [static] |
Q_HASHARR->putStr(): Put string into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value string |
static bool _putInt | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
int | num | |||
) | [static] |
Q_HASHARR->putInt(): Put integer into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value integer |
static void* _get | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
int * | size | |||
) | [static] |
Q_HASHARR->get(): Get object from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
size | if not NULL, oject size will be stored |
static char* _getStr | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) | [static] |
Q_HASHARR->getStr(): Get string from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
static int _getInt | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) | [static] |
Q_HASHARR->getInt(): Get integer from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
static const char* _getNext | ( | Q_HASHARR * | tbl, | |
int * | idx | |||
) | [static] |
Q_HASHARR->getNext(): Get next key name.
tbl | a pointer of Q_HASHARR | |
idx | index pointer |
int idx = 0; while(tbl->getNext(tbl, &idx) != NULL) { (... codes ...) }
static bool _remove | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) | [static] |
Q_HASHARR->remove(): Remove key from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
static bool _truncate | ( | Q_HASHARR * | tbl | ) | [static] |
Q_HASHARR->truncate(): Truncate array-hash table.
tbl | a pointer of Q_HASHARR |
static bool _print | ( | Q_HASHARR * | tbl, | |
FILE * | out | |||
) | [static] |
Q_HASHARR->print(): Print hash table for debugging purpose.
tbl | a pointer of Q_HASHARR | |
out | output stream |
static int _getNum | ( | Q_HASHARR * | tbl | ) | [static] |
Q_HASHARR->getNum(): Get number of objects stored.
tbl | a pointer of Q_HASHARR |
static int _getMax | ( | Q_HASHARR * | tbl | ) | [static] |
Q_HASHARR->getMax(): Get number of object slots.
tbl | a pointer of Q_HASHARR |