14d925215b4316d7511bf822135d62318aa8efbe
[babeltrace.git] / formats / ctf / 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 #include <stdlib.h>
28 #include <babeltrace/list.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <babeltrace/align.h>
31
32 #define OBJSTACK_INIT_LEN 128
33 #define OBJSTACK_POISON 0xcc
34
35 struct objstack {
36 struct bt_list_head head; /* list of struct objstack_node */
37 };
38
39 struct objstack_node {
40 struct bt_list_head node;
41 size_t len;
42 size_t used_len;
43 char __attribute__ ((aligned (sizeof(void *)))) data[];
44 };
45
46 BT_HIDDEN
47 struct objstack *objstack_create(void)
48 {
49 struct objstack *objstack;
50 struct objstack_node *node;
51
52 objstack = calloc(1, sizeof(*objstack));
53 if (!objstack)
54 return NULL;
55 node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN,
56 sizeof(char));
57 if (!node) {
58 free(objstack);
59 return NULL;
60 }
61 BT_INIT_LIST_HEAD(&objstack->head);
62 bt_list_add_tail(&node->node, &objstack->head);
63 node->len = OBJSTACK_INIT_LEN;
64 return objstack;
65 }
66
67 static
68 void objstack_node_free(struct objstack_node *node)
69 {
70 size_t offset, len;
71 char *p;
72
73 if (!node)
74 return;
75 p = (char *) node;
76 len = sizeof(*node) + node->len;
77 for (offset = 0; offset < len; offset++)
78 p[offset] = OBJSTACK_POISON;
79 free(node);
80 }
81
82 BT_HIDDEN
83 void objstack_destroy(struct objstack *objstack)
84 {
85 struct objstack_node *node, *p;
86
87 if (!objstack)
88 return;
89 bt_list_for_each_entry_safe(node, p, &objstack->head, node) {
90 bt_list_del(&node->node);
91 objstack_node_free(node);
92 }
93 free(objstack);
94 }
95
96 static
97 struct objstack_node *objstack_append_node(struct objstack *objstack)
98 {
99 struct objstack_node *last_node, *new_node;
100
101 /* Get last node */
102 last_node = bt_list_entry(objstack->head.prev,
103 struct objstack_node, node);
104
105 /* Allocate new node with double of size of last node */
106 new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1),
107 sizeof(char));
108 if (!new_node) {
109 return NULL;
110 }
111 bt_list_add_tail(&new_node->node, &objstack->head);
112 new_node->len = last_node->len << 1;
113 return new_node;
114 }
115
116 BT_HIDDEN
117 void *objstack_alloc(struct objstack *objstack, size_t len)
118 {
119 struct objstack_node *last_node;
120 void *p;
121
122 len = ALIGN(len, sizeof(void *));
123
124 /* Get last node */
125 last_node = bt_list_entry(objstack->head.prev,
126 struct objstack_node, node);
127 while (last_node->len - last_node->used_len < len) {
128 last_node = objstack_append_node(objstack);
129 if (!last_node) {
130 return NULL;
131 }
132 }
133 p = &last_node->data[last_node->used_len];
134 last_node->used_len += len;
135 return p;
136 }
This page took 0.031628 seconds and 3 git commands to generate.