File: services.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (379 lines) | stat: -rw-r--r-- 8,965 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * Generic COM interface registration module.
 * Basically maintains a list of COM interface pointers
 * for a particular interface GUID (IID).
 * Given an IID, you can find all the registered COM interfaces with that IID.
 * Interfaces will be returned in the order in which they were registered.
 * It's harmless to register an interface multiple times;
 * only a single entry in the table will be retained.
 * Currently just does everything with simple lists;
 * if we end up having lots of objects, we may need smarter algorithms.
 */

/*
 * Locking! This is not thread safe, which is *going* to cause problems.
 */

#include <oskit/com.h>
#include <oskit/com/services.h>
#include <oskit/com/mem.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/assert.h>

static struct oskit_services_ops services_ops;

/*
 * One of these nodes represents each registered COM interface (object)
 */
struct objnode {
	struct objnode *next;
	oskit_iunknown_t *intf;
};

/*
 * We keep one iidnode for each unique IID we see
 */
struct iidnode {
	struct iidnode *next;
	oskit_guid_t iid;
	struct objnode *objs;
	int objcount;
};

/*
 * This is the interface object. Each services database is parameterized
 * with a memory object.
 */
struct db {
	oskit_services_t	servi;		/* COM interface */
	int			count;		/* Reference count */
	struct iidnode		*iids;		/* DB pointer */
	oskit_mem_t		*memi;		/* Memory object to use */
};

static OSKIT_COMDECL 
services_query(oskit_services_t *si,
	       const oskit_iid_t *iid, void **out_ihandle)
{
	struct db	*s = (struct db *) si;
		
        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_services_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &s->servi;
		s->count++;
                return 0; 
        }
  
        *out_ihandle = 0;
        return OSKIT_E_NOINTERFACE;
};

static OSKIT_COMDECL_U
services_addref(oskit_services_t *si)
{
	struct db	*s = (struct db *) si;

	assert(s->count);
	return ++s->count;
}

static OSKIT_COMDECL_U
services_release(oskit_services_t *si)
{
	struct db	*s = (struct db *) si;

	assert(s->count);

	if (--s->count == 0) {
		oskit_mem_t	*memi = s->memi;
		struct iidnode  *in, *nin;
		struct objnode  *on, *non;

		/*
		 * Must free up all the registered interface objects.
		 */
		in = s->iids;
		while (in) {
			on = in->objs;
			while (on) {
				oskit_iunknown_release(on->intf);
				non = on->next;
				oskit_mem_free(memi,
					       (void *) on, sizeof(*on), 0);
				on  = non;
			}

			nin = in->next;
			oskit_mem_free(memi, (void *) in, sizeof(*in), 0);
			in  = nin;
		}

		oskit_mem_free(memi, (void *)s, sizeof(*s), 0);
		oskit_mem_release(memi);
		return 0;
	}
	return s->count;
}

/*
 * Register an interface in the services database.
 * More than one interface can be registered for a particular IID.
 */
OSKIT_COMDECL
services_addservice(oskit_services_t *si,
		    const struct oskit_guid *iid, void *interface)
{
	struct db	 *s = (struct db *) si;
	oskit_iunknown_t *iu = (oskit_iunknown_t*)interface;
	struct iidnode   *in;
	struct objnode   *on, **onp;

	/* Find or create the appropriate iidnode */
	for (in = s->iids; ; in = in->next) {
		if (in == NULL) {
			in = oskit_mem_alloc(s->memi, sizeof(*in), 0);
			if (in == NULL)
				return OSKIT_E_OUTOFMEMORY;
			in->iid = *iid;
			in->objs = NULL;
			in->objcount = 0;
			in->next = s->iids;
			s->iids = in;
			break;
		}
		if (memcmp(&in->iid, iid, sizeof(*iid)) == 0)
			break;
	}

	/* Make sure this interface isn't already registered */
	for (onp = &in->objs; *onp; onp = &(*onp)->next) {
		if ((*onp)->intf == interface)
			return 0;
	}

	/* Create a new objnode for this interface */
	on = oskit_mem_alloc(s->memi, sizeof(*on), 0);
	if (on == NULL)
		return OSKIT_E_OUTOFMEMORY;
	on->next = NULL;
	on->intf = iu;	oskit_iunknown_addref(iu);
	*onp = on;
	in->objcount++;

	return 0;
}

/*
 * Unregister a previously registered interface.
 */
OSKIT_COMDECL
services_remservice(oskit_services_t *si,
		    const struct oskit_guid *iid, void *interface)
{
	struct db	 *s = (struct db *) si;
	struct iidnode   *in;
	struct objnode   *on, **onp;

	/* Find the appropriate iidnode */
	for (in = s->iids; ; in = in->next) {
		if (in == NULL)
			return OSKIT_E_INVALIDARG;
		if (memcmp(&in->iid, iid, sizeof(*iid)) == 0)
			break;
	}

	/* Find and remove the objnode */
	for (onp = &in->objs; ; onp = &on->next) {
		on = *onp;
		if (on == NULL)
			return OSKIT_E_INVALIDARG;
		if (on->intf == interface)
			break;
	}
	*onp = on->next;
	oskit_iunknown_release(on->intf);
	oskit_mem_free(s->memi, (void *) on, sizeof(*on), 0);
	in->objcount--;

	return 0;
}

/*
 * Obtain a list of all the registered interfaces with a specified IID.
 */
OSKIT_COMDECL
services_lookup(oskit_services_t *si,
		const oskit_guid_t *iid, void ***out_interface_array)
{
	struct db	 *s = (struct db *) si;
	struct iidnode *in;
	struct objnode *on;
	void **arr;
	int i;

	/* Find the appropriate iidnode */
	for (in = s->iids; ; in = in->next) {
		if (in == NULL) {
			*out_interface_array = NULL;
			return 0;
		}
		if (memcmp(&in->iid, iid, sizeof(*iid)) == 0)
			break;
	}
	if (in->objcount == 0) {
		*out_interface_array = NULL;
		return 0;
	}

	/*
	 * Allocate an array to hold the interface pointers to return
	 * Note that we *do* use malloc here, since the caller is responsible
	 * for freeing up the array. 
	 */
	arr = malloc(sizeof(*arr)*in->objcount);
	if (arr == NULL)
		return OSKIT_E_OUTOFMEMORY;

	/* Fill it in */
	for (i = 0, on = in->objs; i < in->objcount; i++, on = on->next) {
		assert(on != NULL);
		arr[i] = on->intf;
		oskit_iunknown_addref(on->intf);
	}
	assert(on == NULL);

	*out_interface_array = arr;
	return in->objcount;
}

/*
 * Lookup the first interface registered for a given IID.
 * This is typically used to look up "the" instance of a service
 */
OSKIT_COMDECL
services_lookup_first(oskit_services_t *si,
		     const oskit_guid_t *iid, void **out_intf)
{
	struct db	 *s = (struct db *) si;
	struct iidnode *in;
	oskit_iunknown_t *intf;

	/* Find the appropriate iidnode */
	for (in = s->iids; ; in = in->next) {
		if (in == NULL) {
			*out_intf = NULL;
			return 0;
		}
		if (memcmp(&in->iid, iid, sizeof(*iid)) == 0)
			break;
	}
	if (in->objcount == 0) {
		*out_intf = NULL;
		return 0;
	}

	*out_intf = intf = in->objs->intf;
	oskit_iunknown_addref(intf);
	return 0;
}

oskit_error_t
services_clone(oskit_services_t *si, oskit_services_t **out_intf)
{
	struct db	*s = (struct db *) si;
	struct db	*ns;
	struct iidnode  *in;
	struct objnode  *on;
	oskit_error_t	rc;

	ns = oskit_mem_alloc(s->memi, sizeof(*ns), 0);
	if (ns == NULL)
		return OSKIT_E_OUTOFMEMORY;

	ns->count     = 1;
	ns->memi      = s->memi;
	ns->servi.ops = &services_ops;
	ns->iids      = 0;
	oskit_mem_addref(ns->memi);

	in = s->iids;
	while (in) {
		on = in->objs;
		while (on) {
			if ((rc = services_addservice(&ns->servi,
						      &in->iid, on->intf))
			    != NULL) {
				panic("services_clone");
			}
			on = on->next;
		}
		in = in->next;
	}

	*out_intf = &ns->servi;
	return 0;
}

static struct oskit_services_ops services_ops = {
	services_query,
	services_addref,
	services_release,
	services_addservice,
	services_remservice,
	services_lookup,
	services_lookup_first,
	services_clone,
};

/*
 * The memory object interface is optional, except when creating the
 * initial global registry. Subsequent to the creation of the global
 * registry, we can look there for a default memory object is one is
 * not provided.
 */
oskit_error_t
oskit_services_create(oskit_mem_t *memi, oskit_services_t **out_intf)
{
	struct db	*s;

	/*
	 * If a memory object was not provided, look in the global registry.
	 */
	if (!memi) {
		oskit_lookup_first(&oskit_mem_iid, (void *) &memi);

		if (!memi)
			panic("oskit_services_create: Null memory object!");
	}

	s = oskit_mem_alloc(memi, sizeof(*s), 0);
	if (s == NULL)
		return OSKIT_E_OUTOFMEMORY;

	s->count     = 1;
	s->memi      = memi;
	s->servi.ops = &services_ops;
	s->iids      = 0;
	oskit_mem_addref(memi);

	*out_intf = &s->servi;
	
	return 0;
}