Skip to content

Commit 627622d

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

File tree

5 files changed

+11
-2
lines changed

5 files changed

+11
-2
lines changed

src/common/hashmap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ void hashmap_create(var_p_t map, int size) {
128128
map->v.m.id = -1;
129129
map->v.m.lib_id = -1;
130130
map->v.m.cls_id = -1;
131+
map->v.m.ref = -1;
131132
if (size == 0) {
132133
map->v.m.size = MAP_SIZE;
133134
} else {

src/common/plugins.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +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);
42+
typedef int (*sblib_free_fn) (int, int);
4343
typedef void (*sblib_close_fn) (void);
4444

4545
typedef struct {
@@ -476,6 +476,7 @@ static int slib_exec(slib_t *lib, var_t *ret, int index, int proc) {
476476

477477
if (success && v_is_type(ret, V_MAP)) {
478478
ret->v.m.lib_id = lib->_id;
479+
ret->v.m.ref = 1;
479480
}
480481

481482
return success;

src/common/var.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ void v_move(var_t *dest, const var_t *src) {
567567
dest->v.m.id = src->v.m.id;
568568
dest->v.m.lib_id = src->v.m.lib_id;
569569
dest->v.m.cls_id = src->v.m.cls_id;
570+
dest->v.m.ref = src->v.m.ref;
570571
break;
571572
case V_REF:
572573
dest->v.ref = src->v.ref;

src/common/var_map.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ var_p_t map_elem_key(const var_p_t var_p, int index) {
171171
*/
172172
void map_free(var_p_t var_p) {
173173
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) {
174+
if (var_p->v.m.lib_id != -1 &&
175+
var_p->v.m.cls_id != -1 &&
176+
var_p->v.m.id != -1 &&
177+
--var_p->v.m.ref == 0) {
175178
plugin_free(var_p->v.m.lib_id, var_p->v.m.cls_id, var_p->v.m.id);
176179
}
177180
hashmap_destroy(var_p);
@@ -297,6 +300,7 @@ void map_set(var_p_t dest, const var_p_t src) {
297300
dest->v.m.id = src->v.m.id;
298301
dest->v.m.lib_id = src->v.m.lib_id;
299302
dest->v.m.cls_id = src->v.m.cls_id;
303+
dest->v.m.ref = src->v.m.ref + 1;
300304
}
301305
}
302306

src/include/var.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ typedef struct var_s {
6969
struct {
7070
// pointer the map structure
7171
void *map;
72+
7273
uint32_t count;
7374
uint32_t size;
7475
uint32_t id;
7576
uint32_t lib_id;
7677
uint32_t cls_id;
78+
uint32_t ref;
7779
} m;
7880

7981
// reference variable

0 commit comments

Comments
 (0)