5919fa13f481088c25760ba4986f245d7c3449b7
[babeltrace.git] / src / plugins / ctf / common / metadata / objstack.c
1 /*
2 * objstack.c
3 *
4 * Common Trace Format Object Stack.
5 *
6 * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
28 #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
29 #include "logging.h"
30
31 #include "objstack.h"
32
33 #include <stdlib.h>
34 #include "common/list.h"
35 #include "common/macros.h"
36 #include "common/align.h"
37
38 #define OBJSTACK_ALIGN 8 /* Object stack alignment */
39 #define OBJSTACK_INIT_LEN 128
40 #define OBJSTACK_POISON 0xcc
41
42 struct objstack {
43 struct bt_list_head head; /* list of struct objstack_node */
44 };
45
46 struct objstack_node {
47 struct bt_list_head node;
48 size_t len;
49 size_t used_len;
50 char __attribute__ ((aligned (OBJSTACK_ALIGN))) data[];
51 };
52
53 BT_HIDDEN
54 struct objstack *objstack_create(void)
55 {
56 struct objstack *objstack;
57 struct objstack_node *node;
58
59 objstack = calloc(1, sizeof(*objstack));
60 if (!objstack) {
61 BT_LOGE_STR("Failed to allocate one object stack.");
62 return NULL;
63 }
64 node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN,
65 sizeof(char));
66 if (!node) {
67 BT_LOGE_STR("Failed to allocate one object stack node.");
68 free(objstack);
69 return NULL;
70 }
71 BT_INIT_LIST_HEAD(&objstack->head);
72 bt_list_add_tail(&node->node, &objstack->head);
73 node->len = OBJSTACK_INIT_LEN;
74 return objstack;
75 }
76
77 static
78 void objstack_node_free(struct objstack_node *node)
79 {
80 size_t offset, len;
81 char *p;
82
83 if (!node)
84 return;
85 p = (char *) node;
86 len = sizeof(*node) + node->len;
87 for (offset = 0; offset < len; offset++)
88 p[offset] = OBJSTACK_POISON;
89 free(node);
90 }
91
92 BT_HIDDEN
93 void objstack_destroy(struct objstack *objstack)
94 {
95 struct objstack_node *node, *p;
96
97 if (!objstack)
98 return;
99 bt_list_for_each_entry_safe(node, p, &objstack->head, node) {
100 bt_list_del(&node->node);
101 objstack_node_free(node);
102 }
103 free(objstack);
104 }
105
106 static
107 struct objstack_node *objstack_append_node(struct objstack *objstack)
108 {
109 struct objstack_node *last_node, *new_node;
110
111 /* Get last node */
112 last_node = bt_list_entry(objstack->head.prev,
113 struct objstack_node, node);
114
115 /* Allocate new node with double of size of last node */
116 new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1),
117 sizeof(char));
118 if (!new_node) {
119 BT_LOGE_STR("Failed to allocate one object stack node.");
120 return NULL;
121 }
122 bt_list_add_tail(&new_node->node, &objstack->head);
123 new_node->len = last_node->len << 1;
124 return new_node;
125 }
126
127 BT_HIDDEN
128 void *objstack_alloc(struct objstack *objstack, size_t len)
129 {
130 struct objstack_node *last_node;
131 void *p;
132
133 len = ALIGN(len, OBJSTACK_ALIGN);
134
135 /* Get last node */
136 last_node = bt_list_entry(objstack->head.prev,
137 struct objstack_node, node);
138 while (last_node->len - last_node->used_len < len) {
139 last_node = objstack_append_node(objstack);
140 if (!last_node) {
141 return NULL;
142 }
143 }
144 p = &last_node->data[last_node->used_len];
145 last_node->used_len += len;
146 return p;
147 }
This page took 0.032109 seconds and 4 git commands to generate.