]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
contrib/mempattern: integrate other related functions
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 22 Dec 2020 13:09:20 +0000 (14:09 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 13 Jan 2021 08:49:13 +0000 (09:49 +0100)
contrib/mempattern.c
contrib/mempattern.h
lib/utils.c
lib/utils.h

index 96715c9eadaedd36bcf2b834177ad3cfee823444..177e5bae2b774b1f568b2b435fb0680e5a0bdf55 100644 (file)
@@ -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;
+       }
+}
+
index f8ed5c45440469fbc8025d78edddf8002015b334..f277b6721e03b0a056429a8bcc01bf54418b7475 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <libknot/mm_ctx.h>
 #include "lib/defines.h"
+#include <assert.h>
+#include <stdint.h>
 
 /*! \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;
+}
+
index c398c6a13b5d03c931bd66901e059146cbe33c78..50a6848a053e0be9cf5eac982f60ea97178ca405 100644 (file)
 /* 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.
  */
index 990c6c04ab50c9f514b3465bbebeb746fcbcdf3e..54682bbd7ff4edbfd88f885f7db8c252d603b015 100644 (file)
@@ -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)