From: Kaz Wesley Date: Sun, 28 Jan 2018 03:48:13 +0000 (-0800) Subject: Change pointers for heap allocation X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=3eadff410d2d08246d6f195ef209ed9d88c99be5;p=muddle-interpreter.git Change pointers for heap allocation Signed-off-by: Kaz Wesley --- diff --git a/src/alloc.c b/src/alloc.c index 413490c..ad55630 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -22,6 +22,9 @@ License along with this file. If not, see extern pool_object *pool; extern pool_ptr ptop; +extern object *vhp_base; +extern heap_ptr vhp; + pool_ptr pool_alloc (uint32_t len) { @@ -55,13 +58,31 @@ pool_copy_array_rev (const pool_object * objs, uint32_t len) return p; } +object * +HEAP_OBJECT (heap_ptr p) +{ + assert (p > 0); + return &vhp_base[p]; +} + +heap_ptr +heap_alloc (uint32_t len) +{ + enum + { DOPE_LEN = 1 }; + heap_ptr p = vhp; + vhp += len + DOPE_LEN; + return p; +} + heap_ptr heap_copy_array_rev (const object * objs, uint32_t len) { - object *xs = heap_alloc (len); + heap_ptr p = heap_alloc (len); + object *xs = HEAP_OBJECT (p); for (int i = 0; i < (int) len; i++) { xs[i] = objs[len - 1 - (unsigned) i]; } - return HEAP_PTR_OF_OBJECT (xs); + return p; } diff --git a/src/alloc.h b/src/alloc.h index 4cab1d0..2c7e6a0 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -23,53 +23,21 @@ License along with this file. If not, see #include #include -/// 0, or a "pointer" to an object allocated in the pool typedef uint32_t pool_ptr; -/// 0, or a "pointer" to an object allocated in the heap typedef int32_t heap_ptr; typedef union pool_object pool_object; typedef union object object; -extern char *vhp_base; // object -extern char *vhp; // object - pool_object *POOL_OBJECT (pool_ptr p); - -// TODO make (heap_ptr)0 nullish -static inline object * -OBJECT_OF_HEAP_PTR (heap_ptr p) -{ - assert (p >= 0); - return (object *) (vhp_base + (p << 4)); -} +object *HEAP_OBJECT (heap_ptr p); pool_ptr pool_alloc (uint32_t len); - -static inline heap_ptr -HEAP_PTR_OF_OBJECT (object * p) -{ - assert ((uintptr_t) p >= (uintptr_t) vhp_base); - heap_ptr h = (heap_ptr) ((uintptr_t) p - (uintptr_t) vhp_base); - return h; -} - -static inline object * -heap_alloc (uint32_t len) -{ - enum - { DOPE_LEN = 1 }; - char *p = vhp; - vhp += (len + DOPE_LEN) << 4; - return (object *) p; -} +heap_ptr heap_alloc (uint32_t len); // given a headerless array of objects of known size, // copy it backwards into newly-allocated pool space -pool_ptr pool_copy_array_rev (const pool_object * objs, uint32_t len); - -// given a headerless array of objects of known size, -// copy it backwards into a newly-allocated vector body -heap_ptr heap_copy_array_rev (const object * objs, uint32_t len); +pool_ptr pool_copy_array_rev (const pool_object *objs, uint32_t len); +heap_ptr heap_copy_array_rev (const object *objs, uint32_t len); #endif // ALLOC_H diff --git a/src/main.c b/src/main.c index 6c6124e..0819cc9 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,8 @@ License along with this file. If not, see // TODO: put these in interpreter-wide ctx object pool_object *pool; pool_ptr ptop; -char *vhp_base; -char *vhp; +object *vhp_base; +heap_ptr vhp; // TODO: store these in current PROCESS frame *cf; @@ -71,7 +71,7 @@ main () vhp_base = mmap (0, VECTOR_OBJCT * sizeof (object), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - vhp = vhp_base; + vhp = 1; // Reader stack (TODO: dynamically allocate as VECTOR). object rst_base[READER_OBJCT]; @@ -85,7 +85,7 @@ main () { // mock GC (no object persistence) ptop = 1; - vhp = vhp_base; + vhp = 1; // terminate input assert (buf[n - 1] == '\n'); buf[n - 1] = '\0'; diff --git a/src/print.c b/src/print.c index 9cadda7..8f089a3 100644 --- a/src/print.c +++ b/src/print.c @@ -27,7 +27,7 @@ License along with this file. If not, see static void print_vector_body (const vector_object * o) { - const object *p = OBJECT_OF_HEAP_PTR (o->body); + const object *p = HEAP_OBJECT (o->body); if (!p) return; if (o->len)