From: Vincent Pit Date: Wed, 29 Dec 2010 22:48:28 +0000 (+0100) Subject: Update ptable.h X-Git-Tag: rt62800~1 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=d21fd320d5ae8718535092253b4258aa224a049c;p=perl%2Fmodules%2Fautovivification.git Update ptable.h --- diff --git a/autovivification.xs b/autovivification.xs index a42b980..f198b1d 100644 --- a/autovivification.xs +++ b/autovivification.xs @@ -389,7 +389,7 @@ STATIC void a_map_delete(pTHX_ const OP *o) { MUTEX_LOCK(&a_op_map_mutex); #endif - ptable_map_store(a_op_map, o, NULL); + ptable_map_delete(a_op_map, o); #ifdef USE_ITHREADS MUTEX_UNLOCK(&a_op_map_mutex); diff --git a/ptable.h b/ptable.h index 4c25aa2..97849de 100644 --- a/ptable.h +++ b/ptable.h @@ -9,6 +9,13 @@ /* This header is designed to be included several times with different * definitions for PTABLE_NAME and PTABLE_VAL_FREE(). */ +#undef VOID2 +#ifdef __cplusplus +# define VOID2(T, P) static_cast(P) +#else +# define VOID2(T, P) (P) +#endif + #undef pPTBLMS #undef pPTBLMS_ #undef aPTBLMS @@ -22,7 +29,7 @@ # define aPTBLMS aTHX # define aPTBLMS_ aTHX_ #else -# define pPTBLMS +# define pPTBLMS void # define pPTBLMS_ # define aPTBLMS # define aPTBLMS_ @@ -79,10 +86,11 @@ typedef struct ptable { #ifndef ptable_new STATIC ptable *ptable_new(pPTBLMS) { #define ptable_new() ptable_new(aPTBLMS) - ptable *t = PerlMemShared_malloc(sizeof *t); - t->max = 63; - t->items = 0; - t->ary = PerlMemShared_calloc(t->max + 1, sizeof *t->ary); + ptable *t = VOID2(ptable *, PerlMemShared_malloc(sizeof *t)); + t->max = 63; + t->items = 0; + t->ary = VOID2(ptable_ent **, + PerlMemShared_calloc(t->max + 1, sizeof *t->ary)); return t; } #endif /* !ptable_new */ @@ -125,7 +133,7 @@ STATIC void ptable_split(pPTBLMS_ ptable * const t) { size_t newsize = oldsize * 2; size_t i; - ary = PerlMemShared_realloc(ary, newsize * sizeof(*ary)); + ary = VOID2(ptable_ent **, PerlMemShared_realloc(ary, newsize * sizeof(*ary))); Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary)); t->max = --newsize; t->ary = ary; @@ -157,7 +165,7 @@ STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const ke ent->val = val; } else if (val) { const size_t i = PTABLE_HASH(key) & t->max; - ent = PerlMemShared_malloc(sizeof *ent); + ent = VOID2(ptable_ent *, PerlMemShared_malloc(sizeof *ent)); ent->key = key; ent->val = val; ent->next = t->ary[i]; @@ -168,6 +176,27 @@ STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const ke } } +STATIC void PTABLE_PREFIX(_delete)(pPTBL_ ptable * const t, const void * const key) { + ptable_ent *prev, *ent; + const size_t i = PTABLE_HASH(key) & t->max; + + prev = NULL; + ent = t->ary[i]; + for (; ent; prev = ent, ent = ent->next) { + if (ent->key == key) + break; + } + + if (ent) { + if (prev) + prev->next = ent->next; + else + t->ary[i] = ent->next; + PTABLE_VAL_FREE(ent->val); + PerlMemShared_free(ent); + } +} + #ifndef ptable_walk STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) { #define ptable_walk(T, CB, UD) ptable_walk(aTHX_ (T), (CB), (UD)) @@ -177,7 +206,8 @@ STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent do { ptable_ent *entry; for (entry = array[i]; entry; entry = entry->next) - cb(aTHX_ entry, userdata); + if (entry->val) + cb(aTHX_ entry, userdata); } while (i--); } }