diff --git a/moo/kernel/Apex.moo b/moo/kernel/Apex.moo index 7e0e0b9..898f781 100644 --- a/moo/kernel/Apex.moo +++ b/moo/kernel/Apex.moo @@ -528,18 +528,3 @@ extend Error method(#primitive) asCharacter. method(#primitive) asString. } - - -class SmallPointer(Object) -{ - method(#primitive) asString. - - method(#primitive) getInt8 (offset). - method(#primitive) getInt16 (offset). - method(#primitive) getInt32 (offset). - method(#primitive) getInt64 (offset). - method(#primitive) getUint8 (offset). - method(#primitive) getUint16(offset). - method(#primitive) getUint32(offset). - method(#primitive) getUint64(offset). -} diff --git a/moo/kernel/Mill.moo b/moo/kernel/Mill.moo index 7f059ca..65accf8 100644 --- a/moo/kernel/Mill.moo +++ b/moo/kernel/Mill.moo @@ -323,16 +323,20 @@ class MyObject(Object) (* basicAt: 12 to access the nsdic field. use a proper method once it's defined in Class *) - (System nsdic) keysAndValuesDo: [:k :v | - k dump. - v dump. - '------------' dump. - ]. + ## (System nsdic) keysAndValuesDo: [:k :v | + ## k dump. + ## v dump. + ## '------------' dump. + ## ]. (System at: #Processor) dump. System logNl: 'Sleeping start now....'. +a := System _malloc(200). +a dump. +##System _free(a). +a free. Processor sleepFor: 2. } diff --git a/moo/kernel/System.moo b/moo/kernel/System.moo index bc7a70b..167cee7 100644 --- a/moo/kernel/System.moo +++ b/moo/kernel/System.moo @@ -100,6 +100,11 @@ extend System ^self nsdic at: key put: value } + (* raw memory allocation *) + method(#class,#primitive) _malloc (size). + method(#class,#primitive) _calloc (size). + method(#class,#primitive) _free (rawptr). + (* raw memory access *) method(#class,#primitive) _getInt8 (rawptr, offset). ## method(#class,#primitive) _getInt16 (rawptr, offset). @@ -121,3 +126,20 @@ extend System method(#class,#primitive) _putUint64 (rawptr, offset, value), *) } + + +class SmallPointer(Object) +{ + method(#primitive) asString. + + method(#primitive) getInt8 (offset). + method(#primitive) getInt16 (offset). + method(#primitive) getInt32 (offset). + method(#primitive) getInt64 (offset). + method(#primitive) getUint8 (offset). + method(#primitive) getUint16(offset). + method(#primitive) getUint32(offset). + method(#primitive) getUint64(offset). + + method(#primitive) free. +} diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 58faed1..f40800c 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -2771,6 +2771,84 @@ static moo_pfrc_t pf_error_as_string (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; } + +static MOO_INLINE moo_pfrc_t _system_alloc (moo_t* moo, moo_ooi_t nargs, int clear) +{ + moo_oop_t tmp; + + MOO_ASSERT (moo, nargs == 1); + + tmp = MOO_STACK_GETARG(moo, nargs, 0); + if (MOO_OOP_IS_SMOOI(tmp)) + { + void* ptr; + + ptr = clear? moo_callocmem (moo, MOO_OOP_TO_SMOOI(tmp)): + moo_allocmem (moo, MOO_OOP_TO_SMOOI(tmp)); + if (ptr) + { + MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(ptr)); + } + else + { + MOO_STACK_SETRETTOERRNUM (moo, nargs); + } + } + else + { + MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL); + } + + return MOO_PF_SUCCESS; +} + +static moo_pfrc_t pf_system_calloc (moo_t* moo, moo_ooi_t nargs) +{ + return _system_alloc (moo, nargs, 1); +} +static moo_pfrc_t pf_system_malloc (moo_t* moo, moo_ooi_t nargs) +{ + return _system_alloc (moo, nargs, 0); +} + +static moo_pfrc_t pf_system_free (moo_t* moo, moo_ooi_t nargs) +{ + moo_oop_t tmp; + void* rawptr; + + MOO_ASSERT (moo, nargs == 1); + + tmp = MOO_STACK_GETARG(moo, nargs, 0); + if (MOO_OOP_IS_SMPTR(tmp)) + { + moo_freemem (moo, MOO_OOP_TO_SMPTR(tmp)); + } + else if (moo_inttooow(moo, tmp, (moo_oow_t*)&rawptr) >= 1) + { + moo_freemem (moo, rawptr); + } + + MOO_STACK_SETRETTORCV (moo, nargs); + return MOO_PF_SUCCESS; +} + +static moo_pfrc_t pf_smptr_free (moo_t* moo, moo_ooi_t nargs) +{ + moo_oop_t tmp; + + MOO_ASSERT (moo, nargs == 0); + + tmp = MOO_STACK_GETRCV(moo, nargs); + if (MOO_OOP_IS_SMPTR(tmp)) + { + moo_freemem (moo, MOO_OOP_TO_SMPTR(tmp)); + } + + MOO_STACK_SETRETTORCV (moo, nargs); + return MOO_PF_SUCCESS; +} + + #define FETCH_RAW_INT(value, rawptr,offset,size) \ switch (size) \ { \ @@ -3068,7 +3146,6 @@ static pf_t pftab[] = { "_hash", { pf_hash, 0, 0 } }, - { "_responds_to", { pf_responds_to, 1, 1 } }, { "_perform", { pf_perform, 1, MA } }, { "_exceptionize_error", { pf_exceptionize_error, 1, 1 } }, @@ -3082,9 +3159,6 @@ static pf_t pftab[] = { "_process_yield", { pf_process_yield, 0, 0 } }, { "_process_suspend", { pf_process_suspend, 0, 0 } }, - { "Semaphore_signal", { pf_semaphore_signal, 0, 0 } }, - { "Semaphore_wait", { pf_semaphore_wait, 0, 0 } }, - { "_processor_schedule", { pf_processor_schedule, 1, 1 } }, { "_processor_add_timed_semaphore", { pf_processor_add_timed_semaphore, 2, 3 } }, { "_processor_add_input_semaphore", { pf_processor_add_input_semaphore, 2, 2 } }, @@ -3119,28 +3193,34 @@ static pf_t pftab[] = { "Error_asInteger", { pf_error_as_integer, 0, 0 } }, { "Error_asString", { pf_error_as_string, 0, 0 } }, + { "Semaphore_signal", { pf_semaphore_signal, 0, 0 } }, + { "Semaphore_wait", { pf_semaphore_wait, 0, 0 } }, + { "SmallInteger_asCharacter", { pf_smooi_as_character, 0, 0 } }, { "SmallInteger_asError", { pf_smooi_as_error, 0, 0 } }, { "SmallPointer_asString", { pf_smptr_as_string, 0, 0 } }, - - { "SmallPointer_getInt8", { pf_smptr_get_int8, 1, 1 } }, + { "SmallPointer_free", { pf_smptr_free, 0, 0 } }, { "SmallPointer_getInt16", { pf_smptr_get_int16, 1, 1 } }, { "SmallPointer_getInt32", { pf_smptr_get_int32, 1, 1 } }, { "SmallPointer_getInt64", { pf_smptr_get_int64, 1, 1 } }, - { "SmallPointer_getUint8", { pf_smptr_get_uint8, 1, 1 } }, + { "SmallPointer_getInt8", { pf_smptr_get_int8, 1, 1 } }, { "SmallPointer_getUint16", { pf_smptr_get_uint16, 1, 1 } }, { "SmallPointer_getUint32", { pf_smptr_get_uint32, 1, 1 } }, { "SmallPointer_getUint64", { pf_smptr_get_uint64, 1, 1 } }, + { "SmallPointer_getUint8", { pf_smptr_get_uint8, 1, 1 } }, - { "System__getInt8", { pf_system_get_int8, 2, 2 } }, + { "System__calloc", { pf_system_calloc, 1, 1 } }, + { "System__free", { pf_system_free, 1, 1 } }, { "System__getInt16", { pf_system_get_int16, 2, 2 } }, { "System__getInt32", { pf_system_get_int32, 2, 2 } }, { "System__getInt64", { pf_system_get_int64, 2, 2 } }, - { "System__getUint8", { pf_system_get_uint8, 2, 2 } }, + { "System__getInt8", { pf_system_get_int8, 2, 2 } }, { "System__getUint16", { pf_system_get_uint16, 2, 2 } }, { "System__getUint32", { pf_system_get_uint32, 2, 2 } }, - { "System__getUint64", { pf_system_get_uint64, 2, 2 } } + { "System__getUint64", { pf_system_get_uint64, 2, 2 } }, + { "System__getUint8", { pf_system_get_uint8, 2, 2 } }, + { "System__malloc", { pf_system_malloc, 1, 1 } }, /* { "System__putInt8", { pf_put_int8, 3, 3 } }, @@ -3152,7 +3232,6 @@ static pf_t pftab[] = { "System__putUint32", { pf_put_uint32, 3, 3 } }, { "System__putUint64", { pf_put_uint64, 3, 3 } }, */ - }; moo_pfbase_t* moo_getpfnum (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, moo_ooi_t* pfnum) diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 5c5d9c1..f6bc521 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1137,8 +1137,8 @@ struct moo_t * also you must not call this macro more than once */ #define MOO_STACK_SETRET(moo,nargs,retv) (MOO_STACK_POPS(moo, nargs), MOO_STACK_SETTOP(moo, (retv))) #define MOO_STACK_SETRETTORCV(moo,nargs) (MOO_STACK_POPS(moo, nargs)) -#define MOO_STACK_SETRETTOERROR(moo,nargs) MOO_STACK_SETRET(moo, nargs, MOO_ERROR_TO_OOP(moo->errnum)) -/*#define MOO_STACK_SETRETTOERROR(moo,nargs,ec) MOO_STACK_SETRET(moo, nargs, MOO_ERROR_TO_OOP(ec))*/ +#define MOO_STACK_SETRETTOERRNUM(moo,nargs) MOO_STACK_SETRET(moo, nargs, MOO_ERROR_TO_OOP(moo->errnum)) +#define MOO_STACK_SETRETTOERROR(moo,nargs,ec) MOO_STACK_SETRET(moo, nargs, MOO_ERROR_TO_OOP(ec)) /* ========================================================================= * MOO VM LOGGING diff --git a/moo/mod/console.c b/moo/mod/console.c index c28f35c..478f041 100644 --- a/moo/mod/console.c +++ b/moo/mod/console.c @@ -191,7 +191,7 @@ static moo_pfrc_t pf_write (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; einval: - MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about the error code */ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about the error code */ return MOO_PF_SUCCESS; #endif } @@ -236,7 +236,7 @@ static moo_pfrc_t pf_setcursor (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; einval: - MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about the error code */ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about the error code */ return MOO_PF_SUCCESS; #endif } diff --git a/moo/mod/ffi.c b/moo/mod/ffi.c index f1d6d3f..1da0890 100644 --- a/moo/mod/ffi.c +++ b/moo/mod/ffi.c @@ -130,7 +130,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } @@ -164,7 +164,7 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } @@ -462,7 +462,7 @@ inval: softfail: free_linked_cas (moo, ffi); - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; hardfail: @@ -471,7 +471,7 @@ hardfail: #else moo_seterrnum (moo, MOO_ENOIMPL); - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; #endif } @@ -509,7 +509,7 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } diff --git a/moo/mod/stdio.c b/moo/mod/stdio.c index f0b13ae..81d037d 100644 --- a/moo/mod/stdio.c +++ b/moo/mod/stdio.c @@ -83,7 +83,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } @@ -186,7 +186,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } diff --git a/moo/mod/x11.c b/moo/mod/x11.c index df817d3..f9613cc 100644 --- a/moo/mod/x11.c +++ b/moo/mod/x11.c @@ -126,7 +126,7 @@ static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; softfail: - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_SUCCESS; } @@ -171,7 +171,7 @@ static moo_pfrc_t pf_get_fd (moo_t* moo, moo_ooi_t nargs) else { error: - MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about error code */ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about error code */ } return MOO_PF_SUCCESS; @@ -216,7 +216,7 @@ static moo_pfrc_t pf_getevent (moo_t* moo, moo_ooi_t nargs) { /* TODO: to be specific about the error */ MOO_DEBUG1 (moo, "XCB CONNECTION ERROR %d\n", e); - MOO_STACK_SETRETTOERROR (moo, nargs); + MOO_STACK_SETRETTOERRNUM (moo, nargs); } else { @@ -256,7 +256,7 @@ static moo_pfrc_t pf_gc_draw_line (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; reterr: - MOO_STACK_SETRETTOERROR (moo, nargs); /* More specific error code*/ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* More specific error code*/ return MOO_PF_SUCCESS; } @@ -287,7 +287,7 @@ static moo_pfrc_t pf_gc_draw_rect (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; reterr: - MOO_STACK_SETRETTOERROR (moo, nargs); /* More specific error code*/ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* More specific error code*/ return MOO_PF_SUCCESS; } @@ -310,7 +310,7 @@ static moo_pfrc_t pf_gc_set_foreground (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; reterr: - MOO_STACK_SETRETTOERROR (moo, nargs); /* More specific error code*/ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* More specific error code*/ return MOO_PF_SUCCESS; } @@ -370,7 +370,7 @@ static moo_pfrc_t pf_gc_make (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; reterr: - MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about error */ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about error */ return MOO_PF_SUCCESS; } @@ -482,7 +482,7 @@ MOO_DEBUG0 (moo, " %p\n"); return MOO_PF_SUCCESS; reterr: - MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about error */ + MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about error */ return MOO_PF_SUCCESS; }