[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59817] trunk/blender/source/blender: simplify idprop reallocation with MEM_recallocN

Campbell Barton ideasman42 at gmail.com
Thu Sep 5 06:26:08 CEST 2013


Revision: 59817
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59817
Author:   campbellbarton
Date:     2013-09-05 04:26:08 +0000 (Thu, 05 Sep 2013)
Log Message:
-----------
simplify idprop reallocation with MEM_recallocN

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/idprop.c
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/makesrna/intern/rna_object_force.c

Modified: trunk/blender/source/blender/blenkernel/intern/idprop.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/idprop.c	2013-09-05 04:19:05 UTC (rev 59816)
+++ trunk/blender/source/blender/blenkernel/intern/idprop.c	2013-09-05 04:26:08 UTC (rev 59817)
@@ -133,11 +133,10 @@
 
 void IDP_ResizeIDPArray(IDProperty *prop, int newlen)
 {
-	void *newarr;
-	int newsize = newlen;
+	int newsize;
 
-	/*first check if the array buffer size has room*/
-	/*if newlen is 200 chars less then totallen, reallocate anyway*/
+	/* first check if the array buffer size has room */
+	/* if newlen is 200 items less then totallen, reallocate anyway */
 	if (newlen <= prop->totallen) {
 		if (newlen < prop->len && prop->totallen - newlen < 200) {
 			int i;
@@ -154,6 +153,15 @@
 		}
 	}
 
+	/* free trailing items */
+	if (newlen < prop->len) {
+		/* newlen is smaller */
+		int i;
+		for (i = newlen; i < prop->len; i++) {
+			IDP_FreeProperty(GETPROP(prop, i));
+		}
+	}
+
 	/* - Note: This code comes from python, here's the corresponding comment. - */
 	/* This over-allocates proportional to the list size, making room
 	 * for additional growth.  The over-allocation is mild, but is
@@ -162,25 +170,9 @@
 	 * system realloc().
 	 * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
 	 */
+	newsize = newlen;
 	newsize = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;
-
-	newarr = MEM_callocN(sizeof(IDProperty) * newsize, "idproperty array resized");
-	if (newlen >= prop->len) {
-		/* newlen is bigger */
-		memcpy(newarr, prop->data.pointer, prop->len * sizeof(IDProperty));
-	}
-	else {
-		int i;
-		/* newlen is smaller */
-		for (i = newlen; i < prop->len; i++) {
-			IDP_FreeProperty(GETPROP(prop, i));
-		}
-		memcpy(newarr, prop->data.pointer, newlen * sizeof(IDProperty));
-	}
-
-	if (prop->data.pointer)
-		MEM_freeN(prop->data.pointer);
-	prop->data.pointer = newarr;
+	prop->data.pointer = MEM_recallocN(prop->data.pointer, sizeof(IDProperty) * newsize);
 	prop->len = newlen;
 	prop->totallen = newsize;
 }
@@ -217,11 +209,11 @@
 /*this function works for strings too!*/
 void IDP_ResizeArray(IDProperty *prop, int newlen)
 {
-	void *newarr;
-	int newsize = newlen;
+	int newsize;
+	const bool is_grow = newlen >= prop->len;
 
-	/*first check if the array buffer size has room*/
-	/*if newlen is 200 chars less then totallen, reallocate anyway*/
+	/* first check if the array buffer size has room */
+	/* if newlen is 200 chars less then totallen, reallocate anyway */
 	if (newlen <= prop->totallen && prop->totallen - newlen < 200) {
 		idp_resize_group_array(prop, newlen, prop->data.pointer);
 		prop->len = newlen;
@@ -236,22 +228,17 @@
 	 * system realloc().
 	 * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
 	 */
-	newsize = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;
+	newsize = newlen;
+	newsize = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;\
 
-	newarr = MEM_callocN(idp_size_table[(int)prop->subtype] * newsize, "idproperty array resized");
-	if (newlen >= prop->len) {
-		/* newlen is bigger */
-		memcpy(newarr, prop->data.pointer, prop->len * idp_size_table[(int)prop->subtype]);
-		idp_resize_group_array(prop, newlen, newarr);
-	}
-	else {
-		/* newlen is smaller */
-		idp_resize_group_array(prop, newlen, newarr);
-		memcpy(newarr, prop->data.pointer, newlen * idp_size_table[(int)prop->subtype]);
-	}
+	if (is_grow == false)
+		idp_resize_group_array(prop, newlen, prop->data.pointer);
 
-	MEM_freeN(prop->data.pointer);
-	prop->data.pointer = newarr;
+	prop->data.pointer = MEM_recallocN(prop->data.pointer, idp_size_table[(int)prop->subtype] * newsize);
+
+	if (is_grow == true)
+		idp_resize_group_array(prop, newlen, prop->data.pointer);
+
 	prop->len = newlen;
 	prop->totallen = newsize;
 }
@@ -301,14 +288,23 @@
 }
 
 /* ---------- String Type ------------ */
+
+/**
+ *
+ * \param st  The string to assign.
+ * \param name  The property name.
+ * \param maxlen  The size of the new string (including the \0 terminator)
+ * \return
+ */
 IDProperty *IDP_NewString(const char *st, const char *name, int maxlen)
 {
 	IDProperty *prop = MEM_callocN(sizeof(IDProperty), "IDProperty string");
 
 	if (st == NULL) {
-		prop->data.pointer = MEM_callocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+		prop->data.pointer = MEM_mallocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+		*IDP_String(prop) = '\0';
 		prop->totallen = DEFAULT_ALLOC_FOR_NULL_STRINGS;
-		prop->len = 1; /*NULL string, has len of 1 to account for null byte.*/
+		prop->len = 1;  /* NULL string, has len of 1 to account for null byte. */
 	}
 	else {
 		int stlen = strlen(st);
@@ -318,7 +314,7 @@
 
 		stlen++; /* null terminator '\0' */
 
-		prop->data.pointer = MEM_callocN(stlen, "id property string 2");
+		prop->data.pointer = MEM_mallocN(stlen, "id property string 2");
 		prop->len = prop->totallen = stlen;
 		BLI_strncpy(prop->data.pointer, st, stlen);
 	}
@@ -354,7 +350,7 @@
 		memcpy(prop->data.pointer, st, stlen);
 	}
 	else {
-		stlen++; /* make room for null byte */
+		stlen++;
 		IDP_ResizeArray(prop, stlen);
 		BLI_strncpy(prop->data.pointer, st, stlen);
 	}
@@ -822,7 +818,8 @@
 			if (val->string.subtype == IDP_STRING_SUB_BYTE) {
 				/* note, intentionally not null terminated */
 				if (st == NULL) {
-					prop->data.pointer = MEM_callocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+					prop->data.pointer = MEM_mallocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+					*IDP_String(prop) = '\0';
 					prop->totallen = DEFAULT_ALLOC_FOR_NULL_STRINGS;
 					prop->len = 0;
 				}
@@ -835,7 +832,8 @@
 			}
 			else {
 				if (st == NULL) {
-					prop->data.pointer = MEM_callocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+					prop->data.pointer = MEM_mallocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
+					*IDP_String(prop) = '\0';
 					prop->totallen = DEFAULT_ALLOC_FOR_NULL_STRINGS;
 					prop->len = 1; /*NULL string, has len of 1 to account for null byte.*/
 				}

Modified: trunk/blender/source/blender/editors/interface/interface.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface.c	2013-09-05 04:19:05 UTC (rev 59816)
+++ trunk/blender/source/blender/editors/interface/interface.c	2013-09-05 04:26:08 UTC (rev 59817)
@@ -985,7 +985,7 @@
 			
 			IDPropertyTemplate val = {0};
 			prop_path = IDP_New(IDP_GROUP, &val, __func__);
-			prop_path_value = IDP_NewString(data_path, "data_path", strlen(data_path) + 1); /* len + 1, or else will be truncated */
+			prop_path_value = IDP_NewString(data_path, "data_path", strlen(data_path) + 1);
 			IDP_AddToGroup(prop_path, prop_path_value);
 			
 			/* check each until one works... */

Modified: trunk/blender/source/blender/makesrna/intern/rna_object_force.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_object_force.c	2013-09-05 04:19:05 UTC (rev 59816)
+++ trunk/blender/source/blender/makesrna/intern/rna_object_force.c	2013-09-05 04:26:08 UTC (rev 59817)
@@ -861,7 +861,9 @@
 
 	prop = RNA_def_property(srna, "use_library_path", PROP_BOOLEAN, PROP_NONE);
 	RNA_def_property_boolean_negative_sdna(prop, NULL, "flag", PTCACHE_IGNORE_LIBPATH);
-	RNA_def_property_ui_text(prop, "Library Path", "Use this files path for the disk cache when library linked into another file. For local bakes per scene file, disable this option.");
+	RNA_def_property_ui_text(prop, "Library Path",
+	                         "Use this files path for the disk cache when library linked into another file. "
+	                         "For local bakes per scene file, disable this option");
 	RNA_def_property_update(prop, NC_OBJECT, "rna_Cache_idname_change");
 
 	prop = RNA_def_property(srna, "point_caches", PROP_COLLECTION, PROP_NONE);




More information about the Bf-blender-cvs mailing list