added ase_str_init(), ase_str_fini(), ase_map_init() and ase_map_fini() to support static creation.

This commit is contained in:
hyung-hwan 2008-08-30 08:51:04 +00:00
parent 2390cda730
commit dd00a6f49d
4 changed files with 67 additions and 73 deletions

View File

@ -7,9 +7,7 @@
#include <ase/cmn/dll.h> #include <ase/cmn/dll.h>
#include "mem.h" #include "mem.h"
ase_dll_t* ase_dll_open ( ase_dll_t* ase_dll_open (ase_mmgr_t* mmgr, ase_size_t ext)
ase_mmgr_t* mmgr, ase_size_t ext,
void (*init) (ase_dll_t*, void*), void* init_data)
{ {
ase_dll_t* dll; 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); dll = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(ase_dll_t) + ext);
if (dll == ASE_NULL) return ASE_NULL; 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; dll->mmgr = mmgr;
if (init) init (dll, init_data);
return dll; return dll;
} }

View File

@ -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} * {License}
*/ */
@ -198,7 +198,6 @@ static pair_t* change_pair_val (
map_t* ase_map_open ( map_t* ase_map_open (
mmgr_t* mmgr, size_t ext, mmgr_t* mmgr, size_t ext,
void (*init) (map_t*, void*), void* init_data,
size_t capa, unsigned int load_factor) size_t capa, unsigned int load_factor)
{ {
map_t* map; map_t* map;
@ -229,7 +228,8 @@ map_t* ase_map_open (
return ASE_NULL; 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->mmgr = mmgr;
map->size = 0; map->size = 0;
@ -239,8 +239,6 @@ map_t* ase_map_open (
map->hasher = hash_key; map->hasher = hash_key;
map->comper = comp_key; map->comper = comp_key;
if (init) init (map, init_data);
return map; return map;
} }
@ -307,6 +305,7 @@ hasher_t ase_map_gethasher (map_t* map)
void ase_map_sethasher (map_t* map, hasher_t hasher) void ase_map_sethasher (map_t* map, hasher_t hasher)
{ {
if (hasher == ASE_NULL) hasher = hash_key;
map->hasher = hasher; 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) void ase_map_setcomper (map_t* map, comper_t comper)
{ {
if (comper == ASE_NULL) comper = comp_key;
map->comper = comper; map->comper = comper;
} }
@ -345,8 +345,7 @@ size_t ase_map_getcapa (map_t* map)
return map->capa; return map->capa;
} }
pair_t* ase_map_get ( pair_t* ase_map_search (map_t* map, const void* kptr, size_t klen)
map_t* map, const void* kptr, size_t klen)
{ {
pair_t* pair; pair_t* pair;
size_t hc; size_t hc;
@ -367,18 +366,7 @@ pair_t* ase_map_get (
return ASE_NULL; return ASE_NULL;
} }
pair_t* ase_map_put (map_t* map, int ase_map_put (
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 (
map_t* map, void* kptr, size_t klen, map_t* map, void* kptr, size_t klen,
void* vptr, size_t vlen, pair_t** px) void* vptr, size_t vlen, pair_t** px)
{ {
@ -423,19 +411,6 @@ int ase_map_putx (
ASE_ASSERT (pair == ASE_NULL); 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); pair = alloc_pair (map, kptr, klen, vptr, vlen);
if (pair == ASE_NULL) return -1; /* error */ if (pair == ASE_NULL) return -1; /* error */
@ -447,7 +422,20 @@ int ase_map_putx (
return 1; /* new key added */ 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; pair_t* pair;
size_t hc; 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; 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; pair_t* pair, * p, * prev, * next;
size_t hc; size_t hc;

View File

@ -24,9 +24,7 @@
#define size_t ase_size_t #define size_t ase_size_t
#define mmgr_t ase_mmgr_t #define mmgr_t ase_mmgr_t
sll_t* ase_sll_open ( sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext)
mmgr_t* mmgr, size_t ext,
void (*init) (sll_t*, void*), void* init_data)
{ {
sll_t* sll; sll_t* sll;
@ -43,11 +41,10 @@ sll_t* ase_sll_open (
sll = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(sll_t) + ext); sll = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(sll_t) + ext);
if (sll == ASE_NULL) return ASE_NULL; 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; sll->mmgr = mmgr;
if (init) init (sll, init_data);
return sll; return sll;
} }

View File

@ -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} * {License}
*/ */
@ -7,26 +7,37 @@
#include <ase/cmn/str.h> #include <ase/cmn/str.h>
#include "mem.h" #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) ase_str_t* str;
{
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;
str->mmgr = mmgr; str = (ase_str_t*) ASE_MMGR_ALLOC (mmgr, sizeof(ase_str_t) + ext);
str->buf = (ase_char_t*) ASE_MALLOC ( if (str == ASE_NULL) return ASE_NULL;
mmgr, ASE_SIZEOF(ase_char_t) * (capa + 1));
if (str->buf == 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 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->size = 0;
str->capa = capa; str->capa = capa;
str->buf[0] = ASE_T('\0'); 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; return str;
} }
void ase_str_close (ase_str_t* str) void ase_str_fini (ase_str_t* str)
{ {
ASE_FREE (str->mmgr, str->buf); ASE_MMGR_FREE (str->mmgr, str->buf);
if (str->__dynamic) ASE_FREE (str->mmgr, str);
} }
void ase_str_clear (ase_str_t* str) 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) 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) 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) if (len > str->capa)
{ {
buf = (ase_char_t*) ASE_MALLOC ( buf = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, ASE_SIZEOF(ase_char_t) * (len + 1)); str->mmgr, sizeof(ase_char_t) * (len + 1));
if (buf == ASE_NULL) return (ase_size_t)-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->capa = len;
str->buf = buf; 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 ( tmp = (ase_char_t*) ASE_REALLOC (
str->mmgr, str->buf, 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; if (tmp == ASE_NULL) return (ase_size_t)-1;
} }
else else
{ {
tmp = (ase_char_t*) ASE_MALLOC ( tmp = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, ASE_SIZEOF(ase_char_t)*(capa+1)); str->mmgr, sizeof(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1; if (tmp == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL) if (str->buf != ASE_NULL)
{ {
ASE_MEMCPY (tmp, str->buf, ASE_MEMCPY (tmp, str->buf,
ASE_SIZEOF(ase_char_t)*(str->capa+1)); sizeof(ase_char_t)*(str->capa+1));
ASE_FREE (str->mmgr, str->buf); ASE_MMGR_FREE (str->mmgr, str->buf);
} }
} }