Visibility hidden by default
[babeltrace.git] / src / plugins / ctf / common / metadata / objstack.cpp
CommitLineData
e98a2d6e 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
e98a2d6e 3 *
0235b0db 4 * Copyright 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
e98a2d6e 5 *
0235b0db 6 * Common Trace Format Object Stack.
e98a2d6e
PP
7 */
8
0746848c 9#define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
4164020e 10#define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
087cd0f5 11#include "logging.hpp"
52567b11 12
087cd0f5 13#include "objstack.hpp"
7c7301d5 14
e98a2d6e 15#include <stdlib.h>
578e048b 16#include "common/list.h"
91d81473 17#include "common/macros.h"
578e048b 18#include "common/align.h"
e98a2d6e 19
4164020e
SM
20#define OBJSTACK_ALIGN 8 /* Object stack alignment */
21#define OBJSTACK_INIT_LEN 128
22#define OBJSTACK_POISON 0xcc
e98a2d6e 23
4164020e
SM
24struct objstack
25{
26 struct bt_list_head head; /* list of struct objstack_node */
e98a2d6e
PP
27};
28
4164020e
SM
29struct objstack_node
30{
31 struct bt_list_head node;
32 size_t len;
33 size_t used_len;
34 char __attribute__((aligned(OBJSTACK_ALIGN))) data[];
e98a2d6e
PP
35};
36
e98a2d6e
PP
37struct objstack *objstack_create(void)
38{
4164020e
SM
39 struct objstack *objstack;
40 struct objstack_node *node;
41
42 objstack = (struct objstack *) calloc(1, sizeof(*objstack));
43 if (!objstack) {
44 BT_LOGE_STR("Failed to allocate one object stack.");
45 return NULL;
46 }
47 node = (objstack_node *) calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN, sizeof(char));
48 if (!node) {
49 BT_LOGE_STR("Failed to allocate one object stack node.");
50 free(objstack);
51 return NULL;
52 }
53 BT_INIT_LIST_HEAD(&objstack->head);
54 bt_list_add_tail(&node->node, &objstack->head);
55 node->len = OBJSTACK_INIT_LEN;
56 return objstack;
e98a2d6e
PP
57}
58
4164020e 59static void objstack_node_free(struct objstack_node *node)
e98a2d6e 60{
4164020e
SM
61 size_t offset, len;
62 char *p;
63
64 if (!node)
65 return;
66 p = (char *) node;
67 len = sizeof(*node) + node->len;
68 for (offset = 0; offset < len; offset++)
69 p[offset] = OBJSTACK_POISON;
70 free(node);
e98a2d6e
PP
71}
72
e98a2d6e
PP
73void objstack_destroy(struct objstack *objstack)
74{
4164020e
SM
75 struct objstack_node *node, *p;
76
77 if (!objstack)
78 return;
79 bt_list_for_each_entry_safe (node, p, &objstack->head, node) {
80 bt_list_del(&node->node);
81 objstack_node_free(node);
82 }
83 free(objstack);
e98a2d6e
PP
84}
85
4164020e 86static struct objstack_node *objstack_append_node(struct objstack *objstack)
e98a2d6e 87{
4164020e
SM
88 struct objstack_node *last_node, *new_node;
89
90 /* Get last node */
91 last_node = bt_list_entry(objstack->head.prev, struct objstack_node, node);
92
93 /* Allocate new node with double of size of last node */
94 new_node = (objstack_node *) calloc(sizeof(struct objstack_node) + (last_node->len << 1),
95 sizeof(char));
96 if (!new_node) {
97 BT_LOGE_STR("Failed to allocate one object stack node.");
98 return NULL;
99 }
100 bt_list_add_tail(&new_node->node, &objstack->head);
101 new_node->len = last_node->len << 1;
102 return new_node;
e98a2d6e
PP
103}
104
e98a2d6e
PP
105void *objstack_alloc(struct objstack *objstack, size_t len)
106{
4164020e
SM
107 struct objstack_node *last_node;
108 void *p;
109
110 len = BT_ALIGN(len, OBJSTACK_ALIGN);
111
112 /* Get last node */
113 last_node = bt_list_entry(objstack->head.prev, struct objstack_node, node);
114 while (last_node->len - last_node->used_len < len) {
115 last_node = objstack_append_node(objstack);
116 if (!last_node) {
117 return NULL;
118 }
119 }
120 p = &last_node->data[last_node->used_len];
121 last_node->used_len += len;
122 return p;
e98a2d6e 123}
This page took 0.084435 seconds and 4 git commands to generate.