]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
mm_ctx_mempool2(): factor out the new function
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 22 Dec 2020 13:11:31 +0000 (14:11 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 13 Jan 2021 11:17:52 +0000 (12:17 +0100)
contrib/mempattern.c
contrib/mempattern.h
daemon/io.c
modules/hints/hints.c

index 177e5bae2b774b1f568b2b435fb0680e5a0bdf55..a04c43300286007138fd76df350b45625f5288a1 100644 (file)
@@ -136,3 +136,16 @@ void *mm_malloc_aligned(void *ctx, size_t n)
        }
 }
 
+knot_mm_t * mm_ctx_mempool2(size_t chunk_size)
+{
+       knot_mm_t pool_tmp;
+       mm_ctx_mempool(&pool_tmp, chunk_size);
+       knot_mm_t *pool = mm_alloc(&pool_tmp, sizeof(*pool));
+       if (!pool) {
+               mp_delete(pool_tmp.ctx);
+               return NULL;
+       }
+       memcpy(pool, &pool_tmp, sizeof(*pool));
+       return pool;
+}
+
index f277b6721e03b0a056429a8bcc01bf54418b7475..3bd6049de36f80b855c00899c661b2e1f6d96edb 100644 (file)
@@ -79,3 +79,6 @@ static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment)
        mm->free = free;
 }
 
+/*! \brief New memory pool context, allocated on itself. */
+KR_EXPORT knot_mm_t * mm_ctx_mempool2(size_t chunk_size);
+
index 83263387d1690aa67f04b1f3fe8cb302ba1e7964..11437670aeafba2be254848c1e17784da7efc122 100644 (file)
@@ -735,16 +735,10 @@ void io_tty_alloc(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
 }
 
 struct io_stream_data *io_tty_alloc_data() {
-       knot_mm_t _pool = {
-               .ctx = mp_new(4096),
-               .alloc = (knot_mm_alloc_t) mp_alloc,
-       };
-       knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool));
+       knot_mm_t *pool = mm_ctx_mempool2(MM_DEFAULT_BLKSIZE);
        if (!pool) {
                return NULL;
        }
-       memcpy(pool, &_pool, sizeof(*pool));
-
        struct io_stream_data *data = mm_alloc(pool, sizeof(struct io_stream_data));
 
        data->buf = mp_start(pool->ctx, 512);
index 34edcc5deeed0152541780c9d9af63556452a08d..82b58479965a11806c3bb24edd880f822cc39bdf 100644 (file)
@@ -619,17 +619,10 @@ int hints_init(struct kr_module *module)
        };
        module->props = props;
 
-       /* Create pool and copy itself */
-       knot_mm_t _pool = {
-               .ctx = mp_new(4096),
-               .alloc = (knot_mm_alloc_t) mp_alloc
-       };
-       knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool));
+       knot_mm_t *pool = mm_ctx_mempool2(MM_DEFAULT_BLKSIZE);
        if (!pool) {
                return kr_error(ENOMEM);
        }
-       memcpy(pool, &_pool, sizeof(*pool));
-
        struct hints_data *data = mm_alloc(pool, sizeof(struct hints_data));
        if (!data) {
                mp_delete(pool->ctx);