added moo->igniting and attempted to allocate memory from permheap when it's 1.

relevant gc change is still pending
This commit is contained in:
hyunghwan.chung 2018-12-09 07:21:16 +00:00
parent 647edf3268
commit e73c81e2b7
4 changed files with 42 additions and 26 deletions

View File

@ -583,15 +583,21 @@ int moo_ignite (moo_t* moo, moo_oow_t heapsz)
moo->heap = moo_makeheap(moo, heapsz); moo->heap = moo_makeheap(moo, heapsz);
if (!moo->heap) return -1; if (!moo->heap) return -1;
moo->igniting = 1;
moo->_nil = moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t)); moo->_nil = moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t));
if (!moo->_nil) return -1; if (!moo->_nil) goto oops;
moo->_nil->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 1, 0, 0, 0); moo->_nil->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 1, 1, 0, 0, 0);
moo->_nil->_size = 0; moo->_nil->_size = 0;
if (ignite_1(moo) <= -1 || ignite_2(moo) <= -1 || ignite_3(moo)) return -1; if (ignite_1(moo) <= -1 || ignite_2(moo) <= -1 || ignite_3(moo)) goto oops;;
moo->igniting = 0;
return 0; return 0;
oops:
moo->igniting = 0;
return -1;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------

View File

@ -26,8 +26,8 @@
#include "moo-prv.h" #include "moo-prv.h"
#define MIN_HEAP_SIZE 65536 /* TODO: adjust this value? */ #define MIN_HEAP_SIZE 65536 * 5 /* TODO: adjust this value? */
#define PERM_SPACE_SIZE 2048 /* TODO: adjust perm space size depending on what's allocated in the permspace */ #define PERM_SPACE_SIZE 65536 * 2 /* TODO: adjust perm space size depending on what's allocated in the permspace */
moo_heap_t* moo_makeheap (moo_t* moo, moo_oow_t size) moo_heap_t* moo_makeheap (moo_t* moo, moo_oow_t size)
{ {

View File

@ -438,11 +438,12 @@ typedef enum moo_gcfin_t moo_gcfin_t;
/* [NOTE] this macro doesn't check the range of the actual value. /* [NOTE] this macro doesn't check the range of the actual value.
* make sure that the value of each bit fields given falls within the * make sure that the value of each bit fields given falls within the
* possible range of the defined bits */ * possible range of the defined bits */
#define MOO_OBJ_MAKE_FLAGS(t,u,e,k,m,g,r) ( \ #define MOO_OBJ_MAKE_FLAGS(t,u,e,k,p,m,g,r) ( \
(((moo_oow_t)(t)) << MOO_OBJ_FLAGS_TYPE_SHIFT) | \ (((moo_oow_t)(t)) << MOO_OBJ_FLAGS_TYPE_SHIFT) | \
(((moo_oow_t)(u)) << MOO_OBJ_FLAGS_UNIT_SHIFT) | \ (((moo_oow_t)(u)) << MOO_OBJ_FLAGS_UNIT_SHIFT) | \
(((moo_oow_t)(e)) << MOO_OBJ_FLAGS_EXTRA_SHIFT) | \ (((moo_oow_t)(e)) << MOO_OBJ_FLAGS_EXTRA_SHIFT) | \
(((moo_oow_t)(k)) << MOO_OBJ_FLAGS_KERNEL_SHIFT) | \ (((moo_oow_t)(k)) << MOO_OBJ_FLAGS_KERNEL_SHIFT) | \
(((moo_oow_t)(p)) << MOO_OBJ_FLAGS_PERM_SHIFT) | \
(((moo_oow_t)(m)) << MOO_OBJ_FLAGS_MOVED_SHIFT) | \ (((moo_oow_t)(m)) << MOO_OBJ_FLAGS_MOVED_SHIFT) | \
(((moo_oow_t)(g)) << MOO_OBJ_FLAGS_NGC_SHIFT) | \ (((moo_oow_t)(g)) << MOO_OBJ_FLAGS_NGC_SHIFT) | \
(((moo_oow_t)(r)) << MOO_OBJ_FLAGS_TRAILER_SHIFT) \ (((moo_oow_t)(r)) << MOO_OBJ_FLAGS_TRAILER_SHIFT) \
@ -1410,7 +1411,9 @@ struct moo_t
moo_ooch_t buf[MOO_ERRMSG_CAPA]; moo_ooch_t buf[MOO_ERRMSG_CAPA];
moo_oow_t len; moo_oow_t len;
} errmsg; } errmsg;
int shuterr; int shuterr;
int igniting;
struct struct
{ {
@ -1969,17 +1972,17 @@ MOO_EXPORT int moo_getoption (
MOO_EXPORT int moo_setoption ( MOO_EXPORT int moo_setoption (
moo_t* moo, moo_t* moo,
moo_option_t id, moo_option_t id,
const void* value const void* value
); );
MOO_EXPORT moo_evtcb_t* moo_regevtcb ( MOO_EXPORT moo_evtcb_t* moo_regevtcb (
moo_t* moo, moo_t* moo,
moo_evtcb_t* tmpl moo_evtcb_t* tmpl
); );
MOO_EXPORT void moo_deregevtcb ( MOO_EXPORT void moo_deregevtcb (
moo_t* moo, moo_t* moo,
moo_evtcb_t* cb moo_evtcb_t* cb
); );

View File

@ -28,27 +28,34 @@
void* moo_allocbytes (moo_t* moo, moo_oow_t size) void* moo_allocbytes (moo_t* moo, moo_oow_t size)
{ {
moo_uint8_t* ptr;
#if defined(MOO_BUILD_DEBUG) #if defined(MOO_BUILD_DEBUG)
if ((moo->option.trait & MOO_DEBUG_GC) && !(moo->option.trait & MOO_NOGC)) moo_gc (moo); if ((moo->option.trait & MOO_DEBUG_GC) && !(moo->option.trait & MOO_NOGC)) moo_gc (moo);
#endif #endif
ptr = (moo_uint8_t*)moo_allocheapspace(moo, &moo->heap->curspace, size); if (MOO_UNLIKELY(moo->igniting))
if (!ptr && moo->errnum == MOO_EOOMEM && !(moo->option.trait & MOO_NOGC))
{ {
moo_gc (moo); /* you must increase the size of the permheap if this allocation fails */
MOO_LOG4 (moo, MOO_LOG_GC | MOO_LOG_INFO, return (moo_uint8_t*)moo_allocheapspace(moo, &moo->heap->permspace, size);
"GC completed - current heap ptr %p limit %p size %zd free %zd\n",
moo->heap->curspace.ptr, moo->heap->curspace.limit,
(moo_oow_t)(moo->heap->curspace.limit - moo->heap->curspace.base),
(moo_oow_t)(moo->heap->curspace.limit - moo->heap->curspace.ptr)
);
ptr = (moo_uint8_t*)moo_allocheapspace(moo, &moo->heap->curspace, size);
/* TODO: grow heap if ptr is still null. */
} }
else
{
moo_uint8_t* ptr;
ptr = (moo_uint8_t*)moo_allocheapspace(moo, &moo->heap->curspace, size);
if (!ptr && moo->errnum == MOO_EOOMEM && !(moo->option.trait & MOO_NOGC))
{
moo_gc (moo);
MOO_LOG4 (moo, MOO_LOG_GC | MOO_LOG_INFO,
"GC completed - current heap ptr %p limit %p size %zd free %zd\n",
moo->heap->curspace.ptr, moo->heap->curspace.limit,
(moo_oow_t)(moo->heap->curspace.limit - moo->heap->curspace.base),
(moo_oow_t)(moo->heap->curspace.limit - moo->heap->curspace.ptr)
);
ptr = (moo_uint8_t*)moo_allocheapspace(moo, &moo->heap->curspace, size);
/* TODO: grow heap if ptr is still null. */
}
return ptr; return ptr;
}
} }
moo_oop_t moo_allocoopobj (moo_t* moo, moo_oow_t size) moo_oop_t moo_allocoopobj (moo_t* moo, moo_oow_t size)
@ -69,7 +76,7 @@ moo_oop_t moo_allocoopobj (moo_t* moo, moo_oow_t size)
hdr = (moo_oop_oop_t)moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t) + nbytes_aligned); hdr = (moo_oop_oop_t)moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t) + nbytes_aligned);
if (!hdr) return MOO_NULL; if (!hdr) return MOO_NULL;
hdr->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 0, 0, 0, 0); hdr->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 0, moo->igniting, 0, 0, 0);
MOO_OBJ_SET_SIZE (hdr, size); MOO_OBJ_SET_SIZE (hdr, size);
MOO_OBJ_SET_CLASS (hdr, moo->_nil); MOO_OBJ_SET_CLASS (hdr, moo->_nil);
@ -91,7 +98,7 @@ moo_oop_t moo_allocoopobjwithtrailer (moo_t* moo, moo_oow_t size, const moo_oob_
hdr = (moo_oop_oop_t)moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t) + nbytes_aligned); hdr = (moo_oop_oop_t)moo_allocbytes(moo, MOO_SIZEOF(moo_obj_t) + nbytes_aligned);
if (!hdr) return MOO_NULL; if (!hdr) return MOO_NULL;
hdr->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 0, 0, 0, 1); hdr->_flags = MOO_OBJ_MAKE_FLAGS(MOO_OBJ_TYPE_OOP, MOO_SIZEOF(moo_oop_t), 0, 0, moo->igniting, 0, 0, 1);
MOO_OBJ_SET_SIZE (hdr, size); MOO_OBJ_SET_SIZE (hdr, size);
MOO_OBJ_SET_CLASS (hdr, moo->_nil); MOO_OBJ_SET_CLASS (hdr, moo->_nil);
@ -141,7 +148,7 @@ static MOO_INLINE moo_oop_t alloc_numeric_array (moo_t* moo, const void* ptr, mo
if (!hdr) return MOO_NULL; if (!hdr) return MOO_NULL;
} }
hdr->_flags = MOO_OBJ_MAKE_FLAGS(type, unit, extra, 0, 0, ngc, 0); hdr->_flags = MOO_OBJ_MAKE_FLAGS(type, unit, extra, 0, moo->igniting, 0, ngc, 0); /* TODO: review. ngc and perm flags seems to conflict with each other ... the diff is that ngc is malloc() and perm is allocated in the perm heap */
hdr->_size = len; hdr->_size = len;
MOO_OBJ_SET_SIZE (hdr, len); MOO_OBJ_SET_SIZE (hdr, len);
MOO_OBJ_SET_CLASS (hdr, moo->_nil); MOO_OBJ_SET_CLASS (hdr, moo->_nil);