Commit 2616c87d authored by Pavel Emelyanov's avatar Pavel Emelyanov

core: Allocate CoreEntry (except arch) with single xmalloc

Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Acked-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 307a5e10
...@@ -36,4 +36,24 @@ ...@@ -36,4 +36,24 @@
#define memzero_p(p) memset(p, 0, sizeof(*p)) #define memzero_p(p) memset(p, 0, sizeof(*p))
#define memzero(p, size) memset(p, 0, size) #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__ */ #endif /* __CR_XMALLOC_H__ */
...@@ -18,61 +18,49 @@ struct pstree_item *root_item; ...@@ -18,61 +18,49 @@ struct pstree_item *root_item;
void core_entry_free(CoreEntry *core) void core_entry_free(CoreEntry *core)
{ {
if (core) {
arch_free_thread_info(core); arch_free_thread_info(core);
if (core->thread_core) xfree(core);
xfree(core->thread_core->sas);
xfree(core->thread_core);
xfree(core->tc);
xfree(core->ids);
}
} }
CoreEntry *core_entry_alloc(int alloc_thread_info, int alloc_tc) CoreEntry *core_entry_alloc(int th, int tsk)
{ {
CoreEntry *core; size_t sz;
TaskCoreEntry *tc; CoreEntry *core = NULL;
void *m;
core = xmalloc(sizeof(*core));
if (!core) sz = sizeof(CoreEntry);
return NULL; 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_entry__init(core);
core->mtype = CORE_ENTRY__MARCH; core->mtype = CORE_ENTRY__MARCH;
if (alloc_thread_info) { if (tsk) {
ThreadCoreEntry *thread_core; core->tc = xptr_pull(&m, TaskCoreEntry);
ThreadSasEntry *sas; task_core_entry__init(core->tc);
core->tc->comm = xptr_pull_s(&m, TASK_COMM_LEN);
if (arch_alloc_thread_info(core)) memzero(core->tc->comm, TASK_COMM_LEN);
goto err; }
thread_core = xmalloc(sizeof(*thread_core)); if (th) {
if (!thread_core) core->thread_core = xptr_pull(&m, ThreadCoreEntry);
goto err; thread_core_entry__init(core->thread_core);
thread_core_entry__init(thread_core); core->thread_core->sas = xptr_pull(&m, ThreadSasEntry);
core->thread_core = thread_core; thread_sas_entry__init(core->thread_core->sas);
sas = xmalloc(sizeof(*sas)); if (arch_alloc_thread_info(core)) {
if (!sas) xfree(core);
goto err; core = NULL;
thread_sas_entry__init(sas); }
core->thread_core->sas = 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;
} }
return core; return core;
err:
core_entry_free(core);
return NULL;
} }
int pstree_alloc_cores(struct pstree_item *item) int pstree_alloc_cores(struct pstree_item *item)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment