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