klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit 7cc0ec2ea6faedd07a0463bad929e9c1b81fe41c
parent 8fa3393714a288ee551677ffc56f3283012a2ec3
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Tue, 15 Mar 2011 17:18:53 -0300

Moved stack grow/shrink outside of the inline functions. Shaved off 14k from executable.

Diffstat:
Msrc/kstate.c | 36++++++++++++++++++++++++++++++++++++
Msrc/kstate.h | 46+++++++++++-----------------------------------
2 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/src/kstate.c b/src/kstate.c @@ -122,6 +122,42 @@ klisp_State *klisp_newstate (klisp_Alloc f, void *ud) { } /* +** Stacks memory management +*/ + +/* TODO test this */ +void ks_sgrow(klisp_State *K, int32_t new_top) +{ + size_t old_size = ks_ssize(K); + /* should be powers of two multiple of KS_ISIZE */ + /* TEMP: do it naively for now */ + size_t new_size = old_size * 2; + while(new_top > new_size) + new_size *= 2; + + ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), + new_size*sizeof(TValue)); + ks_ssize(K) = new_size; +} + +void ks_sshrink(klisp_State *K, int32_t new_top) +{ + /* NOTE: may shrink more than once, take it to a multiple of + KS_ISSIZE that is a power of 2 and no smaller than (size * 4) */ + size_t old_size = ks_ssize(K); + /* TEMP: do it naively for now */ + size_t new_size = old_size; + while(new_size > KS_ISSIZE && new_top * 4 < new_size) + new_size /= 2; + + /* NOTE: shrink can't fail */ + ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), + new_size*sizeof(TValue)); + ks_ssize(K) = new_size; +} + + +/* ** ** This is for handling interceptions ** TODO: move to a different file diff --git a/src/kstate.h b/src/kstate.h @@ -115,8 +115,9 @@ struct klisp_State { /* ** Stack functions */ -/* TODO: move grow/shrink to non inline functions to diminish the - executable size */ + +void ks_sshrink(klisp_State *K, int32_t new_top); +void ks_sgrow(klisp_State *K, int32_t new_top); inline void ks_spush(klisp_State *K, TValue obj); inline TValue ks_spop(klisp_State *K); @@ -135,13 +136,8 @@ inline bool ks_sisempty(klisp_State *K); inline void ks_spush(klisp_State *K, TValue obj) { - if (ks_stop(K) == ks_ssize(K)) { - size_t old_size = ks_ssize(K); - size_t new_size = old_size * 2; - ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), - new_size*sizeof(TValue)); - ks_ssize(K) = new_size; - } + if (ks_stop(K) == ks_ssize(K)) + ks_sgrow(K, ks_stop(K)+1); ks_selem(K, ks_stop(K)) = obj; ++ks_stop(K); } @@ -149,14 +145,8 @@ inline void ks_spush(klisp_State *K, TValue obj) inline TValue ks_spop(klisp_State *K) { - if (ks_ssize(K) != KS_ISSIZE && ks_stop(K) < (ks_ssize(K) / 4)) { - /* NOTE: shrink can't fail */ - size_t old_size = ks_ssize(K); - size_t new_size = old_size / 2; - ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), - new_size*sizeof(TValue)); - ks_ssize(K) = new_size; - } + if (ks_ssize(K) != KS_ISSIZE && ks_stop(K)-1 < (ks_ssize(K) / 4)) + ks_sshrink(K, ks_stop(K)-1); TValue obj = ks_selem(K, ks_stop(K) - 1); --ks_stop(K); return obj; @@ -171,29 +161,15 @@ inline void ks_sdiscardn(klisp_State *K, int32_t n) { int32_t new_top = ks_stop(K) - n; ks_stop(K) = new_top; - if (ks_ssize(K) != KS_ISSIZE && new_top < (ks_ssize(K) / 4)) { - /* NOTE: may shrink more than once, take it to a multiple of - KS_ISSIZE that is no smaller than (new_top * 2) */ - size_t old_size = ks_ssize(K); - size_t new_size = new_top * 2; - new_size = new_top + KS_ISSIZE - (new_top % KS_ISSIZE); - /* NOTE: shrink can't fail */ - ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), - new_size*sizeof(TValue)); - ks_ssize(K) = new_size; - } + if (ks_ssize(K) != KS_ISSIZE && new_top < (ks_ssize(K) / 4)) + ks_sshrink(K, new_top); return; } inline void ks_sclear(klisp_State *K) { - if (ks_ssize(K) != KS_ISSIZE) { - size_t old_size = ks_ssize(K); - size_t new_size = KS_ISSIZE; - ks_sbuf(K) = klispM_realloc_(K, ks_sbuf(K), old_size*sizeof(TValue), - new_size*sizeof(TValue)); - ks_ssize(K) = new_size; - } + if (ks_ssize(K) != KS_ISSIZE) + ks_sshrink(K, 0); ks_stop(K) = 0; }