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 | ||
350ad6c1 | 27 | #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK" |
52567b11 PP |
28 | #include "logging.h" |
29 | ||
e98a2d6e | 30 | #include <stdlib.h> |
578e048b | 31 | #include "common/list.h" |
91d81473 | 32 | #include "common/macros.h" |
578e048b | 33 | #include "common/align.h" |
e98a2d6e PP |
34 | |
35 | #define OBJSTACK_ALIGN 8 /* Object stack alignment */ | |
36 | #define OBJSTACK_INIT_LEN 128 | |
37 | #define OBJSTACK_POISON 0xcc | |
38 | ||
39 | struct objstack { | |
40 | struct bt_list_head head; /* list of struct objstack_node */ | |
41 | }; | |
42 | ||
43 | struct objstack_node { | |
44 | struct bt_list_head node; | |
45 | size_t len; | |
46 | size_t used_len; | |
47 | char __attribute__ ((aligned (OBJSTACK_ALIGN))) data[]; | |
48 | }; | |
49 | ||
50 | BT_HIDDEN | |
51 | struct objstack *objstack_create(void) | |
52 | { | |
53 | struct objstack *objstack; | |
54 | struct objstack_node *node; | |
55 | ||
56 | objstack = calloc(1, sizeof(*objstack)); | |
52567b11 PP |
57 | if (!objstack) { |
58 | BT_LOGE_STR("Failed to allocate one object stack."); | |
e98a2d6e | 59 | return NULL; |
52567b11 | 60 | } |
e98a2d6e PP |
61 | node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN, |
62 | sizeof(char)); | |
63 | if (!node) { | |
52567b11 | 64 | BT_LOGE_STR("Failed to allocate one object stack node."); |
e98a2d6e PP |
65 | free(objstack); |
66 | return NULL; | |
67 | } | |
68 | BT_INIT_LIST_HEAD(&objstack->head); | |
69 | bt_list_add_tail(&node->node, &objstack->head); | |
70 | node->len = OBJSTACK_INIT_LEN; | |
71 | return objstack; | |
72 | } | |
73 | ||
74 | static | |
75 | void objstack_node_free(struct objstack_node *node) | |
76 | { | |
77 | size_t offset, len; | |
78 | char *p; | |
79 | ||
80 | if (!node) | |
81 | return; | |
82 | p = (char *) node; | |
83 | len = sizeof(*node) + node->len; | |
84 | for (offset = 0; offset < len; offset++) | |
85 | p[offset] = OBJSTACK_POISON; | |
86 | free(node); | |
87 | } | |
88 | ||
89 | BT_HIDDEN | |
90 | void objstack_destroy(struct objstack *objstack) | |
91 | { | |
92 | struct objstack_node *node, *p; | |
93 | ||
94 | if (!objstack) | |
95 | return; | |
96 | bt_list_for_each_entry_safe(node, p, &objstack->head, node) { | |
97 | bt_list_del(&node->node); | |
98 | objstack_node_free(node); | |
99 | } | |
100 | free(objstack); | |
101 | } | |
102 | ||
103 | static | |
104 | struct objstack_node *objstack_append_node(struct objstack *objstack) | |
105 | { | |
106 | struct objstack_node *last_node, *new_node; | |
107 | ||
108 | /* Get last node */ | |
109 | last_node = bt_list_entry(objstack->head.prev, | |
110 | struct objstack_node, node); | |
111 | ||
112 | /* Allocate new node with double of size of last node */ | |
113 | new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1), | |
114 | sizeof(char)); | |
115 | if (!new_node) { | |
52567b11 | 116 | BT_LOGE_STR("Failed to allocate one object stack node."); |
e98a2d6e PP |
117 | return NULL; |
118 | } | |
119 | bt_list_add_tail(&new_node->node, &objstack->head); | |
120 | new_node->len = last_node->len << 1; | |
121 | return new_node; | |
122 | } | |
123 | ||
124 | BT_HIDDEN | |
125 | void *objstack_alloc(struct objstack *objstack, size_t len) | |
126 | { | |
127 | struct objstack_node *last_node; | |
128 | void *p; | |
129 | ||
130 | len = ALIGN(len, OBJSTACK_ALIGN); | |
131 | ||
132 | /* Get last node */ | |
133 | last_node = bt_list_entry(objstack->head.prev, | |
134 | struct objstack_node, node); | |
135 | while (last_node->len - last_node->used_len < len) { | |
136 | last_node = objstack_append_node(objstack); | |
137 | if (!last_node) { | |
138 | return NULL; | |
139 | } | |
140 | } | |
141 | p = &last_node->data[last_node->used_len]; | |
142 | last_node->used_len += len; | |
143 | return p; | |
144 | } |