intptr_t init_dlist(dlist_t *dlist, intptr_t obj_size) { intptr_t bitmap_size = 0; intptr_t pack_size = obj_size + SIZE_OF(arraydlist_t); intptr_t total_size = 0; dlist->__obj_size__ = obj_size; dlist->__cache_size__ = 8 * SIZE_OF(intptr_t); bitmap_size = (dlist->__cache_size__ - 1) / 8 + 1; total_size = bitmap_size + pack_size * dlist->__cache_size__; dlist->__segment__ = malloc(total_size); (void)memset(dlist->__segment__, 0, total_size); dlist->__cache__ = (int8_t *)dlist->__segment__ + bitmap_size; dlist->__service_condition_bitmap__ = (bitmap_t){ (intptr_t *)dlist->__segment__, bitmap_size, }; dlist->__head__ = (arraydlist_t *)( (int8_t *)dlist->__segment__ + bitmap_size + obj_size ); bitmap_set(&dlist->__service_condition_bitmap__, 0, 1); return 0; }
int main(int argc, char const* argv[]) { #define SIZE_OF(T) "%d\n", sizeof(T) fprintf(stderr, SIZE_OF(JSON)); fprintf(stderr, SIZE_OF(JSONInt)); fprintf(stderr, SIZE_OF(JSONInt64)); fprintf(stderr, SIZE_OF(JSONDouble)); fprintf(stderr, SIZE_OF(JSONString)); fprintf(stderr, SIZE_OF(JSONArray)); fprintf(stderr, SIZE_OF(JSONObject)); fprintf(stderr, SIZE_OF(dictmap_t)); fprintf(stderr, SIZE_OF(hashmap_t)); return 0; }
//------------------------------------------------------------------------------ const char* id_to_kstring( unsigned int nID ) { unsigned int i; for ( i = 0; i < SIZE_OF( g_id2kstr ); i++ ) { if ( g_id2kstr[ i ].m_nID == nID ) { return g_id2kstr[ i ].pszStr; } } assert(false); return nullptr; }
//命令分发函数 void cmd_dispatch(char* cmdstr, int length) { //命令分发 for (int i = 0; i < SIZE_OF(cmdarr); ++i) { struct cmd_action cmd = cmdarr[i]; if (!strncmp(cmdstr, cmd.name, strlen(cmd.name))) { cmd.action(cmdstr, length); return; } } printf("%s\n", "command not found!"); }
static inline intptr_t dlist_next(dlist_t *dlist, intptr_t index) { arraydlist_t *p = NULL; intptr_t shell_size = dlist->__obj_size__ + SIZE_OF(arraydlist_t); p = (arraydlist_t *)( dlist->__cache__ + shell_size * index + dlist->__obj_size__ ); return p->__next__; }
intptr_t dlist_insert(dlist_t *dlist, void *obj) { intptr_t index; intptr_t cache_obj_offset; index = __dlist_search_cache__(dlist, TRUE); if (-1 == index) { __dlist_resize__(dlist); index = __dlist_search_cache__(dlist, TRUE); } assert(index >= 0); cache_obj_offset = (dlist->__obj_size__ + SIZE_OF(arraydlist_t)) * index; (void)memcpy(&dlist->__cache__[cache_obj_offset], obj, dlist->__obj_size__); // add to dlist __dlist_add__(dlist, index); return 0; }
//------------------------------------------------------------------------------ const char* id_to_sql_string( unsigned int nID ) { switch (nID) { case K_JEQ: nID = K_EQ ; break; case K_JAUTO: nID = K_EQ ; break; case K_JNO: nID = K_LT ; break; case K_JINF: nID = K_LT ; break; case K_JIEQ: nID = K_LEQ ; break; case K_JSUP: nID = K_GT ; break; case K_JSEQ: nID = K_GEQ ; break; } unsigned int i; for ( i = 0; i < SIZE_OF( g_id2str ); i++ ) { if ( g_id2str[ i ].m_nID == nID ) { return g_id2str[ i ].pszStr; } } return "K_UNKNOW"; }
static inline void __dlist_add__(dlist_t *dlist, intptr_t index) { arraydlist_t *prev; arraydlist_t *next; arraydlist_t *node; intptr_t shell_size = dlist->__obj_size__ + SIZE_OF(arraydlist_t); prev = dlist->__head__; next = (arraydlist_t *)( dlist->__cache__ + shell_size * dlist->__head__->__next__ + dlist->__obj_size__ ); node = (arraydlist_t *)( dlist->__cache__ + shell_size * index + dlist->__obj_size__ ); arraydlist_add(prev, next, node, index); return; }
int main(int argc, char *argv[]) { #if 0 intptr_t list = 0; data_t data[8]; int i = 0; intptr_t iter = 0; for (i = 0; i < ARRAY_COUNT(data); ++i) { data[i].__x__ = i; list_add(&list, &data[i].__node__); } assert(0 == list_rm(&list, 0)); assert(0 == list_rm(&list, 7)); iter = list; while (0 != iter) { data_t *data = CONTAINER_OF((intptr_t *)iter, data_t, __node__); (void)fprintf(stderr, "%d\n", data->__x__); iter = *(intptr_t *)iter; } return 0; #endif #if 0 intptr_t head = 0; intptr_t node = 0; (void)fprintf(stderr, "&head: %p, &node: %p\n", &head, &node); list_add(&head, &node); (void)fprintf(stderr, "head: %x, node: %x\n", head, node); list_remove(&head); (void)fprintf(stderr, "head: %x, node: %x\n", head, node); return 0; #endif #if 0 intptr_t i = 0; data_t data[8]; print_data((data_t){9, 0, {0, 0}}); memset(data, 0, SIZE_OF(data)); arraylist_add(&data[0].__node__, &data[2].__node__, 2); arraylist_add(&data[2].__node__, &data[3].__node__, 3); arraylist_add(&data[3].__node__, &data[7].__node__, 7); arraylist_del(&data[0].__node__, &data[2].__node__); for (i = 0; i < (intptr_t)ARRAY_COUNT(data); ++i) { (void)fprintf(stderr, "data[%d] : %d\n", i, data[i].__node__); } memset(data, 0, SIZE_OF(data)); arraydlist_add(&data[0].__dnode__, &data[0].__dnode__, &data[0].__dnode__, 0); arraydlist_add(&data[0].__dnode__, &data[0].__dnode__, &data[2].__dnode__, 2); arraydlist_add(&data[0].__dnode__, &data[2].__dnode__, &data[3].__dnode__, 3); arraydlist_add(&data[3].__dnode__, &data[2].__dnode__, &data[7].__dnode__, 7); /* that's NOT right, broke the dlist arraydlist_add(&data[2].__dnode__, &data[3].__dnode__, &data[7].__dnode__, 7);*/ /*arraydlist_del(&data[3].__dnode__, &data[2].__dnode__, &data[7].__dnode__);*/ for (i = 0; i < (intptr_t)ARRAY_COUNT(data); ++i) { (void)fprintf(stderr, "data[%d] : (%d, %d)\n", i, data[i].__dnode__.__prev__, data[i].__dnode__.__next__); } return 0; #endif #if 1 typedef struct { intptr_t a; intptr_t b; } mydata_t; dlist_t dlist; mydata_t x1 = {1, 2}; mydata_t x2 = {3, 4}; mydata_t x3 = {5, 6}; (void)init_dlist(&dlist, SIZE_OF(mydata_t)); dlist_insert(&dlist, &x2); dlist_insert(&dlist, &x3); dlist_insert(&dlist, &x1); for (intptr_t iter = dlist.__head__->__next__; iter != 0; iter = dlist_next(&dlist, iter)) { // mydata_t *d = CONTAINER_OF(iter, mydata_t, __dnode__); intptr_t shell_size = dlist.__obj_size__ + SIZE_OF(arraydlist_t); mydata_t *d = (mydata_t *)(dlist.__cache__ + shell_size * iter); fprintf(stderr, "%d, %d\n", d->a, d->b); } exit_dlist(&dlist); return 0; #endif }