aboutsummaryrefslogtreecommitdiff
path: root/bin/ext_urid.c
blob: d5534b8994a55f29282bd5abe2113171e49ed827 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <ext_urid.h>

#include <Eina.h>

//TODO eina_lock_release may fail

struct _ext_urid_t {
	LV2_URID cnt;

	Eina_Lock mutex;

	Eina_Hash *uris;
	Eina_Hash *urids;
	
	LV2_URID_Map map;
	LV2_URID_Unmap unmap;
};

static void
_uri_hash_uri_del(void *data)
{
	LV2_URID *urid = data;
	free(urid);
}

static void
_uri_hash_urid_del(void *data)
{
	char *uri_dup = data;
	free(uri_dup);
}

static LV2_URID
_urid_map(LV2_URID_Map_Handle handle, const char *uri)
{
	ext_urid_t *uri_hash = handle;
	LV2_URID *urid = NULL;
	
	Eina_Lock_Result res = eina_lock_take(&uri_hash->mutex);
	if(res == EINA_LOCK_SUCCEED)
	{
		// uri already registered?
		urid = eina_hash_find(uri_hash->uris, uri);
		eina_lock_release(&uri_hash->mutex);

		if(urid)
			return *urid;
	}
	else
	{
		fprintf(stderr, "_urid_map: lock failed\n");
		return 0;
	}

	// duplicate uri
	char *uri_dup = strdup(uri);
	if(!uri_dup)
	{
		fprintf(stderr, "_urid_map: out-of-memory\n");
		return 0;
	}

	// create new urid for uri
	urid = malloc(sizeof(LV2_URID));
	if(!urid)
	{
		free(uri_dup);
		fprintf(stderr, "_urid_map: out-of-memory\n");
		return 0;
	}
	
	res = eina_lock_take(&uri_hash->mutex);
	if(res == EINA_LOCK_SUCCEED)
	{
		if(eina_hash_add(uri_hash->uris, uri_dup, urid))
		{
			*urid = uri_hash->cnt++;

			if(eina_hash_add(uri_hash->urids, urid, uri_dup))
			{
				eina_lock_release(&uri_hash->mutex);
				return *urid;
			}
			else
			{
				eina_hash_del(uri_hash->uris, uri_dup, urid);
			}
		}

		eina_lock_release(&uri_hash->mutex);
	}
	fprintf(stderr, "_urid_map: lock failed\n");

	free(urid);
	free(uri_dup);

	return 0;
}

static const char *
_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
{
	ext_urid_t *uri_hash = handle;

	Eina_Lock_Result res = eina_lock_take(&uri_hash->mutex);

	if(res != EINA_LOCK_SUCCEED)
	{
		fprintf(stderr, "_urid_map: lock failed\n");
		return NULL;
	}

	const char *uri = eina_hash_find(uri_hash->urids, &urid);
	eina_lock_release(&uri_hash->mutex);

	return uri;
}

ext_urid_t *
ext_urid_new()
{
	ext_urid_t *ext_urid = malloc(sizeof(ext_urid_t));
	if(!ext_urid)
		return NULL;

	ext_urid->cnt = 1;

	if(eina_lock_new(&ext_urid->mutex) == EINA_TRUE)
	{
		ext_urid->uris = eina_hash_string_superfast_new(_uri_hash_uri_del);
		if(ext_urid->uris)
		{
			ext_urid->urids = eina_hash_int32_new(_uri_hash_urid_del);
			if(ext_urid->urids)
			{
				ext_urid->map.handle = ext_urid;
				ext_urid->map.map = _urid_map;

				ext_urid->unmap.handle = ext_urid;
				ext_urid->unmap.unmap = _urid_unmap;

				return ext_urid;
			} // urids

			eina_hash_free(ext_urid->uris);
		} // uris

		eina_lock_free(&ext_urid->mutex);
	} // mutex

	free(ext_urid);
	return NULL;
}

void
ext_urid_free(ext_urid_t *ext_urid)
{
	if(!ext_urid)
		return;

	if(ext_urid->uris)
		eina_hash_free(ext_urid->uris);
	if(ext_urid->urids)
		eina_hash_free(ext_urid->urids);
	eina_lock_free(&ext_urid->mutex);

	free(ext_urid);
}

LV2_URID_Map *
ext_urid_map_get(ext_urid_t *ext_urid)
{
	return &ext_urid->map;
}

LV2_URID_Unmap *
ext_urid_unmap_get(ext_urid_t *ext_urid)
{
	return &ext_urid->unmap;
}

LV2_URID
ext_urid_map(ext_urid_t *ext_urid, const char *uri)
{
	return ext_urid->map.map(ext_urid->map.handle, uri);
}

const char *
ext_urid_unmap(ext_urid_t *ext_urid, const LV2_URID urid)
{
	return ext_urid->unmap.unmap(ext_urid->unmap.handle, urid);
}