fix: move read event from iterator to event.h
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 17 Feb 2012 19:21:41 +0000 (14:21 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 17 Feb 2012 19:21:41 +0000 (14:21 -0500)
because it is CTF-specific.

Need to move the format registry to libbabeltrace due to
cross-dependencies between libbabeltrace and formats.

Now, the format libs depend on libbabeltrace, never the opposite.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20 files changed:
Makefile.am
converter/Makefile.am
converter/babeltrace.c
formats/Makefile.am
formats/bt-dummy/Makefile.am
formats/bt-dummy/bt-dummy.c
formats/ctf-text/Makefile.am
formats/ctf-text/ctf-text.c
formats/ctf/Makefile.am
formats/ctf/ctf.c
formats/ctf/events.c
formats/registry.c [deleted file]
include/Makefile.am
include/babeltrace/ctf/events-internal.h [new file with mode: 0644]
include/babeltrace/ctf/events.h
include/babeltrace/iterator-internal.h
include/babeltrace/iterator.h
lib/Makefile.am
lib/iterator.c
lib/registry.c [new file with mode: 0644]

index 0a9bb9abe17d5250c3e342fac2bc9c666e70388d..074e17d17ed26ac6bd69ebe2429e1f02cea702e9 100644 (file)
@@ -2,7 +2,7 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS = include types formats lib converter tests doc
+SUBDIRS = include types lib formats converter tests doc
 
 dist_doc_DATA = ChangeLog LICENSE mit-license.txt gpl-2.0.txt \
                std-ext-lib.txt
index 2b16f1e4baa93e3958ff6a9dd96a050dc5d7a306..9443c2148848d681d1817ba462f11ac1c9b2f44a 100644 (file)
@@ -13,7 +13,6 @@ babeltrace_SOURCES = \
 babeltrace_LDFLAGS = -Wl,--no-as-needed
 babeltrace_LDADD = \
        $(top_builddir)/lib/libbabeltrace.la \
-       $(top_builddir)/formats/libbabeltrace_registry.la \
        $(top_builddir)/formats/ctf/libctf.la \
        $(top_builddir)/formats/ctf/metadata/libctf-ast.la \
        $(top_builddir)/formats/ctf-text/libctf-text.la \
@@ -22,5 +21,5 @@ babeltrace_LDADD = \
 babeltrace_log_SOURCES = babeltrace-log.c
 
 babeltrace_log_LDADD = \
-       $(top_builddir)/formats/libbabeltrace_registry.la \
+       $(top_builddir)/lib/libbabeltrace.la \
        $(top_builddir)/formats/ctf/libctf.la
index 5bcfc2a745c68c59e49d597b4bca2517fbc36b1a..baffb195242889211a4d32eb6fd55d049352c474 100644 (file)
@@ -401,7 +401,7 @@ error:
 int convert_trace(struct trace_descriptor *td_write,
                  struct bt_context *ctx)
 {
-       struct bt_iter *iter;
+       struct bt_ctf_iter *iter;
        struct ctf_text_stream_pos *sout;
        struct bt_iter_pos begin_pos;
        struct bt_ctf_event *ctf_event;
@@ -411,25 +411,25 @@ int convert_trace(struct trace_descriptor *td_write,
                        trace_descriptor);
 
        begin_pos.type = BT_SEEK_BEGIN;
-       iter = bt_iter_create(ctx, &begin_pos, NULL);
+       iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
        if (!iter) {
                ret = -1;
                goto error_iter;
        }
-       while ((ctf_event = bt_iter_read_ctf_event(iter))) {
+       while ((ctf_event = bt_ctf_iter_read_event(iter))) {
                ret = sout->parent.event_cb(&sout->parent, ctf_event->stream);
                if (ret) {
                        fprintf(stderr, "[error] Writing event failed.\n");
                        goto end;
                }
-               ret = bt_iter_next(iter);
+               ret = bt_iter_next(bt_ctf_get_iter(iter));
                if (ret < 0)
                        goto end;
        }
        ret = 0;
 
 end:
-       bt_iter_destroy(iter);
+       bt_ctf_iter_destroy(iter);
 error_iter:
        return ret;
 }
index 7f95fcc5009f9bc11c163ef1730862fe447223c8..ee3a6eda66e05da967f1a34908cb04592050d0d9 100644 (file)
@@ -1,7 +1,3 @@
 AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
 
 SUBDIRS = . ctf ctf-text bt-dummy
-
-lib_LTLIBRARIES = libbabeltrace_registry.la
-
-libbabeltrace_registry_la_SOURCES = registry.c
index 50ef9098b2499cc221a8c318fba73ff8bd6613c6..016ffad9983716124ae33128153d4b7639588f86 100644 (file)
@@ -6,4 +6,4 @@ libbt_dummy_la_SOURCES = \
        bt-dummy.c
 
 libbt_dummy_la_LIBADD = \
-       ../libbabeltrace_registry.la
+       $(top_builddir)/lib/libbabeltrace.la
index e251ee7b80c22df13d9f89f6b8e6cc423997aa4d..e0a10c90d61c0767a390e7c94c4f045ca0db1278 100644 (file)
@@ -32,8 +32,7 @@
 #include <stdlib.h>
 
 static
-int bt_dummy_write_event(struct stream_pos *ppos,
-                        struct ctf_stream *stream)
+int bt_dummy_write_event(struct stream_pos *ppos, struct ctf_stream *stream)
 {
        return 0;
 }
index 8db9f39aea07c2b67492bdd9280b02a9ad827f78..ca033e058b31f1ed6dd6451130a1c6ad9a5519e2 100644 (file)
@@ -9,4 +9,4 @@ libctf_text_la_SOURCES = \
 
 libctf_text_la_LIBADD = \
        types/libctf-text-types.la \
-       ../libbabeltrace_registry.la
+       $(top_builddir)/lib/libbabeltrace.la
index 20e18e86fcf1e952c8f090db3dff58eb430a3e76..bf028615fd301fbfd922528bed6471eb14a8474d 100644 (file)
@@ -22,6 +22,7 @@
 #include <babeltrace/ctf-text/types.h>
 #include <babeltrace/ctf/metadata.h>
 #include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ctf/events-internal.h>
 #include <inttypes.h>
 #include <uuid/uuid.h>
 #include <sys/mman.h>
@@ -211,8 +212,8 @@ const char *print_loglevel(int value)
 }
 
 static
-int ctf_text_write_event(struct stream_pos *ppos,
-                        struct ctf_stream *stream)
+int ctf_text_write_event(struct stream_pos *ppos, struct ctf_stream *stream)
+                        
 {
        struct ctf_text_stream_pos *pos =
                container_of(ppos, struct ctf_text_stream_pos, parent);
index c9faac8b5e98d412dc7bb2fe6821777b4882912c..cf01be4baf65bee8448b027ce7cfbb2acec4a41a 100644 (file)
@@ -12,4 +12,4 @@ libctf_la_LIBADD = \
        types/libctf-types.la \
        metadata/libctf-parser.la \
        metadata/libctf-ast.la \
-       ../libbabeltrace_registry.la
+       $(top_builddir)/lib/libbabeltrace.la
index 17f299c3dece67bb334c769a1516fa9898ba0c60..83680cd697f764e23969a19ae86df8763e8bc508 100644 (file)
@@ -22,6 +22,7 @@
 #include <babeltrace/ctf/types.h>
 #include <babeltrace/ctf/metadata.h>
 #include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ctf/events-internal.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <uuid/uuid.h>
index 5ac74e6e1617cff6961713498d7cb21cff282033..27643fcbc61f513ce6d7cc0b021e7d6aea4bd053 100644 (file)
 #include <babeltrace/format.h>
 #include <babeltrace/ctf/events.h>
 #include <babeltrace/ctf-ir/metadata.h>
+#include <babeltrace/prio_heap.h>
+#include <babeltrace/iterator-internal.h>
+#include <babeltrace/ctf/events-internal.h>
+#include <babeltrace/ctf/metadata.h>
 #include <glib.h>
 
 /*
  */
 __thread int bt_ctf_last_field_error = 0;
 
+struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx,
+               struct bt_iter_pos *begin_pos,
+               struct bt_iter_pos *end_pos)
+{
+       struct bt_ctf_iter *iter;
+       int ret;
+
+       iter = g_new0(struct bt_ctf_iter, 1);
+       ret = bt_iter_init(&iter->parent, ctx, begin_pos, end_pos);
+       if (ret) {
+               g_free(iter);
+               return NULL;
+       }
+       return iter;
+}
+
+void bt_ctf_iter_destroy(struct bt_ctf_iter *iter)
+{
+       bt_iter_fini(&iter->parent);
+       g_free(iter);
+}
+
+struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter)
+{
+       return &iter->parent;
+}
+
+struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter)
+{
+       struct ctf_file_stream *file_stream;
+       struct bt_ctf_event *ret = &iter->current_ctf_event;
+
+       file_stream = heap_maximum(iter->parent.stream_heap);
+       if (!file_stream) {
+               /* end of file for all streams */
+               goto stop;
+       }
+       ret->stream = &file_stream->parent;
+       ret->event = g_ptr_array_index(ret->stream->events_by_id,
+                       ret->stream->event_id);
+
+       if (ret->stream->stream_id > iter->parent.callbacks->len)
+               goto end;
+
+       process_callbacks(&iter->parent, ret->stream);
+
+end:
+       return ret;
+stop:
+       return NULL;
+}
+
 struct definition *bt_ctf_get_top_level_scope(struct bt_ctf_event *event,
                enum bt_ctf_scope scope)
 {
diff --git a/formats/registry.c b/formats/registry.c
deleted file mode 100644 (file)
index 829ede9..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * BabelTrace
- *
- * Format Registry
- *
- * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
- *
- * Author: 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.
- */
-
-#include <babeltrace/format.h>
-#include <glib.h>
-#include <errno.h>
-#include <stdio.h>
-#include <assert.h>
-
-struct walk_data {
-       FILE *fp;
-       int iter;
-};
-
-static int init_done;
-void __attribute__((constructor)) format_init(void);
-void __attribute__((destructor)) format_finalize(void);
-
-/*
- * Format registry hash table contains the registered formats. Format
- * registration is typically performed by a format plugin.
- * TODO: support plugin unload (unregistration of formats).
- */
-GHashTable *format_registry;
-
-struct format *bt_lookup_format(bt_intern_str name)
-{
-       if (!init_done)
-               return NULL;
-       return g_hash_table_lookup(format_registry,
-                                  (gconstpointer) (unsigned long) name);
-}
-
-static void show_format(gpointer key, gpointer value, gpointer user_data)
-{
-       struct walk_data *data = user_data;
-
-       fprintf(data->fp, "%s%s", data->iter ? ", " : "",
-               g_quark_to_string((GQuark) (unsigned long) key));
-       data->iter++;
-}
-
-void bt_fprintf_format_list(FILE *fp)
-{
-       struct walk_data data;
-
-       data.fp = fp;
-       data.iter = 0;
-
-       fprintf(fp, "Formats available: ");
-       if (!init_done)
-               return;
-       g_hash_table_foreach(format_registry, show_format, &data);
-       if (data.iter == 0)
-               fprintf(fp, "<none>");
-       fprintf(fp, ".\n");
-}
-
-int bt_register_format(struct format *format)
-{
-       if (!init_done)
-               format_init();
-
-       if (bt_lookup_format(format->name))
-               return -EEXIST;
-
-       g_hash_table_insert(format_registry,
-                           (gpointer) (unsigned long) format->name,
-                           format);
-       return 0;
-}
-
-void format_init(void)
-{
-       if (init_done)
-               return;
-       format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
-       assert(format_registry);
-       init_done = 1;
-}
-
-void format_finalize(void)
-{
-       g_hash_table_destroy(format_registry);
-}
index fea3c25942e94c0104b27ea0b08a1b3a9f37be01..f4e410d53e33e31539a8f55435bed5b407b40549 100644 (file)
@@ -21,6 +21,7 @@ noinst_HEADERS = \
        babeltrace/prio_heap.h \
        babeltrace/types.h \
        babeltrace/ctf-ir/metadata.h \
+       babeltrace/ctf/events-internal.h \
        babeltrace/ctf/metadata.h \
        babeltrace/ctf-text/types.h \
        babeltrace/ctf/types.h \
diff --git a/include/babeltrace/ctf/events-internal.h b/include/babeltrace/ctf/events-internal.h
new file mode 100644 (file)
index 0000000..deff105
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef _BABELTRACE_CTF_EVENTS_INTERNAL_H
+#define _BABELTRACE_CTF_EVENTS_INTERNAL_H
+
+/*
+ * BabelTrace
+ *
+ * CTF events API (internal)
+ *
+ * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *         Julien Desfossez <julien.desfossez@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.
+ */
+
+#include <babeltrace/iterator-internal.h>
+
+struct bt_ctf_iter {
+       struct bt_iter parent;
+       struct bt_ctf_event current_ctf_event;  /* last read event */
+};
+
+#endif /*_BABELTRACE_CTF_EVENTS_INTERNAL_H */
index f2ef22dfbb6491c1ac66aa08919de5419dfc1ba1..61e3f6a31a948d060088b66a165c3db2526dedbd 100644 (file)
  */
 
 #include <stdint.h>
+#include <babeltrace/iterator.h>
 
 struct ctf_stream;
 struct ctf_stream_event;
 struct definition;
+struct bt_ctf_iter;
 
 /*
  * the top-level scopes in CTF
@@ -66,6 +68,42 @@ struct bt_ctf_event {
        struct ctf_stream_event *event;
 };
 
+/*
+ * bt_ctf_iter_read_event: Read the iterator's current event data.
+ *
+ * @iter: trace collection iterator (input)
+ * @stream: stream containing event at current position (output)
+ * @event: current event (output)
+ * Return 0 on success, negative error value on error.
+ */
+struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
+
+/*
+ * bt_ctf_iter_create - Allocate a CTF trace collection iterator.
+ *
+ * begin_pos and end_pos are optional parameters to specify the position
+ * at which the trace collection should be seeked upon iterator
+ * creation, and the position at which iteration will start returning
+ * "EOF".
+ *
+ * By default, if begin_pos is NULL, a BT_SEEK_CUR is performed at
+ * creation. By default, if end_pos is NULL, a BT_SEEK_END (end of
+ * trace) is the EOF criterion.
+ */
+struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx,
+               struct bt_iter_pos *begin_pos,
+               struct bt_iter_pos *end_pos);
+
+/*
+ * bt_ctf_get_iter - get iterator from ctf iterator.
+ */
+struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter);
+
+/*
+ * bt_ctf_iter_destroy - Free a CTF trace collection iterator.
+ */
+void bt_ctf_iter_destroy(struct bt_ctf_iter *iter);
+
 /*
  * bt_ctf_get_top_level_scope: return a definition of the top-level scope
  *
index 121d0dccb3d6c4bf3a74918a64e176632225d2ad..c9b5d60716f43d7eee77b580a58147f35f994b8d 100644 (file)
@@ -21,6 +21,7 @@
  * all copies or substantial portions of the Software.
  */
 
+#include <babeltrace/callbacks-internal.h>
 #include <babeltrace/ctf/events.h>
 
 /*
@@ -31,7 +32,6 @@ struct bt_iter {
        struct ptr_heap *stream_heap;
        struct bt_context *ctx;
        struct bt_iter_pos *end_pos;
-       struct bt_ctf_event current_ctf_event;          /* last read event */
        GArray *callbacks;                              /* Array of struct bt_stream_callbacks */
        struct bt_callback_chain main_callbacks;        /* For all events */
        /*
@@ -51,4 +51,10 @@ struct bt_iter {
        GPtrArray *dep_gc;
 };
 
+int bt_iter_init(struct bt_iter *iter,
+               struct bt_context *ctx,
+               struct bt_iter_pos *begin_pos,
+               struct bt_iter_pos *end_pos);
+void bt_iter_fini(struct bt_iter *iter);
+
 #endif /* _BABELTRACE_ITERATOR_INTERNAL_H */
index 3dcf0825f2efaf03ef7b9545512d0b61f52a7946..e9719fc87718edeb79ffa243f0b7a04fc1d80835 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <babeltrace/format.h>
 #include <babeltrace/context.h>
-#include <babeltrace/ctf/events.h>
 
 /* Forward declarations */
 struct bt_iter;
@@ -107,14 +106,4 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *pos);
 struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
                uint64_t timestamp);
 
-/*
- * bt_iter_read_ctf_event: Read the iterator's current event data.
- *
- * @iter: trace collection iterator (input)
- * @stream: stream containing event at current position (output)
- * @event: current event (output)
- * Return 0 on success, negative error value on error.
- */
-struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter);
-
 #endif /* _BABELTRACE_ITERATOR_H */
index fa591aef6b82b7d9020ab93a699b4509cbb5bebe..5d069a96d9dd702a76eb685767e4afa7ab995cdb 100644 (file)
@@ -9,10 +9,9 @@ libbabeltrace_la_SOURCES = babeltrace.c \
                           iterator.c \
                           context.c \
                           trace-handle.c \
-                          trace-collection.c
+                          trace-collection.c \
+                          registry.c
 
 libbabeltrace_la_LIBADD = \
        $(top_builddir)/types/libbabeltrace_types.la \
-       $(top_builddir)/formats/libbabeltrace_registry.la \
-       $(top_builddir)/formats/ctf/libctf.la \
        prio_heap/libprio_heap.la
index 398b097bce00bdb7723bcb2ea75c22d399888620..bf87193a3b64f8bdf491fc5ba309f4ca4ae80e82 100644 (file)
 #include <babeltrace/callbacks-internal.h>
 #include <babeltrace/context.h>
 #include <babeltrace/context-internal.h>
-#include <babeltrace/ctf/metadata.h>
 #include <babeltrace/iterator-internal.h>
 #include <babeltrace/iterator.h>
 #include <babeltrace/prio_heap.h>
+#include <babeltrace/ctf/metadata.h>
 #include <babeltrace/ctf/events.h>
 #include <inttypes.h>
 
@@ -435,15 +435,14 @@ end:
        return ret;
 }
 
-struct bt_iter *bt_iter_create(struct bt_context *ctx,
+int bt_iter_init(struct bt_iter *iter,
+               struct bt_context *ctx,
                struct bt_iter_pos *begin_pos,
                struct bt_iter_pos *end_pos)
 {
        int i, stream_id;
        int ret = 0;
-       struct bt_iter *iter;
 
-       iter = g_new0(struct bt_iter, 1);
        iter->stream_heap = g_new(struct ptr_heap, 1);
        iter->end_pos = end_pos;
        iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks));
@@ -498,17 +497,32 @@ struct bt_iter *bt_iter_create(struct bt_context *ctx,
                }
        }
 
-       return iter;
+       return 0;
 
 error:
        heap_free(iter->stream_heap);
 error_heap_init:
        g_free(iter->stream_heap);
-       g_free(iter);
-       return NULL;
+       return ret;
 }
 
-void bt_iter_destroy(struct bt_iter *iter)
+struct bt_iter *bt_iter_create(struct bt_context *ctx,
+               struct bt_iter_pos *begin_pos,
+               struct bt_iter_pos *end_pos)
+{
+       struct bt_iter *iter;
+       int ret;
+
+       iter = g_new0(struct bt_iter, 1);
+       ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
+       if (ret) {
+               g_free(iter);
+               return NULL;
+       }
+       return iter;
+}
+
+void bt_iter_fini(struct bt_iter *iter)
 {
        struct bt_stream_callbacks *bt_stream_cb;
        struct bt_callback_chain *bt_chain;
@@ -540,7 +554,11 @@ void bt_iter_destroy(struct bt_iter *iter)
        }
 
        bt_context_put(iter->ctx);
+}
 
+void bt_iter_destroy(struct bt_iter *iter)
+{
+       bt_iter_fini(iter);
        g_free(iter);
 }
 
@@ -572,28 +590,3 @@ int bt_iter_next(struct bt_iter *iter)
 end:
        return ret;
 }
-
-struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter)
-{
-       struct ctf_file_stream *file_stream;
-       struct bt_ctf_event *ret = &iter->current_ctf_event;
-
-       file_stream = heap_maximum(iter->stream_heap);
-       if (!file_stream) {
-               /* end of file for all streams */
-               goto stop;
-       }
-       ret->stream = &file_stream->parent;
-       ret->event = g_ptr_array_index(ret->stream->events_by_id,
-                       ret->stream->event_id);
-
-       if (ret->stream->stream_id > iter->callbacks->len)
-               goto end;
-
-       process_callbacks(iter, ret->stream);
-
-end:
-       return ret;
-stop:
-       return NULL;
-}
diff --git a/lib/registry.c b/lib/registry.c
new file mode 100644 (file)
index 0000000..829ede9
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * BabelTrace
+ *
+ * Format Registry
+ *
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: 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.
+ */
+
+#include <babeltrace/format.h>
+#include <glib.h>
+#include <errno.h>
+#include <stdio.h>
+#include <assert.h>
+
+struct walk_data {
+       FILE *fp;
+       int iter;
+};
+
+static int init_done;
+void __attribute__((constructor)) format_init(void);
+void __attribute__((destructor)) format_finalize(void);
+
+/*
+ * Format registry hash table contains the registered formats. Format
+ * registration is typically performed by a format plugin.
+ * TODO: support plugin unload (unregistration of formats).
+ */
+GHashTable *format_registry;
+
+struct format *bt_lookup_format(bt_intern_str name)
+{
+       if (!init_done)
+               return NULL;
+       return g_hash_table_lookup(format_registry,
+                                  (gconstpointer) (unsigned long) name);
+}
+
+static void show_format(gpointer key, gpointer value, gpointer user_data)
+{
+       struct walk_data *data = user_data;
+
+       fprintf(data->fp, "%s%s", data->iter ? ", " : "",
+               g_quark_to_string((GQuark) (unsigned long) key));
+       data->iter++;
+}
+
+void bt_fprintf_format_list(FILE *fp)
+{
+       struct walk_data data;
+
+       data.fp = fp;
+       data.iter = 0;
+
+       fprintf(fp, "Formats available: ");
+       if (!init_done)
+               return;
+       g_hash_table_foreach(format_registry, show_format, &data);
+       if (data.iter == 0)
+               fprintf(fp, "<none>");
+       fprintf(fp, ".\n");
+}
+
+int bt_register_format(struct format *format)
+{
+       if (!init_done)
+               format_init();
+
+       if (bt_lookup_format(format->name))
+               return -EEXIST;
+
+       g_hash_table_insert(format_registry,
+                           (gpointer) (unsigned long) format->name,
+                           format);
+       return 0;
+}
+
+void format_init(void)
+{
+       if (init_done)
+               return;
+       format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
+       assert(format_registry);
+       init_done = 1;
+}
+
+void format_finalize(void)
+{
+       g_hash_table_destroy(format_registry);
+}
This page took 0.055313 seconds and 4 git commands to generate.