From 87656f803e1ac0c1d98d842febd8b854a0cb829c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 3 Mar 2020 12:00:13 +0000 Subject: [PATCH] some code touch up --- hawk/lib/Hawk.cpp | 26 ++++++++++--------- hawk/lib/Hawk.hpp | 4 +-- hawk/lib/HawkStd.cpp | 4 +-- hawk/lib/arr.c | 10 ++++---- hawk/lib/fnc.c | 14 +++++----- hawk/lib/hawk.h | 47 +++++++++++++++++++++++++++------- hawk/lib/htb.c | 12 ++++----- hawk/lib/mod-str.c | 6 ++--- hawk/lib/mod-sys.c | 4 +-- hawk/lib/run.c | 49 ++++++++++++++++++++++++++++++----- hawk/lib/std.c | 8 +++--- hawk/lib/val.c | 61 +++++++++++++++++++++++++++++--------------- hawk/mod/mod-mysql.c | 2 +- 13 files changed, 166 insertions(+), 81 deletions(-) diff --git a/hawk/lib/Hawk.cpp b/hawk/lib/Hawk.cpp index 344d0b9d..6a3c0dc6 100644 --- a/hawk/lib/Hawk.cpp +++ b/hawk/lib/Hawk.cpp @@ -185,11 +185,11 @@ int Hawk::Console::setFileName (const hawk_ooch_t* name) { if (this->getMode() == READ) { - return hawk_rtx_setfilename(this->run->rtx, name, hawk_count_oocstr(name)); + return hawk_rtx_setfilenamewithoochars(this->run->rtx, name, hawk_count_oocstr(name)); } else { - return hawk_rtx_setofilename(this->run->rtx, name, hawk_count_oocstr(name)); + return hawk_rtx_setofilenamewithoochars(this->run->rtx, name, hawk_count_oocstr(name)); } } @@ -839,7 +839,7 @@ int Hawk::Value::setMbs (const hawk_bch_t* str) int Hawk::Value::setMbs (Run* r, const hawk_bch_t* str) { hawk_val_t* tmp; - tmp = hawk_rtx_makembsval(r->rtx, str, hawk_count_bcstr(str)); + tmp = hawk_rtx_makembsvalwithbchars(r->rtx, str, hawk_count_bcstr(str)); if (!tmp) { r->hawk->retrieveError (r); @@ -1101,7 +1101,7 @@ int Hawk::Value::setIndexedMbs (const Index& idx, const hawk_bch_t* str) int Hawk::Value::setIndexedMbs (Run* r, const Index& idx, const hawk_bch_t* str) { hawk_val_t* tmp; - tmp = hawk_rtx_makembsval(r->rtx, str, hawk_count_bcstr(str)); + tmp = hawk_rtx_makembsvalwithbchars(r->rtx, str, hawk_count_bcstr(str)); if (tmp == HAWK_NULL) { r->hawk->retrieveError (r); @@ -1274,7 +1274,7 @@ int Hawk::Run::setGlobal (int id, hawk_int_t v) HAWK_ASSERT (this->rtx != HAWK_NULL); hawk_val_t* tmp = hawk_rtx_makeintval (this->rtx, v); - if (tmp == HAWK_NULL) return -1; + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (this->rtx, tmp); int n = hawk_rtx_setgbl (this->rtx, id, tmp); @@ -1287,7 +1287,7 @@ int Hawk::Run::setGlobal (int id, hawk_flt_t v) HAWK_ASSERT (this->rtx != HAWK_NULL); hawk_val_t* tmp = hawk_rtx_makefltval (this->rtx, v); - if (tmp == HAWK_NULL) return -1; + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (this->rtx, tmp); int n = hawk_rtx_setgbl (this->rtx, id, tmp); @@ -1295,12 +1295,13 @@ int Hawk::Run::setGlobal (int id, hawk_flt_t v) return n; } -int Hawk::Run::setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len) +int Hawk::Run::setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len, bool mbs) { HAWK_ASSERT (this->rtx != HAWK_NULL); - hawk_val_t* tmp = hawk_rtx_makestrvalwithuchars(this->rtx, ptr, len); - if (tmp == HAWK_NULL) return -1; + hawk_val_t* tmp = mbs? hawk_rtx_makembsvalwithuchars(this->rtx, ptr, len): + hawk_rtx_makestrvalwithuchars(this->rtx, ptr, len); + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (this->rtx, tmp); int n = hawk_rtx_setgbl (this->rtx, id, tmp); @@ -1308,12 +1309,13 @@ int Hawk::Run::setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len) return n; } -int Hawk::Run::setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len) +int Hawk::Run::setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len, bool mbs) { HAWK_ASSERT (this->rtx != HAWK_NULL); - hawk_val_t* tmp = hawk_rtx_makestrvalwithbchars(this->rtx, ptr, len); - if (tmp == HAWK_NULL) return -1; + hawk_val_t* tmp = mbs? hawk_rtx_makembsvalwithbchars(this->rtx, ptr, len): + hawk_rtx_makestrvalwithbchars(this->rtx, ptr, len); + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (this->rtx, tmp); int n = hawk_rtx_setgbl (this->rtx, id, tmp); diff --git a/hawk/lib/Hawk.hpp b/hawk/lib/Hawk.hpp index 12d1d15c..67113ccd 100644 --- a/hawk/lib/Hawk.hpp +++ b/hawk/lib/Hawk.hpp @@ -1238,8 +1238,8 @@ public: /// \a ptr. /// \return 0 on success, -1 on failure /// - int setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len); - int setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len); + int setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len, bool mbs = false); + int setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len, bool mbs = false); /// /// The setGlobal() function sets a global variable diff --git a/hawk/lib/HawkStd.cpp b/hawk/lib/HawkStd.cpp index 2990ca8d..f4c5347b 100644 --- a/hawk/lib/HawkStd.cpp +++ b/hawk/lib/HawkStd.cpp @@ -958,7 +958,7 @@ int HawkStd::open_console_in (Console& io) return -1; } - if (hawk_rtx_setfilename(rtx, file, hawk_count_oocstr(file)) <= -1) + if (hawk_rtx_setfilenamewithoochars(rtx, file, hawk_count_oocstr(file)) <= -1) { hawk_sio_close (sio); hawk_rtx_freevaloocstr (rtx, v, as.ptr); @@ -1031,7 +1031,7 @@ int HawkStd::open_console_out (Console& io) sio = open_sio(HAWK_NULL, io, file, HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR); if (sio == HAWK_NULL) return -1; - if (hawk_rtx_setofilename(rtx, file, hawk_count_oocstr(file)) == -1) + if (hawk_rtx_setofilenamewithoochars(rtx, file, hawk_count_oocstr(file)) == -1) { hawk_sio_close (sio); return -1; diff --git a/hawk/lib/arr.c b/hawk/lib/arr.c index 3b8112b5..67989475 100644 --- a/hawk/lib/arr.c +++ b/hawk/lib/arr.c @@ -71,13 +71,13 @@ static HAWK_INLINE slot_t* alloc_slot (hawk_arr_t* arr, void* dptr, hawk_oow_t d if (arr->copier == HAWK_ARR_COPIER_SIMPLE) { n = (slot_t*)hawk_gem_allocmem(arr->gem, HAWK_SIZEOF(slot_t)); - if (!n) return HAWK_NULL; + if (HAWK_UNLIKELY(!n)) return HAWK_NULL; DPTR(n) = dptr; } else if (arr->copier == HAWK_ARR_COPIER_INLINE) { n = (slot_t*)hawk_gem_allocmem(arr->gem, HAWK_SIZEOF(slot_t) + TOB(arr,dlen)); - if (!n) return HAWK_NULL; + if (HAWK_UNLIKELY(!n)) return HAWK_NULL; HAWK_MEMCPY (n + 1, dptr, TOB(arr,dlen)); DPTR(n) = n + 1; @@ -85,7 +85,7 @@ static HAWK_INLINE slot_t* alloc_slot (hawk_arr_t* arr, void* dptr, hawk_oow_t d else { n = (slot_t*)hawk_gem_allocmem(arr->gem, HAWK_SIZEOF(slot_t)); - if (!n) return HAWK_NULL; + if (HAWK_UNLIKELY(!n)) return HAWK_NULL; DPTR(n) = arr->copier(arr, dptr, dlen); if (DPTR(n) == HAWK_NULL) { @@ -104,7 +104,7 @@ hawk_arr_t* hawk_arr_open (hawk_gem_t* gem, hawk_oow_t xtnsize, hawk_oow_t capa) hawk_arr_t* arr; arr = (hawk_arr_t*)hawk_gem_allocmem(gem, HAWK_SIZEOF(hawk_arr_t) + xtnsize); - if (arr == HAWK_NULL) return HAWK_NULL; + if (HAWK_UNLIKELY(!arr)) return HAWK_NULL; if (hawk_arr_init(arr, gem, capa) <= -1) { @@ -246,7 +246,7 @@ hawk_arr_t* hawk_arr_setcapa (hawk_arr_t* arr, hawk_oow_t capa) if (capa > 0) { tmp = (slot_t**)hawk_gem_reallocmem(arr->gem, arr->slot, HAWK_SIZEOF(*arr->slot) * capa); - if (!tmp) return HAWK_NULL; + if (HAWK_UNLIKELY(!tmp)) return HAWK_NULL; } else { diff --git a/hawk/lib/fnc.c b/hawk/lib/fnc.c index 18180a4f..025171c0 100644 --- a/hawk/lib/fnc.c +++ b/hawk/lib/fnc.c @@ -166,12 +166,12 @@ hawk_fnc_t* hawk_addfncwithbcstr (hawk_t* awk, const hawk_bch_t* name, const haw if (spec->arg.spec) { wcs.ptr = hawk_dupbtoucstr(awk, spec->arg.spec, &wcs.len, 0); - if (!wcs.ptr) return HAWK_NULL; + if (HAWK_UNLIKELY(!wcs.ptr)) return HAWK_NULL; wspec.arg.spec = wcs.ptr; } wcs.ptr = hawk_dupbtoucstr(awk, name, &wcs.len, 0); - if (!wcs.ptr) + if (HAWK_UNLIKELY(!wcs.ptr)) { if (wspec.arg.spec) hawk_freemem (awk, (hawk_uch_t*)wspec.arg.spec); return HAWK_NULL; @@ -197,12 +197,12 @@ hawk_fnc_t* hawk_addfncwithucstr (hawk_t* awk, const hawk_uch_t* name, const haw if (spec->arg.spec) { mbs.ptr = hawk_duputobcstr(awk, spec->arg.spec, &mbs.len); - if (!mbs.ptr) return HAWK_NULL; + if (HAWK_UNLIKELY(!mbs.ptr)) return HAWK_NULL; mspec.arg.spec = mbs.ptr; } mbs.ptr = hawk_duputobcstr(awk, name, &mbs.len); - if (!mbs.ptr) + if (HAWK_UNLIKELY(!mbs.ptr)) { if (mspec.arg.spec) hawk_freemem (awk, (hawk_bch_t*)mspec.arg.spec); return HAWK_NULL; @@ -317,7 +317,7 @@ hawk_fnc_t* hawk_findfncwithbcs (hawk_t* awk, const hawk_bcs_t* name) hawk_fnc_t* fnc; wcs.ptr = hawk_dupbtouchars(awk, name->ptr, name->len, &wcs.len, 0); - if (!wcs.ptr) return HAWK_NULL; + if (HAWK_UNLIKELY(!wcs.ptr)) return HAWK_NULL; fnc = find_fnc(awk, &wcs); hawk_freemem (awk, wcs.ptr); return fnc; @@ -331,7 +331,7 @@ hawk_fnc_t* hawk_findfncwithucs (hawk_t* awk, const hawk_ucs_t* name) hawk_fnc_t* fnc; mbs.ptr = hawk_duputobchars(awk, name->ptr, name->len, &mbs.len); - if (!mbs.ptr) return HAWK_NULL; + if (HAWK_UNLIKELY(!mbs.ptr)) return HAWK_NULL; fnc = find_fnc(awk, &mbs); hawk_freemem (awk, mbs.ptr); return fnc; @@ -774,7 +774,7 @@ int hawk_fnc_substr (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) if (lindex >= (hawk_int_t)len) lindex = (hawk_int_t)len; if (lcount > (hawk_int_t)len - lindex) lcount = (hawk_int_t)len - lindex; - r = hawk_rtx_makembsval(rtx, &str[lindex], (hawk_oow_t)lcount); + r = hawk_rtx_makembsvalwithbchars(rtx, &str[lindex], (hawk_oow_t)lcount); if (!r) return -1; } else diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index df46c7db..f516d7f2 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -2346,7 +2346,7 @@ HAWK_EXPORT hawk_val_t* hawk_rtx_getgbl ( */ HAWK_EXPORT int hawk_rtx_setgbl ( hawk_rtx_t* rtx, - int id, + int id, hawk_val_t* val ); @@ -2366,21 +2366,34 @@ HAWK_EXPORT void hawk_rtx_setretval ( /** * The hawk_rtx_setfilename() function sets FILENAME. */ -HAWK_EXPORT int hawk_rtx_setfilename ( - hawk_rtx_t* rtx, /**< runtime context */ - const hawk_ooch_t* str, /**< name pointer */ - hawk_oow_t len /**< name length */ +HAWK_EXPORT int hawk_rtx_setfilenamewithuchars ( + hawk_rtx_t* rtx, /**< runtime context */ + const hawk_uch_t* str, /**< name pointer */ + hawk_oow_t len /**< name length */ +); + +HAWK_EXPORT int hawk_rtx_setfilenamewithbchars ( + hawk_rtx_t* rtx, /**< runtime context */ + const hawk_bch_t* str, /**< name pointer */ + hawk_oow_t len /**< name length */ ); /** * The hawk_rtx_setofilename() function sets OFILENAME. */ -HAWK_EXPORT int hawk_rtx_setofilename ( +HAWK_EXPORT int hawk_rtx_setofilenamewithuchars ( hawk_rtx_t* rtx, /**< runtime context */ - const hawk_ooch_t* str, /**< name pointer */ + const hawk_uch_t* str, /**< name pointer */ hawk_oow_t len /**< name length */ ); +HAWK_EXPORT int hawk_rtx_setofilenamewithbchars ( + hawk_rtx_t* rtx, /**< runtime context */ + const hawk_bch_t* str, /**< name pointer */ + hawk_oow_t len /**< name length */ +); + + HAWK_EXPORT int hawk_rtx_setscriptnamewithuchars ( hawk_rtx_t* rtx, /**< runtime context */ const hawk_uch_t* str, /**< name pointer */ @@ -2394,8 +2407,12 @@ HAWK_EXPORT int hawk_rtx_setscriptnamewithbchars ( ); #if defined(HAWK_OOCH_IS_UCH) +# define hawk_rtx_setfilenamewithoochars hawk_rtx_setfilenamewithuchars +# define hawk_rtx_setofilenamewithoochars hawk_rtx_setofilenamewithuchars # define hawk_rtx_setscriptnamewithoochars hawk_rtx_setscriptnamewithuchars #else +# define hawk_rtx_setfilenamewithoochars hawk_rtx_setfilenamewithbchars +# define hawk_rtx_setofilenamewithoochars hawk_rtx_setofilenamewithbchars # define hawk_rtx_setscriptnamewithoochars hawk_rtx_setscriptnamewithbchars #endif @@ -2724,10 +2741,10 @@ HAWK_EXPORT hawk_val_t* hawk_rtx_makenstrvalwithbcs ( /* -------------------------------------------------------------------------- */ /** - * The hawk_rtx_makebytearrvaal() function create a byte array value. + * The hawk_rtx_makembsvalwithbchars() function create a byte array value. * \return value on success, #HAWK_NULL on failure */ -hawk_val_t* hawk_rtx_makembsval ( +hawk_val_t* hawk_rtx_makembsvalwithbchars ( hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len @@ -2738,6 +2755,18 @@ hawk_val_t* hawk_rtx_makembsvalwithbcs ( const hawk_bcs_t* bcs ); +hawk_val_t* hawk_rtx_makembsvalwithuchars ( + hawk_rtx_t* rtx, + const hawk_uch_t* ptr, + hawk_oow_t len +); + +hawk_val_t* hawk_rtx_makembsvalwithucs ( + hawk_rtx_t* rtx, + const hawk_ucs_t* ucs +); + + /* -------------------------------------------------------------------------- */ /** diff --git a/hawk/lib/htb.c b/hawk/lib/htb.c index 34eb70b4..c4337f9e 100644 --- a/hawk/lib/htb.c +++ b/hawk/lib/htb.c @@ -62,7 +62,7 @@ HAWK_INLINE pair_t* hawk_htb_allocpair (hawk_htb_t* htb, void* kptr, hawk_oow_t if (vcop == HAWK_HTB_COPIER_INLINE) as += VTOB(htb,vlen); n = (pair_t*) hawk_gem_allocmem(htb->gem, as); - if (n == HAWK_NULL) return HAWK_NULL; + if (HAWK_UNLIKELY(!n)) return HAWK_NULL; NEXT(n) = HAWK_NULL; @@ -160,7 +160,7 @@ static HAWK_INLINE pair_t* change_pair_val (hawk_htb_t* htb, pair_t* pair, void* { /* need to reconstruct the pair */ pair_t* p = hawk_htb_allocpair(htb, KPTR(pair), KLEN(pair), vptr, vlen); - if (p == HAWK_NULL) return HAWK_NULL; + if (HAWK_UNLIKELY(!p)) return HAWK_NULL; hawk_htb_freepair (htb, pair); return p; } @@ -168,7 +168,7 @@ static HAWK_INLINE pair_t* change_pair_val (hawk_htb_t* htb, pair_t* pair, void* else { void* nvptr = vcop(htb, vptr, vlen); - if (nvptr == HAWK_NULL) return HAWK_NULL; + if (HAWK_UNLIKELY(!nvptr)) return HAWK_NULL; VPTR(pair) = nvptr; VLEN(pair) = vlen; } @@ -260,7 +260,7 @@ hawk_htb_t* hawk_htb_open (hawk_gem_t* gem, hawk_oow_t xtnsize, hawk_oow_t capa, hawk_htb_t* htb; htb = (hawk_htb_t*)hawk_gem_allocmem(gem, HAWK_SIZEOF(hawk_htb_t) + xtnsize); - if (!htb) return HAWK_NULL; + if (HAWK_UNLIKELY(!htb)) return HAWK_NULL; if (hawk_htb_init(htb, gem, capa, factor, kscale, vscale) <= -1) { @@ -503,7 +503,7 @@ static HAWK_INLINE pair_t* insert (hawk_htb_t* htb, void* kptr, hawk_oow_t klen, HAWK_ASSERT (pair == HAWK_NULL); pair = hawk_htb_allocpair (htb, kptr, klen, vptr, vlen); - if (pair == HAWK_NULL) return HAWK_NULL; /* error */ + if (HAWK_UNLIKELY(!pair)) return HAWK_NULL; /* error */ NEXT(pair) = htb->bucket[hc]; htb->bucket[hc] = pair; @@ -584,7 +584,7 @@ pair_t* hawk_htb_cbsert (hawk_htb_t* htb, void* kptr, hawk_oow_t klen, cbserter_ HAWK_ASSERT (pair == HAWK_NULL); pair = cbserter(htb, HAWK_NULL, kptr, klen, ctx); - if (pair == HAWK_NULL) return HAWK_NULL; /* error */ + if (HAWK_UNLIKELY(!pair)) return HAWK_NULL; /* error */ NEXT(pair) = htb->bucket[hc]; htb->bucket[hc] = pair; diff --git a/hawk/lib/mod-str.c b/hawk/lib/mod-str.c index 5c59187a..c3825f9d 100644 --- a/hawk/lib/mod-str.c +++ b/hawk/lib/mod-str.c @@ -45,7 +45,7 @@ static int fnc_normspace (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) str0 = hawk_rtx_valtobcstrdup(rtx, a0, &len0); if (!str0) return -1; len0 = hawk_compact_bchars(str0, len0); - retv = hawk_rtx_makembsval(rtx, str0, len0); + retv = hawk_rtx_makembsvalwithbchars(rtx, str0, len0); hawk_rtx_freemem (rtx, str0); } else @@ -80,7 +80,7 @@ static int trim (hawk_rtx_t* rtx, int flags) path.ptr = ((hawk_val_mbs_t*)a0)->val.ptr; path.len = ((hawk_val_mbs_t*)a0)->val.len; npath = hawk_trim_bchars(path.ptr, &path.len, flags); - retv = hawk_rtx_makembsval(rtx, npath, path.len); + retv = hawk_rtx_makembsvalwithbchars(rtx, npath, path.len); } else { @@ -435,7 +435,7 @@ static int fnc_tombs (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) if (!cmgr) { /* if the encoding name is not known, return a zero-length string */ - r = hawk_rtx_makembsval(rtx, HAWK_NULL, 0); /* this never fails for length 0 */ + r = hawk_rtx_makembsvalwithbchars(rtx, HAWK_NULL, 0); /* this never fails for length 0 */ goto done; } } diff --git a/hawk/lib/mod-sys.c b/hawk/lib/mod-sys.c index a5e53829..863bb422 100644 --- a/hawk/lib/mod-sys.c +++ b/hawk/lib/mod-sys.c @@ -675,7 +675,7 @@ static int fnc_read (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_val_t* sv; int x; - sv = hawk_rtx_makembsval(rtx, sys_list->ctx.readbuf, rx); + sv = hawk_rtx_makembsvalwithbchars(rtx, sys_list->ctx.readbuf, rx); if (!sv) { rx = copy_error_to_sys_list(rtx, sys_list); @@ -3533,7 +3533,7 @@ static int fnc_recvfrom (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_val_t* sv; int x; - sv = hawk_rtx_makembsval(rtx, sys_list->ctx.readbuf, rx); + sv = hawk_rtx_makembsvalwithbchars(rtx, sys_list->ctx.readbuf, rx); if (!sv) { rx = copy_error_to_sys_list(rtx, sys_list); diff --git a/hawk/lib/run.c b/hawk/lib/run.c index aa7c5798..8763c069 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -663,13 +663,13 @@ int hawk_rtx_setscriptnamewithbchars (hawk_rtx_t* rtx, const hawk_bch_t* name, h return n; } -int hawk_rtx_setfilename (hawk_rtx_t* rtx, const hawk_ooch_t* name, hawk_oow_t len) +int hawk_rtx_setfilenamewithuchars (hawk_rtx_t* rtx, const hawk_uch_t* name, hawk_oow_t len) { hawk_val_t* tmp; int n; - tmp = hawk_rtx_makestrvalwithoochars(rtx, name, len); - if (tmp == HAWK_NULL) return -1; + tmp = hawk_rtx_makestrvalwithuchars(rtx, name, len); + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (rtx, tmp); n = hawk_rtx_setgbl (rtx, HAWK_GBL_FILENAME, tmp); @@ -678,15 +678,50 @@ int hawk_rtx_setfilename (hawk_rtx_t* rtx, const hawk_ooch_t* name, hawk_oow_t l return n; } -int hawk_rtx_setofilename (hawk_rtx_t* rtx, const hawk_ooch_t* name, hawk_oow_t len) +int hawk_rtx_setfilenamewithbchars (hawk_rtx_t* rtx, const hawk_bch_t* name, hawk_oow_t len) +{ + hawk_val_t* tmp; + int n; + + tmp = hawk_rtx_makestrvalwithbchars(rtx, name, len); + if (HAWK_UNLIKELY(!tmp)) return -1; + + hawk_rtx_refupval (rtx, tmp); + n = hawk_rtx_setgbl (rtx, HAWK_GBL_FILENAME, tmp); + hawk_rtx_refdownval (rtx, tmp); + + return n; +} + + +int hawk_rtx_setofilenamewithuchars (hawk_rtx_t* rtx, const hawk_uch_t* name, hawk_oow_t len) { hawk_val_t* tmp; int n; if (rtx->awk->opt.trait & HAWK_NEXTOFILE) { - tmp = hawk_rtx_makestrvalwithoochars(rtx, name, len); - if (tmp == HAWK_NULL) return -1; + tmp = hawk_rtx_makestrvalwithuchars(rtx, name, len); + if (HAWK_UNLIKELY(!tmp)) return -1; + + hawk_rtx_refupval (rtx, tmp); + n = hawk_rtx_setgbl (rtx, HAWK_GBL_OFILENAME, tmp); + hawk_rtx_refdownval (rtx, tmp); + } + else n = 0; + + return n; +} + +int hawk_rtx_setofilenamewithbchars (hawk_rtx_t* rtx, const hawk_bch_t* name, hawk_oow_t len) +{ + hawk_val_t* tmp; + int n; + + if (rtx->awk->opt.trait & HAWK_NEXTOFILE) + { + tmp = hawk_rtx_makestrvalwithbchars(rtx, name, len); + if (HAWK_UNLIKELY(!tmp)) return -1; hawk_rtx_refupval (rtx, tmp); n = hawk_rtx_setgbl (rtx, HAWK_GBL_OFILENAME, tmp); @@ -6480,7 +6515,7 @@ static hawk_val_t* eval_str (hawk_rtx_t* rtx, hawk_nde_t* nde) static hawk_val_t* eval_mbs (hawk_rtx_t* rtx, hawk_nde_t* nde) { hawk_val_t* val; - val = hawk_rtx_makembsval(rtx, ((hawk_nde_mbs_t*)nde)->ptr, ((hawk_nde_mbs_t*)nde)->len); + val = hawk_rtx_makembsvalwithbchars(rtx, ((hawk_nde_mbs_t*)nde)->ptr, ((hawk_nde_mbs_t*)nde)->len); if (val == HAWK_NULL) ADJERR_LOC (rtx, &nde->loc); return val; } diff --git a/hawk/lib/std.c b/hawk/lib/std.c index b15da5b3..c4342ba7 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -2119,7 +2119,7 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod) if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr); - if (hawk_rtx_setfilename(rtx, file, hawk_count_oocstr(file)) <= -1) + if (hawk_rtx_setfilenamewithoochars(rtx, file, hawk_count_oocstr(file)) <= -1) { hawk_sio_close (sio); hawk_rtx_freevaloocstr (rtx, v, as.ptr); @@ -2181,7 +2181,7 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod) if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr); - if (hawk_rtx_setofilename(rtx, file, hawk_count_oocstr(file)) <= -1) + if (hawk_rtx_setofilenamewithoochars(rtx, file, hawk_count_oocstr(file)) <= -1) { hawk_sio_close (sio); return -1; @@ -2623,7 +2623,7 @@ static hawk_rtx_t* open_rtx_std ( */ if (ocf && ocf[0]) { - if (hawk_rtx_setofilename(rtx, ocf[0], hawk_count_oocstr(ocf[0])) <= -1) + if (hawk_rtx_setofilenamewithoochars(rtx, ocf[0], hawk_count_oocstr(ocf[0])) <= -1) { hawk_rtx_errortohawk (rtx, awk); hawk_rtx_close (rtx); @@ -2755,7 +2755,7 @@ hawk_rtx_t* hawk_rtx_openstdwithucstr ( #else xicf[i] = hawk_dupucstr(awk, icf[i], HAWK_NULL); #endif - if (!xicf[i]) goto done; + if (HAWK_UNLIKELY(!xicf[i])) goto done; } xicf[i] = HAWK_NULL; } diff --git a/hawk/lib/val.c b/hawk/lib/val.c index 439c9833..37b9c518 100644 --- a/hawk/lib/val.c +++ b/hawk/lib/val.c @@ -167,7 +167,7 @@ static HAWK_INLINE hawk_val_t* make_str_val (hawk_rtx_t* rtx, const hawk_ooch_t* hawk_oow_t i; #endif - if (len1 <= 0 && len2 <= 0) return hawk_val_zls; + if (HAWK_UNLIKELY(len1 <= 0 && len2 <= 0)) return hawk_val_zls; aligned_len = HAWK_ALIGN_POW2((len1 + len2 + 1), FEATURE_SCACHE_BLOCK_UNIT); #if defined(ENABLE_FEATURE_SCACHE) @@ -183,7 +183,7 @@ static HAWK_INLINE hawk_val_t* make_str_val (hawk_rtx_t* rtx, const hawk_ooch_t* #endif val = (hawk_val_str_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_str_t) + (aligned_len * HAWK_SIZEOF(hawk_ooch_t))); - if (!val) return HAWK_NULL; + if (HAWK_UNLIKELY(!val)) return HAWK_NULL; #if defined(ENABLE_FEATURE_SCACHE) init: @@ -216,7 +216,7 @@ hawk_val_t* hawk_rtx_makestrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* uc hawk_oow_t bcslen; bcs = hawk_rtx_duputobchars(rtx, ucs, len, &bcslen); - if (!bcs) return HAWK_NULL; + if (HAWK_UNLIKELY(!bcs)) return HAWK_NULL; v = make_str_val(rtx, bcs, bcslen, HAWK_NULL, 0); hawk_rtx_freemem (rtx, bcs); @@ -232,7 +232,7 @@ hawk_val_t* hawk_rtx_makestrvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* bc hawk_oow_t ucslen; ucs = hawk_rtx_dupbtouchars(rtx, bcs, len, &ucslen, 1); - if (!ucs) return HAWK_NULL; + if (HAWK_UNLIKELY(!ucs)) return HAWK_NULL; v = make_str_val(rtx, ucs, ucslen, HAWK_NULL, 0); hawk_rtx_freemem (rtx, ucs); @@ -272,7 +272,7 @@ hawk_val_t* hawk_rtx_makestrvalwithuchars2 (hawk_rtx_t* rtx, const hawk_uch_t* u hawk_oow_t bcslen; bcs = hawk_rtx_dupu2tobchars(rtx, ucs1, len1, ucs2, len2, &bcslen); - if (!bcs) return HAWK_NULL; + if (HAWK_UNLIKELY(!bcs)) return HAWK_NULL; v = make_str_val(rtx, bcs, bcslen, HAWK_NULL, 0); hawk_rtx_freemem (rtx, bcs); @@ -288,7 +288,7 @@ hawk_val_t* hawk_rtx_makestrvalwithbchars2 (hawk_rtx_t* rtx, const hawk_bch_t* b hawk_oow_t ucslen; ucs = hawk_rtx_dupb2touchars(rtx, bcs1, len1, bcs2, len2, &ucslen, 1); - if (!ucs) return HAWK_NULL; + if (HAWK_UNLIKELY(!ucs)) return HAWK_NULL; v = make_str_val(rtx, ucs, ucslen, HAWK_NULL, 0); hawk_rtx_freemem (rtx, ucs); @@ -298,8 +298,6 @@ hawk_val_t* hawk_rtx_makestrvalwithbchars2 (hawk_rtx_t* rtx, const hawk_bch_t* b #endif } - - hawk_val_t* hawk_rtx_makenstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* ptr, hawk_oow_t len) { hawk_t* hawk = hawk_rtx_gethawk(rtx); @@ -370,17 +368,17 @@ hawk_val_t* hawk_rtx_makenstrvalwithbcs (hawk_rtx_t* rtx, const hawk_bcs_t* str) } -hawk_val_t* hawk_rtx_makembsval (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len) +hawk_val_t* hawk_rtx_makembsvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len) { hawk_val_mbs_t* val = HAWK_NULL; hawk_oow_t xsz; - if (len <= 0) return hawk_val_zlm; + if (HAWK_UNLIKELY(len <= 0)) return hawk_val_zlm; xsz = len * HAWK_SIZEOF(*ptr); val = (hawk_val_mbs_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_mbs_t) + xsz + HAWK_SIZEOF(*ptr)); - if (!val) return HAWK_NULL; + if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_MBS; val->ref = 0; @@ -395,9 +393,32 @@ hawk_val_t* hawk_rtx_makembsval (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oo return (hawk_val_t*)val; } -hawk_val_t* hawk_rtx_makembsvalwithbcs (hawk_rtx_t* rtx, const hawk_bcs_t* mxstr) +hawk_val_t* hawk_rtx_makembsvalwithbcs (hawk_rtx_t* rtx, const hawk_bcs_t* bcs) { - return hawk_rtx_makembsval(rtx, mxstr->ptr, mxstr->len); + return hawk_rtx_makembsvalwithbchars(rtx, bcs->ptr, bcs->len); +} + +hawk_val_t* hawk_rtx_makembsvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* ucs, hawk_oow_t len) +{ + hawk_val_t* val; + hawk_bch_t* bcs; + hawk_oow_t bcslen; + + if (HAWK_UNLIKELY(len <= 0)) return hawk_val_zlm; + + bcs = hawk_rtx_duputobchars(rtx, ucs, len, &bcslen); + if (HAWK_UNLIKELY(!bcs)) return HAWK_NULL; + + val = hawk_rtx_makembsvalwithbchars(rtx, bcs, bcslen); + hawk_rtx_freemem (rtx, bcs); + + return val; + +} + +hawk_val_t* hawk_rtx_makembsvalwithucs (hawk_rtx_t* rtx, const hawk_ucs_t* ucs) +{ + return hawk_rtx_makembsvalwithuchars(rtx, ucs->ptr, ucs->len); } hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, hawk_tre_t* code[2]) @@ -412,7 +433,7 @@ hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, hawk_t */ totsz = HAWK_SIZEOF(*val) + (HAWK_SIZEOF(*str->ptr) * (str->len + 1)); val = (hawk_val_rex_t*)hawk_rtx_callocmem(rtx, totsz); - if (!val) return HAWK_NULL; + if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_REX; val->ref = 0; @@ -519,7 +540,7 @@ hawk_val_t* hawk_rtx_makemapvalwithdata (hawk_rtx_t* rtx, hawk_val_map_data_t da hawk_val_map_data_t* p; map = hawk_rtx_makemapval(rtx); - if (!map) return HAWK_NULL; + if (HAWK_UNLIKELY(!map)) return HAWK_NULL; for (p = data; p->key.ptr; p++) { @@ -601,7 +622,7 @@ hawk_val_t* hawk_rtx_getmapvalfld (hawk_rtx_t* rtx, hawk_val_t* map, const hawk_ HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, map) == HAWK_VAL_MAP); pair = hawk_htb_search(((hawk_val_map_t*)map)->map, kptr, klen); - if (pair == HAWK_NULL) + if (!pair) { /* the given key is not found in the map. * we return NULL here as this function is called by @@ -615,16 +636,14 @@ hawk_val_t* hawk_rtx_getmapvalfld (hawk_rtx_t* rtx, hawk_val_t* map, const hawk_ return HAWK_HTB_VPTR(pair); } -hawk_val_map_itr_t* hawk_rtx_getfirstmapvalitr ( - hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr) +hawk_val_map_itr_t* hawk_rtx_getfirstmapvalitr (hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr) { HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, map) == HAWK_VAL_MAP); itr->pair = hawk_htb_getfirstpair (((hawk_val_map_t*)map)->map, &itr->buckno); return itr->pair? itr: HAWK_NULL; } -hawk_val_map_itr_t* hawk_rtx_getnextmapvalitr ( - hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr) +hawk_val_map_itr_t* hawk_rtx_getnextmapvalitr (hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr) { HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, map) == HAWK_VAL_MAP); itr->pair = hawk_htb_getnextpair (((hawk_val_map_t*)map)->map, itr->pair, &itr->buckno); @@ -654,7 +673,7 @@ hawk_val_t* hawk_rtx_makefunval (hawk_rtx_t* rtx, const hawk_fun_t* fun) hawk_val_fun_t* val; val = (hawk_val_fun_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(*val)); - if (!val) return HAWK_NULL; + if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_FUN; val->ref = 0; diff --git a/hawk/mod/mod-mysql.c b/hawk/mod/mod-mysql.c index 74023e3a..8c02395e 100644 --- a/hawk/mod/mod-mysql.c +++ b/hawk/mod/mod-mysql.c @@ -1591,7 +1591,7 @@ static int fnc_stmt_fetch (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) cv = hawk_rtx_makestrvalwithbchars(rtx, data->u.sv.ptr, data->u.sv.len); break; case MYSQL_TYPE_BLOB: - cv = hawk_rtx_makembsval(rtx, data->u.sv.ptr, data->u.sv.len); + cv = hawk_rtx_makembsvalwithbchars(rtx, data->u.sv.ptr, data->u.sv.len); break; default: