From ae8d7714dc7078fa8e4ae270169e3f676208877d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Tue, 22 Dec 2020 14:09:20 +0100 Subject: [PATCH] contrib/mempattern: integrate other related functions --- contrib/mempattern.c | 17 +++++++++++++++++ contrib/mempattern.h | 28 +++++++++++++++++++++++++++- lib/utils.c | 13 ------------- lib/utils.h | 25 ------------------------- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/contrib/mempattern.c b/contrib/mempattern.c index 96715c9ea..177e5bae2 100644 --- a/contrib/mempattern.c +++ b/contrib/mempattern.c @@ -119,3 +119,20 @@ void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size) mm->alloc = (knot_mm_alloc_t)mp_alloc; mm->free = mm_nofree; } + + +/* Code in addition to Knot's mempattern. */ + +void *mm_malloc_aligned(void *ctx, size_t n) +{ + size_t alignment = (size_t)ctx; + void *res; + int err = posix_memalign(&res, alignment, n); + if (err == 0) { + return res; + } else { + assert(err == -1 && errno == ENOMEM); + return NULL; + } +} + diff --git a/contrib/mempattern.h b/contrib/mempattern.h index f8ed5c454..f277b6721 100644 --- a/contrib/mempattern.h +++ b/contrib/mempattern.h @@ -22,6 +22,8 @@ #include #include "lib/defines.h" +#include +#include /*! \brief Default memory block size. */ #define MM_DEFAULT_BLKSIZE 4096 @@ -50,6 +52,30 @@ void mm_ctx_init(knot_mm_t *mm); /*! \brief Memory pool context. */ void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size); -/*! \brief Simple malloc wrapper. Not exposed in knot's mempattern. */ + +/* API in addition to Knot's mempattern. */ + +/*! \brief Simple malloc wrapper. */ void *mm_malloc(void *ctx, size_t n); +/*! \brief Readability: avoid const-casts in code. */ +static inline void free_const(const void *what) +{ + free((void *)what); +} + +/*! \brief posix_memalign() wrapper. */ +void *mm_malloc_aligned(void *ctx, size_t n); + +/*! \brief Initialize mm with malloc+free with specified alignment (a power of two). */ +static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment) +{ + assert(__builtin_popcount(alignment) == 1); + mm->ctx = (uint8_t *)NULL + alignment; /*< roundabout to satisfy linters */ + /* posix_memalign() doesn't allow alignment < sizeof(void*), + * and there's no point in using it for small values anyway, + * as plain malloc() guarantees at least max_align_t. */ + mm->alloc = alignment > sizeof(max_align_t) ? mm_malloc_aligned : mm_malloc; + mm->free = free; +} + diff --git a/lib/utils.c b/lib/utils.c index c398c6a13..50a6848a0 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -39,19 +39,6 @@ /* Logging & debugging */ bool kr_verbose_status = false; -void *mm_malloc_aligned(void *ctx, size_t n) -{ - size_t alignment = (size_t)ctx; - void *res; - int err = posix_memalign(&res, alignment, n); - if (err == 0) { - return res; - } else { - assert(err == -1 && errno == ENOMEM); - return NULL; - } -} - /* * Macros. */ diff --git a/lib/utils.h b/lib/utils.h index 990c6c04a..54682bbd7 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -111,33 +111,8 @@ void kr_log_q(const struct kr_query *qry, const char *source, const char *fmt, . #define static_assert(cond, msg) #endif -/** @cond Memory alloc routines: extension of ./contrib/mempattern.h */ - -/** Readability: avoid const-casts in code. */ -static inline void free_const(const void *what) -{ - free((void *)what); -} - // Use this for alocations with mm. // Use mm_alloc for alocations into mempool -/** posix_memalign() wrapper. */ -void *mm_malloc_aligned(void *ctx, size_t n); - -/** Initialize mm with malloc+free with higher alignment (a power of two). */ -static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment) -{ - assert(__builtin_popcount(alignment) == 1); - mm->ctx = (uint8_t *)NULL + alignment; /*< roundabout to satisfy linters */ - /* posix_memalign() doesn't allow alignment < sizeof(void*), - * and there's no point in using it for small values anyway, - * as plain malloc() guarantees at least max_align_t. - * Nitpick: we might use that type when assuming C11. */ - mm->alloc = alignment > sizeof(void*) ? mm_malloc_aligned : mm_malloc; - mm->free = free; -} - -/* @endcond */ /** A strcmp() variant directly usable for qsort() on an array of strings. */ static inline int strcmp_p(const void *p1, const void *p2) -- 2.47.2