diff --git a/hawk/README.md b/hawk/README.md index 913af8c1..1d6c35d3 100644 --- a/hawk/README.md +++ b/hawk/README.md @@ -1,7 +1,7 @@ # Hawk - [Language](#language) - - [Basic Modules](#basic-modules) + - [Modules](#modules) - [Embedding Guide](#embedding-guide) ## Language @@ -289,7 +289,7 @@ and represents the value of 0. 0b # 0b0 but not desirable. -### Module +### Modules Hawk supports various modules. @@ -545,6 +545,44 @@ Socket functions are available. sys::close (s); } + +### ffi + + +### mysql + + BEGIN { + mysql = mysql::open(); + + if (mysql::connect(mysql, "10.30.1.219", "pdns", "pdns", "pdns") <= -1) + { + print "connect error -", mysql::errmsg(); + } + + if (mysql::query(mysql, "select * from records") <= -1) + { + print "query error -", mysql::errmsg(); + } + + result = mysql::store_result(mysql); + if (result <= -1) + { + print "store result error - ", mysql::errmsg(); + } + + while (mysql::fetch_row(result, row) > 0) + { + ncols = length(row); + for (i = 0; i < ncols; i++) print row[i]; + print "----"; + } + + mysql::free_result(result); + + mysql::close(mysql); + } + + ### Incompatibility with AWK #### Parameter passing @@ -577,14 +615,6 @@ There are subtle differences when you put an expression for a position variable. | $(++$(++i)) | OK | syntax error | -## Basic Modules - -### sys - -### ffi - -### mysql - ## Embedding Guide To use hawk in your program, do the followings: diff --git a/hawk/lib/Makefile.am b/hawk/lib/Makefile.am index e0107241..6092ac7f 100644 --- a/hawk/lib/Makefile.am +++ b/hawk/lib/Makefile.am @@ -80,6 +80,7 @@ libhawk_la_SOURCES = \ gem-nwif2.c \ hawk-prv.h \ hawk.c \ + idmap-imp.h \ mb8.c \ misc-prv.h \ misc.c \ @@ -184,7 +185,6 @@ if ENABLE_STATIC_MODULE # STATIC MODULES BUILT INTO MAIN LIBRARY ################################################## libhawk_la_SOURCES += \ - imap-imp.h \ mod-math.c mod-math.h \ mod-str.c mod-str.h \ mod-sys.c mod-sys.h diff --git a/hawk/lib/Makefile.in b/hawk/lib/Makefile.in index 20c5d989..1b516e0c 100644 --- a/hawk/lib/Makefile.in +++ b/hawk/lib/Makefile.in @@ -100,7 +100,6 @@ host_triplet = @host@ # STATIC MODULES BUILT INTO MAIN LIBRARY ################################################## @ENABLE_STATIC_MODULE_TRUE@am__append_9 = \ -@ENABLE_STATIC_MODULE_TRUE@ imap-imp.h \ @ENABLE_STATIC_MODULE_TRUE@ mod-math.c mod-math.h \ @ENABLE_STATIC_MODULE_TRUE@ mod-str.c mod-str.h \ @ENABLE_STATIC_MODULE_TRUE@ mod-sys.c mod-sys.h @@ -207,9 +206,9 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ hawk-tre.h hawk-unpack.h Hawk.hpp HawkStd.hpp arr.c chr.c \ dir.c ecs-imp.h ecs.c err-prv.h err.c err-sys.c fmt-imp.h \ fmt.c fnc-prv.h fnc.c htb.c gem.c gem-skad.c gem-nwif.c \ - gem-nwif2.c hawk-prv.h hawk.c mb8.c misc-prv.h misc.c \ - parse-prv.h parse.c rbt.c rec.c rio-prv.h rio.c run-prv.h \ - run.c tre-prv.h tre-ast.c tre-ast.h tre-compile.c \ + gem-nwif2.c hawk-prv.h hawk.c idmap-imp.h mb8.c misc-prv.h \ + misc.c parse-prv.h parse.c rbt.c rec.c rio-prv.h rio.c \ + run-prv.h run.c tre-prv.h tre-ast.c tre-ast.h tre-compile.c \ tre-compile.h tre-match-bt.c tre-match-pa.c tre-match-ut.h \ tre-mem.c tre-mem.h tre-parse.c tre-parse.h tre-stack.h \ tre-stack.c tre.c tree-prv.h tree.c uch-prop.h uch-case.h \ @@ -217,8 +216,8 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ utl-str.c utl-sys.c utl.c val-prv.h val.c hawk-cli.h \ hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h \ cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h tio.c \ - std-prv.h std.c Hawk.cpp HawkStd.cpp imap-imp.h mod-math.c \ - mod-math.h mod-str.c mod-str.h mod-sys.c mod-sys.h + std-prv.h std.c Hawk.cpp HawkStd.cpp mod-math.c mod-math.h \ + mod-str.c mod-str.h mod-sys.c mod-sys.h am__objects_1 = am__objects_2 = $(am__objects_1) @ENABLE_CXX_TRUE@am__objects_3 = libhawk_la-Hawk.lo \ @@ -576,14 +575,14 @@ pkglib_LTLIBRARIES = libhawk.la $(am__append_15) libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c dir.c ecs-imp.h \ ecs.c err-prv.h err.c err-sys.c fmt-imp.h fmt.c fnc-prv.h \ fnc.c htb.c gem.c gem-skad.c gem-nwif.c gem-nwif2.c hawk-prv.h \ - hawk.c mb8.c misc-prv.h misc.c parse-prv.h parse.c rbt.c rec.c \ - rio-prv.h rio.c run-prv.h run.c tre-prv.h tre-ast.c tre-ast.h \ - tre-compile.c tre-compile.h tre-match-bt.c tre-match-pa.c \ - tre-match-ut.h tre-mem.c tre-mem.h tre-parse.c tre-parse.h \ - tre-stack.h tre-stack.c tre.c tree-prv.h tree.c uch-prop.h \ - uch-case.h utf16.c utf8.c utl-ass.c utl-skad.c utl-skad.h \ - utl-sort.c utl-str.c utl-sys.c utl.c val-prv.h val.c \ - hawk-cli.h hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h \ + hawk.c idmap-imp.h mb8.c misc-prv.h misc.c parse-prv.h parse.c \ + rbt.c rec.c rio-prv.h rio.c run-prv.h run.c tre-prv.h \ + tre-ast.c tre-ast.h tre-compile.c tre-compile.h tre-match-bt.c \ + tre-match-pa.c tre-match-ut.h tre-mem.c tre-mem.h tre-parse.c \ + tre-parse.h tre-stack.h tre-stack.c tre.c tree-prv.h tree.c \ + uch-prop.h uch-case.h utf16.c utf8.c utl-ass.c utl-skad.c \ + utl-skad.h utl-sort.c utl-str.c utl-sys.c utl.c val-prv.h \ + val.c hawk-cli.h hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h \ hawk-tio.h cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h \ tio.c std-prv.h std.c $(am__append_8) $(am__append_9) libhawk_la_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) $(CPPFLAGS_PFMOD) \ diff --git a/hawk/lib/hawk-prv.h b/hawk/lib/hawk-prv.h index e99f0052..23eed26b 100644 --- a/hawk/lib/hawk-prv.h +++ b/hawk/lib/hawk-prv.h @@ -492,8 +492,8 @@ struct hawk_mod_data_t #define HAWK_RTX_INIT_REF_VAL(refval, _id, _adr, _nrefs) \ do { \ (refval)->v_type = HAWK_VAL_REF; \ - (refval)->ref = (_nrefs); \ - (refval)->stat = 0; \ + (refval)->v_refs = (_nrefs); \ + (refval)->v_static = 0; \ (refval)->nstr = 0; \ (refval)->fcb = 0; \ (refval)->id = (_id); \ diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index ace19064..76b3fc8d 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -127,26 +127,51 @@ struct hawk_rtx_alt_t HAWK_RTX_HDR; }; +/* ------------------------------------------------------------------------ */ + +typedef struct hawk_gc_info_t hawk_gc_info_t; +struct hawk_gc_info_t +{ + hawk_gc_info_t* gc_prev; + hawk_gc_info_t* gc_next; + hawk_uintptr_t gc_refs; +}; + +#if defined(HAWK_HAVE_INLINE) +static HAWK_INLINE hawk_gc_info_t* hawk_val_to_gcinfo(hawk_val_t* v) +{ + return ((hawk_gc_info_t*)v) - 1; +} +static HAWK_INLINE hawk_val_t* hawk_gcinfo_to_val(hawk_gc_info_t* gci) +{ + return (hawk_val_t*)(gci + 1); +} + +#else +# defined hawk_val_to_gcinfo(v) (((hawk_gc_info_t*)(v)) - 1) +# defined hawk_gcinfo_to_val(gci) ((hawk_val_t*)(((hawk_gc_info_t*)(gci)) + 1)) +#endif + /** * The #HAWK_VAL_HDR defines the common header for a value. * Three common fields are: - * - type - type of a value from #hawk_val_type_t - * - ref - reference count - * - stat - static value + * - v_type - type of a value from #hawk_val_type_t + * - v_refs - reference count + * - v_static - static value indicator * - nstr - numeric string marker, 1 -> integer, 2 -> floating-point number */ /* #define HAWK_VAL_HDR \ - unsigned int type: 4; \ - unsigned int ref: 24; \ - unsigned int stat: 1; \ + unsigned int v_type: 4; \ + unsigned int v_refs: 24; \ + unsigned int v_static: 1; \ unsigned int nstr: 2; \ unsigned int fcb: 1 */ #define HAWK_VAL_HDR \ hawk_uintptr_t v_type: 4; \ - hawk_uintptr_t ref: ((HAWK_SIZEOF_UINTPTR_T * 8) - 8); \ - hawk_uintptr_t stat: 1; \ + hawk_uintptr_t v_refs: ((HAWK_SIZEOF_UINTPTR_T * 8) - 8); \ + hawk_uintptr_t v_static: 1; \ hawk_uintptr_t nstr: 2; \ hawk_uintptr_t fcb: 1 diff --git a/hawk/lib/htb.c b/hawk/lib/htb.c index bb50b7b3..7b406172 100644 --- a/hawk/lib/htb.c +++ b/hawk/lib/htb.c @@ -604,7 +604,7 @@ int hawk_htb_delete (hawk_htb_t* htb, const void* kptr, hawk_oow_t klen) while (pair != HAWK_NULL) { - if (htb->style->comper (htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) + if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) { if (prev == HAWK_NULL) htb->bucket[hc] = NEXT(pair); diff --git a/hawk/lib/imap-imp.h b/hawk/lib/idmap-imp.h similarity index 52% rename from hawk/lib/imap-imp.h rename to hawk/lib/idmap-imp.h index 86013503..5ac8ece0 100644 --- a/hawk/lib/imap-imp.h +++ b/hawk/lib/idmap-imp.h @@ -26,46 +26,70 @@ /* THIS FILE IS SUPPOSED TO BE INCLUDED BY MODULE SOURCE THAT MAINTAINS MAPPING BETWEEN ID AND DATA */ -typedef struct __IMAP_NODE_T __IMAP_NODE_T; -struct __IMAP_NODE_T +typedef struct __IDMAP_NODE_T __IDMAP_NODE_T; +struct __IDMAP_NODE_T { - __IMAP_NODE_T* prev; - __IMAP_NODE_T* next; + __IDMAP_NODE_T* prev; + __IDMAP_NODE_T* next; int id; - __IMAP_NODE_T_DATA + __IDMAP_NODE_T_DATA }; -typedef struct __IMAP_LIST_T __IMAP_LIST_T; -struct __IMAP_LIST_T +typedef struct __IDMAP_LIST_T __IDMAP_LIST_T; +struct __IDMAP_LIST_T { - __IMAP_NODE_T* head; - __IMAP_NODE_T* tail; - __IMAP_NODE_T* free; +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) + __IDMAP_NODE_T* head; + __IDMAP_NODE_T* tail; + __IDMAP_NODE_T* free; +#else + struct + { + __IDMAP_NODE_T* prev; + __IDMAP_NODE_T* next; + } used; + struct + { + __IDMAP_NODE_T* prev; + __IDMAP_NODE_T* next; + } free; +#endif /* mapping table to map 'id' to 'node' */ struct { - __IMAP_NODE_T** tab; + __IDMAP_NODE_T** tab; int capa; int high; } map; - __IMAP_LIST_T_DATA + __IDMAP_LIST_T_DATA }; -static __IMAP_NODE_T* __MAKE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list) + +static __IDMAP_NODE_T* __MAKE_IDMAP_NODE (hawk_rtx_t* rtx, __IDMAP_LIST_T* list) { /* create a new context node and append it to the list tail */ - __IMAP_NODE_T* node; + __IDMAP_NODE_T* node; node = HAWK_NULL; +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) if (list->free) { node = list->free; list->free = node->next; } +#else + if (list->free.next != (__IDMAP_NODE_T*)&list->free) + { + /* remove a node from a free list */ + node = list->free.next; + node->prev->next = node->next; + node->next->prev = node->prev; + } +#endif else { node = hawk_rtx_callocmem(rtx, HAWK_SIZEOF(*node)); @@ -74,7 +98,7 @@ static __IMAP_NODE_T* __MAKE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list) if (list->map.high <= list->map.capa) { hawk_oow_t newcapa, inc; - __IMAP_NODE_T** tmp; + __IDMAP_NODE_T** tmp; inc = HAWK_TYPE_MAX(int) - list->map.capa; if (inc == 0) goto oops; /* too many nodes */ @@ -82,7 +106,7 @@ static __IMAP_NODE_T* __MAKE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list) if (inc > 64) inc = 64; newcapa = (hawk_oow_t)list->map.capa + inc; - tmp = (__IMAP_NODE_T**)hawk_rtx_reallocmem(rtx, list->map.tab, HAWK_SIZEOF(*tmp) * newcapa); + tmp = (__IDMAP_NODE_T**)hawk_rtx_reallocmem(rtx, list->map.tab, HAWK_SIZEOF(*tmp) * newcapa); if (!tmp) goto oops; HAWK_MEMSET (&tmp[list->map.capa], 0, HAWK_SIZEOF(*tmp) * (newcapa - list->map.capa)); @@ -98,12 +122,20 @@ static __IMAP_NODE_T* __MAKE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list) HAWK_ASSERT (list->map.tab[node->id] == HAWK_NULL); list->map.tab[node->id] = node; +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) /* append it to the tail */ node->next = HAWK_NULL; node->prev = list->tail; if (list->tail) list->tail->next = node; else list->head = node; list->tail = node; +#else + /* append it */ + node->next = (__IDMAP_NODE_T*)&list->used; + node->prev = list->used.prev; + node->prev->next = node; + list->used.prev = node; +#endif return node; @@ -112,12 +144,17 @@ oops: return HAWK_NULL; } -static void __FREE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list, __IMAP_NODE_T* node) +static void __FREE_IDMAP_NODE (hawk_rtx_t* rtx, __IDMAP_LIST_T* list, __IDMAP_NODE_T* node) { +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) if (node->prev) node->prev->next = node->next; if (node->next) node->next->prev = node->prev; if (list->head == node) list->head = node->next; if (list->tail == node) list->tail = node->prev; +#else + node->prev->next = node->next; + node->next->prev = node->prev; +#endif list->map.tab[node->id] = HAWK_NULL; @@ -131,15 +168,23 @@ static void __FREE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list, __IMAP_NODE_ else { /* otherwise, chain the node to the free list */ +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) node->next = list->free; list->free = node; +#else + node->next = (__IDMAP_NODE_T*)&list->free; + node->prev = list->free.prev; + node->prev->next = node; + list->free.prev = node; +#endif } /* however, i destroy the whole free list when all the nodes are * chanined to the free list */ +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) if (list->head == HAWK_NULL) { - __IMAP_NODE_T* curnode; + __IDMAP_NODE_T* curnode; while (list->free) { @@ -153,4 +198,75 @@ static void __FREE_IMAP_NODE (hawk_rtx_t* rtx, __IMAP_LIST_T* list, __IMAP_NODE_ list->map.capa = 0; list->map.tab = HAWK_NULL; } +#else + if (list->used.next == (__IDMAP_NODE_T*)&list->used) + { + __IDMAP_NODE_T* node; + + while (list->free.next != (__IDMAP_NODE_T*)&list->free) + { + node = list->free.next; + node->prev->next = node->next; + node->next->prev = node->prev; + hawk_rtx_freemem (rtx, node); + } + + hawk_rtx_freemem (rtx, list->map.tab); + list->map.high = 0; + list->map.capa = 0; + list->map.tab = HAWK_NULL; + } +#endif +} + +static void __INIT_IDMAP_LIST (hawk_rtx_t* rtx, __IDMAP_LIST_T* list) +{ +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) + /* nothing */ +#else + list->used.next = (__IDMAP_NODE_T*)&list->used; + list->used.prev = (__IDMAP_NODE_T*)&list->used; + list->free.next = (__IDMAP_NODE_T*)&list->free; + list->free.prev = (__IDMAP_NODE_T*)&list->free; +#endif +} + +static void __FINI_IDMAP_LIST (hawk_rtx_t* rtx, __IDMAP_LIST_T* list) +{ +#if defined(__IDMAP_AVOID_CIRCULAR_LIST) + __IDMAP_NODE_T* node, * next; + node = data->list->head; + while (node) + { + next = node->next; + __FREE_IDMAP_NODE (rtx, list, node); + node = next; + } + + node = data->list->free; + while (node) + { + next = node->next; + __FREE_IDMAP_NODE (rtx, list, node); + node = next; + } +#else + __IDMAP_NODE_T* node; + + while (list->used.next != (__IDMAP_NODE_T*)&list->used) + { + node = list->used.next; + node->prev->next = node->next; + node->next->prev = node->prev; + __FREE_IDMAP_NODE (rtx, list, node); + } + + while (list->free.next != (__IDMAP_NODE_T*)&list->free) + { + node = list->free.next; + node->prev->next = node->next; + node->next->prev = node->prev; + __FREE_IDMAP_NODE (rtx, list, node); + } +#endif } diff --git a/hawk/lib/mod-sys.c b/hawk/lib/mod-sys.c index 19f430e8..eb1b8760 100644 --- a/hawk/lib/mod-sys.c +++ b/hawk/lib/mod-sys.c @@ -166,13 +166,15 @@ struct sys_list_data_t }; typedef struct sys_list_data_t sys_list_data_t; -#define __IMAP_NODE_T_DATA sys_node_data_t ctx; -#define __IMAP_LIST_T_DATA sys_list_data_t ctx; -#define __IMAP_LIST_T sys_list_t -#define __IMAP_NODE_T sys_node_t -#define __MAKE_IMAP_NODE __new_sys_node -#define __FREE_IMAP_NODE __free_sys_node -#include "imap-imp.h" +#define __IDMAP_NODE_T_DATA sys_node_data_t ctx; +#define __IDMAP_LIST_T_DATA sys_list_data_t ctx; +#define __IDMAP_LIST_T sys_list_t +#define __IDMAP_NODE_T sys_node_t +#define __INIT_IDMAP_LIST __init_sys_list +#define __FINI_IDMAP_LIST __fini_sys_list +#define __MAKE_IDMAP_NODE __new_sys_node +#define __FREE_IDMAP_NODE __free_sys_node +#include "idmap-imp.h" struct rtx_data_t { @@ -713,7 +715,7 @@ static int fnc_write (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { hawk_bch_t* dptr; hawk_oow_t dlen; - hawk_ooi_t startpos = 0, maxlen = HAWK_TYPE_MAX(hawk_ooi_t); + hawk_int_t startpos = 0, maxlen = HAWK_TYPE_MAX(hawk_int_t); hawk_val_t* a1; if (hawk_rtx_getnargs(rtx) >= 3 && (hawk_rtx_valtoint(rtx, hawk_rtx_getarg(rtx, 2), &startpos) <= -1 || startpos < 0)) startpos = 0; @@ -4762,14 +4764,19 @@ static int query (hawk_mod_t* mod, hawk_t* awk, const hawk_ooch_t* name, hawk_mo static int init (hawk_mod_t* mod, hawk_rtx_t* rtx) { mod_ctx_t* mctx = (mod_ctx_t*)mod->ctx; - rtx_data_t data; + rtx_data_t data, * datap; + hawk_rbt_pair_t* pair; mctx->log.type = SYSLOG_LOCAL; mctx->log.syslog_opened = 0; mctx->log.sck = -1; HAWK_MEMSET (&data, 0, HAWK_SIZEOF(data)); - if (hawk_rbt_insert(mctx->rtxtab, &rtx, HAWK_SIZEOF(rtx), &data, HAWK_SIZEOF(data)) == HAWK_NULL) return -1; + pair = hawk_rbt_insert(mctx->rtxtab, &rtx, HAWK_SIZEOF(rtx), &data, HAWK_SIZEOF(data)); + if (HAWK_UNLIKELY(!pair)) return -1; + + datap = (rtx_data_t*)HAWK_RBT_VPTR(pair); + __init_sys_list (rtx, &datap->sys_list); return 0; } @@ -4789,24 +4796,19 @@ static void fini (hawk_mod_t* mod, hawk_rtx_t* rtx) if (pair) { rtx_data_t* data; - sys_node_t* sys_node, * sys_next; + data = (rtx_data_t*)HAWK_RBT_VPTR(pair); - sys_node = data->sys_list.head; - while (sys_node) - { - sys_next = sys_node->next; - free_sys_node (rtx, &data->sys_list, sys_node); - sys_node = sys_next; - } - if (data->sys_list.ctx.readbuf) { hawk_rtx_freemem (rtx, data->sys_list.ctx.readbuf); data->sys_list.ctx.readbuf = HAWK_NULL; data->sys_list.ctx.readbuf_capa = 0; } + + __fini_sys_list (rtx, &data->sys_list); + hawk_rbt_delete (mctx->rtxtab, &rtx, HAWK_SIZEOF(rtx)); } diff --git a/hawk/lib/val-prv.h b/hawk/lib/val-prv.h index 9ae5820e..4db6daad 100644 --- a/hawk/lib/val-prv.h +++ b/hawk/lib/val-prv.h @@ -68,7 +68,7 @@ struct hawk_val_rchunk_t #define IS_STATICVAL(val) ((val) == HAWK_NULL || (val) == hawk_val_nil || (val) == hawk_val_zls || (val) == hawk_val_zlm) */ -#define IS_STATICVAL(val) ((val)->stat) +#define IS_STATICVAL(val) ((val)->v_static) /* hawk_val_t pointer encoding assumes the pointer is an even number. diff --git a/hawk/lib/val.c b/hawk/lib/val.c index 690a3537..138cf14d 100644 --- a/hawk/lib/val.c +++ b/hawk/lib/val.c @@ -36,6 +36,22 @@ hawk_val_t* hawk_val_nil = (hawk_val_t*)&awk_nil; hawk_val_t* hawk_val_zls = (hawk_val_t*)&awk_zls; hawk_val_t* hawk_val_zlm = (hawk_val_t*)&awk_zlm; + +/* --------------------------------------------------------------------- */ + +/* TOOD: */ +static hawk_val_t* gc_calloc (hawk_rtx_t* rtx, hawk_oow_t size) +{ + hawk_gc_info_t* gci; + + gci = (hawk_gc_info_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(*gci) + size); + if (HAWK_UNLIKELY(!gci)) return HAWK_NULL; + + return hawk_gcinfo_to_val(gci); +} + +/* --------------------------------------------------------------------- */ + hawk_val_t* hawk_get_awk_nil_val (void) { return (hawk_val_t*)&awk_nil; @@ -95,8 +111,8 @@ hawk_val_t* hawk_rtx_makeintval (hawk_rtx_t* rtx, hawk_int_t v) rtx->vmgr.ifree = (hawk_val_int_t*)val->nde; val->v_type = HAWK_VAL_INT; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->i_val = v; @@ -146,8 +162,8 @@ hawk_val_t* hawk_rtx_makefltval (hawk_rtx_t* rtx, hawk_flt_t v) rtx->vmgr.rfree = (hawk_val_flt_t*)val->nde; val->v_type = HAWK_VAL_FLT; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->val = v; @@ -189,8 +205,8 @@ static HAWK_INLINE hawk_val_t* make_str_val (hawk_rtx_t* rtx, const hawk_ooch_t* init: #endif val->v_type = HAWK_VAL_STR; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->val.len = len1 + len2; @@ -337,8 +353,7 @@ hawk_val_t* hawk_rtx_makenstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* p x = hawk_uchars_to_num(HAWK_OOCHARS_TO_NUM_MAKE_OPTION(1, 0, HAWK_RTX_IS_STRIPSTRSPC_ON(rtx), 0), ptr, len, &l, &r); v = hawk_rtx_makestrvalwithuchars(rtx, ptr, len); - - if (!v) return HAWK_NULL; + if (HAWK_UNLIKELY(!v)) return HAWK_NULL; if (x >= 0) { @@ -360,8 +375,7 @@ hawk_val_t* hawk_rtx_makenstrvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* p x = hawk_bchars_to_num(HAWK_OOCHARS_TO_NUM_MAKE_OPTION(1, 0, HAWK_RTX_IS_STRIPSTRSPC_ON(rtx), 0), ptr, len, &l, &r); v = hawk_rtx_makestrvalwithbchars(rtx, ptr, len); - - if (!v) return HAWK_NULL; + if (HAWK_UNLIKELY(!v)) return HAWK_NULL; if (x >= 0) { @@ -410,8 +424,8 @@ hawk_val_t* hawk_rtx_makembsvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* pt if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_MBS; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->val.len = len; @@ -496,8 +510,8 @@ hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, hawk_t if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_REX; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->str.len = str->len; @@ -549,17 +563,19 @@ hawk_val_t* hawk_rtx_makemapval (hawk_rtx_t* rtx) }, HAWK_MAP_COMPER_DEFAULT, same_mapval, + #if defined(HAWK_MAP_IS_HTB) HAWK_MAP_SIZER_DEFAULT, HAWK_MAP_HASHER_DEFAULT + #endif }; hawk_val_map_t* val; val = (hawk_val_map_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_map_t) + HAWK_SIZEOF(hawk_map_t) + HAWK_SIZEOF(rtx)); - if (!val) return HAWK_NULL; + if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_MAP; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->map = (hawk_map_t*)(val + 1); @@ -734,8 +750,8 @@ hawk_val_t* hawk_rtx_makefunval (hawk_rtx_t* rtx, const hawk_fun_t* fun) if (HAWK_UNLIKELY(!val)) return HAWK_NULL; val->v_type = HAWK_VAL_FUN; - val->ref = 0; - val->stat = 0; + val->v_refs = 0; + val->v_static = 0; val->nstr = 0; val->fcb = 0; val->fun = (hawk_fun_t*)fun; @@ -888,9 +904,9 @@ void hawk_rtx_refupval (hawk_rtx_t* rtx, hawk_val_t* val) if (IS_STATICVAL(val)) return; #if defined(DEBUG_VAL) - hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("ref up [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->ref, val); + hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("ref up [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->v_refs, val); #endif - val->ref++; + val->v_refs++; } } @@ -901,15 +917,14 @@ void hawk_rtx_refdownval (hawk_rtx_t* rtx, hawk_val_t* val) if (IS_STATICVAL(val)) return; #if defined(DEBUG_VAL) - hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("ref down [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->ref, val); + hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("ref down [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->v_refs, val); #endif /* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */ - HAWK_ASSERT (val->ref > 0); - + HAWK_ASSERT (val->v_refs > 0); - val->ref--; - if (val->ref <= 0) + val->v_refs--; + if (val->v_refs <= 0) { hawk_rtx_freeval(rtx, val, 1); } @@ -923,9 +938,9 @@ void hawk_rtx_refdownval_nofree (hawk_rtx_t* rtx, hawk_val_t* val) if (IS_STATICVAL(val)) return; /* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */ - HAWK_ASSERT (val->ref > 0); + HAWK_ASSERT (val->v_refs > 0); - val->ref--; + val->v_refs--; } } diff --git a/hawk/mod/mod-mysql.c b/hawk/mod/mod-mysql.c index 8c02395e..1653658b 100644 --- a/hawk/mod/mod-mysql.c +++ b/hawk/mod/mod-mysql.c @@ -49,43 +49,53 @@ struct param_data_t typedef struct param_data_t param_data_t; typedef struct param_data_t res_data_t; -#define __IMAP_NODE_T_DATA MYSQL* mysql; int connect_ever_attempted; -#define __IMAP_LIST_T_DATA int errnum; hawk_ooch_t errmsg[256]; -#define __IMAP_LIST_T sql_list_t -#define __IMAP_NODE_T sql_node_t -#define __MAKE_IMAP_NODE __new_sql_node -#define __FREE_IMAP_NODE __free_sql_node -#include "../lib/imap-imp.h" +#define __IDMAP_NODE_T_DATA MYSQL* mysql; int connect_ever_attempted; +#define __IDMAP_LIST_T_DATA int errnum; hawk_ooch_t errmsg[256]; +#define __IDMAP_LIST_T sql_list_t +#define __IDMAP_NODE_T sql_node_t +#define __INIT_IDMAP_LIST __init_sql_list +#define __FINI_IDMAP_LIST __fini_sql_list +#define __MAKE_IDMAP_NODE __new_sql_node +#define __FREE_IDMAP_NODE __free_sql_node +#include "../lib/idmap-imp.h" -#undef __IMAP_NODE_T_DATA -#undef __IMAP_LIST_T_DATA -#undef __IMAP_LIST_T -#undef __IMAP_NODE_T -#undef __MAKE_IMAP_NODE -#undef __FREE_IMAP_NODE +#undef __IDMAP_NODE_T_DATA +#undef __IDMAP_LIST_T_DATA +#undef __IDMAP_LIST_T +#undef __IDMAP_NODE_T +#undef __INIT_IDMAP_LIST +#undef __FINI_IDMAP_LIST +#undef __MAKE_IDMAP_NODE +#undef __FREE_IDMAP_NODE -#define __IMAP_NODE_T_DATA MYSQL_RES* res; unsigned int num_fields; -#define __IMAP_LIST_T_DATA /* int errnum; */ -#define __IMAP_LIST_T res_list_t -#define __IMAP_NODE_T res_node_t -#define __MAKE_IMAP_NODE __new_res_node -#define __FREE_IMAP_NODE __free_res_node -#include "../lib/imap-imp.h" +#define __IDMAP_NODE_T_DATA MYSQL_RES* res; unsigned int num_fields; +#define __IDMAP_LIST_T_DATA /* int errnum; */ +#define __IDMAP_LIST_T res_list_t +#define __IDMAP_NODE_T res_node_t +#define __INIT_IDMAP_LIST __init_res_list +#define __FINI_IDMAP_LIST __fini_res_list +#define __MAKE_IDMAP_NODE __new_res_node +#define __FREE_IDMAP_NODE __free_res_node +#include "../lib/idmap-imp.h" -#undef __IMAP_NODE_T_DATA -#undef __IMAP_LIST_T_DATA -#undef __IMAP_LIST_T -#undef __IMAP_NODE_T -#undef __MAKE_IMAP_NODE -#undef __FREE_IMAP_NODE +#undef __IDMAP_NODE_T_DATA +#undef __IDMAP_LIST_T_DATA +#undef __IDMAP_LIST_T +#undef __IDMAP_NODE_T +#undef __INIT_IDMAP_LIST +#undef __FINI_IDMAP_LIST +#undef __MAKE_IDMAP_NODE +#undef __FREE_IDMAP_NODE -#define __IMAP_NODE_T_DATA MYSQL_STMT* stmt; MYSQL_BIND* param_binds; param_data_t* param_data; hawk_oow_t param_capa; MYSQL_BIND* res_binds; res_data_t* res_data; hawk_oow_t res_capa; -#define __IMAP_LIST_T_DATA /* int errnum; */ -#define __IMAP_LIST_T stmt_list_t -#define __IMAP_NODE_T stmt_node_t -#define __MAKE_IMAP_NODE __new_stmt_node -#define __FREE_IMAP_NODE __free_stmt_node -#include "../lib/imap-imp.h" +#define __IDMAP_NODE_T_DATA MYSQL_STMT* stmt; MYSQL_BIND* param_binds; param_data_t* param_data; hawk_oow_t param_capa; MYSQL_BIND* res_binds; res_data_t* res_data; hawk_oow_t res_capa; +#define __IDMAP_LIST_T_DATA /* int errnum; */ +#define __IDMAP_LIST_T stmt_list_t +#define __IDMAP_NODE_T stmt_node_t +#define __INIT_IDMAP_LIST __init_stmt_list +#define __FINI_IDMAP_LIST __fini_stmt_list +#define __MAKE_IDMAP_NODE __new_stmt_node +#define __FREE_IDMAP_NODE __free_stmt_node +#include "../lib/idmap-imp.h" struct rtx_data_t { @@ -1832,12 +1842,19 @@ static int query (hawk_mod_t* mod, hawk_t* awk, const hawk_ooch_t* name, hawk_mo static int init (hawk_mod_t* mod, hawk_rtx_t* rtx) { hawk_rbt_t* rbt; - rtx_data_t data; + rtx_data_t data, * datap; + hawk_rbt_pair_t* pair; rbt = (hawk_rbt_t*)mod->ctx; HAWK_MEMSET (&data, 0, HAWK_SIZEOF(data)); - if (hawk_rbt_insert(rbt, &rtx, HAWK_SIZEOF(rtx), &data, HAWK_SIZEOF(data)) == HAWK_NULL) return -1; + pair = hawk_rbt_insert(rbt, &rtx, HAWK_SIZEOF(rtx), &data, HAWK_SIZEOF(data)); + if (HAWK_UNLIKELY(!pair)) return -1; + + datap = (rtx_data_t*)HAWK_RBT_VPTR(pair); + __init_sql_list (rtx, &datap->sql_list); + __init_res_list (rtx, &datap->res_list); + __init_stmt_list (rtx, &datap->stmt_list); return 0; } @@ -1854,26 +1871,12 @@ static void fini (hawk_mod_t* mod, hawk_rtx_t* rtx) if (pair) { rtx_data_t* data; - sql_node_t* sql_node, * sql_next; - res_node_t* res_node, * res_next; data = (rtx_data_t*)HAWK_RBT_VPTR(pair); - res_node = data->res_list.head; - while (res_node) - { - res_next = res_node->next; - free_res_node (rtx, &data->res_list, res_node); - res_node = res_next; - } - - sql_node = data->sql_list.head; - while (sql_node) - { - sql_next = sql_node->next; - free_sql_node (rtx, &data->sql_list, sql_node); - sql_node = sql_next; - } + __fini_stmt_list (rtx, &data->stmt_list); + __fini_res_list (rtx, &data->res_list); + __fini_sql_list (rtx, &data->sql_list); hawk_rbt_delete (rbt, &rtx, HAWK_SIZEOF(rtx)); } @@ -1901,7 +1904,7 @@ int hawk_mod_mysql (hawk_mod_t* mod, hawk_t* hawk) mod->fini = fini; rbt = hawk_rbt_open(hawk_getgem(hawk), 0, 1, 1); - if (rbt == HAWK_NULL) return -1; + if (HAWK_UNLIKELY(!rbt)) return -1; hawk_rbt_setstyle (rbt, hawk_get_rbt_style(HAWK_RBT_STYLE_INLINE_COPIERS));