Skip to content

Commit

Permalink
[hmap]update hmap c library
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunjiang committed May 13, 2022
1 parent 973f440 commit 7c0e8fc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
12 changes: 6 additions & 6 deletions examples/benchmark/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/hmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down
21 changes: 11 additions & 10 deletions src/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ extern "C" {

#include <onnxconf.h>

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;
char * key;
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);
Expand Down
10 changes: 5 additions & 5 deletions src/onnx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -131,6 +131,6 @@ int main(int argc, char * argv[])
{
testcase(e->key, NULL, 0);
}
hmap_free(m, NULL);
hmap_free(m);
return 0;
}

0 comments on commit 7c0e8fc

Please sign in to comment.