File: object.c

package info (click to toggle)
atheme-services 7.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,992 kB
  • sloc: ansic: 93,230; sh: 7,440; php: 5,032; perl: 3,327; makefile: 1,261; sed: 16; ruby: 15; python: 3
file content (325 lines) | stat: -rw-r--r-- 7,085 bytes parent folder | download | duplicates (2)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * atheme-services: A collection of minimalist IRC services
 * object.c: Object management.
 *
 * Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "atheme.h"

#ifdef OBJECT_DEBUG
mowgli_list_t object_list = { NULL, NULL, 0 };
#endif

mowgli_heap_t *metadata_heap;	/* HEAP_CHANUSER */

void init_metadata(void)
{
	metadata_heap = sharedheap_get(sizeof(metadata_t));

	if (metadata_heap == NULL)
	{
		slog(LG_ERROR, "init_metadata(): block allocator failure.");
		exit(EXIT_FAILURE);
	}
}

/*
 * object_init
 *
 * Populates the object manager part of an object.
 *
 * Inputs:
 *      - pointer to object manager area
 *      - (optional) name of object
 *      - (optional) custom destructor; if this is specified it must free
 *        the metadata itself
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - none
 */
void object_init(object_t *obj, const char *name, destructor_t des)
{
	return_if_fail(obj != NULL);

	obj->destructor = des;
	obj->refcount = 1;

#ifdef OBJECT_DEBUG
	mowgli_node_add(obj, &obj->dnode, &object_list);
#endif
}

/*
 * object_ref
 *
 * Increment the reference counter on an object.
 *
 * Inputs:
 *      - the object to refcount
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - none
 */
void * object_ref(void *object)
{
	return_val_if_fail(object != NULL, NULL);

	/*
	 * refcount == 0 really should not be possible here
	 * but in fact it happens for modules/exttarget/.  --jilles
	 *
	 * actually... refcount == 0 means the object is initially
	 * unowned, which is intentional for the exttarget code, the
	 * exttarget virtual entity object is built by a 'factory' so
	 * we want to sink the reference to make it an unowned object.
	 *   --nenolod
	 */
	return_val_if_fail(object(object)->refcount >= 0, object);

	object(object)->refcount++;
#ifdef DEBUG_OBJECT_REF
	slog(LG_DEBUG, "object_ref(%p): %d references", object, object(object)->refcount);
#endif

	return object;
}

/*
 * object_sink_ref
 *
 * Decrement the reference counter on an object.
 *
 * Inputs:
 *      - the object to refcount
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - none
 */
void *object_sink_ref(void *obj)
{
	return_val_if_fail(obj != NULL, NULL);
	return_val_if_fail(object(obj)->refcount > 0, obj);
	object(obj)->refcount--;

#ifdef DEBUG_OBJECT_REF
	slog(LG_DEBUG, "object_sink_ref(%p): %d references", obj, object(obj)->refcount);
#endif

	return obj;
}

/*
 * object_unref
 *
 * Decrement the reference counter on an object.
 *
 * Inputs:
 *      - the object to refcount
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - if the refcount is 0, the object is destroyed.
 */
void object_unref(void *obj)
{
	return_if_fail(obj != NULL);
	return_if_fail(object(obj)->refcount >= -1);

	if (object(obj)->refcount == -1)
		return;
	else if (object(obj)->refcount > 0)
	{
		object_sink_ref(obj);
		if (object(obj)->refcount == 0)
			object_dispose(obj);
	}
}

/*
 * object_dispose
 *
 * Disposes of an object.
 *
 * Inputs:
 *      - the object to dispose
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - the object is destroyed
 */
void object_dispose(void *object)
{
	object_t *obj;
	mowgli_patricia_t *privatedata, *metadata;

	return_if_fail(object != NULL);
	obj = object(object);

	/* we shouldn't be disposing an object more than once in it's
	 * lifecycle.  so do a soft assert and get out if that's the case.
	 *    --nenolod
	 */
	return_if_fail(obj->refcount != -1);

	/* set refcount to -1 to ensure that object_unref() doesn't cause a loop */
	obj->refcount = -1;

	privatedata = obj->privatedata;
	metadata = obj->metadata;

#ifdef OBJECT_DEBUG
	mowgli_node_delete(&obj->dnode, &object_list);
#endif

	if (obj->destructor != NULL)
		obj->destructor(obj);
	else
	{
		metadata_delete_all(obj);
		free(obj);
	}

	if (privatedata != NULL)
		mowgli_patricia_destroy(privatedata, NULL, NULL);

	if (metadata != NULL)
		mowgli_patricia_destroy(metadata, NULL, NULL);
}

metadata_t *metadata_add(void *target, const char *name, const char *value)
{
	object_t *obj;
	metadata_t *md;

	return_val_if_fail(name != NULL, NULL);
	return_val_if_fail(value != NULL, NULL);

	obj = object(target);

	if (obj->metadata == NULL)
		obj->metadata = mowgli_patricia_create(strcasecanon);

	if (metadata_find(target, name))
		metadata_delete(target, name);

	md = mowgli_heap_alloc(metadata_heap);

	md->name = strshare_get(name);
	md->value = sstrdup(value);

	mowgli_patricia_add(obj->metadata, md->name, md);

	return md;
}

void metadata_delete(void *target, const char *name)
{
	object_t *obj;
	metadata_t *md = metadata_find(target, name);

	if (!md)
		return;

	obj = object(target);

	if (obj->metadata == NULL)
		obj->metadata = mowgli_patricia_create(strcasecanon);

	mowgli_patricia_delete(obj->metadata, name);

	strshare_unref(md->name);
	free(md->value);

	mowgli_heap_free(metadata_heap, md);
}

metadata_t *metadata_find(void *target, const char *name)
{
	object_t *obj;

	return_val_if_fail(target != NULL, NULL);
	return_val_if_fail(name != NULL, NULL);

	obj = object(target);

	if (obj->metadata == NULL)
		obj->metadata = mowgli_patricia_create(strcasecanon);

	return mowgli_patricia_retrieve(obj->metadata, name);
}

void metadata_delete_all(void *target)
{
	object_t *obj;
	metadata_t *md;
	mowgli_patricia_iteration_state_t state;

	obj = object(target);

	if (obj->metadata == NULL)
		obj->metadata = mowgli_patricia_create(strcasecanon);

	MOWGLI_PATRICIA_FOREACH(md, &state, obj->metadata)
	{
		metadata_delete(obj, md->name);
	}
}

void *privatedata_get(void *target, const char *key)
{
	object_t *obj;

	obj = object(target);
	if (obj->privatedata == NULL)
		return NULL;

	return mowgli_patricia_retrieve(obj->privatedata, key);
}

void privatedata_set(void *target, const char *key, void *data)
{
	object_t *obj;

	obj = object(target);
	if (obj->privatedata == NULL)
		obj->privatedata = mowgli_patricia_create(noopcanon);

	mowgli_patricia_add(obj->privatedata, key, data);
}

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
 * vim:ts=8
 * vim:sw=8
 * vim:noexpandtab
 */