Re-format new 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 #define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
10 #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
11 #include "logging.hpp"
12
13 #include "objstack.hpp"
14
15 #include <stdlib.h>
16 #include "common/list.h"
17 #include "common/macros.h"
18 #include "common/align.h"
19
20 #define OBJSTACK_ALIGN 8 /* Object stack alignment */
21 #define OBJSTACK_INIT_LEN 128
22 #define OBJSTACK_POISON 0xcc
23
24 struct objstack
25 {
26 struct bt_list_head head; /* list of struct objstack_node */
27 };
28
29 struct 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[];
35 };
36
37 BT_HIDDEN
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 BT_HIDDEN
75 void objstack_destroy(struct objstack *objstack)
76 {
77 struct objstack_node *node, *p;
78
79 if (!objstack)
80 return;
81 bt_list_for_each_entry_safe (node, p, &objstack->head, node) {
82 bt_list_del(&node->node);
83 objstack_node_free(node);
84 }
85 free(objstack);
86 }
87
88 static struct objstack_node *objstack_append_node(struct objstack *objstack)
89 {
90 struct objstack_node *last_node, *new_node;
91
92 /* Get last node */
93 last_node = bt_list_entry(objstack->head.prev, struct objstack_node, node);
94
95 /* Allocate new node with double of size of last node */
96 new_node = (objstack_node *) calloc(sizeof(struct objstack_node) + (last_node->len << 1),
97 sizeof(char));
98 if (!new_node) {
99 BT_LOGE_STR("Failed to allocate one object stack node.");
100 return NULL;
101 }
102 bt_list_add_tail(&new_node->node, &objstack->head);
103 new_node->len = last_node->len << 1;
104 return new_node;
105 }
106
107 BT_HIDDEN
108 void *objstack_alloc(struct objstack *objstack, size_t len)
109 {
110 struct objstack_node *last_node;
111 void *p;
112
113 len = BT_ALIGN(len, OBJSTACK_ALIGN);
114
115 /* Get last node */
116 last_node = bt_list_entry(objstack->head.prev, struct objstack_node, node);
117 while (last_node->len - last_node->used_len < len) {
118 last_node = objstack_append_node(objstack);
119 if (!last_node) {
120 return NULL;
121 }
122 }
123 p = &last_node->data[last_node->used_len];
124 last_node->used_len += len;
125 return p;
126 }
This page took 0.031555 seconds and 4 git commands to generate.