From d1d8e7ae166459fcfa580b2bec8971065e217109 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Tue, 24 Dec 2013 00:23:51 +0400
Subject: [PATCH] Use the small allocator where possible instead of malloc.

---
 src/admin.cc     |  2 +-
 src/coro.cc      | 18 +++++++++++-------
 src/fiber.cc     |  5 ++++-
 src/iobuf.cc     | 11 ++++++++++-
 src/iobuf.h      |  7 ++-----
 src/iproto.cc    |  8 +++++++-
 src/tarantool.cc |  2 +-
 7 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/admin.cc b/src/admin.cc
index 487c7400b9..9f21ece810 100644
--- a/src/admin.cc
+++ b/src/admin.cc
@@ -88,7 +88,7 @@ admin_handler(va_list ap)
 	 * a remote client: it's used in Lua
 	 * stored procedures.
 	 */
-	session_create(coio.fd, *(uint64_t *) addr);
+	(void) session_create(coio.fd, *(uint64_t *) addr);
 
 	for (;;) {
 		if (admin_dispatch(&coio, iobuf, L) < 0)
diff --git a/src/coro.cc b/src/coro.cc
index 9adbcca5d0..3e1bdc6a3f 100644
--- a/src/coro.cc
+++ b/src/coro.cc
@@ -35,6 +35,7 @@
 #include <sys/mman.h>
 
 #include "third_party/valgrind/memcheck.h"
+#include "memory.h"
 
 void
 tarantool_coro_create(struct tarantool_coro *coro,
@@ -45,17 +46,18 @@ tarantool_coro_create(struct tarantool_coro *coro,
 	memset(coro, 0, sizeof(*coro));
 
 	/* TODO: guard pages */
-	coro->stack_size = page * 16;
-	coro->stack = mmap(0, coro->stack_size, PROT_READ | PROT_WRITE | PROT_EXEC,
-			   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+	coro->stack_size = page * 16 - slab_sizeof();
+	coro->stack = (char *) slab_get(slabc_runtime, coro->stack_size)
+					+ slab_sizeof();
 
-	if (coro->stack == MAP_FAILED) {
+	if (coro->stack == NULL) {
 		tnt_raise(LoggedError, ER_MEMORY_ISSUE,
 			  sizeof(coro->stack_size),
 			  "mmap", "coro stack");
 	}
 
-	(void) VALGRIND_STACK_REGISTER(coro->stack, (char *) coro->stack + coro->stack_size);
+	(void) VALGRIND_STACK_REGISTER(coro->stack, (char *)
+				       coro->stack + coro->stack_size);
 
 	coro_create(&coro->ctx, f, data, coro->stack, coro->stack_size);
 }
@@ -63,6 +65,8 @@ tarantool_coro_create(struct tarantool_coro *coro,
 void
 tarantool_coro_destroy(struct tarantool_coro *coro)
 {
-	if (coro->stack != NULL && coro->stack != MAP_FAILED)
-		munmap(coro->stack, coro->stack_size);
+	if (coro->stack != NULL) {
+		slab_put(slabc_runtime, (struct slab *)
+			 ((char *) coro->stack - slab_sizeof()));
+	}
 }
diff --git a/src/fiber.cc b/src/fiber.cc
index 305385d524..aaf78fc46b 100644
--- a/src/fiber.cc
+++ b/src/fiber.cc
@@ -47,6 +47,7 @@ static __thread uint32_t last_used_fid;
 static __thread struct mh_i32ptr_t *fiber_registry;
 static __thread struct rlist fibers, zombie_fibers, ready_fibers;
 static __thread ev_async ready_async;
+static struct mempool fiber_pool;
 
 static void
 update_last_stack_frame(struct fiber *fiber)
@@ -426,7 +427,8 @@ fiber_new(const char *name, void (*f) (va_list))
 		fiber = rlist_first_entry(&zombie_fibers, struct fiber, link);
 		rlist_move_entry(&fibers, fiber, link);
 	} else {
-		fiber = (struct fiber *) calloc(1, sizeof(*fiber));
+		fiber = (struct fiber *) mempool_alloc(&fiber_pool);
+		memset(fiber, 0, sizeof(*fiber));
 
 		tarantool_coro_create(&fiber->coro, fiber_loop, NULL);
 
@@ -484,6 +486,7 @@ fiber_destroy_all()
 void
 fiber_init(void)
 {
+	mempool_create(&fiber_pool, slabc_runtime, sizeof(struct fiber));
 	rlist_create(&fibers);
 	rlist_create(&ready_fibers);
 	rlist_create(&zombie_fibers);
diff --git a/src/iobuf.cc b/src/iobuf.cc
index fc8b0779b8..6e6731c961 100644
--- a/src/iobuf.cc
+++ b/src/iobuf.cc
@@ -30,6 +30,8 @@
 #include "coio_buf.h"
 #include "memory.h"
 
+struct mempool iobuf_pool;
+
 /* {{{ struct ibuf */
 
 /** Initialize an input buffer. */
@@ -274,7 +276,7 @@ iobuf_new(const char *name)
 {
 	struct iobuf *iobuf;
 	if (SLIST_EMPTY(&iobuf_cache)) {
-		iobuf = (struct iobuf *) malloc(sizeof(struct iobuf));
+		iobuf = (struct iobuf *) mempool_alloc(&iobuf_pool);
 		region_create(&iobuf->pool, slabc_runtime);
 		/* Note: do not allocate memory upfront. */
 		ibuf_create(&iobuf->in, &iobuf->pool);
@@ -342,4 +344,11 @@ iobuf_gc(struct iobuf *iobuf)
 
 int cfg_readahead;
 
+void
+iobuf_init(int readahead)
+{
+	mempool_create(&iobuf_pool, slabc_runtime, sizeof(struct iobuf));
+	cfg_readahead =  readahead;
+}
+
 /* struct iobuf }}} */
diff --git a/src/iobuf.h b/src/iobuf.h
index 3a7fead9f0..ff65bd70ff 100644
--- a/src/iobuf.h
+++ b/src/iobuf.h
@@ -240,11 +240,8 @@ iobuf_is_idle(struct iobuf *iobuf)
  */
 extern int cfg_readahead;
 
-static inline void
-iobuf_init_readahead(int readahead)
-{
-	cfg_readahead =  readahead;
-}
+void
+iobuf_init(int readahead);
 
 /* }}} */
 
diff --git a/src/iproto.cc b/src/iproto.cc
index 0316f24eaa..0563b9e7f5 100644
--- a/src/iproto.cc
+++ b/src/iproto.cc
@@ -41,6 +41,7 @@
 #include "evio.h"
 #include "session.h"
 #include "scoped_guard.h"
+#include "memory.h"
 
 static struct iproto_header dummy_header = { 0, 0, 0 };
 const uint32_t msg_ping = 0xff00;
@@ -283,6 +284,8 @@ struct iproto_session
 SLIST_HEAD(, iproto_session) iproto_session_cache =
 	SLIST_HEAD_INITIALIZER(iproto_session_cache);
 
+static struct mempool iproto_session_pool;
+
 /**
  * A session is idle when the client is gone
  * and there are no outstanding requests in the request queue.
@@ -334,7 +337,8 @@ iproto_session_create(const char *name, int fd, struct sockaddr_in *addr,
 {
 	struct iproto_session *session;
 	if (SLIST_EMPTY(&iproto_session_cache)) {
-		session = (struct iproto_session *) malloc(sizeof(*session));
+		session = (struct iproto_session *)
+			mempool_alloc(&iproto_session_pool);
 		session->input.data = session->output.data = session;
 	} else {
 		session = SLIST_FIRST(&iproto_session_cache);
@@ -791,5 +795,7 @@ iproto_init(const char *bind_ipaddr, int primary_port,
 	}
 	iproto_queue_init(&request_queue, IPROTO_REQUEST_QUEUE_SIZE,
 			  iproto_queue_handler);
+	mempool_create(&iproto_session_pool, slabc_runtime,
+		       sizeof(struct iproto_session));
 }
 
diff --git a/src/tarantool.cc b/src/tarantool.cc
index f52f6c74e4..49a3148026 100644
--- a/src/tarantool.cc
+++ b/src/tarantool.cc
@@ -777,7 +777,7 @@ main(int argc, char **argv)
 	ev_default_loop(EVFLAG_AUTO);
 	fiber_init();
 	replication_prefork();
-	iobuf_init_readahead(cfg.readahead);
+	iobuf_init(cfg.readahead);
 	coeio_init();
 	signal_init();
 
-- 
GitLab