Commit | Line | Data |
---|---|---|
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 |
4164020e | 10 | #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK" |
087cd0f5 | 11 | #include "logging.hpp" |
52567b11 | 12 | |
087cd0f5 | 13 | #include "objstack.hpp" |
7c7301d5 | 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 | 19 | |
4164020e SM |
20 | #define OBJSTACK_ALIGN 8 /* Object stack alignment */ |
21 | #define OBJSTACK_INIT_LEN 128 | |
22 | #define OBJSTACK_POISON 0xcc | |
e98a2d6e | 23 | |
4164020e SM |
24 | struct objstack |
25 | { | |
26 | struct bt_list_head head; /* list of struct objstack_node */ | |
e98a2d6e PP |
27 | }; |
28 | ||
4164020e SM |
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[]; | |
e98a2d6e PP |
35 | }; |
36 | ||
37 | BT_HIDDEN | |
38 | struct 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 | 60 | static 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 | ||
74 | BT_HIDDEN | |
75 | void objstack_destroy(struct objstack *objstack) | |
76 | { | |
4164020e SM |
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); | |
e98a2d6e PP |
86 | } |
87 | ||
4164020e | 88 | static struct objstack_node *objstack_append_node(struct objstack *objstack) |
e98a2d6e | 89 | { |
4164020e SM |
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; | |
e98a2d6e PP |
105 | } |
106 | ||
107 | BT_HIDDEN | |
108 | void *objstack_alloc(struct objstack *objstack, size_t len) | |
109 | { | |
4164020e SM |
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; | |
e98a2d6e | 126 | } |