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 @@
#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__ */
......@@ -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);
}
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;
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 (alloc_thread_info) {
ThreadCoreEntry *thread_core;
ThreadSasEntry *sas;
if (arch_alloc_thread_info(core))
goto err;
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);
}
thread_core = xmalloc(sizeof(*thread_core));
if (!thread_core)
goto err;
thread_core_entry__init(thread_core);
core->thread_core = thread_core;
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);
sas = xmalloc(sizeof(*sas));
if (!sas)
goto err;
thread_sas_entry__init(sas);
core->thread_core->sas = sas;
if (arch_alloc_thread_info(core)) {
xfree(core);
core = NULL;
}
}
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;
err:
core_entry_free(core);
return NULL;
}
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