diff runtime/object.c @ 62:b218af069da7

merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Oct 2009 16:43:37 -0400
parents d2f9b0a9403d 1b86a1ee500a
children 04baa003de5a
line wrap: on
line diff
--- a/runtime/object.c	Thu Oct 08 23:16:26 2009 -0400
+++ b/runtime/object.c	Sat Oct 10 16:43:37 2009 -0400
@@ -3,12 +3,15 @@
 #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;
@@ -126,19 +129,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)
@@ -173,7 +170,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];
@@ -196,7 +193,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;
@@ -211,18 +208,33 @@
 	return copy;
 }
 
-void free_object(object * obj)
+object * naked_to_boxed(uint32_t type, void * rawdata)
 {
-	blueprint * bp = get_blueprint(obj);
-	if(bp->cleanup)
-		bp->cleanup(obj);
-	dealloc_object(bp, obj);
+	object * dest;
+	blueprint * bp = get_blueprint_byid(type);
+	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);
+	dest->bprint = bp;
+	rh_atomic_set(dest, refcount, 1);
+	bp->copy(dest);
+	return dest;
+}
+
+void boxed_to_naked(object * src, void * dest)
+{
+	blueprint * bp = get_blueprint(src);
+	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 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)
@@ -268,18 +280,41 @@
 {
 }
 
+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->type_id = type;
 		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;