diff runtime/object.c @ 41:1b86a1ee500a

Added faster allocator for small objects
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Oct 2009 16:40:50 -0400
parents 789a146a48e1
children 3e20ed8959c4 b218af069da7
line wrap: on
line diff
--- a/runtime/object.c	Fri Oct 09 01:01:26 2009 -0400
+++ b/runtime/object.c	Sat Oct 10 16:40:50 2009 -0400
@@ -3,11 +3,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include "fixed_alloc.h"
 
 blueprint ** registered_types = NULL;
 uint32_t max_registered_type = 0;
 uint32_t type_storage = 0;
 
+mem_manager * manager = NULL;
+
 returntype call_method(uint32_t methodid, calldata * params)
 {
 	int i;
@@ -125,19 +128,13 @@
 
 object * alloc_object(blueprint * bp)
 {
-	//TODO: Replace with something more performant
-	return malloc(bp->boxed_size);
-}
-
-void * alloc_variable(uint32_t size)
-{
-	return malloc(size);
+	return falloc(bp->boxed_size, manager);
+	//return malloc(bp->boxed_size);
 }
 
 void dealloc_object(blueprint * bp, object * obj)
 {
-	//TODO: Replace with something more performant
-	free(obj);
+	ffree(obj, bp->boxed_size, manager);
 }
 
 object * new_object(uint32_t type)
@@ -172,7 +169,7 @@
 	multisize * ret;
 	if(type >= max_registered_type || !registered_types[type])
 		return NULL;
-	ret = alloc_variable(sizeof(multisize) + type);
+	ret = falloc(sizeof(multisize) + size, manager);
 	if(ret)
 	{
 		bp = registered_types[type];
@@ -195,7 +192,7 @@
 	bp = get_blueprint(tocopy);
 	if(bp->size < 0) {
 		mtocopy = (multisize *)tocopy;
-		mcopy = alloc_variable(sizeof(multisize) + mtocopy->size);
+		mcopy = falloc(sizeof(multisize) + mtocopy->size, manager);
 		mcopy->size = mtocopy->size;
 		memcpy(((char *)mcopy)+sizeof(multisize), ((char *)mtocopy)+sizeof(multisize), mtocopy->size);
 		copy = (object *)mcopy;
@@ -214,7 +211,7 @@
 {
 	object * dest;
 	blueprint * bp = get_blueprint_byid(type);
-	if(!bp->size)
+	if(!bp->boxed_size)
 		return NULL; //We don't know how big a naked multi-size object is so we can't do anything with it
 	dest = alloc_object(bp);
 	memcpy(((char *)dest) + sizeof(object), rawdata, bp->size);
@@ -227,24 +224,16 @@
 void boxed_to_naked(object * src, void * dest)
 {
 	blueprint * bp = get_blueprint(src);
-	if(!bp->size)
+	if(!bp->boxed_size)
 		return; //We don't know how big a naked multi-size object is so we can't do anything with it
 	memcpy(dest, ((char *)src) + sizeof(object), bp->size);
 	bp->copy(src);
 }
 
-void free_object(object * obj)
-{
-	blueprint * bp = get_blueprint(obj);
-	if(bp->cleanup)
-		bp->cleanup(obj);
-	dealloc_object(bp, obj);
-}
-
 void release_ref(object * obj)
 {
 	if(rh_atomic_sub_testzero(obj, refcount, 1))
-		free_object(obj);
+		get_blueprint(obj)->free(obj);
 }
 
 void check_type_storage(type)
@@ -290,17 +279,40 @@
 {
 }
 
+void normal_free(object * obj)
+{
+	blueprint * bp = get_blueprint(obj);
+	if(bp->cleanup)
+		bp->cleanup(obj);
+	ffree(obj, bp->boxed_size, manager);
+}
+
+void multi_free(object * obj)
+{
+	multisize * multi = (multisize *)obj;
+	blueprint * bp = get_blueprint(obj);
+	if(bp->cleanup)
+		bp->cleanup(obj);
+	ffree(multi, sizeof(multi) + multi->size, manager);
+}
+
 blueprint * new_blueprint(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup)
 {
 	blueprint * bp = malloc(sizeof(blueprint));
+	//dirty hack!, move elsewhere
+	if (!manager) {
+		fixed_alloc_init();
+		manager = new_mem_manager();
+	}
 	if(bp)
 	{
 		bp->size = size;
-		bp->boxed_size = size >= 0 ? size + sizeof(object) : size;
+		bp->boxed_size = size >= 0 ? size + sizeof(object) : 0;
 		bp->method_lookup = bp->getter_lookup = bp->setter_lookup = bp->convert_to = bp->convert_from = NULL;
 		bp->init = init ? init : default_action;
 		bp->copy = copy ? copy : default_action;
 		bp->cleanup = cleanup ? cleanup : default_action;
+		bp->free = size >= 0 ? normal_free : multi_free;
 		bp->first_methodid = bp->last_methodid = bp->first_getfieldid = bp->last_getfieldid = bp->first_setfieldid = bp->last_setfieldid = bp->first_convertto = bp->last_convertto = bp->first_convertfrom = bp->last_convertfrom = 0;
 		//TODO: Handle names
 		bp->name = NULL;