Nodes: Use destructor callbacks for bNodeSocketType and bNodeType

Reviewers: brecht

Differential Revision: https://developer.blender.org/D6963
This commit is contained in:
Jacques Lucke 2020-02-28 13:28:16 +01:00
parent da1140f75e
commit c60be37f3e
5 changed files with 20 additions and 10 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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;

View File

@ -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);
}

View File

@ -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