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