comparison 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
comparison
equal deleted inserted replaced
61:fa24ef3b026f 62:b218af069da7
1 #include "object.h" 1 #include "object.h"
2 #include "builtin.h" 2 #include "builtin.h"
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <string.h> 4 #include <string.h>
5 #include <stdio.h> 5 #include <stdio.h>
6 #include "fixed_alloc.h"
6 7
7 blueprint ** registered_types = NULL; 8 blueprint ** registered_types = NULL;
8 uint32_t max_registered_type = 0; 9 uint32_t max_registered_type = 0;
9 uint32_t type_storage = 0; 10 uint32_t type_storage = 0;
10 11
12 mem_manager * manager = NULL;
11 /* 13 /*
14
12 returntype call_method(uint32_t methodid, calldata * params) 15 returntype call_method(uint32_t methodid, calldata * params)
13 { 16 {
14 int i; 17 int i;
15 blueprint * bp = get_blueprint(params->params[0]); 18 blueprint * bp = get_blueprint(params->params[0]);
16 if(methodid >= bp->first_methodid && methodid < bp->last_methodid && bp->method_lookup[methodid - bp->first_methodid]) 19 if(methodid >= bp->first_methodid && methodid < bp->last_methodid && bp->method_lookup[methodid - bp->first_methodid])
124 return EXCEPTION_RETURN; 127 return EXCEPTION_RETURN;
125 }*/ 128 }*/
126 129
127 object * alloc_object(blueprint * bp) 130 object * alloc_object(blueprint * bp)
128 { 131 {
129 //TODO: Replace with something more performant 132 return falloc(bp->boxed_size, manager);
130 return malloc(bp->boxed_size); 133 //return malloc(bp->boxed_size);
131 }
132
133 void * alloc_variable(uint32_t size)
134 {
135 return malloc(size);
136 } 134 }
137 135
138 void dealloc_object(blueprint * bp, object * obj) 136 void dealloc_object(blueprint * bp, object * obj)
139 { 137 {
140 //TODO: Replace with something more performant 138 ffree(obj, bp->boxed_size, manager);
141 free(obj);
142 } 139 }
143 140
144 object * new_object(uint32_t type) 141 object * new_object(uint32_t type)
145 { 142 {
146 blueprint * bp; 143 blueprint * bp;
171 { 168 {
172 blueprint *bp; 169 blueprint *bp;
173 multisize * ret; 170 multisize * ret;
174 if(type >= max_registered_type || !registered_types[type]) 171 if(type >= max_registered_type || !registered_types[type])
175 return NULL; 172 return NULL;
176 ret = alloc_variable(sizeof(multisize) + type); 173 ret = falloc(sizeof(multisize) + size, manager);
177 if(ret) 174 if(ret)
178 { 175 {
179 bp = registered_types[type]; 176 bp = registered_types[type];
180 ret->base.bprint = bp; 177 ret->base.bprint = bp;
181 ret->size = size; 178 ret->size = size;
194 if(rh_atomic_get(tocopy, refcount) == 1) 191 if(rh_atomic_get(tocopy, refcount) == 1)
195 return tocopy; 192 return tocopy;
196 bp = get_blueprint(tocopy); 193 bp = get_blueprint(tocopy);
197 if(bp->size < 0) { 194 if(bp->size < 0) {
198 mtocopy = (multisize *)tocopy; 195 mtocopy = (multisize *)tocopy;
199 mcopy = alloc_variable(sizeof(multisize) + mtocopy->size); 196 mcopy = falloc(sizeof(multisize) + mtocopy->size, manager);
200 mcopy->size = mtocopy->size; 197 mcopy->size = mtocopy->size;
201 memcpy(((char *)mcopy)+sizeof(multisize), ((char *)mtocopy)+sizeof(multisize), mtocopy->size); 198 memcpy(((char *)mcopy)+sizeof(multisize), ((char *)mtocopy)+sizeof(multisize), mtocopy->size);
202 copy = (object *)mcopy; 199 copy = (object *)mcopy;
203 } else { 200 } else {
204 copy = alloc_object(bp); 201 copy = alloc_object(bp);
209 bp->copy(copy); 206 bp->copy(copy);
210 release_ref(tocopy); 207 release_ref(tocopy);
211 return copy; 208 return copy;
212 } 209 }
213 210
214 void free_object(object * obj) 211 object * naked_to_boxed(uint32_t type, void * rawdata)
215 { 212 {
216 blueprint * bp = get_blueprint(obj); 213 object * dest;
217 if(bp->cleanup) 214 blueprint * bp = get_blueprint_byid(type);
218 bp->cleanup(obj); 215 if(!bp->boxed_size)
219 dealloc_object(bp, obj); 216 return NULL; //We don't know how big a naked multi-size object is so we can't do anything with it
217 dest = alloc_object(bp);
218 memcpy(((char *)dest) + sizeof(object), rawdata, bp->size);
219 dest->bprint = bp;
220 rh_atomic_set(dest, refcount, 1);
221 bp->copy(dest);
222 return dest;
223 }
224
225 void boxed_to_naked(object * src, void * dest)
226 {
227 blueprint * bp = get_blueprint(src);
228 if(!bp->boxed_size)
229 return; //We don't know how big a naked multi-size object is so we can't do anything with it
230 memcpy(dest, ((char *)src) + sizeof(object), bp->size);
231 bp->copy(src);
220 } 232 }
221 233
222 void release_ref(object * obj) 234 void release_ref(object * obj)
223 { 235 {
224 if(rh_atomic_sub_testzero(obj, refcount, 1)) 236 if(rh_atomic_sub_testzero(obj, refcount, 1))
225 free_object(obj); 237 get_blueprint(obj)->free(obj);
226 } 238 }
227 239
228 void check_type_storage(type) 240 void check_type_storage(type)
229 { 241 {
230 uint32_t type_storage_temp; 242 uint32_t type_storage_temp;
266 278
267 void default_action(object * obj) 279 void default_action(object * obj)
268 { 280 {
269 } 281 }
270 282
283 void normal_free(object * obj)
284 {
285 blueprint * bp = get_blueprint(obj);
286 if(bp->cleanup)
287 bp->cleanup(obj);
288 ffree(obj, bp->boxed_size, manager);
289 }
290
291 void multi_free(object * obj)
292 {
293 multisize * multi = (multisize *)obj;
294 blueprint * bp = get_blueprint(obj);
295 if(bp->cleanup)
296 bp->cleanup(obj);
297 ffree(multi, sizeof(multi) + multi->size, manager);
298 }
299
271 blueprint * new_blueprint(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup) 300 blueprint * new_blueprint(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup)
272 { 301 {
273 blueprint * bp = malloc(sizeof(blueprint)); 302 blueprint * bp = malloc(sizeof(blueprint));
303 //dirty hack!, move elsewhere
304 if (!manager) {
305 fixed_alloc_init();
306 manager = new_mem_manager();
307 }
274 if(bp) 308 if(bp)
275 { 309 {
276 bp->size = size; 310 bp->size = size;
277 bp->boxed_size = size >= 0 ? size + sizeof(object) : size; 311 bp->boxed_size = size >= 0 ? size + sizeof(object) : 0;
278 bp->method_lookup = bp->getter_lookup = bp->setter_lookup = bp->convert_to = bp->convert_from = NULL; 312 bp->method_lookup = bp->getter_lookup = bp->setter_lookup = bp->convert_to = bp->convert_from = NULL;
279 bp->type_id = type; 313 bp->type_id = type;
280 bp->init = init ? init : default_action; 314 bp->init = init ? init : default_action;
281 bp->copy = copy ? copy : default_action; 315 bp->copy = copy ? copy : default_action;
282 bp->cleanup = cleanup ? cleanup : default_action; 316 bp->cleanup = cleanup ? cleanup : default_action;
317 bp->free = size >= 0 ? normal_free : multi_free;
283 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; 318 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;
284 //TODO: Handle names 319 //TODO: Handle names
285 bp->name = NULL; 320 bp->name = NULL;
286 } 321 }
287 return bp; 322 return bp;