]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
map_t, set_t: unify memory allocation to mm_*
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 19 Feb 2018 17:56:03 +0000 (18:56 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 1 Mar 2018 13:50:29 +0000 (14:50 +0100)
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...

daemon/engine.c
daemon/lua/kres-gen.lua
daemon/network.c
daemon/worker.c
lib/generic/map.c
lib/generic/map.h
lib/generic/set.h
lib/zonecut.c
modules/stats/stats.c
tests/test_map.c
tests/test_set.c

index c86bc68c6839dc393c74b03a2d250b313361834b..502bf046eef4e3da71e3eb91331e446f1a852866 100644 (file)
@@ -581,8 +581,8 @@ static int l_trampoline(lua_State *L)
 static int init_resolver(struct engine *engine)
 {
        /* Open resolution context */
-       engine->resolver.trust_anchors = map_make();
-       engine->resolver.negative_anchors = map_make();
+       engine->resolver.trust_anchors = map_make(NULL);
+       engine->resolver.negative_anchors = map_make(NULL);
        engine->resolver.pool = engine->pool;
        engine->resolver.modules = &engine->modules;
        /* Create OPT RR */
index 629fe899a3037b7b3e20fbb6ef5d9ebd57d017f7..a0210962e43cc3bcd00131422e1f15286f08eeff 100644 (file)
@@ -59,9 +59,7 @@ struct knot_pkt {
 typedef struct knot_pkt knot_pkt_t;
 typedef struct {
        void *root;
-       map_alloc_f malloc;
-       map_free_f free;
-       void *baton;
+       struct knot_mm *pool;
 } map_t;
 struct kr_qflags {
        _Bool NO_MINIMIZE : 1;
index c3d26af164c60842da6173280b5b2324094e425e..b3564567f4d299c3eb17bc173a9e477aba83c7a6 100644 (file)
@@ -50,8 +50,8 @@ void network_init(struct network *net, uv_loop_t *loop)
 {
        if (net != NULL) {
                net->loop = loop;
-               net->endpoints = map_make();
-               net->tls_client_params = map_make();
+               net->endpoints = map_make(NULL);
+               net->tls_client_params = map_make(NULL);
        }
 }
 
index 290c00599c3bc171781e933dbc2d8c1dda392ba6..eec5671397c9abf064da7c628cf24f97390af344 100644 (file)
@@ -2337,9 +2337,9 @@ static int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen)
        memset(&worker->pkt_pool, 0, sizeof(worker->pkt_pool));
        worker->pkt_pool.ctx = mp_new (4 * sizeof(knot_pkt_t));
        worker->pkt_pool.alloc = (knot_mm_alloc_t) mp_alloc;
-       worker->outgoing = map_make();
-       worker->tcp_connected = map_make();
-       worker->tcp_waiting = map_make();
+       worker->outgoing = map_make(NULL);
+       worker->tcp_connected = map_make(NULL);
+       worker->tcp_waiting = map_make(NULL);
        worker->tcp_pipeline_max = MAX_PIPELINED;
        memset(&worker->stats, 0, sizeof(worker->stats));
        return kr_ok();
index 73de090a39e09e227ccd431050ba40144afa9f3c..350f3fc5fc7640acfbf8380f8c2c6a64cd10a7db 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 
 #include "map.h"
+#include "lib/utils.h"
 
  /* Exports */
 #if defined _WIN32 || defined __CYGWIN__
@@ -59,17 +60,6 @@ static inline cb_node_t *ref_get_internal(uint8_t *p)
        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 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);
                cbt_traverse_delete(map, q->child[0]);
                cbt_traverse_delete(map, q->child[1]);
-               map->free(map->baton, q);
+               mm_free(map->pool, q);
        } else {
-               map->free(map->baton, p);
+               mm_free(map->pool, p);
        }
 }
 
@@ -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)
 {
-       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) {
                x->value = value;
                memcpy(x->key, str, len);
@@ -154,17 +144,6 @@ static int cbt_get(map_t *map, const char *str, void **value)
        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 */
 EXPORT int map_contains(map_t *map, const char *str)
 {
@@ -234,14 +213,14 @@ different_byte_found:
        c = data->key[newbyte];
        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) {
                return ENOMEM;
        }
 
        x = (uint8_t *)cbt_make_data(map, ubytes, ulen + 1, val);
        if (x == NULL) {
-               map->free(map->baton, newnode);
+               mm_free(map->pool, newnode);
                return ENOMEM;
        }
 
@@ -312,7 +291,7 @@ EXPORT int map_del(map_t *map, const char *str)
        if (strcmp(str, (const char *)data->key) != 0) {
                return 1;
        }
-       map->free(map->baton, p);
+       mm_free(map->pool, p);
 
        if (!whereq) {
                map->root = NULL;
@@ -320,7 +299,7 @@ EXPORT int map_del(map_t *map, const char *str)
        }
 
        *whereq = q->child[1 - direction];
-       map->free(map->baton, q);
+       mm_free(map->pool, q);
        return 0;
 }
 
index 9a4c3c08dea61db0089de85f4e73484b3d5a324a..79eeb5c38d07fd770e0d22f5df0ddc5a139d3133 100644 (file)
@@ -12,7 +12,7 @@
  * # Example usage:
  *
  * @code{.c}
- *      map_t map = map_make();
+ *      map_t map = map_make(NULL);
  *
  *      // Custom allocator (optional)
  *      map.malloc = &mymalloc;
 extern "C" {
 #endif
 
-typedef void *(*map_alloc_f)(void *, size_t);
-typedef void (*map_free_f)(void *baton, void *ptr);
+struct knot_mm; /* avoid the unnecessary include */
 
 /** Main data structure */
 typedef struct {
        void *root;
-       map_alloc_f malloc;
-       map_free_f free;
-       void *baton; /** Passed to malloc() and free() */
+       struct knot_mm *pool;
 } map_t;
 
-/** Creates an new, empty critbit map */
-map_t map_make(void);
+/** Creates an new empty critbit map.  Pass NULL for malloc+free. */
+static inline map_t map_make(struct knot_mm *pool)
+{
+       return (map_t){ .root = NULL, .pool = pool };
+}
 
 /** Returns non-zero if map contains str */
 int map_contains(map_t *map, const char *str);
index c2a8776b5f90b0a389fae6cc31e72f4e13d7020a..dd6bbde8f3cf9e34cbaef7cd716898c7b4e99319 100644 (file)
@@ -23,7 +23,7 @@
  * # Example usage:
  *
  * @code{.c}
- *      set_t set = set_make();
+ *      set_t set = set_make(NULL);
  *
  *      // Insert keys
  *      if (set_add(&set, "princess") != 0 ||
@@ -70,8 +70,8 @@ typedef map_t set_t;
 typedef int (set_walk_cb)(const char *, void *);
 
 /*! Creates an new, empty critbit set */
-#define set_make() \
-       map_make()
+#define set_make \
+       map_make
 
 /*! Returns non-zero if set contains str */
 #define set_contains(set, str) \
index 841aa76b8135f66bae4c373d219655cd5d2562e3..70d097278c2b254871cd8e1d76b75bc1f9b16ab7 100644 (file)
@@ -60,10 +60,7 @@ int kr_zonecut_init(struct kr_zonecut *cut, const knot_dname_t *name, knot_mm_t
        cut->key  = NULL;
        cut->trust_anchor = NULL;
        cut->parent = NULL;
-       cut->nsset = map_make();
-       cut->nsset.malloc = (map_alloc_f) mm_alloc;
-       cut->nsset.free = (map_free_f) mm_free;
-       cut->nsset.baton = pool;
+       cut->nsset = map_make(pool);
        return kr_ok();
 }
 
index 8a593ca5d4796d350bc985cf35561e3c2329e29f..6b87137a779662e1d3e469d9618abdbfd5658a97 100644 (file)
@@ -430,7 +430,7 @@ int stats_init(struct kr_module *module)
                return kr_error(ENOMEM);
        }
        memset(data, 0, sizeof(*data));
-       data->map = map_make();
+       data->map = map_make(NULL);
        module->data = data;
        lru_create(&data->queries.frequent, FREQUENT_COUNT, NULL, NULL);
        /* Initialize ring buffer of recently visited upstreams */
index baf16e967944f304578ea0cac9aa1acc75f10a4b..b9e624a209f2f171ffa7a5575dd04339209f1342 100644 (file)
@@ -104,7 +104,7 @@ static void test_null_value(void **state)
 static void test_init(void **state)
 {
        static map_t tree;
-       tree = map_make();
+       tree = map_make(NULL);
        *state = &tree;
        assert_non_null(*state);
 }
index b1aa791f6d191d9001faeca6f13fa12fff94aafe..5f82d8298b9f02f513ec3e8c3cbd2303d54ff3aa 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "tests/test.h"
 #include "lib/generic/set.h"
+#include "lib/utils.h"
 
 
 /*
@@ -134,8 +135,8 @@ static void test_delete_all(void **state)
 static void *fake_malloc(void *b, size_t s) { return NULL; }
 static void test_allocator(void **state)
 {
-       set_t set = set_make();
-       set.malloc = fake_malloc;
+       knot_mm_t fake_pool = { .ctx = NULL, .alloc = fake_malloc, .free = NULL };
+       set_t set = set_make(&fake_pool);
        assert_int_equal(set_add(&set, dict[0]), ENOMEM);
 }
 
@@ -181,7 +182,7 @@ static void test_clear(void **state)
 static void test_init(void **state)
 {
        static set_t set;
-       set = set_make();
+       set = set_make(NULL);
        *state = &set;
        assert_non_null(*state);
 }