Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com
Cc: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [PATCH v1 7/8] box: introduce memtx_slab_alloc helper
Date: Thu, 30 May 2019 13:45:34 +0300	[thread overview]
Message-ID: <d9576e35496c5dc6f1316ce5feb9d294dc1b5f96.1559212747.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1559212747.git.kshcherbatov@tarantool.org>

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

  parent reply	other threads:[~2019-05-30 10:45 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-30 10:45 [PATCH v1 0/8] box: functional indexes Kirill Shcherbatov
2019-05-30 10:45 ` [PATCH v1 1/8] box: refactor box_lua_find helper Kirill Shcherbatov
2019-05-30 10:45 ` [PATCH v1 2/8] box: rework func cache update machinery Kirill Shcherbatov
2019-05-30 10:45 ` [PATCH v1 3/8] schema: rework _func system space format Kirill Shcherbatov
2019-05-30 12:06   ` [tarantool-patches] " Konstantin Osipov
2019-06-03 16:14     ` Vladimir Davydov
2019-05-30 13:10   ` Vladislav Shpilevoy
2019-05-30 10:45 ` [PATCH v1 4/8] box: load persistent Lua functions on creation Kirill Shcherbatov
2019-05-31  8:16   ` [tarantool-patches] " Konstantin Osipov
2019-06-03  8:26     ` [tarantool-patches] " Kirill Shcherbatov
2019-05-30 10:45 ` [PATCH v1 5/8] netbox: call persistent functions in netbox Kirill Shcherbatov
2019-05-30 10:45 ` [PATCH v1 6/8] box: export _func functions with box.func folder Kirill Shcherbatov
2019-06-03 16:22   ` Vladimir Davydov
2019-06-03 16:24   ` Vladimir Davydov
2019-05-30 10:45 ` Kirill Shcherbatov [this message]
2019-05-30 10:45 ` [PATCH v1 8/8] box: introduce functional indexes in memtx Kirill Shcherbatov
2019-05-30 11:18 ` [tarantool-patches] [PATCH v1 0/8] box: functional indexes Vladislav Shpilevoy
2019-05-30 11:43   ` [tarantool-patches] " Kirill Shcherbatov
2019-05-30 11:47 ` [tarantool-patches] " Konstantin Osipov
2019-06-03 15:55 ` Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d9576e35496c5dc6f1316ce5feb9d294dc1b5f96.1559212747.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v1 7/8] box: introduce memtx_slab_alloc helper' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox