source: roaraudio/libroar/plugincontainer.c @ 5488:cd3f4f76a154

Last change on this file since 5488:cd3f4f76a154 was 5488:cd3f4f76a154, checked in by phi, 12 years ago

Improved plugincontainer (pr1).

File size: 14.1 KB
Line 
1//plugincontainer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#define MAX_PLUGINS 64
39
40struct roar_plugincontainer {
41 size_t refc;
42 const struct roar_plugincontainer_callbacks * callbacks;
43 void * userdata;
44 struct roar_dl_librarypara * default_para;
45 struct roar_dl_lhandle * handle[MAX_PLUGINS];
46 size_t deprefc[MAX_PLUGINS];
47 void * context[MAX_PLUGINS];
48 size_t numhandles;
49};
50
51static inline void _unload(struct roar_plugincontainer * cont, size_t index) {
52 int err = roar_error;
53
54 if ( cont->callbacks != NULL && cont->callbacks->preunload != NULL )
55  cont->callbacks->preunload(cont, &(cont->context[index]), cont->handle[index]);
56
57 roar_err_set(err);
58 roar_dl_unref(cont->handle[index]);
59 err = roar_error;
60 cont->handle[index] = NULL;
61 cont->numhandles--;
62
63 if ( cont->callbacks != NULL && cont->callbacks->postunload != NULL )
64  cont->callbacks->postunload(cont, &(cont->context[index]));
65
66 if ( cont->context[index] != NULL )
67  if ( cont->callbacks != NULL && cont->callbacks->freecontext != NULL )
68  cont->callbacks->freecontext(cont, &(cont->context[index]));
69
70 if ( cont->context[index] != NULL ) {
71  roar_mm_free(cont->context[index]);
72  cont->context[index] = NULL;
73 }
74
75 roar_err_set(err);
76}
77
78static int _loader(struct roar_dl_librarypara * lhandle, void * loader_userdata, enum roar_dl_loadercmd cmd, void * argp) {
79 roar_err_set(ROAR_ERROR_NOSYS);
80 return -1;
81}
82
83static inline void _copy_str(char ** dst, const char * src) {
84 for (; *src; src++, (*dst)++) **dst = *src;
85 *((*dst)++) = 0;
86}
87
88static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) {
89 size_t buflen = 0;
90 size_t i;
91 void * ret;
92 char * p;
93 struct roar_keyval * c;
94
95 for (i = 0; i < len; i++) {
96  if ( src[i].key != NULL )
97   buflen += roar_mm_strlen(src[i].key) + 1;
98  if ( src[i].value != NULL )
99   buflen += roar_mm_strlen(src[i].value) + 1;
100 }
101
102 c   = roar_mm_malloc(len * sizeof(struct roar_keyval));
103 if ( c == NULL )
104  return NULL;
105
106 memset(c, 0, len * sizeof(struct roar_keyval));
107
108 ret = roar_mm_malloc(buflen);
109 if ( ret == NULL )
110  return NULL;
111
112 memset(ret, 0, buflen);
113
114 p = ret;
115
116 for (i = 0; i < len; i++) {
117  if ( src[i].key == NULL ) {
118   c[i].key = NULL;
119  } else {
120   c[i].key = p;
121   _copy_str(&p, src[i].key);
122  }
123
124  if ( src[i].value == NULL ) {
125   c[i].value = NULL;
126  } else {
127   c[i].value = p;
128   _copy_str(&p, src[i].value);
129  }
130 }
131
132 *copy = c;
133 return ret;
134}
135
136static struct roar_dl_librarypara * _copy_para(struct roar_dl_librarypara * para, struct roar_plugincontainer * cont) {
137 struct roar_dl_librarypara * ret = NULL;
138 int err;
139
140 if ( para == NULL || cont == NULL ) {
141  roar_err_set(ROAR_ERROR_FAULT);
142  return NULL;
143 }
144
145 ret = roar_dl_para_new(NULL, para->binargv, para->appname, para->abiversion);
146
147 if ( para->argc && para->argv != NULL ) {
148  ret->argc       = para->argc;
149  ret->args_store = _copy_kv(&(ret->argv), para->argv, para->argc);
150  if ( ret->args_store == NULL ) {
151   err = roar_error;
152   roar_dl_para_unref(ret);
153   roar_error = err;
154   return NULL;
155  }
156 }
157
158 ret->notifycore      = para->notifycore;
159 ret->container       = cont;
160 ret->loader          = _loader;
161 ret->loader_userdata = NULL;
162
163 return ret;
164}
165
166static struct roar_plugincontainer * _new_init(void) {
167 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
168 if ( ret == NULL )
169  return NULL;
170
171 memset(ret, 0, sizeof(struct roar_plugincontainer));
172
173 ret->refc = 1;
174
175 return ret;
176}
177
178struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
179 struct roar_plugincontainer * ret = _new_init();
180 int err;
181
182 if ( ret == NULL )
183  return NULL;
184
185 if ( default_para != NULL ) {
186  ret->default_para = _copy_para(default_para, ret);
187  if ( ret->default_para == NULL ) {
188   err = roar_error;
189   roar_plugincontainer_unref(ret);
190   roar_err_set(err);
191   return NULL;
192  }
193 }
194
195 return ret;
196}
197
198struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
199 struct roar_plugincontainer * ret;
200 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
201 int err;
202
203 ret = roar_plugincontainer_new(para);
204 err = roar_error;
205
206 roar_dl_para_unref(para);
207
208 roar_err_set(err);
209 return ret;
210}
211
212int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
213 if ( cont == NULL ) {
214  ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont);
215  roar_err_set(ROAR_ERROR_FAULT);
216  return -1;
217 }
218
219 cont->refc++;
220
221 ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont);
222 return 0;
223}
224
225int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
226 size_t i;
227
228 if ( cont == NULL ) {
229  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont);
230  roar_err_set(ROAR_ERROR_FAULT);
231  return -1;
232 }
233
234 cont->refc--;
235
236 if ( cont->refc ) {
237  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
238  return 0;
239 }
240
241 if ( cont->callbacks != NULL && cont->callbacks->prefree != NULL )
242  cont->callbacks->prefree(cont, &(cont->userdata));
243
244 while (cont->numhandles) {
245  for (i = 0; i < MAX_PLUGINS; i++) {
246   if ( cont->handle[i] == NULL )
247    continue;
248
249   // skip plugins in use by others. We will unload them in a later loop.
250   if ( cont->deprefc[i] )
251    continue;
252
253   _unload(cont, i);
254  }
255 }
256
257 // try to free user data...
258 if ( cont->userdata != NULL ) {
259  if ( cont->callbacks != NULL && cont->callbacks->freeuserdata != NULL ) {
260   cont->callbacks->freeuserdata(cont, &(cont->userdata));
261  }
262 }
263
264 // if still not freed by caller, free it using memmgr.
265 if ( cont->userdata != NULL )
266  roar_mm_free(cont->userdata);
267
268 if ( cont->default_para != NULL )
269  roar_dl_para_unref(cont->default_para);
270
271 roar_mm_free(cont);
272
273 ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
274 return 0;
275}
276
277int roar_plugincontainer_set_callbacks(struct roar_plugincontainer * cont,
278                                       const struct roar_plugincontainer_callbacks * callbacks) {
279 if ( cont == NULL ) {
280  roar_err_set(ROAR_ERROR_FAULT);
281  return -1;
282 }
283
284 cont->callbacks = callbacks;
285
286 return 0;
287}
288
289int roar_plugincontainer_set_userdata(struct roar_plugincontainer * cont, void * userdata) {
290 if ( cont == NULL ) {
291  roar_err_set(ROAR_ERROR_FAULT);
292  return -1;
293 }
294
295 cont->userdata = userdata;
296
297 return 0;
298}
299
300void * roar_plugincontainer_get_userdata(struct roar_plugincontainer * cont) {
301 if ( cont == NULL ) {
302  roar_err_set(ROAR_ERROR_FAULT);
303  return NULL;
304 }
305
306 if ( cont->userdata == NULL ) {
307  roar_err_set(ROAR_ERROR_NOENT);
308  return NULL;
309 }
310
311 return cont->userdata;
312}
313
314struct roar_plugincontainer_plugininfo roar_plugincontainer_get_info_by_name (struct roar_plugincontainer * cont,
315                                                                              const char * name) {
316 struct roar_plugincontainer_plugininfo ret;
317 const struct roar_dl_libraryname * libname;
318 size_t i;
319
320 memset(&ret, 0, sizeof(ret));
321
322 if ( cont == NULL || name == NULL ) {
323  roar_err_set(ROAR_ERROR_FAULT);
324  return ret;
325 }
326
327 for (i = 0; i < MAX_PLUGINS; i++) {
328  if ( cont->handle[i] == NULL )
329   continue;
330  libname = roar_dl_getlibname(cont->handle[i]);
331  if ( libname == NULL )
332   continue;
333
334  if ( !strcmp(libname->name, name) ) {
335   if ( roar_dl_ref(cont->handle[i]) == -1 )
336    return ret;
337
338   ret.libname  = libname->name;
339   ret.handle   = cont->handle[i];
340   ret.rdepends = cont->deprefc[i];
341   ret.context  = &(cont->context[i]);
342   return ret;
343  }
344 }
345
346 roar_err_set(ROAR_ERROR_NOENT);
347 return ret;
348}
349
350struct roar_dl_lhandle * roar_plugincontainer_get_lhandle_by_name (struct roar_plugincontainer * cont,
351                                                                   const char * name) {
352 struct roar_plugincontainer_plugininfo info = roar_plugincontainer_get_info_by_name(cont, name);
353
354 if ( info.libname == NULL )
355  return NULL;
356
357 return info.handle;
358}
359
360// plugin loading and unloading:
361int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
362 struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para);
363
364 if ( ret == NULL )
365  return -1;
366
367 roar_dl_unref(ret);
368
369 return 0;
370}
371
372struct roar_dl_lhandle * roar_plugincontainer_load_lhandle    (struct roar_plugincontainer * cont,
373                                                               const char * name,
374                                                               int flags,
375                                                               int ra_init,
376                                                               struct roar_dl_librarypara * para) {
377 ssize_t idx = -1;
378 size_t i;
379 int err;
380
381 if ( cont == NULL || name == NULL ) {
382  roar_err_set(ROAR_ERROR_FAULT);
383  return NULL;
384 }
385
386 if ( para == NULL ) {
387  para = cont->default_para;
388  roar_dl_para_ref(para);
389 } else {
390  para = _copy_para(para, cont);
391 }
392
393
394 // search for a free index.
395 if ( cont->numhandles == MAX_PLUGINS ) {
396  // return early if we are full.
397  roar_dl_para_unref(para);
398  roar_err_set(ROAR_ERROR_NOSPC);
399  return NULL;
400 }
401 for (i = 0; i < MAX_PLUGINS; i++) {
402  if ( cont->handle[i] == NULL ) {
403   idx = i;
404   break;
405  }
406 }
407
408 if ( idx == -1 ) {
409  roar_dl_para_unref(para);
410  roar_err_set(ROAR_ERROR_NOSPC);
411  return NULL;
412 }
413
414 cont->context[idx] = NULL; // clear context.
415 cont->deprefc[idx] = 0;
416
417 if ( cont->callbacks != NULL && cont->callbacks->preload != NULL )
418  cont->callbacks->preload(cont, &(cont->context[idx]), name, flags, para);
419
420 cont->handle[idx] = roar_dl_open(name, flags, 0, para);
421 if ( cont->handle[idx] == NULL ) {
422  err = roar_error;
423  roar_dl_para_unref(para);
424  roar_error = err;
425  return NULL;
426 }
427
428 cont->numhandles++;
429
430 if ( cont->callbacks != NULL && cont->callbacks->postload != NULL )
431  cont->callbacks->postload(cont, &(cont->context[idx]), cont->handle[idx], name, flags, para);
432
433 if ( ra_init ) {
434  if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL )
435   cont->callbacks->prera_init(cont, &(cont->context[idx]), cont->handle[idx], para);
436  if ( roar_dl_ra_init(cont->handle[idx], NULL, para) == -1 ) {
437   err = roar_error;
438
439   if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
440    cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para);
441
442   _unload(cont, idx);
443
444   roar_dl_para_unref(para);
445   cont->handle[idx] = NULL;
446   roar_error = err;
447   return NULL;
448  }
449
450  if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
451   cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para);
452 }
453
454 roar_dl_para_unref(para);
455 roar_dl_ref(cont->handle[idx]);
456 return cont->handle[idx];
457}
458
459int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
460 struct roar_dl_lhandle * lhandle;
461
462 if ( cont == NULL || name == NULL ) {
463  roar_err_set(ROAR_ERROR_FAULT);
464  return -1;
465 }
466
467 lhandle = roar_plugincontainer_get_lhandle_by_name(cont, name);
468 if ( lhandle == NULL )
469  return -1;
470
471 roar_dl_unref(lhandle); // we can do this early here as the handle stay valid in the container's array.
472
473 return roar_plugincontainer_unload_lhandle(cont, lhandle);
474}
475
476int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
477 size_t i;
478
479 if ( cont == NULL ) {
480  roar_err_set(ROAR_ERROR_FAULT);
481  return -1;
482 }
483
484 if ( lhandle == NULL ) {
485  roar_err_set(ROAR_ERROR_INVAL);
486  return -1;
487 }
488
489 for (i = 0; i < MAX_PLUGINS; i++) {
490  if ( cont->handle[i] == lhandle ) {
491   if ( cont->deprefc[i] ) {
492    // still in use.
493    roar_err_set(ROAR_ERROR_BUSY);
494    return -1;
495   }
496   _unload(cont, i);
497   return 0;
498  }
499 }
500
501 roar_err_set(ROAR_ERROR_NOENT);
502 return -1;
503}
504
505int                      roar_plugincontainer_ra_init         (struct roar_plugincontainer * cont) {
506 size_t i;
507
508 if ( cont == NULL ) {
509  roar_err_set(ROAR_ERROR_FAULT);
510  return -1;
511 }
512
513 for (i = 0; i < MAX_PLUGINS; i++) {
514  if ( cont->handle[i] == NULL )
515   continue;
516
517  if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL )
518   cont->callbacks->prera_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para);
519
520  roar_dl_ra_init(cont->handle[i], NULL, cont->default_para);
521
522  if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
523   cont->callbacks->postra_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para);
524 }
525
526 return 0;
527}
528
529// appsched:
530int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
531 size_t i;
532 int ret = -1;
533 int rv;
534
535 if ( cont == NULL ) {
536  roar_err_set(ROAR_ERROR_FAULT);
537  return -1;
538 }
539
540 if ( cont->numhandles == 1 ) {
541  for (i = 0; i < MAX_PLUGINS; i++) {
542   if ( cont->handle[i] == NULL )
543    continue;
544   return roar_dl_appsched_trigger(cont->handle[i], trigger);
545  }
546  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
547  roar_err_set(ROAR_ERROR_FAULT);
548  return -1;
549 }
550
551 for (i = 0; i < MAX_PLUGINS; i++) {
552  if ( cont->handle[i] == NULL )
553   continue;
554
555  rv = roar_dl_appsched_trigger(cont->handle[i], trigger);
556  if ( rv == 0 )
557   ret = 0;
558 }
559
560 return ret;
561}
562
563//ll
Note: See TracBrowser for help on using the repository browser.