From d801e47411b3fd9e7aa0bd422d809a2c13df9c5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= <vladimir.cunat@nic.cz>
Date: Mon, 4 Oct 2021 09:53:34 +0200
Subject: [PATCH] contrib/mempattern: add mm_ctx_delete()

It was a bit weird that the API had mempool creation but no deletion.
---
 contrib/mempattern.h   | 15 ++++++++++++---
 tests/unit/meson.build |  2 +-
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/contrib/mempattern.h b/contrib/mempattern.h
index 0b9957b1d..a0cd9a62d 100644
--- a/contrib/mempattern.h
+++ b/contrib/mempattern.h
@@ -21,6 +21,7 @@
 #pragma once
 
 #include <libknot/mm_ctx.h>
+#include "contrib/ucw/mempool.h"
 #include "lib/defines.h"
 #include <assert.h>
 #include <stdint.h>
@@ -55,6 +56,17 @@ void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size);
 
 /* API in addition to Knot's mempattern. */
 
+/*! \brief New memory pool context, allocated on itself. */
+KR_EXPORT knot_mm_t * mm_ctx_mempool2(size_t chunk_size);
+
+/*! \brief Delete a memory pool.  OK to call on a non-pool. */
+static inline void mm_ctx_delete(knot_mm_t *mm)
+{
+	/* The mp_alloc comparison bears a risk of missing the private symbol from knot. */
+	if (mm && mm->ctx && mm->alloc == (knot_mm_alloc_t)mp_alloc)
+		mp_delete(mm->ctx);
+}
+
 /*! \brief Readability: avoid const-casts in code. */
 static inline void free_const(const void *what)
 {
@@ -78,6 +90,3 @@ static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment)
 	}
 }
 
-/*! \brief New memory pool context, allocated on itself. */
-KR_EXPORT knot_mm_t * mm_ctx_mempool2(size_t chunk_size);
-
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index a133e6180..b10789cc2 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -11,7 +11,7 @@ mock_cmodule_mod = shared_module(
   mock_cmodule_src,
   name_prefix: '',
   dependencies: libknot,
-  include_directories: include_directories('../../'),
+  include_directories: mod_inc_dir,
 )
 
 # executables with tests
-- 
GitLab