object.h

Go to the documentation of this file.
00001 /* TinyaML
00002  * Copyright (C) 2007 Damien Leroux
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018 
00019 #ifndef _BML_OBJ_H_
00020 #define _BML_OBJ_H_
00021 
00022 #include <stdio.h>
00029 #define VM_OBJ_OFS sizeof(struct _vm_obj_t)
00030 
00032 #define PTR_TO_OBJ(_x) ((vm_obj_t)(((char*)(_x))-VM_OBJ_OFS))
00033 
00034 #define OBJ_TO_PTR(_x) ((void*)(((char*)(_x))+VM_OBJ_OFS))
00035 
00037 #define VM_OBJ_MAGIC    0x61060000
00038 
00039 #define VM_OBJ_MASK     0xFFFF0000
00040 
00042 #define _obj_type(_x) (((vm_obj_t)(_x))->magic^VM_OBJ_MAGIC)
00043 
00046 #define _is_obj_obj(_x) (((_x)&&(((_x)->magic&VM_OBJ_MASK) == VM_OBJ_MAGIC)) || (vm_printerrf("Data at %p is not a managed object (magic = %8.8lX)\n",(_x),(_x)?(_x)->magic:0) && 0))
00047 #define _is_a_obj(_x,_t) (_is_obj(_x) && (_obj_type(_x)==(_t)))
00048 
00049 #define _is_obj_ptr(_x) ((_x)&&((PTR_TO_OBJ(_x)->magic&VM_OBJ_MASK) == VM_OBJ_MAGIC))
00050 #define _is_a_ptr(_x,_t) (_is_obj_ptr(_x) && (_obj_type(PTR_TO_OBJ(_x))==(_t)))
00051 
00059 static inline void* vm_obj_new(word_t struc_size, void(*_free)(vm_t,void*), void*(*_clone)(vm_t,void*), vm_data_type_t obj_type) {
00060         vm_obj_t o = (vm_obj_t) malloc(VM_OBJ_OFS+struc_size);
00061         assert((obj_type&DataManagedObjectFlag)&&obj_type<DataTypeMax);
00062         o->ref_count=0;
00063         o->magic = VM_OBJ_MAGIC | obj_type;
00064         o->_free=_free;
00065         o->_clone=_clone;
00066         /*vm_printf("obj new, %u+%lu bytes long at %p ; magic is %8.8lX\n",VM_OBJ_OFS,struc_size,o,o->magic);*/
00067         return (void*)(((char*)o)+VM_OBJ_OFS);
00068 }
00069 
00073 static inline void vm_obj_free_obj(vm_t vm, vm_obj_t o) {
00074         if(!_is_obj_obj(o)) {
00075                 vm_printerrf("[VM:ERR] trying to free something not a managed object (%p).\n",o);
00076                 return;
00077         }
00078         /*assert(o->magic==VM_OBJ_MAGIC);*/
00079         /*vm_printf("obj free %p (%li refs)\n",o,o->ref_count);*/
00080         if(o->_free) {
00081                 o->_free(vm, (void*)(((char*)o)+VM_OBJ_OFS));
00082         }
00083         free(o);
00084 }
00085 
00088 static inline vm_obj_t vm_obj_clone_obj(vm_t vm, vm_obj_t o) {
00089         assert(_is_obj_obj(o));
00090         
00091         if(o->_clone) {
00092                 return o->_clone(vm, (void*)(((char*)o)+VM_OBJ_OFS));
00093         }
00094         return o;
00095 }
00096 
00099 static inline word_t vm_obj_refcount_ptr(void* ptr) {
00100         vm_obj_t o = PTR_TO_OBJ(ptr);
00101         assert(_is_obj_obj(o));
00102         return o->ref_count;
00103 }
00104 
00107 static inline void vm_obj_ref_ptr(vm_t vm, void* ptr) {
00108         vm_obj_t o = PTR_TO_OBJ(ptr);
00109         assert(_is_obj_obj(o)/*||(1/0)*/);
00110         assert(o->ref_count>=0);
00111         o->ref_count+=1;
00112         /*vm_printf("obj ref %p => %li\n",o,o->ref_count);*/
00113         if(o->ref_count==1) {
00114                 vm_uncollect(vm, o);
00115         }
00116 }
00117 
00118 
00121 static inline void vm_obj_deref_ptr(vm_t vm, void* ptr) {
00122         vm_obj_t o = PTR_TO_OBJ(ptr);
00123         assert(_is_obj_obj(o));
00124         /*assert(o->ref_count>0);*/
00125         if(o->ref_count>0) {
00126                 o->ref_count-=1;
00127                 /*vm_printf("obj deref %p => %li\n",o,o->ref_count);*/
00128                 if(o->ref_count==0) {
00129                         vm_collect(vm, o);
00130                 }
00131         } else {
00132                 vm_collect(vm,o);
00133         }
00134 }
00135 
00138 char* vm_string_new(const char*src);
00139 
00142 char* vm_string_new_buf(word_t sz);
00143 
00146 text_seg_t vm_symtab_new();
00147 
00150 mutex_t vm_mutex_new();
00151 
00154 thread_t vm_thread_new(vm_t vm,word_t prio, program_t p, word_t ip);
00155 
00158 dynarray_t vm_array_new();
00159 
00162 vm_dyn_env_t vm_env_new();
00163 
00166 generic_stack_t vm_stack_new();
00167 
00170 vm_dyn_func_t vm_dyn_fun_new();
00171 
00174 #endif
00175 

Generated on Wed Feb 6 14:46:04 2008 for TinyaML by  doxygen 1.5.3