From 17b0dfcee3744a1d5800655c204076fe36262761 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 2 Aug 2025 14:50:38 +0900 Subject: [PATCH] minor code refactoring --- bin/cut.c | 3 +- bin/hawk.c | 34 ++++++------------- lib/Std.cpp | 94 ++++++++++++++++++++++++++--------------------------- lib/xma.c | 34 ++++++++++++++----- 4 files changed, 84 insertions(+), 81 deletions(-) diff --git a/bin/cut.c b/bin/cut.c index 306663d7..8f3c4cb9 100644 --- a/bin/cut.c +++ b/bin/cut.c @@ -466,8 +466,9 @@ static void stop_run (int signo) #endif } -static void do_nothing (int unucut) +static void do_nothing (int unused) { + /* do nothing */ } static void set_intr_run (void) diff --git a/bin/hawk.c b/bin/hawk.c index 72e7d626..82d67482 100644 --- a/bin/hawk.c +++ b/bin/hawk.c @@ -105,7 +105,6 @@ struct xarg_t struct arg_t { - int incl_conv; hawk_parsestd_t* psin; /* input source streams */ hawk_bch_t* osf; /* output source file */ xarg_t icf; /* input console files */ @@ -377,7 +376,7 @@ static int add_gvs_to_hawk (hawk_t* hawk, arg_t* arg) return 0; } -static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, arg_t* arg) +static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, const arg_t* arg) { hawk_oow_t i; @@ -399,8 +398,6 @@ static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, arg_t* arg) { /* set the value of user-defined global variables * to a runtime context */ - hawk_oow_t i; - for (i = 0; i < arg->gvm.size; i++) { hawk_val_t* v; @@ -579,8 +576,8 @@ static void purge_xarg (xarg_t* xarg) { hawk_oow_t i; - for (i = 0; i < xarg->size; i++) free (xarg->ptr[i]); - free (xarg->ptr); + for (i = 0; i < xarg->size; i++) free(xarg->ptr[i]); + free(xarg->ptr); xarg->size = 0; xarg->capa = 0; @@ -825,8 +822,6 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar case '\0': { /* a long option with no corresponding short option */ - hawk_oow_t i; - if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) { arg->script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); @@ -946,29 +941,20 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar return 1; oops: - if (arg->gvm.ptr) free (arg->gvm.ptr); - purge_xarg (&arg->icf); - purge_xarg (&arg->ocf); - if (isf) - { - if (arg->incl_conv) free (isf[0].u.bcs.ptr); - free (isf); - } + if (arg->gvm.ptr) free(arg->gvm.ptr); + purge_xarg(&arg->icf); + purge_xarg(&arg->ocf); + if (isf) free(isf); return oops_ret; } static void freearg (struct arg_t* arg) { - if (arg->psin) - { - if (arg->incl_conv) free (arg->psin[0].u.bcs.ptr); - free (arg->psin); - } - - /*if (arg->osf != HAWK_NULL) free (arg->osf);*/ + if (arg->psin) free(arg->psin); + /*if (arg->osf) free(arg->osf);*/ purge_xarg (&arg->icf); purge_xarg (&arg->ocf); - if (arg->gvm.ptr) free (arg->gvm.ptr); + if (arg->gvm.ptr) free(arg->gvm.ptr); } static void print_hawk_error (hawk_t* hawk) diff --git a/lib/Std.cpp b/lib/Std.cpp index 48c0af40..25f5cfc3 100644 --- a/lib/Std.cpp +++ b/lib/Std.cpp @@ -89,13 +89,13 @@ static hawk_sio_t* open_sio (Hawk* hawk, HawkStd::Run* run, const hawk_ooch_t* f { const hawk_ooch_t* bem = hawk_rtx_backuperrmsg((hawk_rtx_t*)*run); //run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem); - hawk_rtx_seterrfmt ((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem); + hawk_rtx_seterrfmt((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem); } else { const hawk_ooch_t* bem = hawk_backuperrmsg((hawk_t*)*hawk); //hawk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem); - hawk_seterrfmt ((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem); + hawk_seterrfmt((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem); } } return sio; @@ -119,13 +119,13 @@ static hawk_sio_t* open_sio_std (Hawk* hawk, HawkStd::Run* run, hawk_sio_std_t s { const hawk_ooch_t* bem = hawk_rtx_backuperrmsg((hawk_rtx_t*)*run); //run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem); - hawk_rtx_seterrfmt ((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem); + hawk_rtx_seterrfmt((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem); } else { const hawk_ooch_t* bem = hawk_backuperrmsg((hawk_t*)*hawk); //hawk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem); - hawk_seterrfmt ((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem); + hawk_seterrfmt((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem); } } return sio; @@ -294,8 +294,8 @@ int HawkStd::build_environ (Run* run, env_char_t* envarr[]) vptr = hawk_rtx_dupbtoucstr(rtx, eq + 1, HAWK_NULL, 1); if (kptr == HAWK_NULL || vptr == HAWK_NULL) { - if (kptr) hawk_rtx_freemem (rtx, kptr); - if (vptr) hawk_rtx_freemem (rtx, kptr); + if (kptr) hawk_rtx_freemem(rtx, kptr); + if (vptr) hawk_rtx_freemem(rtx, kptr); /* mbstowcsdup() may fail for invalid encoding. * so setting the error code to ENOMEM may not @@ -319,8 +319,8 @@ int HawkStd::build_environ (Run* run, env_char_t* envarr[]) (defined(HAWK_STD_ENV_CHAR_IS_UCH) && defined(HAWK_OOCH_IS_UCH))) /* nothing to do */ #else - if (vptr) hawk_rtx_freemem (rtx, vptr); - if (kptr) hawk_rtx_freemem (rtx, kptr); + if (vptr) hawk_rtx_freemem(rtx, vptr); + if (kptr) hawk_rtx_freemem(rtx, kptr); #endif this->setError (HAWK_ENOMEM); @@ -337,8 +337,8 @@ int HawkStd::build_environ (Run* run, env_char_t* envarr[]) (defined(HAWK_STD_ENV_CHAR_IS_UCH) && defined(HAWK_OOCH_IS_UCH))) /* nothing to do */ #else - if (vptr) hawk_rtx_freemem (rtx, vptr); - if (kptr) hawk_rtx_freemem (rtx, kptr); + if (vptr) hawk_rtx_freemem(rtx, vptr); + if (kptr) hawk_rtx_freemem(rtx, kptr); #endif } } @@ -357,7 +357,7 @@ int HawkStd::make_additional_globals (Run* run) hawk_cmgr_t* HawkStd::getiocmgr (const hawk_ooch_t* ioname) { - HAWK_ASSERT (this->cmgrtab_inited == true); + HAWK_ASSERT(this->cmgrtab_inited == true); #if defined(HAWK_OOCH_IS_UCH) ioattr_t* ioattr = get_ioattr(ioname, hawk_count_oocstr(ioname)); @@ -410,7 +410,7 @@ int HawkStd::setioattr ( Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len) { - HAWK_ASSERT (this->cmgrtab_inited == true); + HAWK_ASSERT(this->cmgrtab_inited == true); hawk_oow_t l[3]; const hawk_ooch_t* ptr[3]; @@ -485,7 +485,7 @@ int HawkStd::getioattr ( Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len) { - HAWK_ASSERT (this->cmgrtab_inited == true); + HAWK_ASSERT(this->cmgrtab_inited == true); hawk_oow_t l[2]; const hawk_ooch_t* ptr[2]; @@ -549,7 +549,7 @@ int HawkStd::open_nwio (Pipe& io, int flags, void* nwad) #if defined(HAWK_OOCH_IS_UCH) hawk_cmgr_t* cmgr = this->getiocmgr(io.getName()); - if (cmgr) hawk_nwio_setcmgr (handle, cmgr); + if (cmgr) hawk_nwio_setcmgr(handle, cmgr); #endif io.setHandle ((void*)handle); @@ -588,9 +588,9 @@ int HawkStd::open_pio (Pipe& io) hawk_cmgr_t* cmgr = this->getiocmgr(io.getName()); if (cmgr) { - hawk_pio_setcmgr (pio, HAWK_PIO_IN, cmgr); - hawk_pio_setcmgr (pio, HAWK_PIO_OUT, cmgr); - hawk_pio_setcmgr (pio, HAWK_PIO_ERR, cmgr); + hawk_pio_setcmgr(pio, HAWK_PIO_IN, cmgr); + hawk_pio_setcmgr(pio, HAWK_PIO_OUT, cmgr); + hawk_pio_setcmgr(pio, HAWK_PIO_ERR, cmgr); } #endif io.setHandle (pio); @@ -772,7 +772,7 @@ int HawkStd::openFile (File& io) if (!sio) return -1; #if defined(HAWK_OOCH_IS_UCH) hawk_cmgr_t* cmgr = this->getiocmgr(ioname); - if (cmgr) hawk_sio_setcmgr (sio, cmgr); + if (cmgr) hawk_sio_setcmgr(sio, cmgr); #endif io.setHandle (sio); @@ -822,7 +822,7 @@ const hawk_cmgr_t* HawkStd::getConsoleCmgr () const int HawkStd::addConsoleOutput (const hawk_uch_t* arg, hawk_oow_t len) { - HAWK_ASSERT (this->hawk != HAWK_NULL); + HAWK_ASSERT(this->hawk != HAWK_NULL); int n = this->ofile.add(this->hawk, arg, len); if (n <= -1) this->setError (HAWK_ENOMEM); return n; @@ -835,7 +835,7 @@ int HawkStd::addConsoleOutput (const hawk_uch_t* arg) int HawkStd::addConsoleOutput (const hawk_bch_t* arg, hawk_oow_t len) { - HAWK_ASSERT (this->hawk != HAWK_NULL); + HAWK_ASSERT(this->hawk != HAWK_NULL); int n = this->ofile.add(this->hawk, arg, len); if (n <= -1) this->setError (HAWK_ENOMEM); return n; @@ -876,7 +876,7 @@ static int check_var_assign (hawk_rtx_t* rtx, const hawk_ooch_t* str) n = 0; } - hawk_rtx_freemem (rtx, dstr); + hawk_rtx_freemem(rtx, dstr); return n; } @@ -895,7 +895,7 @@ int HawkStd::open_console_in (Console& io) int x; v_argc = hawk_rtx_getgbl(rtx, this->gbl_argc); - HAWK_ASSERT (v_argc != HAWK_NULL); + HAWK_ASSERT(v_argc != HAWK_NULL); if (hawk_rtx_valtoint(rtx, v_argc, &i_argc) <= -1) return -1; /* handle special case when ARGV[x] has been altered. @@ -906,18 +906,18 @@ int HawkStd::open_console_in (Console& io) * { print $0; }' file1 file2 */ v_argv = hawk_rtx_getgbl(rtx, this->gbl_argv); - HAWK_ASSERT (v_argv != HAWK_NULL); + HAWK_ASSERT(v_argv != HAWK_NULL); if (HAWK_RTX_GETVALTYPE(rtx, v_argv) != HAWK_VAL_MAP) { /* with flexmap on, you can change ARGV to a scalar. * BEGIN { ARGV="xxx"; } * you must not do this. */ - hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_EINVAL, HAWK_T("phony value in ARGV")); + hawk_rtx_seterrfmt(rtx, HAWK_NULL, HAWK_EINVAL, HAWK_T("phony value in ARGV")); return -1; } map = ((hawk_val_map_t*)v_argv)->map; - HAWK_ASSERT (map != HAWK_NULL); + HAWK_ASSERT(map != HAWK_NULL); nextfile: if ((hawk_int_t)this->runarg_index >= (i_argc - 1)) /* ARGV is a kind of 0-based array unlike other normal arrays or substring indexing scheme */ @@ -931,7 +931,7 @@ nextfile: sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); if (sio == HAWK_NULL) return -1; - if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr); + if (this->console_cmgr) hawk_sio_setcmgr(sio, this->console_cmgr); io.setHandle (sio); this->runarg_count++; @@ -950,7 +950,7 @@ nextfile: } v_pair = (hawk_val_t*)HAWK_HTB_VPTR(pair); - HAWK_ASSERT (v_pair != HAWK_NULL); + HAWK_ASSERT(v_pair != HAWK_NULL); as.ptr = hawk_rtx_getvaloocstr(rtx, v_pair, &as.len); if (HAWK_UNLIKELY(!as.ptr)) return -1; @@ -1010,7 +1010,7 @@ nextfile: hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr); - if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr); + if (this->console_cmgr) hawk_sio_setcmgr(sio, this->console_cmgr); io.setHandle (sio); @@ -1026,7 +1026,7 @@ int HawkStd::open_console_out (Console& io) if (this->ofile.ptr == HAWK_NULL) { - HAWK_ASSERT (this->ofile.len == 0 && this->ofile.capa == 0); + HAWK_ASSERT(this->ofile.len == 0 && this->ofile.capa == 0); if (this->ofile_count == 0) { @@ -1036,7 +1036,7 @@ int HawkStd::open_console_out (Console& io) HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK); if (sio == HAWK_NULL) return -1; - if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr); + if (this->console_cmgr) hawk_sio_setcmgr(sio, this->console_cmgr); io.setHandle (sio); this->ofile_count++; @@ -1078,7 +1078,7 @@ int HawkStd::open_console_out (Console& io) return -1; } - if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr); + if (this->console_cmgr) hawk_sio_setcmgr(sio, this->console_cmgr); io.setHandle (sio); this->ofile_index++; @@ -1099,7 +1099,7 @@ int HawkStd::openConsole (Console& io) } else { - HAWK_ASSERT (mode == Console::WRITE); + HAWK_ASSERT(mode == Console::WRITE); this->ofile_count = 0; this->ofile_index = 0; @@ -1283,7 +1283,7 @@ int HawkStd::SourceFile::open (Data& io) if (sio == HAWK_NULL) return -1; } - if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr); + if (this->cmgr) hawk_sio_setcmgr(sio, this->cmgr); io.setName (xpath); io.setPath (xpath); // let the parser use this path, especially upon an error } @@ -1294,7 +1294,7 @@ int HawkStd::SourceFile::open (Data& io) hawk_ooch_t* xpath; ioname = io.getName(); - HAWK_ASSERT (ioname != HAWK_NULL); + HAWK_ASSERT(ioname != HAWK_NULL); if (io.getPrevHandle()) { @@ -1336,7 +1336,7 @@ int HawkStd::SourceFile::open (Data& io) } xpath = hawk_addsionamewithoochars((hawk_t*)io, path, hawk_count_oocstr(path)); - if (dbuf) hawk_freemem ((hawk_t*)io, dbuf); + if (dbuf) hawk_freemem((hawk_t*)io, dbuf); } else { @@ -1364,7 +1364,7 @@ int HawkStd::SourceFile::open (Data& io) io.setPath (xpath); io.setHandle (sio); - if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr); + if (this->cmgr) hawk_sio_setcmgr(sio, this->cmgr); } io.setHandle (sio); @@ -1391,8 +1391,8 @@ hawk_ooi_t HawkStd::SourceFile::write (Data& io, const hawk_ooch_t* buf, hawk_oo HawkStd::SourceString::~SourceString () { - HAWK_ASSERT (this->_hawk == HAWK_NULL); - HAWK_ASSERT (this->str == HAWK_NULL); + HAWK_ASSERT(this->_hawk == HAWK_NULL); + HAWK_ASSERT(this->str == HAWK_NULL); } int HawkStd::SourceString::open (Data& io) @@ -1422,7 +1422,7 @@ int HawkStd::SourceString::open (Data& io) } else { - HAWK_ASSERT (this->_type == STR_BCH); + HAWK_ASSERT(this->_type == STR_BCH); #if defined(HAWK_OOCH_IS_UCH) this->str = hawk_dupbtoucstr(this->_hawk, (const hawk_bch_t*)this->_str, HAWK_NULL, 0); #else @@ -1445,7 +1445,7 @@ int HawkStd::SourceString::open (Data& io) hawk_ooch_t* xpath; ioname = io.getName(); - HAWK_ASSERT (ioname != HAWK_NULL); + HAWK_ASSERT(ioname != HAWK_NULL); if (io.getPrevHandle()) { @@ -1479,7 +1479,7 @@ int HawkStd::SourceString::open (Data& io) } } xpath = hawk_addsionamewithoochars((hawk_t*)io, path, hawk_count_oocstr(path)); - if (dbuf) hawk_freemem ((hawk_t*)io, dbuf); + if (dbuf) hawk_freemem((hawk_t*)io, dbuf); } else { @@ -1495,9 +1495,9 @@ int HawkStd::SourceString::open (Data& io) ); if (!sio) return -1; - io.setPath (xpath); - io.setHandle (sio); - if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr); + io.setPath(xpath); + io.setHandle(sio); + if (this->cmgr) hawk_sio_setcmgr(sio, this->cmgr); } return 1; @@ -1510,9 +1510,9 @@ int HawkStd::SourceString::close (Data& io) // free the resources and nullify this->_hawk in particular // to prevent this object from outliving the hawk instance pointed to // by this->_hawk. - HAWK_ASSERT (this->_hawk != HAWK_NULL); - HAWK_ASSERT (this->str != HAWK_NULL); - hawk_freemem (this->_hawk, this->str); + HAWK_ASSERT(this->_hawk != HAWK_NULL); + HAWK_ASSERT(this->str != HAWK_NULL); + hawk_freemem(this->_hawk, this->str); this->str = HAWK_NULL; this->ptr = HAWK_NULL; this->_hawk = HAWK_NULL; diff --git a/lib/xma.c b/lib/xma.c index 39f68ddc..98030ef6 100644 --- a/lib/xma.c +++ b/lib/xma.c @@ -167,28 +167,28 @@ static HAWK_INLINE hawk_oow_t szlog2 (hawk_oow_t n) #endif #if HAWK_SIZEOF_OOW_T >= 64 - if ((n & (~(hawk_oow_t)0 << (BITS-128))) == 0) { x -= 256; n <<= 256; } + if ((n & (~(hawk_oow_t)0 << (BITS - 128))) == 0) { x -= 256; n <<= 256; } #endif #if HAWK_SIZEOF_OOW_T >= 32 - if ((n & (~(hawk_oow_t)0 << (BITS-128))) == 0) { x -= 128; n <<= 128; } + if ((n & (~(hawk_oow_t)0 << (BITS - 128))) == 0) { x -= 128; n <<= 128; } #endif #if HAWK_SIZEOF_OOW_T >= 16 - if ((n & (~(hawk_oow_t)0 << (BITS-64))) == 0) { x -= 64; n <<= 64; } + if ((n & (~(hawk_oow_t)0 << (BITS - 64))) == 0) { x -= 64; n <<= 64; } #endif #if HAWK_SIZEOF_OOW_T >= 8 - if ((n & (~(hawk_oow_t)0 << (BITS-32))) == 0) { x -= 32; n <<= 32; } + if ((n & (~(hawk_oow_t)0 << (BITS - 32))) == 0) { x -= 32; n <<= 32; } #endif #if HAWK_SIZEOF_OOW_T >= 4 - if ((n & (~(hawk_oow_t)0 << (BITS-16))) == 0) { x -= 16; n <<= 16; } + if ((n & (~(hawk_oow_t)0 << (BITS - 16))) == 0) { x -= 16; n <<= 16; } #endif #if HAWK_SIZEOF_OOW_T >= 2 - if ((n & (~(hawk_oow_t)0 << (BITS-8))) == 0) { x -= 8; n <<= 8; } + if ((n & (~(hawk_oow_t)0 << (BITS - 8))) == 0) { x -= 8; n <<= 8; } #endif #if HAWK_SIZEOF_OOW_T >= 1 - if ((n & (~(hawk_oow_t)0 << (BITS-4))) == 0) { x -= 4; n <<= 4; } + if ((n & (~(hawk_oow_t)0 << (BITS - 4))) == 0) { x -= 4; n <<= 4; } #endif - if ((n & (~(hawk_oow_t)0 << (BITS-2))) == 0) { x -= 2; n <<= 2; } - if ((n & (~(hawk_oow_t)0 << (BITS-1))) == 0) { x -= 1; } + if ((n & (~(hawk_oow_t)0 << (BITS - 2))) == 0) { x -= 2; n <<= 2; } + if ((n & (~(hawk_oow_t)0 << (BITS - 1))) == 0) { x -= 1; } return x; #undef BITS @@ -196,8 +196,24 @@ static HAWK_INLINE hawk_oow_t szlog2 (hawk_oow_t n) static HAWK_INLINE hawk_oow_t getxfi (hawk_xma_t* xma, hawk_oow_t size) { + /* if the aligned size is below the first threshold(FIXED) + * the slot to the xma->xfree field is the aligned size itself. + * For example, assuming ALIGN is 8, size 117 produces xfi 13. ((117 / 8) - 1) */ hawk_oow_t xfi = ((size) / ALIGN) - 1; + + /* If the caculated index is greater than or equal to the threshold, + * each slot in the xfree field will span across a wider range of sizes. + * Roughly, like this. but all these values are dependent on ALIGN. + * Words-1 Size XFI + * 32~62 264~511 => 32 + * 63~126 512~1023 => 33 + * 127~254 1024~2047 => 34 + * 255~510 2048~4095 => 35 + * .... + */ if (xfi >= FIXED) xfi = szlog2(size) - (xma)->bdec + FIXED; + + /* The very large size is place at the last index */ if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma); return xfi; }