From: Mathieu Desnoyers Date: Tue, 16 Apr 2013 02:42:57 +0000 (-0400) Subject: Implement objstack for parser X-Git-Tag: v1.1.1~33 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=eb67868e595308b91786864e5a96ee16df2e199e Implement objstack for parser Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/metadata/Makefile.am b/formats/ctf/metadata/Makefile.am index a3710047..b33ce553 100644 --- a/formats/ctf/metadata/Makefile.am +++ b/formats/ctf/metadata/Makefile.am @@ -7,9 +7,10 @@ noinst_LTLIBRARIES = libctf-parser.la libctf-ast.la noinst_HEADERS = \ ctf-scanner.h \ ctf-ast.h \ - ctf-scanner-symbols.h + ctf-scanner-symbols.h \ + objstack.h -libctf_parser_la_SOURCES = ctf-lexer.l ctf-parser.y +libctf_parser_la_SOURCES = ctf-lexer.l ctf-parser.y objstack.c libctf_parser_la_CFLAGS = $(AM_CFLAGS) -include ctf-scanner-symbols.h libctf_ast_la_SOURCES = ctf-visitor-xml.c \ diff --git a/formats/ctf/metadata/objstack.c b/formats/ctf/metadata/objstack.c new file mode 100644 index 00000000..9e264a41 --- /dev/null +++ b/formats/ctf/metadata/objstack.c @@ -0,0 +1,133 @@ +/* + * objstack.c + * + * Common Trace Format Object Stack. + * + * Copyright 2013 - Mathieu Desnoyers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +#define OBJSTACK_INIT_LEN 128 +#define OBJSTACK_POISON 0xcc + +struct objstack { + struct bt_list_head head; /* list of struct objstack_node */ +}; + +struct objstack_node { + struct bt_list_head node; + size_t len; + size_t used_len; + char data[]; +}; + +BT_HIDDEN +struct objstack *objstack_create(void) +{ + struct objstack *objstack; + struct objstack_node *node; + + objstack = calloc(1, sizeof(*objstack)); + if (!objstack) + return NULL; + node = calloc(sizeof(struct objstack_node) + OBJSTACK_INIT_LEN, + sizeof(char)); + if (!node) { + free(objstack); + return NULL; + } + BT_INIT_LIST_HEAD(&objstack->head); + bt_list_add_tail(&node->node, &objstack->head); + node->len = OBJSTACK_INIT_LEN; + return objstack; +} + +static +void objstack_node_free(struct objstack_node *node) +{ + size_t offset, len; + char *p; + + if (!node) + return; + p = (char *) node; + len = sizeof(*node) + node->len; + for (offset = 0; offset < len; offset++) + p[offset] = OBJSTACK_POISON; + free(node); +} + +BT_HIDDEN +void objstack_destroy(struct objstack *objstack) +{ + struct objstack_node *node, *p; + + if (!objstack) + return; + bt_list_for_each_entry_safe(node, p, &objstack->head, node) { + bt_list_del(&node->node); + objstack_node_free(node); + } + free(objstack); +} + +static +struct objstack_node *objstack_append_node(struct objstack *objstack) +{ + struct objstack_node *last_node, *new_node; + + /* Get last node */ + last_node = bt_list_entry(objstack->head.prev, + struct objstack_node, node); + + /* Allocate new node with double of size of last node */ + new_node = calloc(sizeof(struct objstack_node) + (last_node->len << 1), + sizeof(char)); + if (!new_node) { + return NULL; + } + bt_list_add_tail(&new_node->node, &objstack->head); + new_node->len = last_node->len << 1; + return new_node; +} + +BT_HIDDEN +void *objstack_alloc(struct objstack *objstack, size_t len) +{ + struct objstack_node *last_node; + void *p; + + /* Get last node */ + last_node = bt_list_entry(objstack->head.prev, + struct objstack_node, node); + while (last_node->len - last_node->used_len < len) { + last_node = objstack_append_node(objstack); + if (!last_node) { + return NULL; + } + } + p = &last_node->data[last_node->used_len]; + last_node->used_len += len; + return p; +} diff --git a/formats/ctf/metadata/objstack.h b/formats/ctf/metadata/objstack.h new file mode 100644 index 00000000..8f83f902 --- /dev/null +++ b/formats/ctf/metadata/objstack.h @@ -0,0 +1,39 @@ +#ifndef _OBJSTACK_H +#define _OBJSTACK_H + +/* + * objstack.h + * + * Common Trace Format Object Stack. + * + * Copyright 2013 - Mathieu Desnoyers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +struct objstack; + +BT_HIDDEN +struct objstack *objstack_create(void); +BT_HIDDEN +void objstack_destroy(struct objstack *objstack); +BT_HIDDEN +void *objstack_alloc(struct objstack *objstack, size_t len); + +#endif /* _OBJSTACK_H */