Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * objstack.c | |
3 | * | |
4 | * Common Trace Format Object Stack. | |
5 | * | |
6 | * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
0746848c | 27 | #define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level |
350ad6c1 | 28 | #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK" |
52567b11 PP |
29 | #include "logging.h" |
30 | ||
e98a2d6e | 31 | #include <stdlib.h> |
578e048b | 32 | #include "common/list.h" |
91d81473 | 33 | #include "common/macros.h" |
578e048b | 34 | #include "common/align.h" |
e98a2d6e PP |
35 | |
36 | #define OBJSTACK_ALIGN 8 /* Object stack alignment */ | |
37 | #define OBJSTACK_INIT_LEN 128 | |
38 | #define OBJSTACK_POISON 0xcc | |
39 | ||
40 | struct objstack { | |
41 | struct bt_list_head head; /* list of struct objstack_node */ | |
42 | }; | |
43 | ||
44 | struct objstack_node { | |
45 | struct bt_list_head node; | |
46 | size_t len; | |
47 | size_t used_len; | |
48 | char __attribute__ ((aligned (OBJSTACK_ALIGN))) data[]; | |
49 | }; | |
50 | ||
51 | BT_HIDDEN | |
52 | struct objstack *objstack_create(void) | |
53 | { | |
54 | struct objstack *objstack; | |
55 | struct objstack_node *node; | |
56 | ||
57 | objstack = calloc(1, sizeof(*objstack)); | |
52567b11 PP |
58 | if (!objstack) { |
59 | BT_LOGE_STR("Failed to allocate one object stack."); | |
e98a2d6e | 60 | return NULL; |
52567b11 | 61 | } |
e98a2d6e PP |
62 | node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN, |
63 | sizeof(char)); | |
64 | if (!node) { | |
52567b11 | 65 | BT_LOGE_STR("Failed to allocate one object stack node."); |
e98a2d6e PP |
66 | free(objstack); |
67 | return NULL; | |
68 | } | |
69 | BT_INIT_LIST_HEAD(&objstack->head); | |
70 | bt_list_add_tail(&node->node, &objstack->head); | |
71 | node->len = OBJSTACK_INIT_LEN; | |
72 | return objstack; | |
73 | } | |
74 | ||
75 | static | |
76 | void objstack_node_free(struct objstack_node *node) | |
77 | { | |
78 | size_t offset, len; | |
79 | char *p; | |
80 | ||
81 | if (!node) | |
82 | return; | |
83 | p = (char *) node; | |
84 | len = sizeof(*node) + node->len; | |
85 | for (offset = 0; offset < len; offset++) | |
86 | p[offset] = OBJSTACK_POISON; | |
87 | free(node); | |
88 | } | |
89 | ||
90 | BT_HIDDEN | |
91 | void objstack_destroy(struct objstack *objstack) | |
92 | { | |
93 | struct objstack_node *node, *p; | |
94 | ||
95 | if (!objstack) | |
96 | return; | |
97 | bt_list_for_each_entry_safe(node, p, &objstack->head, node) { | |
98 | bt_list_del(&node->node); | |
99 | objstack_node_free(node); | |
100 | } | |
101 | free(objstack); | |
102 | } | |
103 | ||
104 | static | |
105 | struct objstack_node *objstack_append_node(struct objstack *objstack) | |
106 | { | |
107 | struct objstack_node *last_node, *new_node; | |
108 | ||
109 | /* Get last node */ | |
110 | last_node = bt_list_entry(objstack->head.prev, | |
111 | struct objstack_node, node); | |
112 | ||
113 | /* Allocate new node with double of size of last node */ | |
114 | new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1), | |
115 | sizeof(char)); | |
116 | if (!new_node) { | |
52567b11 | 117 | BT_LOGE_STR("Failed to allocate one object stack node."); |
e98a2d6e PP |
118 | return NULL; |
119 | } | |
120 | bt_list_add_tail(&new_node->node, &objstack->head); | |
121 | new_node->len = last_node->len << 1; | |
122 | return new_node; | |
123 | } | |
124 | ||
125 | BT_HIDDEN | |
126 | void *objstack_alloc(struct objstack *objstack, size_t len) | |
127 | { | |
128 | struct objstack_node *last_node; | |
129 | void *p; | |
130 | ||
131 | len = ALIGN(len, OBJSTACK_ALIGN); | |
132 | ||
133 | /* Get last node */ | |
134 | last_node = bt_list_entry(objstack->head.prev, | |
135 | struct objstack_node, node); | |
136 | while (last_node->len - last_node->used_len < len) { | |
137 | last_node = objstack_append_node(objstack); | |
138 | if (!last_node) { | |
139 | return NULL; | |
140 | } | |
141 | } | |
142 | p = &last_node->data[last_node->used_len]; | |
143 | last_node->used_len += len; | |
144 | return p; | |
145 | } |