4 * Common Trace Format Object Stack.
6 * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-OBJSTACK"
31 #include "common/list.h"
32 #include "common/macros.h"
33 #include "common/align.h"
35 #define OBJSTACK_ALIGN 8 /* Object stack alignment */
36 #define OBJSTACK_INIT_LEN 128
37 #define OBJSTACK_POISON 0xcc
40 struct bt_list_head head
; /* list of struct objstack_node */
43 struct objstack_node
{
44 struct bt_list_head node
;
47 char __attribute__ ((aligned (OBJSTACK_ALIGN
))) data
[];
51 struct objstack
*objstack_create(void)
53 struct objstack
*objstack
;
54 struct objstack_node
*node
;
56 objstack
= calloc(1, sizeof(*objstack
));
58 BT_LOGE_STR("Failed to allocate one object stack.");
61 node
= calloc(sizeof(struct objstack_node
) + OBJSTACK_INIT_LEN
,
64 BT_LOGE_STR("Failed to allocate one object stack node.");
68 BT_INIT_LIST_HEAD(&objstack
->head
);
69 bt_list_add_tail(&node
->node
, &objstack
->head
);
70 node
->len
= OBJSTACK_INIT_LEN
;
75 void objstack_node_free(struct objstack_node
*node
)
83 len
= sizeof(*node
) + node
->len
;
84 for (offset
= 0; offset
< len
; offset
++)
85 p
[offset
] = OBJSTACK_POISON
;
90 void objstack_destroy(struct objstack
*objstack
)
92 struct objstack_node
*node
, *p
;
96 bt_list_for_each_entry_safe(node
, p
, &objstack
->head
, node
) {
97 bt_list_del(&node
->node
);
98 objstack_node_free(node
);
104 struct objstack_node
*objstack_append_node(struct objstack
*objstack
)
106 struct objstack_node
*last_node
, *new_node
;
109 last_node
= bt_list_entry(objstack
->head
.prev
,
110 struct objstack_node
, node
);
112 /* Allocate new node with double of size of last node */
113 new_node
= calloc(sizeof(struct objstack_node
) + (last_node
->len
<< 1),
116 BT_LOGE_STR("Failed to allocate one object stack node.");
119 bt_list_add_tail(&new_node
->node
, &objstack
->head
);
120 new_node
->len
= last_node
->len
<< 1;
125 void *objstack_alloc(struct objstack
*objstack
, size_t len
)
127 struct objstack_node
*last_node
;
130 len
= ALIGN(len
, OBJSTACK_ALIGN
);
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
);
141 p
= &last_node
->data
[last_node
->used_len
];
142 last_node
->used_len
+= len
;
This page took 0.032439 seconds and 4 git commands to generate.