0be8dbf7af801689b2115020524abf90a5791875
2 * SPDX-License-Identifier: MIT
4 * Copyright 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Common Trace Format Object Stack.
9 #define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
10 #define BT_LOG_TAG "PLUGIN/CTF/META/OBJSTACK"
11 #include "logging.hpp"
13 #include "objstack.hpp"
16 #include "common/list.h"
17 #include "common/macros.h"
18 #include "common/align.h"
20 #define OBJSTACK_ALIGN 8 /* Object stack alignment */
21 #define OBJSTACK_INIT_LEN 128
22 #define OBJSTACK_POISON 0xcc
25 struct bt_list_head head
; /* list of struct objstack_node */
28 struct objstack_node
{
29 struct bt_list_head node
;
32 char __attribute__ ((aligned (OBJSTACK_ALIGN
))) data
[];
36 struct objstack
*objstack_create(void)
38 struct objstack
*objstack
;
39 struct objstack_node
*node
;
41 objstack
= (struct objstack
*) calloc(1, sizeof(*objstack
));
43 BT_LOGE_STR("Failed to allocate one object stack.");
46 node
= (objstack_node
*) calloc(sizeof(struct objstack_node
) + OBJSTACK_INIT_LEN
,
49 BT_LOGE_STR("Failed to allocate one object stack node.");
53 BT_INIT_LIST_HEAD(&objstack
->head
);
54 bt_list_add_tail(&node
->node
, &objstack
->head
);
55 node
->len
= OBJSTACK_INIT_LEN
;
60 void objstack_node_free(struct objstack_node
*node
)
68 len
= sizeof(*node
) + node
->len
;
69 for (offset
= 0; offset
< len
; offset
++)
70 p
[offset
] = OBJSTACK_POISON
;
75 void objstack_destroy(struct objstack
*objstack
)
77 struct objstack_node
*node
, *p
;
81 bt_list_for_each_entry_safe(node
, p
, &objstack
->head
, node
) {
82 bt_list_del(&node
->node
);
83 objstack_node_free(node
);
89 struct objstack_node
*objstack_append_node(struct objstack
*objstack
)
91 struct objstack_node
*last_node
, *new_node
;
94 last_node
= bt_list_entry(objstack
->head
.prev
,
95 struct objstack_node
, node
);
97 /* Allocate new node with double of size of last node */
98 new_node
= (objstack_node
*) calloc(sizeof(struct objstack_node
) + (last_node
->len
<< 1),
101 BT_LOGE_STR("Failed to allocate one object stack node.");
104 bt_list_add_tail(&new_node
->node
, &objstack
->head
);
105 new_node
->len
= last_node
->len
<< 1;
110 void *objstack_alloc(struct objstack
*objstack
, size_t len
)
112 struct objstack_node
*last_node
;
115 len
= BT_ALIGN(len
, OBJSTACK_ALIGN
);
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
);
126 p
= &last_node
->data
[last_node
->used_len
];
127 last_node
->used_len
+= len
;
This page took 0.044105 seconds and 4 git commands to generate.