diff --git a/ase/lib/cmn/dll.c b/ase/lib/cmn/dll.c index 3a5a39bb..76288b46 100644 --- a/ase/lib/cmn/dll.c +++ b/ase/lib/cmn/dll.c @@ -7,9 +7,7 @@ #include #include "mem.h" -ase_dll_t* ase_dll_open ( - ase_mmgr_t* mmgr, ase_size_t ext, - void (*init) (ase_dll_t*, void*), void* init_data) +ase_dll_t* ase_dll_open (ase_mmgr_t* mmgr, ase_size_t ext) { ase_dll_t* dll; @@ -26,11 +24,10 @@ ase_dll_t* ase_dll_open ( dll = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(ase_dll_t) + ext); if (dll == ASE_NULL) return ASE_NULL; - ASE_MEMSET (dll, 0, ASE_SIZEOF(ase_dll_t) + ext); + /* do not zero the extension */ + ASE_MEMSET (dll, 0, ASE_SIZEOF(ase_dll_t)); dll->mmgr = mmgr; - if (init) init (dll, init_data); - return dll; } diff --git a/ase/lib/cmn/map.c b/ase/lib/cmn/map.c index 54ddece1..a71009bc 100644 --- a/ase/lib/cmn/map.c +++ b/ase/lib/cmn/map.c @@ -1,5 +1,5 @@ /* - * $Id: map.c 349 2008-08-28 14:21:25Z baconevi $ + * $Id: map.c 350 2008-08-29 14:51:04Z baconevi $ * * {License} */ @@ -198,7 +198,6 @@ static pair_t* change_pair_val ( map_t* ase_map_open ( mmgr_t* mmgr, size_t ext, - void (*init) (map_t*, void*), void* init_data, size_t capa, unsigned int load_factor) { map_t* map; @@ -229,7 +228,8 @@ map_t* ase_map_open ( return ASE_NULL; } - ASE_MEMSET (map, 0, SIZEOF(map_t)+SIZEOF(priv_t)+ext); + /* do not zero out the extension */ + ASE_MEMSET (map, 0, SIZEOF(map_t)+SIZEOF(priv_t)); map->mmgr = mmgr; map->size = 0; @@ -239,8 +239,6 @@ map_t* ase_map_open ( map->hasher = hash_key; map->comper = comp_key; - if (init) init (map, init_data); - return map; } @@ -307,6 +305,7 @@ hasher_t ase_map_gethasher (map_t* map) void ase_map_sethasher (map_t* map, hasher_t hasher) { + if (hasher == ASE_NULL) hasher = hash_key; map->hasher = hasher; } @@ -317,6 +316,7 @@ comper_t ase_map_getcomper (map_t* map) void ase_map_setcomper (map_t* map, comper_t comper) { + if (comper == ASE_NULL) comper = comp_key; map->comper = comper; } @@ -345,8 +345,7 @@ size_t ase_map_getcapa (map_t* map) return map->capa; } -pair_t* ase_map_get ( - map_t* map, const void* kptr, size_t klen) +pair_t* ase_map_search (map_t* map, const void* kptr, size_t klen) { pair_t* pair; size_t hc; @@ -367,18 +366,7 @@ pair_t* ase_map_get ( return ASE_NULL; } -pair_t* ase_map_put (map_t* map, - void* kptr, size_t klen, void* vptr, size_t vlen) -{ - int n; - pair_t* px; - - n = ase_map_putx (map, kptr, klen, vptr, vlen, &px); - if (n < 0) return ASE_NULL; - return px; -} - -int ase_map_putx ( +int ase_map_put ( map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen, pair_t** px) { @@ -423,19 +411,6 @@ int ase_map_putx ( ASE_ASSERT (pair == ASE_NULL); -#if 0 - pair = (pair_t*) ASE_MMGR_ALLOC (map->mmgr, - SIZEOF(pair_t) + ((klen+1)*SIZEOF(*kptr))); - if (pair == ASE_NULL) return -1; /* error */ - - /* duplicate the key if it is new */ - KPTR(pair) = (void*)(pair + 1); - ase_strncpy (KPTR(pair), kptr, klen); - - KLEN(pair) = klen; - VPTR(pair) = vptr; - VLEN(pair) = vlen; -#endif pair = alloc_pair (map, kptr, klen, vptr, vlen); if (pair == ASE_NULL) return -1; /* error */ @@ -447,7 +422,20 @@ int ase_map_putx ( return 1; /* new key added */ } -pair_t* ase_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) +pair_t* ase_map_upsert ( + map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) +{ + /* update if the key exists, otherwise insert a new pair */ + int n; + pair_t* px; + + n = ase_map_put (map, kptr, klen, vptr, vlen, &px); + if (n < 0) return ASE_NULL; + return px; +} + +pair_t* ase_map_insert ( + map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) { pair_t* pair; size_t hc; @@ -485,7 +473,8 @@ pair_t* ase_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t return pair; } -pair_t* ase_map_update (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) +pair_t* ase_map_update ( + map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) { pair_t* pair, * p, * prev, * next; size_t hc; diff --git a/ase/lib/cmn/sll.c b/ase/lib/cmn/sll.c index aff2a233..41fb0bff 100644 --- a/ase/lib/cmn/sll.c +++ b/ase/lib/cmn/sll.c @@ -24,9 +24,7 @@ #define size_t ase_size_t #define mmgr_t ase_mmgr_t -sll_t* ase_sll_open ( - mmgr_t* mmgr, size_t ext, - void (*init) (sll_t*, void*), void* init_data) +sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext) { sll_t* sll; @@ -43,11 +41,10 @@ sll_t* ase_sll_open ( sll = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(sll_t) + ext); if (sll == ASE_NULL) return ASE_NULL; - ASE_MEMSET (sll, 0, ASE_SIZEOF(sll_t) + ext); + /* do not zero out the extension */ + ASE_MEMSET (sll, 0, ASE_SIZEOF(sll_t)); sll->mmgr = mmgr; - if (init) init (sll, init_data); - return sll; } diff --git a/ase/lib/cmn/str_dyn.c b/ase/lib/cmn/str_dyn.c index 127a676c..3c4a4ee3 100644 --- a/ase/lib/cmn/str_dyn.c +++ b/ase/lib/cmn/str_dyn.c @@ -1,5 +1,5 @@ /* - * $Id: str_dyn.c 332 2008-08-18 11:21:48Z baconevi $ + * $Id: str_dyn.c 350 2008-08-29 14:51:04Z baconevi $ * * {License} */ @@ -7,26 +7,37 @@ #include #include "mem.h" -ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa, ase_mmgr_t* mmgr) +ase_str_t* ase_str_open (ase_mmgr_t* mmgr, ase_size_t ext, ase_size_t capa) { - if (str == ASE_NULL) - { - str = (ase_str_t*) - ASE_MALLOC (mmgr, ASE_SIZEOF(ase_str_t)); - if (str == ASE_NULL) return ASE_NULL; - str->__dynamic = ASE_TRUE; - } - else str->__dynamic = ASE_FALSE; + ase_str_t* str; - str->mmgr = mmgr; - str->buf = (ase_char_t*) ASE_MALLOC ( - mmgr, ASE_SIZEOF(ase_char_t) * (capa + 1)); - if (str->buf == ASE_NULL) + str = (ase_str_t*) ASE_MMGR_ALLOC (mmgr, sizeof(ase_str_t) + ext); + if (str == ASE_NULL) return ASE_NULL; + + if (ase_str_init (str, mmgr, capa) == ASE_NULL) { - if (str->__dynamic) ASE_FREE (mmgr, str); + ASE_MMGR_FREE (mmgr, str); return ASE_NULL; } + return str; +} + +void ase_str_close (ase_str_t* str) +{ + ase_str_fini (str); + ASE_MMGR_FREE (str->mmgr, str); +} + +ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa) +{ + ASE_MEMSET (str, 0, sizeof(ase_str_t)); + + str->mmgr = mmgr; + str->buf = (ase_char_t*) ASE_MMGR_ALLOC ( + mmgr, sizeof(ase_char_t) * (capa + 1)); + if (str->buf == ASE_NULL) return ASE_NULL; + str->size = 0; str->capa = capa; str->buf[0] = ASE_T('\0'); @@ -34,10 +45,9 @@ ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa, ase_mmgr_t* mmgr) return str; } -void ase_str_close (ase_str_t* str) +void ase_str_fini (ase_str_t* str) { - ASE_FREE (str->mmgr, str->buf); - if (str->__dynamic) ASE_FREE (str->mmgr, str); + ASE_MMGR_FREE (str->mmgr, str->buf); } void ase_str_clear (ase_str_t* str) @@ -48,7 +58,8 @@ void ase_str_clear (ase_str_t* str) void ase_str_forfeit (ase_str_t* str) { - if (str->__dynamic) ASE_FREE (str->mmgr, str); + // TODO: how to handle this?????????????????????? + if (str->__dynamic) ASE_MMGR_FREE (str->mmgr, str); } void ase_str_swap (ase_str_t* str, ase_str_t* str1) @@ -83,11 +94,11 @@ ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len) if (len > str->capa) { - buf = (ase_char_t*) ASE_MALLOC ( - str->mmgr, ASE_SIZEOF(ase_char_t) * (len + 1)); + buf = (ase_char_t*) ASE_MMGR_ALLOC ( + str->mmgr, sizeof(ase_char_t) * (len + 1)); if (buf == ASE_NULL) return (ase_size_t)-1; - ASE_FREE (str->mmgr, str->buf); + ASE_MMGR_FREE (str->mmgr, str->buf); str->capa = len; str->buf = buf; } @@ -119,19 +130,19 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) { tmp = (ase_char_t*) ASE_REALLOC ( str->mmgr, str->buf, - ASE_SIZEOF(ase_char_t) * (capa + 1)); + sizeof(ase_char_t) * (capa + 1)); if (tmp == ASE_NULL) return (ase_size_t)-1; } else { - tmp = (ase_char_t*) ASE_MALLOC ( - str->mmgr, ASE_SIZEOF(ase_char_t)*(capa+1)); + tmp = (ase_char_t*) ASE_MMGR_ALLOC ( + str->mmgr, sizeof(ase_char_t)*(capa+1)); if (tmp == ASE_NULL) return (ase_size_t)-1; if (str->buf != ASE_NULL) { ASE_MEMCPY (tmp, str->buf, - ASE_SIZEOF(ase_char_t)*(str->capa+1)); - ASE_FREE (str->mmgr, str->buf); + sizeof(ase_char_t)*(str->capa+1)); + ASE_MMGR_FREE (str->mmgr, str->buf); } }