From: Vladimír Čunát Date: Tue, 22 Dec 2020 13:11:31 +0000 (+0100) Subject: mm_ctx_mempool2(): factor out the new function X-Git-Tag: v5.3.0~21^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff11f32661b4e971b36aec0dffde4a0a275eb948;p=thirdparty%2Fknot-resolver.git mm_ctx_mempool2(): factor out the new function --- diff --git a/contrib/mempattern.c b/contrib/mempattern.c index 177e5bae2..a04c43300 100644 --- a/contrib/mempattern.c +++ b/contrib/mempattern.c @@ -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; +} + diff --git a/contrib/mempattern.h b/contrib/mempattern.h index f277b6721..3bd6049de 100644 --- a/contrib/mempattern.h +++ b/contrib/mempattern.h @@ -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); + diff --git a/daemon/io.c b/daemon/io.c index 83263387d..11437670a 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -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); diff --git a/modules/hints/hints.c b/modules/hints/hints.c index 34edcc5de..82b584799 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -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);