diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-20 22:20:54 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-20 22:20:54 +0200 |
commit | 2c6ce788d039ac5aed91955b0be5640bf1318ef5 (patch) | |
tree | 6e78dbb9113d67b3a210a5d1c9630229d4c8ab58 | |
parent | 37daee3aaad91536fc57e01e50f44319ee494ee3 (diff) |
treewide: rework extended type handling
Register prototype object directly together with the type instead of
setting it manually whenever an extended type value is instantiated.
This also allows freeing the various prototype objects in dlopened
modules.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | ast.c | 10 | ||||
-rw-r--r-- | ast.h | 5 | ||||
-rw-r--r-- | lib/fs.c | 20 | ||||
-rw-r--r-- | lib/ubus.c | 10 | ||||
-rw-r--r-- | lib/uci.c | 10 | ||||
-rw-r--r-- | module.h | 4 |
6 files changed, 31 insertions, 28 deletions
@@ -291,6 +291,9 @@ ut_free(struct ut_state *s) ut_reset(s); } + while (ut_ext_types_count > 0) + json_object_put(ut_ext_types[--ut_ext_types_count].proto); + free(ut_ext_types); free(s); } @@ -343,7 +346,7 @@ out: } bool -ut_register_extended_type(const char *name, void (*freefn)(void *)) +ut_register_extended_type(const char *name, struct json_object *proto, void (*freefn)(void *)) { struct ut_extended_type *tmp; @@ -355,6 +358,7 @@ ut_register_extended_type(const char *name, void (*freefn)(void *)) ut_ext_types = tmp; ut_ext_types[ut_ext_types_count].name = name; ut_ext_types[ut_ext_types_count].free = freefn; + ut_ext_types[ut_ext_types_count].proto = proto; ut_ext_types_count++; return true; @@ -393,7 +397,7 @@ ut_extended_type_free(struct json_object *v, void *ud) } struct json_object * -ut_set_extended_type(struct json_object *v, struct json_object *proto, const char *name, void *data) +ut_set_extended_type(struct json_object *v, const char *name, void *data) { struct ut_extended_type *et = NULL; struct ut_op *op; @@ -416,7 +420,7 @@ ut_set_extended_type(struct json_object *v, struct json_object *proto, const cha op->val = v; op->type = T_RESSOURCE; - op->tag.proto = json_object_get(proto); + op->tag.proto = json_object_get(et->proto); op->tag.type = n + 1; op->tag.data = data; @@ -101,6 +101,7 @@ struct ut_state { struct ut_extended_type { const char *name; + struct json_object *proto; void (*free)(void *); }; @@ -128,8 +129,8 @@ struct json_object *ut_new_object(struct json_object *proto); struct json_object *ut_new_double(double v); struct json_object *ut_new_null(void); -bool ut_register_extended_type(const char *name, void (*freefn)(void *)); -struct json_object *ut_set_extended_type(struct json_object *v, struct json_object *proto, const char *name, void *data); +bool ut_register_extended_type(const char *name, struct json_object *proto, void (*freefn)(void *)); +struct json_object *ut_set_extended_type(struct json_object *v, const char *name, void *data); void **ut_get_extended_type(struct json_object *val, const char *name); void *ParseAlloc(void *(*mfunc)(size_t)); @@ -29,10 +29,6 @@ static const struct ut_ops *ops; -static struct json_object *proc_proto; -static struct json_object *file_proto; -static struct json_object *dir_proto; - static int last_error = 0; static struct json_object * @@ -221,7 +217,7 @@ ut_fs_popen(struct ut_state *s, uint32_t off, struct json_object *args) err_return(ENOMEM); } - return ops->set_type(fo, proc_proto, "fs.proc", fp); + return ops->set_type(fo, "fs.proc", fp); } @@ -328,7 +324,7 @@ ut_fs_open(struct ut_state *s, uint32_t off, struct json_object *args) err_return(ENOMEM); } - return ops->set_type(fo, file_proto, "fs.file", fp); + return ops->set_type(fo, "fs.file", fp); } @@ -423,7 +419,7 @@ ut_fs_opendir(struct ut_state *s, uint32_t off, struct json_object *args) err_return(ENOMEM); } - return ops->set_type(diro, dir_proto, "fs.dir", dp); + return ops->set_type(diro, "fs.dir", dp); } static struct json_object * @@ -723,11 +719,9 @@ static void close_dir(void *ud) { void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_object *scope) { - ops = ut; - ops->register_type("fs.proc", close_proc); - ops->register_type("fs.file", close_file); - ops->register_type("fs.dir", close_dir); + struct json_object *proc_proto, *file_proto, *dir_proto; + ops = ut; proc_proto = ops->new_object(NULL); file_proto = ops->new_object(NULL); dir_proto = ops->new_object(NULL); @@ -736,4 +730,8 @@ void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_obj register_functions(ops, proc_fns, proc_proto); register_functions(ops, file_fns, file_proto); register_functions(ops, dir_fns, dir_proto); + + ops->register_type("fs.proc", proc_proto, close_proc); + ops->register_type("fs.file", file_proto, close_file); + ops->register_type("fs.dir", dir_proto, close_dir); } @@ -25,8 +25,6 @@ static const struct ut_ops *ops; -static struct json_object *conn_proto; - static enum ubus_msg_status last_error = 0; struct ubus_connection { @@ -171,7 +169,7 @@ ut_ubus_connect(struct ut_state *s, uint32_t off, struct json_object *args) ubus_add_uloop(c->ctx); - return ops->set_type(co, conn_proto, "ubus.connection", c); + return ops->set_type(co, "ubus.connection", c); } static void @@ -317,11 +315,13 @@ static void close_connection(void *ud) { void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_object *scope) { - ops = ut; - ops->register_type("ubus.connection", close_connection); + struct json_object *conn_proto; + ops = ut; conn_proto = ops->new_object(NULL); register_functions(ops, global_fns, scope); register_functions(ops, conn_fns, conn_proto); + + ops->register_type("ubus.connection", conn_proto, close_connection); } @@ -23,8 +23,6 @@ static const struct ut_ops *ops; -static struct json_object *uci_proto; - static int last_error = 0; enum pkg_cmd { @@ -105,7 +103,7 @@ ut_uci_cursor(struct ut_state *s, uint32_t off, struct json_object *args) err_return(UCI_ERR_MEM); } - return ops->set_type(co, uci_proto, "uci.cursor", c); + return ops->set_type(co, "uci.cursor", c); } @@ -1015,11 +1013,13 @@ static void close_uci(void *ud) { void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_object *scope) { - ops = ut; - ops->register_type("uci.cursor", close_uci); + struct json_object *uci_proto; + ops = ut; uci_proto = ops->new_object(NULL); register_functions(ops, global_fns, scope); register_functions(ops, cursor_fns, uci_proto); + + ops->register_type("uci.cursor", uci_proto, close_uci); } @@ -22,8 +22,8 @@ struct ut_ops { bool (*register_function)(struct json_object *, const char *, ut_c_fn *); - bool (*register_type)(const char *, void (*)(void *)); - struct json_object *(*set_type)(struct json_object *, struct json_object *, const char *, void *); + bool (*register_type)(const char *, struct json_object *, void (*)(void *)); + struct json_object *(*set_type)(struct json_object *, const char *, void *); void **(*get_type)(struct json_object *, const char *); struct json_object *(*new_object)(struct json_object *); struct json_object *(*new_double)(double); |