[CRIU] [PATCH 1/2] core: Allocate CoreEntry (except arch) with single xmalloc

Pavel Emelyanov xemul at parallels.com
Thu Mar 13 04:23:12 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 include/xmalloc.h |   20 ++++++++++++
 pstree.c          |   86 +++++++++++++++++++++++------------------------------
 2 files changed, 57 insertions(+), 49 deletions(-)

diff --git a/include/xmalloc.h b/include/xmalloc.h
index a5cc608..167c3b9 100644
--- a/include/xmalloc.h
+++ b/include/xmalloc.h
@@ -36,4 +36,24 @@
 #define memzero_p(p)		memset(p, 0, sizeof(*p))
 #define memzero(p, size)	memset(p, 0, size)
 
+/*
+ * Helper for allocating trees with single xmalloc.
+ * This one advances the void *pointer on s bytes and
+ * returns the previous value. Use like this
+ *
+ * m = xmalloc(total_size);
+ * a = xptr_pull(&m, tree_root_t);
+ * a->b = xptr_pull(&m, leaf_a_t);
+ * a->c = xptr_pull(&m, leaf_c_t);
+ * ...
+ */
+static inline void *xptr_pull_s(void **m, size_t s)
+{
+	void *ret = (*m);
+	(*m) += s;
+	return ret;
+}
+
+#define xptr_pull(m, type)	xptr_pull_s(m, sizeof(type))
+
 #endif /* __CR_XMALLOC_H__ */
diff --git a/pstree.c b/pstree.c
index fc0f707..eba0c30 100644
--- a/pstree.c
+++ b/pstree.c
@@ -18,61 +18,49 @@ struct pstree_item *root_item;
 
 void core_entry_free(CoreEntry *core)
 {
-	if (core) {
-		arch_free_thread_info(core);
-		if (core->thread_core)
-			xfree(core->thread_core->sas);
-		xfree(core->thread_core);
-		xfree(core->tc);
-		xfree(core->ids);
-	}
+	arch_free_thread_info(core);
+	xfree(core);
 }
 
-CoreEntry *core_entry_alloc(int alloc_thread_info, int alloc_tc)
+CoreEntry *core_entry_alloc(int th, int tsk)
 {
-	CoreEntry *core;
-	TaskCoreEntry *tc;
-
-	core = xmalloc(sizeof(*core));
-	if (!core)
-		return NULL;
-	core_entry__init(core);
-
-	core->mtype = CORE_ENTRY__MARCH;
-
-	if (alloc_thread_info) {
-		ThreadCoreEntry *thread_core;
-		ThreadSasEntry *sas;
-
-		if (arch_alloc_thread_info(core))
-			goto err;
-
-		thread_core = xmalloc(sizeof(*thread_core));
-		if (!thread_core)
-			goto err;
-		thread_core_entry__init(thread_core);
-		core->thread_core = thread_core;
+	size_t sz;
+	CoreEntry *core = NULL;
+	void *m;
+
+	sz = sizeof(CoreEntry);
+	if (tsk)
+		sz += sizeof(TaskCoreEntry) + TASK_COMM_LEN;
+	if (th)
+		sz += sizeof(ThreadCoreEntry) + sizeof(ThreadSasEntry);
+
+	m = xmalloc(sz);
+	if (m) {
+		core = xptr_pull(&m, CoreEntry);
+		core_entry__init(core);
+		core->mtype = CORE_ENTRY__MARCH;
+
+		if (tsk) {
+			core->tc = xptr_pull(&m, TaskCoreEntry);
+			task_core_entry__init(core->tc);
+			core->tc->comm = xptr_pull_s(&m, TASK_COMM_LEN);
+			memzero(core->tc->comm, TASK_COMM_LEN);
+		}
 
-		sas = xmalloc(sizeof(*sas));
-		if (!sas)
-			goto err;
-		thread_sas_entry__init(sas);
-		core->thread_core->sas = sas;
-	}
+		if (th) {
+			core->thread_core = xptr_pull(&m, ThreadCoreEntry);
+			thread_core_entry__init(core->thread_core);
+			core->thread_core->sas = xptr_pull(&m, ThreadSasEntry);
+			thread_sas_entry__init(core->thread_core->sas);
 
-	if (alloc_tc) {
-		tc = xzalloc(sizeof(*tc) + TASK_COMM_LEN);
-		if (!tc)
-			goto err;
-		task_core_entry__init(tc);
-		tc->comm = (void *)tc + sizeof(*tc);
-		core->tc = tc;
+			if (arch_alloc_thread_info(core)) {
+				xfree(core);
+				core = NULL;
+			}
+		}
 	}
 
 	return core;
-err:
-	core_entry_free(core);
-	return NULL;
 }
 
 int pstree_alloc_cores(struct pstree_item *item)
-- 
1.7.6.5


More information about the CRIU mailing list