From 7c0e8fcfd51164f5259d8907f6c5c886d37ad30b Mon Sep 17 00:00:00 2001 From: JianjunJiang <8192542@qq.com> Date: Fri, 13 May 2022 17:03:51 +0800 Subject: [PATCH] [hmap]update hmap c library --- examples/benchmark/main.c | 12 ++++++------ src/hmap.c | 13 +++++++------ src/hmap.h | 21 +++++++++++---------- src/onnx.c | 10 +++++----- tests/main.c | 4 ++-- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/examples/benchmark/main.c b/examples/benchmark/main.c index 22b85300..b2d74fd2 100644 --- a/examples/benchmark/main.c +++ b/examples/benchmark/main.c @@ -16,20 +16,20 @@ static inline uint64_t time_get(void) return (uint64_t)(tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000); } -static struct hmap_t * profiler_alloc(int size) +static void hmap_entry_callback(struct hmap_t * m, struct hmap_entry_t * e) { - return hmap_alloc(size); + if(e && e->value) + free(e->value); } -static void hmap_entry_callback(struct hmap_entry_t * e) +static struct hmap_t * profiler_alloc(int size) { - if(e && e->value) - free(e->value); + return hmap_alloc(size, hmap_entry_callback); } static void profiler_free(struct hmap_t * m) { - hmap_free(m, hmap_entry_callback); + hmap_free(m); } static struct profiler_t * profiler_search(struct hmap_t * m, const char * name) diff --git a/src/hmap.c b/src/hmap.c index ae8c27c3..1abb938a 100644 --- a/src/hmap.c +++ b/src/hmap.c @@ -48,7 +48,7 @@ static inline unsigned int roundup_pow_of_two(unsigned int x) return 1; } -struct hmap_t * hmap_alloc(unsigned int size) +struct hmap_t * hmap_alloc(int size, void (*cb)(struct hmap_t *, struct hmap_entry_t *)) { struct hmap_t * m; int i; @@ -73,21 +73,22 @@ struct hmap_t * hmap_alloc(unsigned int size) init_list_head(&m->list); m->size = size; m->n = 0; + m->callback = cb; return m; } -void hmap_free(struct hmap_t * m, void (*cb)(struct hmap_entry_t *)) +void hmap_free(struct hmap_t * m) { if(m) { - hmap_clear(m, cb); + hmap_clear(m); free(m->hash); free(m); } } -void hmap_clear(struct hmap_t * m, void (*cb)(struct hmap_entry_t *)) +void hmap_clear(struct hmap_t * m) { struct hmap_entry_t * pos, * n; @@ -98,8 +99,8 @@ void hmap_clear(struct hmap_t * m, void (*cb)(struct hmap_entry_t *)) hlist_del(&pos->node); list_del(&pos->head); m->n--; - if(cb) - cb(pos); + if(m->callback) + m->callback(m, pos); free(pos->key); free(pos); } diff --git a/src/hmap.h b/src/hmap.h index 89636548..6a47b585 100644 --- a/src/hmap.h +++ b/src/hmap.h @@ -7,13 +7,6 @@ extern "C" { #include -struct hmap_t { - struct hlist_head * hash; - struct list_head list; - unsigned int size; - unsigned int n; -}; - struct hmap_entry_t { struct hlist_node node; struct list_head head; @@ -21,15 +14,23 @@ struct hmap_entry_t { void * value; }; +struct hmap_t { + struct hlist_head * hash; + struct list_head list; + unsigned int size; + unsigned int n; + void (*callback)(struct hmap_t * m, struct hmap_entry_t * e); +}; + #define hmap_for_each_entry(entry, m) \ list_for_each_entry(entry, &(m)->list, head) #define hmap_for_each_entry_reverse(entry, m) \ list_for_each_entry_reverse(entry, &(m)->list, head) -struct hmap_t * hmap_alloc(unsigned int size); -void hmap_free(struct hmap_t * m, void (*cb)(struct hmap_entry_t *)); -void hmap_clear(struct hmap_t * m, void (*cb)(struct hmap_entry_t *)); +struct hmap_t * hmap_alloc(int size, void (*cb)(struct hmap_t *, struct hmap_entry_t *)); +void hmap_free(struct hmap_t * m); +void hmap_clear(struct hmap_t * m); void hmap_add(struct hmap_t * m, const char * key, void * value); void hmap_remove(struct hmap_t * m, const char * key); void hmap_sort(struct hmap_t * m); diff --git a/src/onnx.c b/src/onnx.c index 172fbf4b..c622759b 100644 --- a/src/onnx.c +++ b/src/onnx.c @@ -30,7 +30,7 @@ #define ONNX_LOG(...) printf(__VA_ARGS__) -static void hmap_entry_callback(struct hmap_entry_t * e) +static void hmap_entry_callback(struct hmap_t * m, struct hmap_entry_t * e) { if(e && e->value) onnx_tensor_free((struct onnx_tensor_t *)e->value); @@ -56,7 +56,7 @@ struct onnx_context_t * onnx_context_alloc(const void * buf, size_t len, struct return NULL; } - ctx->map = hmap_alloc(0); + ctx->map = hmap_alloc(0, hmap_entry_callback); if(!ctx->map) { if(ctx->model) @@ -78,7 +78,7 @@ struct onnx_context_t * onnx_context_alloc(const void * buf, size_t len, struct if(ctx->r) free(ctx->r); if(ctx->map) - hmap_free(ctx->map, hmap_entry_callback); + hmap_free(ctx->map); if(ctx->model) onnx__model_proto__free_unpacked(ctx->model, NULL); if(ctx) @@ -112,7 +112,7 @@ struct onnx_context_t * onnx_context_alloc(const void * buf, size_t len, struct if(ctx->r) free(ctx->r); if(ctx->map) - hmap_free(ctx->map, hmap_entry_callback); + hmap_free(ctx->map); if(ctx->model) onnx__model_proto__free_unpacked(ctx->model, NULL); if(ctx) @@ -169,7 +169,7 @@ void onnx_context_free(struct onnx_context_t * ctx) if(ctx->r) free(ctx->r); if(ctx->map) - hmap_free(ctx->map, hmap_entry_callback); + hmap_free(ctx->map); if(ctx->model) onnx__model_proto__free_unpacked(ctx->model, NULL); free(ctx); diff --git a/tests/main.c b/tests/main.c index beef912c..fdc277e3 100644 --- a/tests/main.c +++ b/tests/main.c @@ -107,7 +107,7 @@ int main(int argc, char * argv[]) usage(); return -1; } - m = hmap_alloc(0); + m = hmap_alloc(0, NULL); if((dir = opendir(argv[1])) != NULL) { if(chdir(argv[1]) == 0) @@ -131,6 +131,6 @@ int main(int argc, char * argv[]) { testcase(e->key, NULL, 0); } - hmap_free(m, NULL); + hmap_free(m); return 0; }