Implement objstack for parser
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 16 Apr 2013 02:42:57 +0000 (22:42 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 19 Apr 2013 08:52:01 +0000 (04:52 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/metadata/Makefile.am
formats/ctf/metadata/objstack.c [new file with mode: 0644]
formats/ctf/metadata/objstack.h [new file with mode: 0644]

index a37100470c8607959c307b456e42b096fd7479fb..b33ce5536b4124284100a52f1bc60c664987065a 100644 (file)
@@ -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 (file)
index 0000000..9e264a4
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * objstack.c
+ *
+ * Common Trace Format Object Stack.
+ *
+ * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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 <stdlib.h>
+#include <babeltrace/list.h>
+#include <babeltrace/babeltrace-internal.h>
+
+#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 (file)
index 0000000..8f83f90
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef _OBJSTACK_H
+#define _OBJSTACK_H
+
+/*
+ * objstack.h
+ *
+ * Common Trace Format Object Stack.
+ *
+ * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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 */
This page took 0.026265 seconds and 4 git commands to generate.