[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [33739] branches/particles-2010/source/ blender/nodes/intern: Fixed memory leaks.

Lukas Toenne lukas.toenne at googlemail.com
Fri Dec 17 15:44:39 CET 2010


Revision: 33739
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=33739
Author:   lukastoenne
Date:     2010-12-17 15:44:38 +0100 (Fri, 17 Dec 2010)

Log Message:
-----------
Fixed memory leaks.
Socket data buffers are now only reallocated when the context size increases. This is not ideal, but it can at least help to avoid frequent allocations in many common cases. Still doesn't help much when doing iterations with steadily increasing buffer sizes though.
Kernel allocation needs a little redesign to clarify ownership.

Modified Paths:
--------------
    branches/particles-2010/source/blender/nodes/intern/SIM_intern.c
    branches/particles-2010/source/blender/nodes/intern/SIM_intern.h
    branches/particles-2010/source/blender/nodes/intern/SIM_nodetree.c
    branches/particles-2010/source/blender/nodes/intern/SIM_util.c

Modified: branches/particles-2010/source/blender/nodes/intern/SIM_intern.c
===================================================================
--- branches/particles-2010/source/blender/nodes/intern/SIM_intern.c	2010-12-17 14:20:20 UTC (rev 33738)
+++ branches/particles-2010/source/blender/nodes/intern/SIM_intern.c	2010-12-17 14:44:38 UTC (rev 33739)
@@ -62,8 +62,18 @@
 	}
 }
 
-void sim_socket_buffer_init(SimExecData *execdata, SimSocketStack *sock)
+void sim_socket_buffer_init(SimExecData *execdata, SimSocketStack *sock, SimDataContext *ctx)
 {
+	/* no need to allocate new buffer for smaller contexts */
+	if (sock->data && ctx->size <= sock->context.size)
+		return;
+	
+	/* free old buffer */
+	sim_socket_buffer_free(execdata, sock);
+	
+	/* set new context */
+	sock->context = *ctx;
+	
 #ifdef WITH_OPENCL
 	if (BKE_opencl_is_active()) {
 		cl_int res;

Modified: branches/particles-2010/source/blender/nodes/intern/SIM_intern.h
===================================================================
--- branches/particles-2010/source/blender/nodes/intern/SIM_intern.h	2010-12-17 14:20:20 UTC (rev 33738)
+++ branches/particles-2010/source/blender/nodes/intern/SIM_intern.h	2010-12-17 14:44:38 UTC (rev 33739)
@@ -67,7 +67,7 @@
 } SimWorkerThread;
 
 int sim_socket_buffer_valid(struct SimExecData *execdata, struct SimSocketStack *sock);
-void sim_socket_buffer_init(struct SimExecData *execdata, struct SimSocketStack *sock);
+void sim_socket_buffer_init(struct SimExecData *execdata, struct SimSocketStack *sock, struct SimDataContext *ctx);
 void sim_socket_buffer_free(struct SimExecData *execdata, struct SimSocketStack *sock);
 
 #endif

Modified: branches/particles-2010/source/blender/nodes/intern/SIM_nodetree.c
===================================================================
--- branches/particles-2010/source/blender/nodes/intern/SIM_nodetree.c	2010-12-17 14:20:20 UTC (rev 33738)
+++ branches/particles-2010/source/blender/nodes/intern/SIM_nodetree.c	2010-12-17 14:44:38 UTC (rev 33739)
@@ -1270,7 +1270,8 @@
 {
 	SimExecData *execdata = (SimExecData*)vexecdata;
 	SimNodeStack *node;
-	int n;
+	SimSocketStack *sock;
+	int n, i;
 	
 	#ifdef WITH_OPENCL
 	if (BKE_opencl_is_active()) {
@@ -1283,7 +1284,13 @@
 		pthread_cond_broadcast(&execdata->workercond);
 	}
 	
+	/* free data buffers for input constants */
 	free_constant_inputs(execdata);
+	/* free remaining output buffers */
+	for (n=0, node=execdata->nodestack; n < execdata->totnode; ++n, ++node) {
+		for (i=0, sock=node->outstack; i < node->totout; ++i, ++sock)
+			sim_socket_buffer_free(execdata, sock);
+	}
 	
 #ifdef WITH_OPENCL
 	if (BKE_opencl_is_active()) {

Modified: branches/particles-2010/source/blender/nodes/intern/SIM_util.c
===================================================================
--- branches/particles-2010/source/blender/nodes/intern/SIM_util.c	2010-12-17 14:20:20 UTC (rev 33738)
+++ branches/particles-2010/source/blender/nodes/intern/SIM_util.c	2010-12-17 14:44:38 UTC (rev 33739)
@@ -739,11 +739,13 @@
 	}
 	
 	/* set the socket context */
-	if (ctx)
-		sock->context = *ctx;
-	else
-		sim_context_create(NULL, NULL, NULL, 0, &sock->context);
-	sim_socket_buffer_init(execdata, sock);
+	if (ctx) 
+		sim_socket_buffer_init(execdata, sock, ctx);
+	else {
+		SimDataContext defctx;
+		sim_context_create(NULL, NULL, NULL, 0, &defctx);
+		sim_socket_buffer_init(execdata, sock, &defctx);
+	}
 	
 	iter->sock = sock;
 	iter->stride = sim_sockettype_size(sock->type);
@@ -1868,6 +1870,9 @@
 			}
 		}
 	}
+	
+	/* XXX this needs cleanup (who's the owner of the kernel?) */
+	MEM_freeN(kernel);
 }
 
 void sim_kernel_set_output(struct SimExecData *execdata, struct SimKernel *kernel, struct SimSocketStack *sock, struct SimDataContext *ctx)
@@ -1876,11 +1881,13 @@
 		return;
 	
 	/* set the socket context */
-	if (ctx)
-		sock->context = *ctx;
-	else
-		sim_node_get_input_context(kernel->node, &sock->context);
-	sim_socket_buffer_init(execdata, sock);
+	if (ctx) 
+		sim_socket_buffer_init(execdata, sock, ctx);
+	else {
+		SimDataContext defctx;
+		sim_node_get_input_context(kernel->node, &defctx);
+		sim_socket_buffer_init(execdata, sock, &defctx);
+	}
 	
 	if (kernel->totworkitems < sock->context.size)
 		kernel->totworkitems = sock->context.size;
@@ -1895,11 +1902,13 @@
 		if (execdata->error)
 			return;
 		
-		if (ctx)
-			out->context = *ctx;
-		else
-			sim_node_get_input_context(kernel->node, &out->context);
-		sim_socket_buffer_init(execdata, out);
+		if (ctx) 
+			sim_socket_buffer_init(execdata, out, ctx);
+		else {
+			SimDataContext defctx;
+			sim_node_get_input_context(kernel->node, &defctx);
+			sim_socket_buffer_init(execdata, out, &defctx);
+		}
 		
 		if (kernel->totworkitems < out->context.size)
 			kernel->totworkitems = out->context.size;





More information about the Bf-blender-cvs mailing list