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