source: roaraudio/libroar/plugincontainer.c @ 5320:d583f3008d43

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

support usage counter and started to implement roar_plugincontainer_load()

File size: 5.7 KB
Line 
1//plugincontainer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
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 struct roar_dl_librarypara * default_para;
43 struct roar_dl_lhandle * handle[MAX_PLUGINS];
44 size_t deprefc[MAX_PLUGINS];
45 size_t numhandles;
46 // TODO: add a depref counter so we do not unload libs we depend on.
47};
48
49static struct roar_plugincontainer * _new_init(void) {
50 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
51 if ( ret == NULL )
52  return NULL;
53
54 memset(ret, 0, sizeof(struct roar_plugincontainer));
55
56 ret->refc = 1;
57
58 return ret;
59}
60
61struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
62 struct roar_plugincontainer * ret = _new_init();
63 int err;
64
65 if ( ret == NULL )
66  return NULL;
67
68 if ( default_para != NULL ) {
69  if ( roar_dl_para_ref(default_para) == -1 ) {
70   err = roar_error;
71   roar_plugincontainer_unref(ret);
72   roar_err_set(err);
73   return NULL;
74  }
75  ret->default_para = default_para;
76 }
77
78 return ret;
79}
80
81struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
82 struct roar_plugincontainer * ret;
83 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
84 int err;
85
86 ret = roar_plugincontainer_new(para);
87 err = roar_error;
88
89 roar_dl_para_unref(para);
90
91 roar_err_set(err);
92 return ret;
93}
94
95int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
96 if ( cont == NULL ) {
97  roar_err_set(ROAR_ERROR_FAULT);
98  return -1;
99 }
100
101 cont->refc++;
102
103 return 0;
104}
105
106int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
107 size_t i;
108
109 if ( cont == NULL ) {
110  roar_err_set(ROAR_ERROR_FAULT);
111  return -1;
112 }
113
114 cont->refc--;
115
116 if ( cont->refc )
117  return 0;
118
119 while (cont->numhandles) {
120  for (i = 0; i < MAX_PLUGINS; i++) {
121   if ( cont->handle[i] == NULL )
122    continue;
123
124   // skip plugins in use by others. We will unload them in a later loop.
125   if ( cont->deprefc[i] )
126    continue;
127
128   roar_dl_close(cont->handle[i]);
129  }
130 }
131
132 if ( cont->default_para != NULL )
133  roar_dl_para_unref(cont->default_para);
134
135 roar_mm_free(cont);
136
137 return 0;
138}
139
140// plugin loading and unloading:
141int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
142 ssize_t idx = -1;
143 size_t i;
144
145 if ( cont == NULL || name == NULL ) {
146  roar_err_set(ROAR_ERROR_FAULT);
147  return -1;
148 }
149
150 if ( para == NULL )
151  para = cont->default_para;
152 // we do not need to _ref(para) here. This will be done by the roardl open function.
153
154
155 // search for a free index.
156 if ( cont->numhandles == MAX_PLUGINS ) {
157  // return early if we are full.
158  roar_err_set(ROAR_ERROR_NOSPC);
159  return -1;
160 }
161 for (i = 0; i < MAX_PLUGINS; i++) {
162  if ( cont->handle[i] == NULL ) {
163   idx = i;
164   break;
165  }
166 }
167
168 if ( idx == -1 ) {
169  roar_err_set(ROAR_ERROR_NOSPC);
170  return -1;
171 }
172
173 if ( strstr(name, "/") != NULL ) {
174  cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para);
175  if ( cont->handle[idx] == NULL )
176   return -1;
177
178  cont->deprefc[idx] = 0;
179  cont->numhandles++;
180
181  return 0;
182 } else {
183  roar_err_set(ROAR_ERROR_NOSYS);
184  return -1;
185 }
186
187 roar_err_set(ROAR_ERROR_NOSYS);
188 return -1;
189}
190
191int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
192 // get b name, then call roar_plugincontainer_unload_lhandle().
193 roar_err_set(ROAR_ERROR_NOSYS);
194 return -1;
195}
196
197int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
198 size_t i;
199
200 if ( cont == NULL ) {
201  roar_err_set(ROAR_ERROR_FAULT);
202  return -1;
203 }
204
205 if ( lhandle == NULL ) {
206  roar_err_set(ROAR_ERROR_INVAL);
207  return -1;
208 }
209
210 for (i = 0; i < MAX_PLUGINS; i++) {
211  if ( cont->handle[i] == lhandle ) {
212   if ( cont->deprefc[i] ) {
213    // still in use.
214    roar_err_set(ROAR_ERROR_BUSY);
215    return -1;
216   }
217   roar_dl_close(cont->handle[i]);
218   cont->handle[i] = NULL;
219   cont->numhandles--;
220   return 0;
221  }
222 }
223
224 roar_err_set(ROAR_ERROR_NOENT);
225 return -1;
226}
227
228// appsched:
229int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
230 size_t i;
231
232 if ( cont == NULL ) {
233  roar_err_set(ROAR_ERROR_FAULT);
234  return -1;
235 }
236
237 for (i = 0; i < MAX_PLUGINS; i++) {
238  if ( cont->handle[i] == NULL )
239   continue;
240
241  roar_dl_appsched_trigger(cont->handle[i], trigger);
242 }
243
244 return 0;
245}
246
247//ll
Note: See TracBrowser for help on using the repository browser.