diff --git a/moo/kernel/test-002.moo b/moo/kernel/test-002.moo index 3b6f423..3794122 100644 --- a/moo/kernel/test-002.moo +++ b/moo/kernel/test-002.moo @@ -103,6 +103,34 @@ class MyObject(Object) } */ + method(#class) test_quicksort: anArray + { + | quicksort | + quicksort := nil. + quicksort := [ :data :from :to | + (from < to) + ifTrue: [ | pivot index | + pivot := data at: from. + data swap: from with: to. + index := from. + from to: (to - 1) do: [ :each | + (data at: each) < pivot + ifTrue: [ + data swap: each with: index. + index := index + 1 + ] + ]. + + data swap: to with: index. + quicksort value: data value: from value: index - 1. + quicksort value: data value: index + 1 value: to + ]. + data + ]. + + ^(quicksort value: anArray value: 0 value: (anArray size - 1)) + } + method(#class) main { | tc limit | @@ -114,6 +142,8 @@ class MyObject(Object) [ self test_semaphore_heap == true ], [ self test_mutex = #(2000 6000) ], ####[ self test_sem_sig ], + [ (self test_quicksort: #(7 12 3 20 5 8 2) copy) = #(2 3 5 7 8 12 20)], + [ (self test_quicksort: %(99, 12, 18, 7, 12, 3, 20, 5, 8, 2)) = #(2 3 5 7 8 12 12 18 20 99)], [ System sleepForSecs: 2. self.a == 300 ] ## sleep before checking self.a to prevent different result depending on process switching frequency and speed ). @@ -125,6 +155,8 @@ class MyObject(Object) tb := tc at: idx. System log(System.Log.INFO, idx asString, (if (tb value) { ' PASS' } else { ' FAIL' }), S'\n'). ]. + +'AAAAAAAAAAAAAAAAAAAAAaa' dump. } } diff --git a/moo/lib/comp.c b/moo/lib/comp.c index bff286e..e195411 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -9524,7 +9524,7 @@ oops: prev = moo->c->curinp->includer; MOO_ASSERT (moo, moo->c->curinp->name != MOO_NULL); - MOO_MMGR_FREE (moo->mmgr, moo->c->curinp); + moo_freemem (moo, moo->c->curinp); moo->c->curinp = prev; } diff --git a/moo/lib/err.c b/moo/lib/err.c index 22957d8..537315f 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -225,7 +225,7 @@ static int err_bcs (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len) #if defined(MOO_OOCH_IS_UCH) if (max <= 0) return 1; - moo_conv_bchars_to_uchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo->cmgr, 1); + moo_conv_bchars_to_uchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo_getcmgr(moo), 1); moo->errmsg.len += max; #else if (len > max) len = max; @@ -253,7 +253,7 @@ static int err_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len) moo->errmsg.len += len; #else if (max <= 0) return 1; - moo_conv_uchars_to_bchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo->cmgr); + moo_conv_uchars_to_bchars_with_cmgr (ptr, &len, &moo->errmsg.buf[moo->errmsg.len], &max, moo_getcmgr(moo)); moo->errmsg.len += max; #endif moo->errmsg.buf[moo->errmsg.len] = '\0'; diff --git a/moo/lib/fmt.c b/moo/lib/fmt.c index 5bf5139..a1882be 100644 --- a/moo/lib/fmt.c +++ b/moo/lib/fmt.c @@ -1683,7 +1683,7 @@ static int log_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len) { len = rem; bcslen = MOO_COUNTOF(bcs); - moo_conv_uchars_to_bchars_with_cmgr(ptr, &len, bcs, &bcslen, moo->cmgr); + moo_conv_uchars_to_bchars_with_cmgr(ptr, &len, bcs, &bcslen, moo_getcmgr(moo)); log_bcs(fmtout, bcs, bcslen); rem -= len; ptr += len; @@ -1707,7 +1707,7 @@ static int log_bcs (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len) { len = rem; ucslen = MOO_COUNTOF(ucs); - moo_conv_bchars_to_uchars_with_cmgr(ptr, &len, ucs, &ucslen, moo->cmgr, 1); + moo_conv_bchars_to_uchars_with_cmgr(ptr, &len, ucs, &ucslen, moo_getcmgr(moo), 1); log_ucs(fmtout, ucs, ucslen); rem -= len; ptr += len; @@ -1837,7 +1837,7 @@ static int sprint_bcs (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len #if defined(MOO_OOCH_IS_UCH) blen = len; - moo_conv_bchars_to_uchars_with_cmgr (ptr, &blen, MOO_NULL, &oolen, moo->cmgr, 1); + moo_conv_bchars_to_uchars_with_cmgr (ptr, &blen, MOO_NULL, &oolen, moo_getcmgr(moo), 1); #else oolen = len; #endif @@ -1858,7 +1858,7 @@ static int sprint_bcs (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len } #if defined(MOO_OOCH_IS_UCH) - moo_conv_bchars_to_uchars_with_cmgr (ptr, &len, &moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], &oolen, moo->cmgr, 1); + moo_conv_bchars_to_uchars_with_cmgr (ptr, &len, &moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], &oolen, moo_getcmgr(moo), 1); #else MOO_MEMCPY (&moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], ptr, len * MOO_SIZEOF(*ptr)); #endif @@ -1878,7 +1878,7 @@ static int sprint_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len oolen = len; #else ulen = len; - moo_conv_uchars_to_bchars_with_cmgr (ptr, &ulen, MOO_NULL, &oolen, moo->cmgr); + moo_conv_uchars_to_bchars_with_cmgr (ptr, &ulen, MOO_NULL, &oolen, moo_getcmgr(moo)); #endif if (oolen > unused) @@ -1899,7 +1899,7 @@ static int sprint_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len #if defined(MOO_OOCH_IS_UCH) MOO_MEMCPY (&moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], ptr, len * MOO_SIZEOF(*ptr)); #else - moo_conv_uchars_to_bchars_with_cmgr (ptr, &len, &moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], &oolen, moo->cmgr); + moo_conv_uchars_to_bchars_with_cmgr (ptr, &len, &moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], &oolen, moo_getcmgr(moo)); #endif moo->sprintf.xbuf.len += oolen; diff --git a/moo/lib/moo-std.h b/moo/lib/moo-std.h index 38bd683..eb70da8 100644 --- a/moo/lib/moo-std.h +++ b/moo/lib/moo-std.h @@ -104,10 +104,6 @@ MOO_EXPORT moo_t* moo_openstd ( moo_errinf_t* errinfo ); -MOO_EXPORT void* moo_getxtnstd ( - moo_t* moo -); - MOO_EXPORT void moo_abortstd ( moo_t* moo ); diff --git a/moo/lib/moo.c b/moo/lib/moo.c index a244e5f..e66cb3e 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -56,7 +56,7 @@ moo_t* moo_open (moo_mmgr_t* mmgr, moo_oow_t xtnsize, moo_cmgr_t* cmgr, const mo void moo_close (moo_t* moo) { moo_fini (moo); - MOO_MMGR_FREE (moo->mmgr, moo); + MOO_MMGR_FREE (moo_getmmgr(moo), moo); } static void fill_bigint_tables (moo_t* moo) @@ -86,16 +86,14 @@ static void fill_bigint_tables (moo_t* moo) } } -static void* alloc_heap (moo_t* moo, moo_oow_t size) +static MOO_INLINE void* alloc_heap (moo_t* moo, moo_oow_t size) { - void* ptr = MOO_MMGR_ALLOC(moo->mmgr, size); - if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM); - return ptr; + return moo_allocmem(moo, size); } -static void free_heap (moo_t* moo, void* ptr) +static MOO_INLINE void free_heap (moo_t* moo, void* ptr) { - MOO_MMGR_FREE(moo->mmgr, ptr); + moo_freemem (moo, ptr); } int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_cmgr_t* cmgr, const moo_vmprim_t* vmprim) @@ -109,8 +107,10 @@ int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_cmgr_t* cmgr, const moo_vmprim_t } MOO_MEMSET (moo, 0, MOO_SIZEOF(*moo)); - moo->mmgr = mmgr; - moo->cmgr = cmgr; + moo->_instsize = MOO_SIZEOF(*moo); + moo->_mmgr = mmgr; + moo->_cmgr = cmgr; + moo->vmprim = *vmprim; if (!moo->vmprim.alloc_heap) moo->vmprim.alloc_heap = alloc_heap; if (!moo->vmprim.free_heap) moo->vmprim.free_heap = free_heap; @@ -427,7 +427,7 @@ void* moo_allocmem (moo_t* moo, moo_oow_t size) { void* ptr; - ptr = MOO_MMGR_ALLOC (moo->mmgr, size); + ptr = MOO_MMGR_ALLOC(moo_getmmgr(moo), size); if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM); return ptr; } @@ -436,7 +436,7 @@ void* moo_callocmem (moo_t* moo, moo_oow_t size) { void* ptr; - ptr = MOO_MMGR_ALLOC (moo->mmgr, size); + ptr = MOO_MMGR_ALLOC(moo_getmmgr(moo), size); if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM); else MOO_MEMSET (ptr, 0, size); return ptr; @@ -444,14 +444,14 @@ void* moo_callocmem (moo_t* moo, moo_oow_t size) void* moo_reallocmem (moo_t* moo, void* ptr, moo_oow_t size) { - ptr = MOO_MMGR_REALLOC (moo->mmgr, ptr, size); + ptr = MOO_MMGR_REALLOC (moo_getmmgr(moo), ptr, size); if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM); return ptr; } void moo_freemem (moo_t* moo, void* ptr) { - MOO_MMGR_FREE (moo->mmgr, ptr); + MOO_MMGR_FREE (moo_getmmgr(moo), ptr); } /* -------------------------------------------------------------------------- */ diff --git a/moo/lib/moo.h b/moo/lib/moo.h index f3cefd8..1734164 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1351,8 +1351,9 @@ typedef struct moo_errinf_t moo_errinf_t; struct moo_t { - moo_mmgr_t* mmgr; - moo_cmgr_t* cmgr; + moo_oow_t _instsize; + moo_mmgr_t* _mmgr; + moo_cmgr_t* _cmgr; moo_errnum_t errnum; struct { @@ -1844,19 +1845,19 @@ MOO_EXPORT void moo_fini ( ); #if defined(MOO_HAVE_INLINE) - static MOO_INLINE moo_mmgr_t* moo_getmmgr (moo_t* moo) { return moo->mmgr; } - static MOO_INLINE void* moo_getxtn (moo_t* moo) { return (void*)(moo + 1); } + static MOO_INLINE moo_mmgr_t* moo_getmmgr (moo_t* moo) { return moo->_mmgr; } + static MOO_INLINE void* moo_getxtn (moo_t* moo) { return (void*)((moo_uint8_t*)moo + moo->_instsize); } - static MOO_INLINE moo_cmgr_t* moo_getcmgr (moo_t* moo) { return moo->cmgr; } - static MOO_INLINE void moo_setcmgr (moo_t* moo, moo_cmgr_t* cmgr) { moo->cmgr = cmgr; } + static MOO_INLINE moo_cmgr_t* moo_getcmgr (moo_t* moo) { return moo->_cmgr; } + static MOO_INLINE void moo_setcmgr (moo_t* moo, moo_cmgr_t* cmgr) { moo->_cmgr = cmgr; } static MOO_INLINE moo_errnum_t moo_geterrnum (moo_t* moo) { return moo->errnum; } #else -# define moo_getmmgr(moo) (((moo_t*)(moo))->mmgr) -# define moo_getxtn(moo) ((void*)((moo_t*)(moo) + 1)) +# define moo_getmmgr(moo) (((moo_t*)(moo))->_mmgr) +# define moo_getxtn(moo) ((void*)((moo_uint8_t*)moo + ((moo_t*)moo)->_instsize)) -# define moo_getcmgr(moo) (((moo_t*)(moo))->cmgr) -# define moo_setcmgr(moo,mgr) (((moo_t*)(moo))->cmgr = (mgr)) +# define moo_getcmgr(moo) (((moo_t*)(moo))->_cmgr) +# define moo_setcmgr(moo,cmgr) (((moo_t*)(moo))->_cmgr = (cmgr)) # define moo_geterrnum(moo) (((moo_t*)(moo))->errnum) #endif diff --git a/moo/lib/rbt.c b/moo/lib/rbt.c index 83ae026..5d5feca 100644 --- a/moo/lib/rbt.c +++ b/moo/lib/rbt.c @@ -67,8 +67,8 @@ MOO_INLINE moo_rbt_pair_t* moo_rbt_allocpair ( if (kcop == MOO_RBT_COPIER_INLINE) as += MOO_ALIGN_POW2(KTOB(rbt,klen), MOO_SIZEOF_VOID_P); if (vcop == MOO_RBT_COPIER_INLINE) as += VTOB(rbt,vlen); - pair = (moo_rbt_pair_t*) MOO_MMGR_ALLOC (rbt->moo->mmgr, as); - if (pair == MOO_NULL) return MOO_NULL; + pair = (moo_rbt_pair_t*)moo_allocmem(rbt->moo, as); + if (!pair) return MOO_NULL; pair->color = MOO_RBT_RED; pair->parent = MOO_NULL; @@ -87,10 +87,10 @@ MOO_INLINE moo_rbt_pair_t* moo_rbt_allocpair ( } else { - KPTR(pair) = kcop (rbt, kptr, klen); + KPTR(pair) = kcop(rbt, kptr, klen); if (KPTR(pair) == MOO_NULL) { - MOO_MMGR_FREE (rbt->moo->mmgr, pair); + moo_freemem (rbt->moo, pair); return MOO_NULL; } } @@ -114,7 +114,7 @@ MOO_INLINE moo_rbt_pair_t* moo_rbt_allocpair ( { if (rbt->style->freeer[MOO_RBT_KEY] != MOO_NULL) rbt->style->freeer[MOO_RBT_KEY] (rbt, KPTR(pair), KLEN(pair)); - MOO_MMGR_FREE (rbt->moo->mmgr, pair); + moo_freemem (rbt->moo, pair); return MOO_NULL; } } @@ -128,7 +128,7 @@ MOO_INLINE void moo_rbt_freepair (moo_rbt_t* rbt, moo_rbt_pair_t* pair) rbt->style->freeer[MOO_RBT_KEY] (rbt, KPTR(pair), KLEN(pair)); if (rbt->style->freeer[MOO_RBT_VAL] != MOO_NULL) rbt->style->freeer[MOO_RBT_VAL] (rbt, VPTR(pair), VLEN(pair)); - MOO_MMGR_FREE (rbt->moo->mmgr, pair); + moo_freemem (rbt->moo, pair); } static moo_rbt_style_t style[] = @@ -195,12 +195,12 @@ moo_rbt_t* moo_rbt_open (moo_t* moo, moo_oow_t xtnsize, int kscale, int vscale) { moo_rbt_t* rbt; - rbt = (moo_rbt_t*) MOO_MMGR_ALLOC (moo->mmgr, MOO_SIZEOF(moo_rbt_t) + xtnsize); - if (rbt == MOO_NULL) return MOO_NULL; + rbt = (moo_rbt_t*)moo_allocmem(moo, MOO_SIZEOF(moo_rbt_t) + xtnsize); + if (!rbt) return MOO_NULL; - if (moo_rbt_init (rbt, moo, kscale, vscale) <= -1) + if (moo_rbt_init(rbt, moo, kscale, vscale) <= -1) { - MOO_MMGR_FREE (moo->mmgr, rbt); + moo_freemem (moo, rbt); return MOO_NULL; } @@ -211,7 +211,7 @@ moo_rbt_t* moo_rbt_open (moo_t* moo, moo_oow_t xtnsize, int kscale, int vscale) void moo_rbt_close (moo_rbt_t* rbt) { moo_rbt_fini (rbt); - MOO_MMGR_FREE (rbt->moo->mmgr, rbt); + moo_freemem (rbt->moo, rbt); } int moo_rbt_init (moo_rbt_t* rbt, moo_t* moo, int kscale, int vscale) diff --git a/moo/lib/std.c b/moo/lib/std.c index b51e739..68bb0ce 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -379,7 +379,7 @@ struct xtn_t } ev; }; -#define GET_XTN(moo) ((xtn_t*)moo_getxtn(moo)) +#define GET_XTN(moo) ((xtn_t*)((moo_uint8_t*)moo_getxtn(moo) - MOO_SIZEOF(xtn_t))) static moo_t* g_moo = MOO_NULL; @@ -3649,6 +3649,9 @@ static void fini_moo (moo_t* moo) moo = moo_open(&sys_mmgr, MOO_SIZEOF(xtn_t) + xtnsize, ((cfg && cfg->cmgr)? cfg->cmgr: moo_get_utf8_cmgr()), &vmprim, errinfo); if (!moo) return MOO_NULL; + /* adjust the object size by the sizeof xtn_t so that qse_getxtn() returns the right pointer. */ + moo->_instsize += MOO_SIZEOF(xtn_t); + xtn = GET_XTN(moo); if (cfg) xtn->input_cmgr = cfg->input_cmgr; if (!xtn->input_cmgr) xtn->input_cmgr = moo_getcmgr(moo); @@ -3691,11 +3694,6 @@ static void fini_moo (moo_t* moo) return moo; } -void* moo_getxtnstd (moo_t* moo) -{ - return (void*)((moo_uint8_t*)GET_XTN(moo) + MOO_SIZEOF(xtn_t)); -} - void moo_abortstd (moo_t* moo) { #if defined(USE_THREAD) diff --git a/moo/lib/utl.c b/moo/lib/utl.c index 092b2d3..982db29 100644 --- a/moo/lib/utl.c +++ b/moo/lib/utl.c @@ -879,7 +879,7 @@ int moo_convbtouchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_ { /* length bound */ int n; - n = moo_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, moo->cmgr, 0); + n = moo_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, moo_getcmgr(moo), 0); /* -1: illegal character, -2: buffer too small, -3: incomplete sequence */ if (n <= -1) moo_seterrnum (moo, (n == -2)? MOO_EBUFFULL: MOO_EECERR); return n; @@ -889,7 +889,7 @@ int moo_convutobchars (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_ { /* length bound */ int n; - n = moo_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, moo->cmgr); + n = moo_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, moo_getcmgr(moo)); if (n <= -1) moo_seterrnum (moo, (n == -2)? MOO_EBUFFULL: MOO_EECERR); return n; } @@ -898,7 +898,7 @@ int moo_convbtoucstr (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_u { /* null-terminated. */ int n; - n = moo_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, moo->cmgr, 0); + n = moo_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, moo_getcmgr(moo), 0); if (n <= -1) moo_seterrnum (moo, (n == -2)? MOO_EBUFFULL: MOO_EECERR); return n; } @@ -907,7 +907,7 @@ int moo_convutobcstr (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_b { /* null-terminated */ int n; - n = moo_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, moo->cmgr); + n = moo_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, moo_getcmgr(moo)); if (n <= -1) moo_seterrnum (moo, (n == -2)? MOO_EBUFFULL: MOO_EECERR); return n; }