Commit a159bab9 authored by Vladimír Čunát's avatar Vladimír Čunát

map_t, set_t: unify memory allocation to mm_*

We use the knot style everywhere else; this was very similar and yet
different, so really annoying to me.  In the long term we might better
migrate to qp-tries from knot, but the API differs, so it's delayed...

(cherry picked from map-alloc - commit b1b8157b94fb41)
parent 3c5c1d22
...@@ -581,8 +581,8 @@ static int l_trampoline(lua_State *L) ...@@ -581,8 +581,8 @@ static int l_trampoline(lua_State *L)
static int init_resolver(struct engine *engine) static int init_resolver(struct engine *engine)
{ {
/* Open resolution context */ /* Open resolution context */
engine->resolver.trust_anchors = map_make(); engine->resolver.trust_anchors = map_make(NULL);
engine->resolver.negative_anchors = map_make(); engine->resolver.negative_anchors = map_make(NULL);
engine->resolver.pool = engine->pool; engine->resolver.pool = engine->pool;
engine->resolver.modules = &engine->modules; engine->resolver.modules = &engine->modules;
/* Create OPT RR */ /* Create OPT RR */
......
...@@ -59,9 +59,7 @@ struct knot_pkt { ...@@ -59,9 +59,7 @@ struct knot_pkt {
typedef struct knot_pkt knot_pkt_t; typedef struct knot_pkt knot_pkt_t;
typedef struct { typedef struct {
void *root; void *root;
map_alloc_f malloc; struct knot_mm *pool;
map_free_f free;
void *baton;
} map_t; } map_t;
struct kr_qflags { struct kr_qflags {
_Bool NO_MINIMIZE : 1; _Bool NO_MINIMIZE : 1;
......
...@@ -50,8 +50,8 @@ void network_init(struct network *net, uv_loop_t *loop) ...@@ -50,8 +50,8 @@ void network_init(struct network *net, uv_loop_t *loop)
{ {
if (net != NULL) { if (net != NULL) {
net->loop = loop; net->loop = loop;
net->endpoints = map_make(); net->endpoints = map_make(NULL);
net->tls_client_params = map_make(); net->tls_client_params = map_make(NULL);
} }
} }
......
...@@ -2263,9 +2263,9 @@ static int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen) ...@@ -2263,9 +2263,9 @@ static int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen)
memset(&worker->pkt_pool, 0, sizeof(worker->pkt_pool)); memset(&worker->pkt_pool, 0, sizeof(worker->pkt_pool));
worker->pkt_pool.ctx = mp_new (4 * sizeof(knot_pkt_t)); worker->pkt_pool.ctx = mp_new (4 * sizeof(knot_pkt_t));
worker->pkt_pool.alloc = (knot_mm_alloc_t) mp_alloc; worker->pkt_pool.alloc = (knot_mm_alloc_t) mp_alloc;
worker->outgoing = map_make(); worker->outgoing = map_make(NULL);
worker->tcp_connected = map_make(); worker->tcp_connected = map_make(NULL);
worker->tcp_waiting = map_make(); worker->tcp_waiting = map_make(NULL);
worker->tcp_pipeline_max = MAX_PIPELINED; worker->tcp_pipeline_max = MAX_PIPELINED;
memset(&worker->stats, 0, sizeof(worker->stats)); memset(&worker->stats, 0, sizeof(worker->stats));
return kr_ok(); return kr_ok();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "map.h" #include "map.h"
#include "lib/utils.h"
/* Exports */ /* Exports */
#if defined _WIN32 || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
...@@ -59,17 +60,6 @@ static inline cb_node_t *ref_get_internal(uint8_t *p) ...@@ -59,17 +60,6 @@ static inline cb_node_t *ref_get_internal(uint8_t *p)
return (cb_node_t *)(p - 1); return (cb_node_t *)(p - 1);
} }
/* Standard memory allocation functions */
static void *malloc_std(void *baton, size_t size) {
(void)baton; /* Prevent compiler warnings */
return malloc(size);
}
static void free_std(void *baton, void *ptr) {
(void)baton; /* Prevent compiler warnings */
free(ptr);
}
/* Static helper functions */ /* Static helper functions */
static void cbt_traverse_delete(map_t *map, void *top) static void cbt_traverse_delete(map_t *map, void *top)
{ {
...@@ -78,9 +68,9 @@ static void cbt_traverse_delete(map_t *map, void *top) ...@@ -78,9 +68,9 @@ static void cbt_traverse_delete(map_t *map, void *top)
cb_node_t *q = ref_get_internal(p); cb_node_t *q = ref_get_internal(p);
cbt_traverse_delete(map, q->child[0]); cbt_traverse_delete(map, q->child[0]);
cbt_traverse_delete(map, q->child[1]); cbt_traverse_delete(map, q->child[1]);
map->free(map->baton, q); mm_free(map->pool, q);
} else { } else {
map->free(map->baton, p); mm_free(map->pool, p);
} }
} }
...@@ -110,7 +100,7 @@ static int cbt_traverse_prefixed(void *top, ...@@ -110,7 +100,7 @@ static int cbt_traverse_prefixed(void *top,
static cb_data_t *cbt_make_data(map_t *map, const uint8_t *str, size_t len, void *value) static cb_data_t *cbt_make_data(map_t *map, const uint8_t *str, size_t len, void *value)
{ {
cb_data_t *x = map->malloc(map->baton, sizeof(cb_data_t) + len); cb_data_t *x = mm_alloc(map->pool, sizeof(cb_data_t) + len);
if (x != NULL) { if (x != NULL) {
x->value = value; x->value = value;
memcpy(x->key, str, len); memcpy(x->key, str, len);
...@@ -154,17 +144,6 @@ static int cbt_get(map_t *map, const char *str, void **value) ...@@ -154,17 +144,6 @@ static int cbt_get(map_t *map, const char *str, void **value)
return 0; return 0;
} }
/*! Creates a new, empty critbit map */
EXPORT map_t map_make(void)
{
map_t map;
map.root = NULL;
map.malloc = &malloc_std;
map.free = &free_std;
map.baton = NULL;
return map;
}
/*! Returns non-zero if map contains str */ /*! Returns non-zero if map contains str */
EXPORT int map_contains(map_t *map, const char *str) EXPORT int map_contains(map_t *map, const char *str)
{ {
...@@ -234,14 +213,14 @@ EXPORT int map_set(map_t *map, const char *str, void *val) ...@@ -234,14 +213,14 @@ EXPORT int map_set(map_t *map, const char *str, void *val)
c = data->key[newbyte]; c = data->key[newbyte];
newdirection = (1 + (newotherbits | c)) >> 8; newdirection = (1 + (newotherbits | c)) >> 8;
newnode = map->malloc(map->baton, sizeof(cb_node_t)); newnode = mm_alloc(map->pool, sizeof(cb_node_t));
if (newnode == NULL) { if (newnode == NULL) {
return ENOMEM; return ENOMEM;
} }
x = (uint8_t *)cbt_make_data(map, ubytes, ulen + 1, val); x = (uint8_t *)cbt_make_data(map, ubytes, ulen + 1, val);
if (x == NULL) { if (x == NULL) {
map->free(map->baton, newnode); mm_free(map->pool, newnode);
return ENOMEM; return ENOMEM;
} }
...@@ -312,7 +291,7 @@ EXPORT int map_del(map_t *map, const char *str) ...@@ -312,7 +291,7 @@ EXPORT int map_del(map_t *map, const char *str)
if (strcmp(str, (const char *)data->key) != 0) { if (strcmp(str, (const char *)data->key) != 0) {
return 1; return 1;
} }
map->free(map->baton, p); mm_free(map->pool, p);
if (!whereq) { if (!whereq) {
map->root = NULL; map->root = NULL;
...@@ -320,7 +299,7 @@ EXPORT int map_del(map_t *map, const char *str) ...@@ -320,7 +299,7 @@ EXPORT int map_del(map_t *map, const char *str)
} }
*whereq = q->child[1 - direction]; *whereq = q->child[1 - direction];
map->free(map->baton, q); mm_free(map->pool, q);
return 0; return 0;
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* # Example usage: * # Example usage:
* *
* @code{.c} * @code{.c}
* map_t map = map_make(); * map_t map = map_make(NULL);
* *
* // Custom allocator (optional) * // Custom allocator (optional)
* map.malloc = &mymalloc; * map.malloc = &mymalloc;
...@@ -59,19 +59,19 @@ ...@@ -59,19 +59,19 @@
extern "C" { extern "C" {
#endif #endif
typedef void *(*map_alloc_f)(void *, size_t); struct knot_mm; /* avoid the unnecessary include */
typedef void (*map_free_f)(void *baton, void *ptr);
/** Main data structure */ /** Main data structure */
typedef struct { typedef struct {
void *root; void *root;
map_alloc_f malloc; struct knot_mm *pool;
map_free_f free;
void *baton; /** Passed to malloc() and free() */
} map_t; } map_t;
/** Creates an new, empty critbit map */ /** Creates an new empty critbit map. Pass NULL for malloc+free. */
map_t map_make(void); static inline map_t map_make(struct knot_mm *pool)
{
return (map_t){ .root = NULL, .pool = pool };
}
/** Returns non-zero if map contains str */ /** Returns non-zero if map contains str */
int map_contains(map_t *map, const char *str); int map_contains(map_t *map, const char *str);
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* # Example usage: * # Example usage:
* *
* @code{.c} * @code{.c}
* set_t set = set_make(); * set_t set = set_make(NULL);
* *
* // Insert keys * // Insert keys
* if (set_add(&set, "princess") != 0 || * if (set_add(&set, "princess") != 0 ||
...@@ -70,8 +70,8 @@ typedef map_t set_t; ...@@ -70,8 +70,8 @@ typedef map_t set_t;
typedef int (set_walk_cb)(const char *, void *); typedef int (set_walk_cb)(const char *, void *);
/*! Creates an new, empty critbit set */ /*! Creates an new, empty critbit set */
#define set_make() \ #define set_make \
map_make() map_make
/*! Returns non-zero if set contains str */ /*! Returns non-zero if set contains str */
#define set_contains(set, str) \ #define set_contains(set, str) \
......
...@@ -60,10 +60,7 @@ int kr_zonecut_init(struct kr_zonecut *cut, const knot_dname_t *name, knot_mm_t ...@@ -60,10 +60,7 @@ int kr_zonecut_init(struct kr_zonecut *cut, const knot_dname_t *name, knot_mm_t
cut->key = NULL; cut->key = NULL;
cut->trust_anchor = NULL; cut->trust_anchor = NULL;
cut->parent = NULL; cut->parent = NULL;
cut->nsset = map_make(); cut->nsset = map_make(pool);
cut->nsset.malloc = (map_alloc_f) mm_alloc;
cut->nsset.free = (map_free_f) mm_free;
cut->nsset.baton = pool;
return kr_ok(); return kr_ok();
} }
......
...@@ -444,7 +444,7 @@ int stats_init(struct kr_module *module) ...@@ -444,7 +444,7 @@ int stats_init(struct kr_module *module)
return kr_error(ENOMEM); return kr_error(ENOMEM);
} }
memset(data, 0, sizeof(*data)); memset(data, 0, sizeof(*data));
data->map = map_make(); data->map = map_make(NULL);
module->data = data; module->data = data;
lru_create(&data->queries.frequent, FREQUENT_COUNT, NULL, NULL); lru_create(&data->queries.frequent, FREQUENT_COUNT, NULL, NULL);
lru_create(&data->queries.expiring, FREQUENT_COUNT, NULL, NULL); lru_create(&data->queries.expiring, FREQUENT_COUNT, NULL, NULL);
......
...@@ -104,7 +104,7 @@ static void test_null_value(void **state) ...@@ -104,7 +104,7 @@ static void test_null_value(void **state)
static void test_init(void **state) static void test_init(void **state)
{ {
static map_t tree; static map_t tree;
tree = map_make(); tree = map_make(NULL);
*state = &tree; *state = &tree;
assert_non_null(*state); assert_non_null(*state);
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "tests/test.h" #include "tests/test.h"
#include "lib/generic/set.h" #include "lib/generic/set.h"
#include "lib/utils.h"
/* /*
...@@ -134,8 +135,8 @@ static void test_delete_all(void **state) ...@@ -134,8 +135,8 @@ static void test_delete_all(void **state)
static void *fake_malloc(void *b, size_t s) { return NULL; } static void *fake_malloc(void *b, size_t s) { return NULL; }
static void test_allocator(void **state) static void test_allocator(void **state)
{ {
set_t set = set_make(); knot_mm_t fake_pool = { .ctx = NULL, .alloc = fake_malloc, .free = NULL };
set.malloc = fake_malloc; set_t set = set_make(&fake_pool);
assert_int_equal(set_add(&set, dict[0]), ENOMEM); assert_int_equal(set_add(&set, dict[0]), ENOMEM);
} }
...@@ -181,7 +182,7 @@ static void test_clear(void **state) ...@@ -181,7 +182,7 @@ static void test_clear(void **state)
static void test_init(void **state) static void test_init(void **state)
{ {
static set_t set; static set_t set;
set = set_make(); set = set_make(NULL);
*state = &set; *state = &set;
assert_non_null(*state); assert_non_null(*state);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment