Skip to content

Commit 3e87530

Browse files
committed
PLUGINS: mechanism for cleaning up resources when out of scope
1 parent c046bdf commit 3e87530

File tree

10 files changed

+45
-3
lines changed

10 files changed

+45
-3
lines changed

src/common/hashmap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ void hashmap_create(var_p_t map, int size) {
126126
map->type = V_MAP;
127127
map->v.m.count = 0;
128128
map->v.m.id = -1;
129+
map->v.m.lib_id = -1;
130+
map->v.m.cls_id = -1;
129131
if (size == 0) {
130132
map->v.m.size = MAP_SIZE;
131133
} else {

src/common/plugins.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef int (*sblib_exec_fn)(int, int, slib_par_t *, var_t *);
3939
typedef int (*sblib_getname_fn) (int, char *);
4040
typedef int (*sblib_count_fn) (void);
4141
typedef int (*sblib_init_fn) (const char *);
42+
typedef int (*sblib_free_fn)(int, int);
4243
typedef void (*sblib_close_fn) (void);
4344

4445
typedef struct {
@@ -47,6 +48,7 @@ typedef struct {
4748
void *_handle;
4849
sblib_exec_fn _sblib_proc_exec;
4950
sblib_exec_fn _sblib_func_exec;
51+
sblib_free_fn _sblib_free;
5052
ext_func_node_t *_func_list;
5153
ext_proc_node_t *_proc_list;
5254
uint32_t _id;
@@ -304,6 +306,7 @@ static void slib_import_routines(slib_t *lib, int comp) {
304306

305307
lib->_sblib_func_exec = slib_getoptptr(lib, "sblib_func_exec");
306308
lib->_sblib_proc_exec = slib_getoptptr(lib, "sblib_proc_exec");
309+
lib->_sblib_free = slib_getoptptr(lib, "sblib_free");
307310
sblib_count_fn fcount = slib_getoptptr(lib, "sblib_proc_count");
308311
sblib_getname_fn fgetname = slib_getoptptr(lib, "sblib_proc_getname");
309312

@@ -471,6 +474,10 @@ static int slib_exec(slib_t *lib, var_t *ret, int index, int proc) {
471474
free(ptable);
472475
}
473476

477+
if (success && v_is_type(ret, V_MAP)) {
478+
ret->v.m.lib_id = lib->_id;
479+
}
480+
474481
return success;
475482
}
476483

@@ -598,6 +605,13 @@ int plugin_funcexec(int lib_id, int index, var_t *ret) {
598605
return result;
599606
}
600607

608+
void plugin_free(int lib_id, int cls_id, int id) {
609+
slib_t *lib = get_lib(lib_id);
610+
if (lib && lib->_sblib_free) {
611+
lib->_sblib_free(cls_id, id);
612+
}
613+
}
614+
601615
void plugin_close() {
602616
for (int i = 0; i < MAX_SLIBS; i++) {
603617
if (plugins[i]) {
@@ -626,5 +640,6 @@ int plugin_get_kid(int lib_id, const char *keyword) { return -1; }
626640
void *plugin_get_func(const char *name) { return 0; }
627641
int plugin_procexec(int lib_id, int index) { return -1; }
628642
int plugin_funcexec(int lib_id, int index, var_t *ret) { return -1; }
643+
void plugin_free(int lib_id, int cls_id, int id) {}
629644
void plugin_close() {}
630645
#endif

src/common/plugins.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ int plugin_procexec(int lib_id, int index);
5555
//
5656
int plugin_funcexec(int lib_id, int index, var_t *ret);
5757

58+
//
59+
// cleanup any resources held against the map data
60+
//
61+
void plugin_free(int lib_id, int cls_id, int id);
62+
5863
//
5964
// closes the plugin system
6065
//

src/common/var.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ void v_move(var_t *dest, const var_t *src) {
565565
dest->v.m.count = src->v.m.count;
566566
dest->v.m.size = src->v.m.size;
567567
dest->v.m.id = src->v.m.id;
568+
dest->v.m.lib_id = src->v.m.lib_id;
569+
dest->v.m.cls_id = src->v.m.cls_id;
568570
break;
569571
case V_REF:
570572
dest->v.ref = src->v.ref;

src/common/var_map.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "common/sys.h"
1212
#include "common/pproc.h"
1313
#include "common/hashmap.h"
14+
#include "common/plugins.h"
1415
#include "include/var_map.h"
1516

1617
#define BUFFER_GROW_SIZE 64
@@ -170,6 +171,9 @@ var_p_t map_elem_key(const var_p_t var_p, int index) {
170171
*/
171172
void map_free(var_p_t var_p) {
172173
if (var_p->type == V_MAP) {
174+
if (var_p->v.m.lib_id != -1 && var_p->v.m.cls_id != -1 && var_p->v.m.id != -1) {
175+
plugin_free(var_p->v.m.lib_id, var_p->v.m.cls_id, var_p->v.m.id);
176+
}
173177
hashmap_destroy(var_p);
174178
v_init(var_p);
175179
}
@@ -291,6 +295,8 @@ void map_set(var_p_t dest, const var_p_t src) {
291295
hashmap_foreach(src, map_set_cb, &cb);
292296
dest->v.m.count = src->v.m.count;
293297
dest->v.m.id = src->v.m.id;
298+
dest->v.m.lib_id = src->v.m.lib_id;
299+
dest->v.m.cls_id = src->v.m.cls_id;
294300
}
295301
}
296302

src/include/module.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ int sblib_func_getname(int index, char *func_name);
116116
*/
117117
int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retval);
118118

119+
/**
120+
* @ingroup modlib
121+
*
122+
* executes a function
123+
*
124+
* @param cls_id the variable class identifier
125+
* @param id the variable instance identifier
126+
*/
127+
void sblib_free(int cls_id, int id);
128+
119129
/**
120130
* @ingroup modlib
121131
*

src/include/var.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ typedef struct var_s {
7272
uint32_t count;
7373
uint32_t size;
7474
uint32_t id;
75+
uint32_t lib_id;
76+
uint32_t cls_id;
7577
} m;
7678

7779
// reference variable

src/lib/miniaudio

0 commit comments

Comments
 (0)