[CRIU] [PATCH 2/2] Introduce slab facility

Cyrill Gorcunov gorcunov at openvz.org
Mon Apr 9 09:28:29 EDT 2012


At restore time we use shared memory maps
a lot, so instead of open-coded procedure
introduce slab facility, which tracks memory
slab in once place.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile       |    1 +
 include/slab.h |   14 ++++++++++++++
 slab.c         |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100644 include/slab.h
 create mode 100644 slab.c

diff --git a/Makefile b/Makefile
index 9d01097..91e0bb7 100644
--- a/Makefile
+++ b/Makefile
@@ -47,6 +47,7 @@ OBJS		+= file-ids.o
 OBJS		+= namespaces.o
 OBJS		+= uts_ns.o
 OBJS		+= ipc_ns.o
+OBJS		+= slab.o
 
 OBJS-BLOB	+= parasite.o
 SRCS-BLOB	+= $(patsubst %.o,%.c,$(OBJS-BLOB))
diff --git a/include/slab.h b/include/slab.h
new file mode 100644
index 0000000..bbdbe64
--- /dev/null
+++ b/include/slab.h
@@ -0,0 +1,14 @@
+#ifndef SLAB_H__
+#define SLAB_H__
+
+typedef struct {
+	unsigned long	start;
+	unsigned long	next;
+	unsigned long	end;
+} slab_t;
+
+extern void *slab_alloc(slab_t *slab, unsigned long size);
+extern slab_t *slab_init(unsigned int order);
+extern void slab_fini(slab_t **slab);
+
+#endif /* SLAB_H__ */
diff --git a/slab.c b/slab.c
new file mode 100644
index 0000000..57825f9
--- /dev/null
+++ b/slab.c
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+#include <sys/mman.h>
+
+#include "compiler.h"
+#include "types.h"
+
+#include "slab.h"
+#include "log.h"
+
+void *slab_alloc(slab_t *slab, unsigned long size)
+{
+	unsigned long end = slab->next + round_up(size, 8);
+	unsigned long cur = slab->next;
+
+	if (end >= slab->end)
+		return NULL;
+	slab->next = end;
+	return (void *)cur;
+}
+
+slab_t *slab_init(unsigned int order)
+{
+	unsigned long size = PAGE_SIZE << order;
+	slab_t *slab;
+
+	slab = mmap(NULL, size, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANON, 0, 0);
+	if (slab == MAP_FAILED) {
+		pr_perror("Can't allocate slab");
+		return NULL;
+	}
+
+	slab->start	= round_up((unsigned long)slab, sizeof(*slab));
+	slab->end	= (unsigned long)slab + size;
+	slab->next	= slab->start;
+
+	return slab;
+}
+
+void slab_fini(slab_t **slab)
+{
+	if (slab && *slab) {
+		munmap(*slab, (*slab)->end - (*slab)->start);
+		*slab = NULL;
+	}
+}
-- 
1.7.7.6



More information about the CRIU mailing list