]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
copy mempattern files from Knot DNS as they are
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 22 Dec 2020 11:44:39 +0000 (12:44 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 13 Jan 2021 08:49:11 +0000 (09:49 +0100)
It seems just easier than having the copies in the current way.
I don't think the `static inline` were helping us anyway,
except for avoiding KR_EXPORT in some cases.

Still, differences when copying:
 - we use plain memset() in the implementation
   (no motivation here to use the complex memzero() approach)
 - we expose mm_malloc(), as we've been referring to it
 - we KR_EXPORT some of the functions (for lua and modules)

contrib/mempattern.c [new file with mode: 0644]
contrib/mempattern.h [new file with mode: 0644]
contrib/meson.build
lib/utils.c
lib/utils.h

diff --git a/contrib/mempattern.c b/contrib/mempattern.c
new file mode 100644 (file)
index 0000000..96715c9
--- /dev/null
@@ -0,0 +1,121 @@
+/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+
+#include "contrib/mempattern.h"
+#include "contrib/ucw/mempool.h"
+
+static void mm_nofree(void *p)
+{
+       /* nop */
+}
+
+void *mm_malloc(void *ctx, size_t n)
+{
+       (void)ctx;
+       return malloc(n);
+}
+
+void *mm_alloc(knot_mm_t *mm, size_t size)
+{
+       if (mm) {
+               return mm->alloc(mm->ctx, size);
+       } else {
+               return malloc(size);
+       }
+}
+
+void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size)
+{
+       if (nmemb == 0 || size == 0) {
+               return NULL;
+       }
+       if (mm) {
+               size_t total_size = nmemb * size;
+               if (total_size / nmemb != size) { // Overflow check
+                       return NULL;
+               }
+               void *mem = mm_alloc(mm, total_size);
+               if (mem == NULL) {
+                       return NULL;
+               }
+               return memset(mem, 0, total_size);
+       } else {
+               return calloc(nmemb, size);
+       }
+}
+
+void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
+{
+       if (mm) {
+               void *p = mm->alloc(mm->ctx, size);
+               if (p == NULL) {
+                       return NULL;
+               } else {
+                       if (what) {
+                               memcpy(p, what,
+                                      prev_size < size ? prev_size : size);
+                       }
+                       mm_free(mm, what);
+                       return p;
+               }
+       } else {
+               return realloc(what, size);
+       }
+}
+
+char *mm_strdup(knot_mm_t *mm, const char *s)
+{
+       if (s == NULL) {
+               return NULL;
+       }
+       if (mm) {
+               size_t len = strlen(s) + 1;
+               void *mem = mm_alloc(mm, len);
+               if (mem == NULL) {
+                       return NULL;
+               }
+               return memcpy(mem, s, len);
+       } else {
+               return strdup(s);
+       }
+}
+
+void mm_free(knot_mm_t *mm, void *what)
+{
+       if (mm) {
+               if (mm->free) {
+                       mm->free(what);
+               }
+       } else {
+               free(what);
+       }
+}
+
+void mm_ctx_init(knot_mm_t *mm)
+{
+       mm->ctx = NULL;
+       mm->alloc = mm_malloc;
+       mm->free = free;
+}
+
+void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size)
+{
+       mm->ctx = mp_new(chunk_size);
+       mm->alloc = (knot_mm_alloc_t)mp_alloc;
+       mm->free = mm_nofree;
+}
diff --git a/contrib/mempattern.h b/contrib/mempattern.h
new file mode 100644 (file)
index 0000000..f8ed5c4
--- /dev/null
@@ -0,0 +1,55 @@
+/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*!
+ * \brief Memory allocation related functions.
+ */
+
+#pragma once
+
+#include <libknot/mm_ctx.h>
+#include "lib/defines.h"
+
+/*! \brief Default memory block size. */
+#define MM_DEFAULT_BLKSIZE 4096
+
+/*! \brief Allocs using 'mm' if any, uses system malloc() otherwise. */
+KR_EXPORT
+void *mm_alloc(knot_mm_t *mm, size_t size);
+
+/*! \brief Callocs using 'mm' if any, uses system calloc() otherwise. */
+void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size);
+
+/*! \brief Reallocs using 'mm' if any, uses system realloc() otherwise. */
+KR_EXPORT
+void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
+
+/*! \brief Strdups using 'mm' if any, uses system strdup() otherwise. */
+char *mm_strdup(knot_mm_t *mm, const char *s);
+
+/*! \brief Free using 'mm' if any, uses system free() otherwise. */
+KR_EXPORT
+void mm_free(knot_mm_t *mm, void *what);
+
+/*! \brief Initialize default memory allocation context. */
+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. */
+void *mm_malloc(void *ctx, size_t n);
+
index effbafda4cd1698921d8e7b57ad44aceeb6ed10c..5d97e8891a4600e26ebb4a33ae1d2bc9c63e491a 100644 (file)
@@ -6,6 +6,7 @@ contrib_src = files([
   'ccan/json/json.c',
   'ucw/mempool.c',
   'ucw/mempool-fmt.c',
+  'mempattern.c',
   'murmurhash3/murmurhash3.c',
   'base32hex.c',
   'base64.c',
index f2027a90d39d5dbbcc82f636311d9c4318004a53..c398c6a13b5d03c931bd66901e059146cbe33c78 100644 (file)
 /* Logging & debugging */
 bool kr_verbose_status = false;
 
-void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
-{
-       if (mm) {
-               void *p = mm->alloc(mm->ctx, size);
-               if (p == NULL) {
-                       return NULL;
-               } else {
-                       if (what) {
-                               memcpy(p, what,
-                                      prev_size < size ? prev_size : size);
-                       }
-                       mm_free(mm, what);
-                       return p;
-               }
-       } else {
-               return realloc(what, size);
-       }
-}
-
-void *mm_malloc(void *ctx, size_t n)
-{
-       (void)ctx;
-       return malloc(n);
-}
 void *mm_malloc_aligned(void *ctx, size_t n)
 {
        size_t alignment = (size_t)ctx;
index b4f92fa63037dc6c233d2bbf0a8b7ebd45a3d50c..990c6c04ab50c9f514b3465bbebeb746fcbcdf3e 100644 (file)
@@ -23,8 +23,9 @@
 #include <uv.h>
 
 #include "kresconfig.h"
-#include "lib/generic/array.h"
+#include "contrib/mempattern.h"
 #include "lib/defines.h"
+#include "lib/generic/array.h"
 
 struct kr_query;
 struct kr_request;
@@ -110,7 +111,7 @@ 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 */
+/** @cond Memory alloc routines: extension of ./contrib/mempattern.h */
 
 /** Readability: avoid const-casts in code. */
 static inline void free_const(const void *what)
@@ -119,38 +120,10 @@ static inline void free_const(const void *what)
 }
 
 // Use this for alocations with mm.
-static inline void *mm_alloc(knot_mm_t *mm, size_t size)
-{
-       if (mm) return mm->alloc(mm->ctx, size);
-       else return malloc(size);
-}
-static inline void mm_free(knot_mm_t *mm, const void *what)
-{
-       if (mm) {
-               if (mm->free)
-                       mm->free((void *)what);
-       }
-       else free_const(what);
-}
-
-/** Realloc implementation using memory context. */
-KR_EXPORT
-void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
-
-/** Trivial malloc() wrapper. */
 // Use mm_alloc for alocations into mempool
-void *mm_malloc(void *ctx, size_t n);
 /** posix_memalign() wrapper. */
 void *mm_malloc_aligned(void *ctx, size_t n);
 
-/** Initialize mm with standard malloc+free. */
-static inline void mm_ctx_init(knot_mm_t *mm)
-{
-       mm->ctx = NULL;
-       mm->alloc = mm_malloc;
-       mm->free = free;
-}
-
 /** 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)
 {