Sort includes in C++ files
[babeltrace.git] / src / plugins / ctf / common / metadata / 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
SM
9#include <stdlib.h>
10
0746848c 11#define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
4164020e 12#define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
087cd0f5 13#include "logging.hpp"
52567b11 14
c802cacb 15#include "common/align.h"
578e048b 16#include "common/list.h"
91d81473 17#include "common/macros.h"
c802cacb
SM
18
19#include "objstack.hpp"
e98a2d6e 20
4164020e
SM
21#define OBJSTACK_ALIGN 8 /* Object stack alignment */
22#define OBJSTACK_INIT_LEN 128
23#define OBJSTACK_POISON 0xcc
e98a2d6e 24
4164020e
SM
25struct objstack
26{
27 struct bt_list_head head; /* list of struct objstack_node */
e98a2d6e
PP
28};
29
4164020e
SM
30struct 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[];
e98a2d6e
PP
36};
37
e98a2d6e
PP
38struct objstack *objstack_create(void)
39{
4164020e
SM
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;
e98a2d6e
PP
58}
59
4164020e 60static void objstack_node_free(struct objstack_node *node)
e98a2d6e 61{
4164020e
SM
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);
e98a2d6e
PP
72}
73
e98a2d6e
PP
74void objstack_destroy(struct objstack *objstack)
75{
4164020e
SM
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);
e98a2d6e
PP
85}
86
4164020e 87static struct objstack_node *objstack_append_node(struct objstack *objstack)
e98a2d6e 88{
4164020e
SM
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;
e98a2d6e
PP
104}
105
e98a2d6e
PP
106void *objstack_alloc(struct objstack *objstack, size_t len)
107{
4164020e
SM
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;
e98a2d6e 124}
This page took 0.091996 seconds and 4 git commands to generate.