From c60be37f3ebf20ab9b4563d03c0acb97ecf047cc Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 28 Feb 2020 13:28:16 +0100 Subject: Nodes: Use destructor callbacks for bNodeSocketType and bNodeType Reviewers: brecht Differential Revision: https://developer.blender.org/D6963 --- source/blender/blenkernel/BKE_node.h | 6 +++++- source/blender/blenkernel/intern/node.c | 7 ++++--- source/blender/makesrna/intern/rna_nodetree.c | 7 +++++-- source/blender/nodes/intern/node_common.c | 8 ++++---- source/blender/nodes/intern/node_socket.c | 2 ++ 5 files changed, 20 insertions(+), 10 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 712e97a77f0..ee02fabe6a8 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -141,6 +141,9 @@ typedef struct bNodeSocketType { /* for standard socket types in C */ int type, subtype; + + /* Callback to free the socket type. */ + void (*free_self)(struct bNodeSocketType *stype); } bNodeSocketType; typedef void *(*NodeInitExecFunction)(struct bNodeExecContext *context, @@ -167,7 +170,6 @@ typedef int (*NodeGPUExecFunction)(struct GPUMaterial *mat, */ typedef struct bNodeType { void *next, *prev; - short needs_free; /* set for allocated types that need to be freed */ char idname[64]; /* identifier name */ int type; @@ -247,6 +249,8 @@ typedef struct bNodeType { /* Update the internal links list, for muting and disconnect operators. */ void (*update_internal_links)(struct bNodeTree *, struct bNode *node); + void (*free_self)(struct bNodeType *ntype); + /* **** execution callbacks **** */ NodeInitExecFunction initexecfunc; NodeFreeExecFunction freeexecfunc; diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c index 3d413b8fd62..9e52f7ea939 100644 --- a/source/blender/blenkernel/intern/node.c +++ b/source/blender/blenkernel/intern/node.c @@ -412,8 +412,9 @@ static void node_free_type(void *nodetype_v) free_dynamic_typeinfo(nodetype); } - if (nodetype->needs_free) { - MEM_freeN(nodetype); + /* Can be NULL when the type is not dynamically allocated. */ + if (nodetype->free_self) { + nodetype->free_self(nodetype); } } @@ -468,7 +469,7 @@ static void node_free_socket_type(void *socktype_v) * or we'd want to update *all* active Mains, which we cannot do anyway currently. */ update_typeinfo(G_MAIN, NULL, NULL, NULL, socktype, true); - MEM_freeN(socktype); + socktype->free_self(socktype); } void nodeRegisterSocketType(bNodeSocketType *st) diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index c4672ead29d..a0f7f9b585e 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -1652,8 +1652,7 @@ static bNodeType *rna_Node_register_base(Main *bmain, /* create a new node type */ nt = MEM_callocN(sizeof(bNodeType), "node type"); memcpy(nt, &dummynt, sizeof(dummynt)); - /* make sure the node type struct is freed on unregister */ - nt->needs_free = 1; + nt->free_self = (void (*)(bNodeType *))MEM_freeN; nt->ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, nt->idname, basetype); nt->ext.data = data; @@ -2185,6 +2184,8 @@ static StructRNA *rna_NodeSocket_register(Main *UNUSED(bmain), nodeRegisterSocketType(st); } + st->free_self = (void (*)(bNodeSocketType * stype)) MEM_freeN; + /* if RNA type is already registered, unregister first */ if (st->ext_socket.srna) { StructRNA *srna = st->ext_socket.srna; @@ -2499,6 +2500,8 @@ static StructRNA *rna_NodeSocketInterface_register(Main *UNUSED(bmain), nodeRegisterSocketType(st); } + st->free_self = (void (*)(bNodeSocketType * stype)) MEM_freeN; + /* if RNA type is already registered, unregister first */ if (st->ext_interface.srna) { StructRNA *srna = st->ext_interface.srna; diff --git a/source/blender/nodes/intern/node_common.c b/source/blender/nodes/intern/node_common.c index 6b3e378c94e..ba00190b1da 100644 --- a/source/blender/nodes/intern/node_common.c +++ b/source/blender/nodes/intern/node_common.c @@ -208,13 +208,13 @@ void register_node_type_frame(void) { /* frame type is used for all tree types, needs dynamic allocation */ bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type"); + ntype->free_self = (void (*)(bNodeType *))MEM_freeN; node_type_base(ntype, NODE_FRAME, "Frame", NODE_CLASS_LAYOUT, NODE_BACKGROUND); node_type_init(ntype, node_frame_init); node_type_storage(ntype, "NodeFrame", node_free_standard_storage, node_copy_standard_storage); node_type_size(ntype, 150, 100, 0); - ntype->needs_free = 1; nodeRegisterType(ntype); } @@ -253,12 +253,12 @@ void register_node_type_reroute(void) { /* frame type is used for all tree types, needs dynamic allocation */ bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type"); + ntype->free_self = (void (*)(bNodeType *))MEM_freeN; node_type_base(ntype, NODE_REROUTE, "Reroute", NODE_CLASS_LAYOUT, 0); node_type_init(ntype, node_reroute_init); node_type_internal_links(ntype, node_reroute_update_internal_links); - ntype->needs_free = 1; nodeRegisterType(ntype); } @@ -491,13 +491,13 @@ void register_node_type_group_input(void) { /* used for all tree types, needs dynamic allocation */ bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type"); + ntype->free_self = (void (*)(bNodeType *))MEM_freeN; node_type_base(ntype, NODE_GROUP_INPUT, "Group Input", NODE_CLASS_INTERFACE, 0); node_type_size(ntype, 140, 80, 400); node_type_init(ntype, node_group_input_init); node_type_update(ntype, node_group_input_update); - ntype->needs_free = 1; nodeRegisterType(ntype); } @@ -589,12 +589,12 @@ void register_node_type_group_output(void) { /* used for all tree types, needs dynamic allocation */ bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type"); + ntype->free_self = (void (*)(bNodeType *))MEM_freeN; node_type_base(ntype, NODE_GROUP_OUTPUT, "Group Output", NODE_CLASS_INTERFACE, 0); node_type_size(ntype, 140, 80, 400); node_type_init(ntype, node_group_output_init); node_type_update(ntype, node_group_output_update); - ntype->needs_free = 1; nodeRegisterType(ntype); } diff --git a/source/blender/nodes/intern/node_socket.c b/source/blender/nodes/intern/node_socket.c index 13f9039379d..740e45bc5e1 100644 --- a/source/blender/nodes/intern/node_socket.c +++ b/source/blender/nodes/intern/node_socket.c @@ -403,6 +403,7 @@ static bNodeSocketType *make_standard_socket_type(int type, int subtype) StructRNA *srna; stype = MEM_callocN(sizeof(bNodeSocketType), "node socket C type"); + stype->free_self = (void (*)(bNodeSocketType * stype)) MEM_freeN; BLI_strncpy(stype->idname, socket_idname, sizeof(stype->idname)); /* set the RNA type @@ -441,6 +442,7 @@ static bNodeSocketType *make_socket_type_virtual(void) StructRNA *srna; stype = MEM_callocN(sizeof(bNodeSocketType), "node socket C type"); + stype->free_self = (void (*)(bNodeSocketType * stype)) MEM_freeN; BLI_strncpy(stype->idname, socket_idname, sizeof(stype->idname)); /* set the RNA type -- cgit v1.2.3