Skip to content

Commit

Permalink
Always have a default dtor in both stack and map API.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDP committed Jul 15, 2019
1 parent c6f5aed commit 4623cf1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
15 changes: 8 additions & 7 deletions Lib/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static map_ret_code hashmap_put(map_t *m, const char *key, void *value, const bo
m->data[index].val_needs_free = autofree;

/* In case of value update, avoid incrementing size */
if (! m->data[index].in_use) {
if (!m->data[index].in_use) {
m->data[index].in_use = true;
m->size++;
}
Expand All @@ -217,11 +217,7 @@ static void clear_elem(map_t *m, const int idx) {
m->data[idx].key_needs_free = false;

if (m->data[idx].val_needs_free) {
if (m->dtor) {
m->dtor(m->data[idx].data);
} else {
memhook._free((void *)m->data[idx].data);
}
m->dtor(m->data[idx].data);
}
m->data[idx].data = NULL;
m->data[idx].val_needs_free = false;
Expand All @@ -242,6 +238,7 @@ map_t *map_new(void) {
if (m->data) {
m->table_size = INITIAL_SIZE;
m->size = 0;
m->dtor = memhook._free;
} else {
map_free(m);
m = NULL;
Expand Down Expand Up @@ -415,6 +412,10 @@ int map_length(const map_t *m) {
map_ret_code map_set_dtor(map_t *m, map_dtor fn) {
MAP_PARAM_ASSERT(m);

m->dtor = fn;
if (fn) {
m->dtor = fn;
} else {
m->dtor = memhook._free;
}
return MAP_OK;
}
2 changes: 1 addition & 1 deletion Lib/public/module/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef enum {
typedef map_ret_code (*map_cb)(void *, const char *, void *);

/* Fn for map_set_dtor */
typedef map_ret_code (*map_dtor)(void *);
typedef void (*map_dtor)(void *);

/* Incomplete struct declaration for hashmap */
typedef struct _map map_t;
Expand Down
4 changes: 2 additions & 2 deletions Lib/public/module/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ typedef enum {
} stack_ret_code;

/* Callback for stack_iterate */
typedef stack_ret_code(*stack_cb)(void *, void *);
typedef stack_ret_code (*stack_cb)(void *, void *);

/* Fn for stack_set_dtor */
typedef stack_ret_code(*stack_dtor)(void *);
typedef void (*stack_dtor)(void *);

/* Incomplete struct declaration for stack */
typedef struct _stack stack_t;
Expand Down
18 changes: 11 additions & 7 deletions Lib/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ struct _stack_itr {
/** Public API **/

stack_t *stack_new(void) {
return memhook._calloc(1, sizeof(stack_t));
stack_t *s = memhook._calloc(1, sizeof(stack_t));
if (s) {
s->dtor = memhook._free;
}
return s;
}

stack_itr_t *stack_itr_new(const stack_t *s) {
Expand Down Expand Up @@ -112,11 +116,7 @@ stack_ret_code stack_clear(stack_t *s) {
const bool autofree = elem->autofree;
void *data = stack_pop(s);
if (autofree) {
if (s->dtor) {
s->dtor(data);
} else {
memhook._free(data);
}
s->dtor(data);
}
}
return STACK_OK;
Expand All @@ -139,6 +139,10 @@ int stack_length(const stack_t *s) {
stack_ret_code stack_set_dtor(stack_t *s, stack_dtor fn) {
STACK_PARAM_ASSERT(s);

s->dtor = fn;
if (fn) {
s->dtor = fn;
} else {
s->dtor = memhook._free;
}
return STACK_OK;
}
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

### Map
- [x] FIx: avoid incrementing map size on value update
- [x] map->dtor should default to memhook._free, and fallback to default if map_set_dtor is called with NULL callback parameter
- [x] Add map_itr_t interface
- [x] Add tests for new interface
- [ ] Add Doc

### Stack
- [x] stack->dtor should default to memhook._free, and fallback to default if stack_set_dtor is called with NULL callback parameter
- [X] Add stack_itr_t interface
- [x] Add tests for new interface
- [ ] Add Doc
Expand Down

0 comments on commit 4623cf1

Please sign in to comment.