klisp

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

commit 0ee171b358bf4a71b87bd085aab2ff672e944888
parent 7cc0ec2ea6faedd07a0463bad929e9c1b81fe41c
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Tue, 15 Mar 2011 17:25:19 -0300

Moved the shrink/grow of token buffer to the outside of the inlined function. Shave an additional 5k from the binary.

Diffstat:
Msrc/kstate.c | 32++++++++++++++++++++++++++++++++
Msrc/kstate.h | 28+++++++++-------------------
2 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/src/kstate.c b/src/kstate.c @@ -157,6 +157,38 @@ void ks_sshrink(klisp_State *K, int32_t new_top) } +/* TODO test this */ +void ks_tbgrow(klisp_State *K, int32_t new_top) +{ + size_t old_size = ks_tbsize(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_tbuf(K) = klispM_realloc_(K, ks_tbuf(K), old_size*sizeof(TValue), + new_size*sizeof(TValue)); + ks_tbsize(K) = new_size; +} + +void ks_tbshrink(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_tbsize(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_tbuf(K) = klispM_realloc_(K, ks_tbuf(K), old_size*sizeof(TValue), + new_size*sizeof(TValue)); + ks_tbsize(K) = new_size; +} + + /* ** ** This is for handling interceptions diff --git a/src/kstate.h b/src/kstate.h @@ -182,6 +182,9 @@ inline bool ks_sisempty(klisp_State *K) ** Tokenizer char buffer functions */ +void ks_tbshrink(klisp_State *K, int32_t new_top); +void ks_tbgrow(klisp_State *K, int32_t new_top); + inline void ks_tbadd(klisp_State *K, char ch); #define ks_tbpush(K_, ch_) (ks_tbadd((K_), (ch_))) inline char ks_tbget(klisp_State *K); @@ -201,13 +204,8 @@ inline bool ks_tbisempty(klisp_State *K); inline void ks_tbadd(klisp_State *K, char ch) { - if (ks_tbidx(K) == ks_tbsize(K)) { - size_t old_size = ks_tbsize(K); - size_t new_size = old_size * 2; - ks_tbuf(K) = klispM_realloc_(K, ks_tbuf(K), old_size, - new_size); - ks_tbsize(K) = new_size; - } + if (ks_tbidx(K) == ks_tbsize(K)) + ks_tbgrow(K, ks_tbidx(K)+1); ks_tbelem(K, ks_tbidx(K)) = ch; ++ks_tbidx(K); } @@ -219,13 +217,8 @@ inline char ks_tbget(klisp_State *K) inline char ks_tbpop(klisp_State *K) { - if (ks_tbsize(K) != KS_ITBSIZE && ks_tbidx(K) < (ks_tbsize(K) / 4)) { - /* NOTE: shrink can't fail */ - size_t old_size = ks_tbsize(K); - size_t new_size = old_size / 2; - ks_tbuf(K) = klispM_realloc_(K, ks_tbuf(K), old_size, new_size); - ks_tbsize(K) = new_size; - } + if (ks_tbsize(K) != KS_ITBSIZE && ks_tbidx(K)-1 < (ks_tbsize(K) / 4)) + ks_tbshrink(K, ks_tbidx(K)-1); char ch = ks_tbelem(K, ks_tbidx(K) - 1); --ks_tbidx(K); return ch; @@ -239,11 +232,8 @@ inline char *ks_tbget_buffer(klisp_State *K) inline void ks_tbclear(klisp_State *K) { - if (ks_tbsize(K) != KS_ITBSIZE) { - ks_tbuf(K) = klispM_realloc_(K, ks_tbuf(K), ks_tbsize(K), - KS_ITBSIZE); - ks_tbsize(K) = KS_ITBSIZE; - } + if (ks_tbsize(K) != KS_ITBSIZE) + ks_tbshrink(K, 0); ks_tbidx(K) = 0; }