cpp-common/bt2c/logging.hpp: remove no-formatting ("str") alternatives
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / objstack.cpp
... / ...
CommitLineData
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 "common/align.h"
10#include "common/list.h"
11#include "cpp-common/bt2c/logging.hpp"
12
13#include "objstack.hpp"
14
15#define OBJSTACK_ALIGN 8 /* Object stack alignment */
16#define OBJSTACK_INIT_LEN 128
17#define OBJSTACK_POISON 0xcc
18
19struct objstack
20{
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;
30};
31
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[];
38};
39
40objstack *objstack_create(const bt2c::Logger& parentLogger)
41{
42 struct objstack *objstack;
43 struct objstack_node *node;
44
45 objstack = new ::objstack {parentLogger};
46 node = (objstack_node *) calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN, sizeof(char));
47 if (!node) {
48 BT_CPPLOGE_SPEC(objstack->logger, "Failed to allocate one object stack node.");
49 delete objstack;
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;
56}
57
58static void objstack_node_free(struct objstack_node *node)
59{
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);
70}
71
72void objstack_destroy(struct objstack *objstack)
73{
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 }
82
83 delete objstack;
84}
85
86static struct objstack_node *objstack_append_node(struct objstack *objstack)
87{
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) {
97 BT_CPPLOGE_SPEC(objstack->logger, "Failed to allocate one object stack node.");
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;
103}
104
105void *objstack_alloc(struct objstack *objstack, size_t len)
106{
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;
123}
This page took 0.023124 seconds and 4 git commands to generate.