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