From f9f7ff66c3fbe2676dd63418eb162e88bb44386b Mon Sep 17 00:00:00 2001
From: Nikita Pettik <korablev@tarantool.org>
Date: Thu, 3 Jun 2021 18:09:46 +0300
Subject: [PATCH] memtx: introduce interfaces for MemtxAllocator

That includes:
 - foreach_memtx_allocator() - for each basic allocator (Small and Sys
   so far) we declare corresponding MemtxAllocator. So to iterate over
   all allocators and invoke same function we are going to use this
   helper;
 - memtx_allocators_init() - invoke ::create() of each Allocator and
   corresponding MemtxAllocator;
 - memtx_allocators_destroy() - invoke ::destroy() of each Allocator
   and corresponding MemtxAllocator;
 - memtx_allocators_set_mode() - set given delayed free mode for each
   MemtxAllocator.

Follow-up #5419
---
 src/box/CMakeLists.txt     |  1 +
 src/box/memtx_allocator.cc | 65 ++++++++++++++++++++++++++++++++++++++
 src/box/memtx_allocator.h  | 22 +++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 src/box/memtx_allocator.cc

diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index d07f816fe8..0fb47fa7a5 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -115,6 +115,7 @@ target_link_libraries(xlog core box_error crc32 ${ZSTD_LIBRARIES})
 
 add_library(box STATIC
     allocator.cc
+    memtx_allocator.cc
     msgpack.c
     iproto.cc
     xrow_io.cc
diff --git a/src/box/memtx_allocator.cc b/src/box/memtx_allocator.cc
new file mode 100644
index 0000000000..433f22ee6a
--- /dev/null
+++ b/src/box/memtx_allocator.cc
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2010-2021, Tarantool AUTHORS, please see AUTHORS file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ *    copyright notice, this list of conditions and the
+ *    following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials
+ *    provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include "memtx_allocator.h"
+
+struct memtx_allocator_set_mode {
+	template<typename Allocator, typename...Arg>
+	void
+	invoke(Arg&&...mode)
+	{
+		Allocator::set_mode(mode...);
+	}
+};
+
+void
+memtx_allocators_init(struct memtx_engine *memtx,
+		      struct allocator_settings *settings)
+{
+	foreach_allocator<allocator_create,
+		struct allocator_settings *&>(settings);
+
+	foreach_memtx_allocator<allocator_create,
+		enum memtx_engine_free_mode &>(memtx->free_mode);
+}
+
+void
+memtx_allocators_set_mode(enum memtx_engine_free_mode mode)
+{
+	foreach_memtx_allocator<memtx_allocator_set_mode,
+		enum memtx_engine_free_mode &>(mode);
+}
+
+void
+memtx_allocators_destroy()
+{
+	foreach_memtx_allocator<allocator_destroy>();
+	foreach_allocator<allocator_destroy>();
+}
diff --git a/src/box/memtx_allocator.h b/src/box/memtx_allocator.h
index 6bf649de2d..e7c8ecaf22 100644
--- a/src/box/memtx_allocator.h
+++ b/src/box/memtx_allocator.h
@@ -118,3 +118,25 @@ struct lifo MemtxAllocator<Allocator>::lifo;
 
 template<class Allocator>
 enum memtx_engine_free_mode MemtxAllocator<Allocator>::mode;
+
+void
+memtx_allocators_init(struct memtx_engine *memtx,
+		      struct allocator_settings *settings);
+
+void
+memtx_allocators_set_mode(enum memtx_engine_free_mode mode);
+
+void
+memtx_allocators_destroy();
+
+using memtx_allocators = std::tuple<MemtxAllocator<SmallAlloc>,
+				    MemtxAllocator<SysAlloc>>;
+
+template<class F, class...Arg>
+static void
+foreach_memtx_allocator(Arg&&...arg)
+{
+	F f;
+	foreach_allocator_internal((memtx_allocators *) nullptr, f,
+				   std::forward<Arg>(arg)...);
+}
-- 
GitLab