changeset 119:72c648bca43b

Improved memory debug output and fixed memory leak in Array
author Mike Pavone <pavone@retrodev.com>
date Mon, 18 Oct 2010 00:50:18 -0400
parents b3f56e1d54a0
children cd25171e1bb2
files cbackend.rhope runtime/array.c runtime/fixed_alloc.c runtime/fixed_alloc.h
diffstat 4 files changed, 131 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend.rhope	Fri Oct 15 00:55:02 2010 -0400
+++ b/cbackend.rhope	Mon Oct 18 00:50:18 2010 -0400
@@ -400,6 +400,11 @@
 	}
 }
 
+Defined?@C Type Registry[reg,name:yep,nope]
+{
+	yep,nope <- [[reg]Definitions >>]Index[name]
+}
+
 Blueprint C Function
 {
 	Name
@@ -1457,6 +1462,13 @@
 		]Append[")\n\n"]
 }
 
+Init Type Names[text,typeid,name,reg:out]
+{
+	[reg]Defined?[name]
+	{ out <- [text]Append[ [[[["\tregistered_types["]Append[typeid]]Append["]->name = "]]Append[Const Construct C[name, reg]]]Append[";\n"] ] }
+	{ out <- text }
+}
+
 Text@C Program[program:out]
 {
 	type defs <- [[program]Type Registry >>]Definitions >>
@@ -1474,7 +1486,7 @@
 #include \"worker.h\"
 #include \"bool.h\"
 #include <sys/time.h>\n\n"
-	out <- [[[[[[[[[[[[[[[[[headers
+	out <- [[[[[[[[[[[[[[[[[[headers
 		]Append[[program]Dispatch[all methods]]
 		]Append[[[program]Type Registry >>]Type Defs]
 		]Append[Fold["_Consts C Program", 
@@ -1590,13 +1602,18 @@
 int main(int argc, char **argv)
 {
 	blueprint * bp;
-	int numret;
-	int idx;
+	int numret,retval;
+	int idx,sidx;
 	object * inout[3];
+	int32_t *counts, string_type_id=0;
+	t_String ** live_strings;
+	char ** typenames;
+	t_Array * typename;
 	register_builtin_types();\n\n"]
 		]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
 		]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]]
 		]Append[Fold[["_Set Late Consts C"]Set Input[3, [program]Type Registry >>], "", constants]]
+		]Append[Fold[["Init Type Names"]Set Input[3, [program]Type Registry >>], "", [[program]Type Registry >>]Lookup >>]]
 		]Append["
 	rhope(FUNC_List, inout, 0, 1);
 	for (idx = 0; idx < argc; ++idx)
@@ -1608,8 +1625,64 @@
 		]Append[Fold["_Consts C Release", "", constants]]
 		]Append[
 	"
+	if (!numret)
+		retval = 0;
+	else if (numret < 0)
+		retval = numret;
+	else if (get_blueprint(inout[0])->type_id == TYPE_INT32)
+	{
+		retval = ((t_Int32 *)inout[0])->Num;
+		release_ref(inout[0]);
+	} else {
+		rhope(FUNC_If, inout, 1, 2);
+		if (inout[0])
+		{
+			retval = 0;
+			release_ref(inout[0]);
+		} else {
+			retval = 1;
+			release_ref(inout[1]);
+		}
+	}
+	typenames = malloc(sizeof(char *)*max_registered_type);
+	for(idx = 0; idx < max_registered_type; ++idx)
+	{
+		if(registered_types[idx])
+		{
+			typename = (t_Array *)( ((t_String *)registered_types[idx]->name)->payload.Buffer );
+			typenames[idx] = malloc(typename->payload.Length+1);
+			memcpy(typenames[idx], typename+1, typename->payload.Length);
+			typenames[idx][typename->payload.Length] = 0;
+			if(!strcmp(typenames[idx], \"String\"))
+				string_type_id = idx;
+			release_ref(registered_types[idx]->name);
+		} else {
+			typenames[idx] = NULL;
+		}
+	}
+		
 	print_mem_info(manager);
-	print_live_object_types(manager);
+	counts = malloc(sizeof(int32_t)*max_registered_type);
+	get_live_object_counts(manager, counts);
+	for(idx = 0; idx < max_registered_type; ++idx)
+	{
+		if(counts[idx])
+		{
+			printf(\"%d objects of type %s (%d)\\n\", counts[idx], typenames[idx], idx);
+			if(idx == string_type_id)
+			{
+				live_strings = malloc(sizeof(t_String *)*counts[idx]);
+				find_live_objects_oftype(manager, string_type_id, (void **)live_strings);
+				for(sidx = 0; sidx < counts[idx]; ++sidx)
+				{
+					printf(\"\t%d: \", sidx);
+					fwrite(((t_Array *)live_strings[sidx]->payload.Buffer)+1, 1, ((t_Array *)live_strings[sidx]->payload.Buffer)->payload.Length, stdout);
+					puts(\"\");
+				}
+			}
+		}
+	}
+	fflush(stdout);
 	
 #ifdef ENABLE_PROFILING
 	for (idx = 0; idx < END; ++idx)
@@ -1618,17 +1691,7 @@
 			printf(\"Func: %d\tCount: %llu\tTime: %llu\tAvg: %f\tSelf: %llu\tAvg: %f\tNested Count: %llu\\n\", idx, profile_counts[idx], profile_totals[idx], ((double)profile_totals[idx])/((double)profile_counts[idx]), profile_selftotals[idx], ((double)profile_selftotals[idx])/((double)profile_counts[idx]), profile_nestedcounts[idx]);
 	}
 #endif
-	if (!numret)
-		return 0;
-	if (numret < 0)
-		return numret;
-	if (get_blueprint(inout[0])->type_id == TYPE_INT32)
-		return ((t_Int32 *)inout[0])->Num;
-
-	rhope(FUNC_If, inout, 1, 2);
-	if (inout[0])
-		return 0;
-	return 1;
+	return retval;
 }\n\n"]
 
 }
--- a/runtime/array.c	Fri Oct 15 00:55:02 2010 -0400
+++ b/runtime/array.c	Mon Oct 18 00:50:18 2010 -0400
@@ -12,7 +12,15 @@
 
 void _internal_array_copyin(object * array, int32_t index, object * val)
 {
+	char *dest;
 	t_Array * arr = (t_Array *)array;
+	t_Blueprint * bp = arr->payload.Eltype;
+	if(bp->bp->type_id >= TYPE_ARRAY && index < arr->payload.Length)
+	{
+		//Ugly hack
+		dest = ((char *)(arr+1))-sizeof(object) + bp->bp->size * index;
+		bp->bp->cleanup((object *)dest);
+	}
 	memcpy(((char *)array) + sizeof(t_Array) + arr->payload.Eltype->bp->size * index, ((char *)val) + sizeof(object), arr->payload.Eltype->bp->size);
 	get_blueprint(val)->copy(val);
 	release_ref(val);
@@ -47,6 +55,10 @@
 void _internal_array_setboxed(object *array, int32_t index, object * val)
 {
 	object ** intarr = (object **)(((char *) array) + sizeof(t_BoxedSP_Array));
+	if (index < ((t_BoxedSP_Array *)array)->payload.Length)
+	{
+		release_ref(intarr[index]);
+	}
 	intarr[index] = val;
 }
 
--- a/runtime/fixed_alloc.c	Fri Oct 15 00:55:02 2010 -0400
+++ b/runtime/fixed_alloc.c	Mon Oct 18 00:50:18 2010 -0400
@@ -45,11 +45,42 @@
 	fflush(stdout);
 }
 
-void print_live_object_types(mem_manager * manager)
+void find_live_objects_oftype(mem_manager * manager, int32_t type_id, void ** output)
 {
 	object * obj;
 	mem_block * cur;
-	int32_t i,j,bitslots,bit,*counts = malloc(sizeof(int32_t)*max_registered_type);
+	int32_t i,j,bitslots,bit,outpos=0;
+	for (i = 0; i < (MAX_SIZE-MIN_SIZE)/STRIDE; i++)
+	{
+		cur = manager->inuse[i];
+		while(cur)
+		{
+			bitslots = max_free[i]/8;
+			if(max_free[i]&7)
+				++bitslots;
+			for(j = 0; j < bitslots; ++j)
+				if(cur->bitmap[j] != 0xFF)
+				{
+					for (bit = 0; bit < 8; ++bit)
+					{
+						if (!(cur->bitmap[j] & (1 << bit)))
+						{
+							obj = (object *)(((char *)cur)+BLOCK_SIZE-(((j*8+bit)+1)*(i*STRIDE+MIN_SIZE)));
+							if(obj->bprint->type_id == type_id)
+								output[outpos++] = obj;
+						}
+					}
+				}
+			cur = cur->next;
+		}
+	}
+}
+
+void get_live_object_counts(mem_manager * manager, int32_t * counts)
+{
+	object * obj;
+	mem_block * cur;
+	int32_t i,j,bitslots,bit;
 	memset(counts, 0, sizeof(int32_t)*max_registered_type);
 	for (i = 0; i < (MAX_SIZE-MIN_SIZE)/STRIDE; i++)
 	{
@@ -74,6 +105,12 @@
 			cur = cur->next;
 		}
 	}
+}
+
+void print_live_object_types(mem_manager * manager)
+{
+	int32_t i,*counts = malloc(sizeof(int32_t)*max_registered_type);
+	get_live_object_counts(manager, counts);
 	for (i = 0; i < max_registered_type; ++i)
 		if(counts[i])
 			printf("%d live objects of type %d\n", counts[i], i);
--- a/runtime/fixed_alloc.h	Fri Oct 15 00:55:02 2010 -0400
+++ b/runtime/fixed_alloc.h	Mon Oct 18 00:50:18 2010 -0400
@@ -45,5 +45,7 @@
 void ffree(void * ptr, size_t size, mem_manager * manager);
 void print_mem_info(mem_manager * manager);
 void print_live_object_types(mem_manager * manager);
+void find_live_objects_oftype(mem_manager * manager, int32_t type_id, void ** output);
+void get_live_object_counts(mem_manager * manager, int32_t * counts);
 
 #endif //FIXED_ALLOC_H_