File: registry.c

package info (click to toggle)
babeltrace 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,636 kB
  • ctags: 3,016
  • sloc: ansic: 24,798; sh: 11,915; yacc: 2,318; makefile: 377; lex: 142
file content (154 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * 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.
 *
 * 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 <babeltrace/format.h>
#include <glib.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>

struct walk_data {
	FILE *fp;
	int iter;
};

/*
 * Format registry hash table contains the registered formats. Format
 * registration is typically performed by a format plugin.
 */
static GHashTable *format_registry;
static int format_refcount;
static int init_done;

static __attribute__((constructor)) void format_init(void);

static
void format_refcount_inc(void)
{
	format_refcount++;
}

static
void format_cleanup(void)
{
	if (format_registry)
		g_hash_table_destroy(format_registry);
}

static
void format_refcount_dec(void)
{
	if (!--format_refcount)
		format_cleanup();
}

struct bt_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;

	assert(fp);

	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 bt_format *format)
{
	if (!format)
		return -EINVAL;

	if (!init_done)
		format_init();

	if (bt_lookup_format(format->name))
		return -EEXIST;

	format_refcount_inc();
	g_hash_table_insert(format_registry,
			    (gpointer) (unsigned long) format->name,
			    format);
	return 0;
}

void bt_unregister_format(struct bt_format *format)
{
	assert(bt_lookup_format(format->name));
	g_hash_table_remove(format_registry,
			    (gpointer) (unsigned long) format->name);
	format_refcount_dec();
}

/*
 * We cannot assume that the constructor and destructor order will be
 * right: another library might be loaded before us, and initialize us
 * from bt_register_format(). This is why we use a reference count to
 * handle cleanup of this module. The format_finalize destructor
 * refcount decrement matches format_init refcount increment.
 */
static __attribute__((constructor))
void format_init(void)
{
	if (init_done)
		return;
	format_refcount_inc();
	format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
	assert(format_registry);
	init_done = 1;
}

static __attribute__((destructor))
void format_finalize(void)
{
	format_refcount_dec();
}