port: namespace align.h with BT_ prefix
[babeltrace.git] / src / plugins / ctf / common / metadata / objstack.c
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
0746848c 9#define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
350ad6c1 10#define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
52567b11
PP
11#include "logging.h"
12
7c7301d5
SM
13#include "objstack.h"
14
e98a2d6e 15#include <stdlib.h>
578e048b 16#include "common/list.h"
91d81473 17#include "common/macros.h"
578e048b 18#include "common/align.h"
e98a2d6e
PP
19
20#define OBJSTACK_ALIGN 8 /* Object stack alignment */
21#define OBJSTACK_INIT_LEN 128
22#define OBJSTACK_POISON 0xcc
23
24struct objstack {
25 struct bt_list_head head; /* list of struct objstack_node */
26};
27
28struct objstack_node {
29 struct bt_list_head node;
30 size_t len;
31 size_t used_len;
32 char __attribute__ ((aligned (OBJSTACK_ALIGN))) data[];
33};
34
35BT_HIDDEN
36struct objstack *objstack_create(void)
37{
38 struct objstack *objstack;
39 struct objstack_node *node;
40
41 objstack = calloc(1, sizeof(*objstack));
52567b11
PP
42 if (!objstack) {
43 BT_LOGE_STR("Failed to allocate one object stack.");
e98a2d6e 44 return NULL;
52567b11 45 }
e98a2d6e
PP
46 node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN,
47 sizeof(char));
48 if (!node) {
52567b11 49 BT_LOGE_STR("Failed to allocate one object stack node.");
e98a2d6e
PP
50 free(objstack);
51 return NULL;
52 }
53 BT_INIT_LIST_HEAD(&objstack->head);
54 bt_list_add_tail(&node->node, &objstack->head);
55 node->len = OBJSTACK_INIT_LEN;
56 return objstack;
57}
58
59static
60void 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
74BT_HIDDEN
75void 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
88static
89struct objstack_node *objstack_append_node(struct objstack *objstack)
90{
91 struct objstack_node *last_node, *new_node;
92
93 /* Get last node */
94 last_node = bt_list_entry(objstack->head.prev,
95 struct objstack_node, node);
96
97 /* Allocate new node with double of size of last node */
98 new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1),
99 sizeof(char));
100 if (!new_node) {
52567b11 101 BT_LOGE_STR("Failed to allocate one object stack node.");
e98a2d6e
PP
102 return NULL;
103 }
104 bt_list_add_tail(&new_node->node, &objstack->head);
105 new_node->len = last_node->len << 1;
106 return new_node;
107}
108
109BT_HIDDEN
110void *objstack_alloc(struct objstack *objstack, size_t len)
111{
112 struct objstack_node *last_node;
113 void *p;
114
328342cd 115 len = BT_ALIGN(len, OBJSTACK_ALIGN);
e98a2d6e
PP
116
117 /* Get last node */
118 last_node = bt_list_entry(objstack->head.prev,
119 struct objstack_node, node);
120 while (last_node->len - last_node->used_len < len) {
121 last_node = objstack_append_node(objstack);
122 if (!last_node) {
123 return NULL;
124 }
125 }
126 p = &last_node->data[last_node->used_len];
127 last_node->used_len += len;
128 return p;
129}
This page took 0.074933 seconds and 4 git commands to generate.