[PATCH v1 7/8] box: introduce memtx_slab_alloc helper

Kirill Shcherbatov kshcherbatov at tarantool.org
Thu May 30 13:45:34 MSK 2019


The new memtx_slab_alloc routine allocates a memory using memtx
engine slab allocator. Previously this code used to be call only
to allocate a new tuple for memtx engine. In further patches we
are going to allocate the keys returned by functional index
extractor routine so we need this function in engine module API.
The pair call memtx_slab_free is also exported.

Needed for #1260
---
 src/box/memtx_engine.c | 41 +++++++++++++++++++++++++++--------------
 src/box/memtx_engine.h | 19 +++++++++++++++++++
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/src/box/memtx_engine.c b/src/box/memtx_engine.c
index f4312484a..8faa9fa57 100644
--- a/src/box/memtx_engine.c
+++ b/src/box/memtx_engine.c
@@ -1127,6 +1127,29 @@ memtx_engine_set_max_tuple_size(struct memtx_engine *memtx, size_t max_size)
 	memtx->max_tuple_size = max_size;
 }
 
+void *
+memtx_slab_alloc(struct memtx_engine *memtx, size_t size)
+{
+	void *ptr;
+	while ((ptr = smalloc(&memtx->alloc, size)) == NULL) {
+		bool stop;
+		memtx_engine_run_gc(memtx, &stop);
+		if (stop)
+			break;
+	}
+	return ptr;
+}
+
+void
+memtx_slab_free(struct memtx_engine *memtx, void *ptr, size_t size,
+		bool is_not_delayed)
+{
+	if (memtx->alloc.free_mode != SMALL_DELAYED_FREE || is_not_delayed)
+		smfree(&memtx->alloc, ptr, size);
+	else
+		smfree_delayed(&memtx->alloc, ptr, size);
+}
+
 struct tuple *
 memtx_tuple_new(struct tuple_format *format, const char *data, const char *end)
 {
@@ -1152,14 +1175,7 @@ memtx_tuple_new(struct tuple_format *format, const char *data, const char *end)
 		error_log(diag_last_error(diag_get()));
 		goto end;
 	}
-
-	struct memtx_tuple *memtx_tuple;
-	while ((memtx_tuple = smalloc(&memtx->alloc, total)) == NULL) {
-		bool stop;
-		memtx_engine_run_gc(memtx, &stop);
-		if (stop)
-			break;
-	}
+	struct memtx_tuple *memtx_tuple = memtx_slab_alloc(memtx, total);
 	if (memtx_tuple == NULL) {
 		diag_set(OutOfMemory, total, "slab allocator", "memtx_tuple");
 		goto end;
@@ -1196,12 +1212,9 @@ memtx_tuple_delete(struct tuple_format *format, struct tuple *tuple)
 	struct memtx_tuple *memtx_tuple =
 		container_of(tuple, struct memtx_tuple, base);
 	size_t total = tuple_size(tuple) + offsetof(struct memtx_tuple, base);
-	if (memtx->alloc.free_mode != SMALL_DELAYED_FREE ||
-	    memtx_tuple->version == memtx->snapshot_version ||
-	    format->is_temporary)
-		smfree(&memtx->alloc, memtx_tuple, total);
-	else
-		smfree_delayed(&memtx->alloc, memtx_tuple, total);
+	memtx_slab_free(memtx, memtx_tuple, total,
+			memtx_tuple->version == memtx->snapshot_version ||
+			format->is_temporary);
 }
 
 struct tuple_format_vtab memtx_tuple_format_vtab = {
diff --git a/src/box/memtx_engine.h b/src/box/memtx_engine.h
index ccb51678d..60f3a11bf 100644
--- a/src/box/memtx_engine.h
+++ b/src/box/memtx_engine.h
@@ -250,6 +250,25 @@ bool
 memtx_index_def_change_requires_rebuild(struct index *index,
 					const struct index_def *new_def);
 
+/**
+ * Allocate size bytes using memtx engine slab allocator and
+ * return a pointer to the allocated memory.
+ * The memory is not initialized.
+ *
+ * Returns not NULL pointer to the allocated memory is case of
+ * success and NULL otherwise.
+ */
+void *
+memtx_slab_alloc(struct memtx_engine *memtx, size_t size);
+
+/**
+ * Release ptr size bytes memory chunk allocated with
+ * memtx_slab_alloc.
+ */
+void
+memtx_slab_free(struct memtx_engine *memtx, void *ptr, size_t size,
+		bool is_not_delayed);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 
-- 
2.21.0




More information about the Tarantool-patches mailing list