]> git.vpit.fr Git - perl/modules/indirect.git/blobdiff - ptable.h
Remove the code coverage link
[perl/modules/indirect.git] / ptable.h
index ba275bac8c717523f844034a31336d817820aaaa..4b48aa600ea9f594b5a6e98612973192ae36eee2 100644 (file)
--- 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<T>(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_
 # define PTABLE_NAME ptable
 #endif
 
-#ifndef PTABLE_VAL_FREE
-# define PTABLE_VAL_FREE(V)
-#endif
-
 #ifndef PTABLE_JOIN
 # define PTABLE_PASTE(A, B) A ## B
 # define PTABLE_JOIN(A, B)  PTABLE_PASTE(A, B)
 # define PTABLE_PREFIX(X) PTABLE_JOIN(PTABLE_NAME, X)
 #endif
 
+#ifndef PTABLE_NEED_DELETE
+# define PTABLE_NEED_DELETE 1
+#endif
+
+#ifndef PTABLE_NEED_WALK
+# define PTABLE_NEED_WALK 1
+#endif
+
 #ifndef ptable_ent
 typedef struct ptable_ent {
  struct ptable_ent *next;
@@ -70,19 +81,20 @@ typedef struct ptable_ent {
 #ifndef ptable
 typedef struct ptable {
  ptable_ent **ary;
UV           max;
UV           items;
size_t       max;
size_t       items;
 } ptable;
 #define ptable ptable
 #endif /* !ptable */
 
 #ifndef ptable_new
-STATIC ptable *ptable_new(pPTBLMS) {
+static ptable *ptable_new(pPTBLMS) {
 #define ptable_new() ptable_new(aPTBLMS)
- ptable *t = PerlMemShared_malloc(sizeof *t);
- t->max   = 15;
- t->items = 0;
- t->ary   = PerlMemShared_calloc(t->max + 1, sizeof *t->ary);
+ ptable *t = VOID2(ptable *, PerlMemShared_malloc(sizeof *t));
+ t->max    = 15;
+ t->items  = 0;
+ t->ary    = VOID2(ptable_ent **,
+                              PerlMemShared_calloc(t->max + 1, sizeof *t->ary));
  return t;
 }
 #endif /* !ptable_new */
@@ -93,7 +105,7 @@ STATIC ptable *ptable_new(pPTBLMS) {
 #endif
 
 #ifndef ptable_find
-STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
+static ptable_ent *ptable_find(const ptable * const t, const void * const key) {
 #define ptable_find ptable_find
  ptable_ent *ent;
  const UV hash = PTABLE_HASH(key);
@@ -109,7 +121,7 @@ STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
 #endif /* !ptable_find */
 
 #ifndef ptable_fetch
-STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
+static void *ptable_fetch(const ptable * const t, const void * const key) {
 #define ptable_fetch ptable_fetch
  const ptable_ent *const ent = ptable_find(t, key);
 
@@ -117,25 +129,15 @@ STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
 }
 #endif /* !ptable_fetch */
 
-STATIC void PTABLE_PREFIX(_delete)(pPTBL_ const ptable * const t, const void * const key) {
- ptable_ent *const ent = ptable_find(t, key);
-
- if (ent) {
-  void *val = ent->val;
-  PTABLE_VAL_FREE(val);
-  ent->val = NULL;
- }
-}
-
 #ifndef ptable_split
-STATIC void ptable_split(pPTBLMS_ ptable * const t) {
+static void ptable_split(pPTBLMS_ ptable * const t) {
 #define ptable_split(T) ptable_split(aPTBLMS_ (T))
  ptable_ent **ary = t->ary;
- const UV oldsize = t->max + 1;
UV newsize = oldsize * 2;
UV i;
+ const size_t oldsize = t->max + 1;
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;
@@ -158,16 +160,18 @@ STATIC void ptable_split(pPTBLMS_ ptable * const t) {
 }
 #endif /* !ptable_split */
 
-STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) {
+static void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) {
  ptable_ent *ent = ptable_find(t, key);
 
  if (ent) {
+#ifdef PTABLE_VAL_FREE
   void *oldval = ent->val;
   PTABLE_VAL_FREE(oldval);
+#endif
   ent->val = val;
- } else {
-  const UV i = PTABLE_HASH(key) & t->max;
-  ent = PerlMemShared_malloc(sizeof *ent);
+ } else if (val) {
+  const size_t i = PTABLE_HASH(key) & t->max;
+  ent = VOID2(ptable_ent *, PerlMemShared_malloc(sizeof *ent));
   ent->key  = key;
   ent->val  = val;
   ent->next = t->ary[i];
@@ -178,34 +182,65 @@ STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const ke
  }
 }
 
-#ifndef ptable_walk
-STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) {
+#if PTABLE_NEED_DELETE
+
+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;
+#ifdef PTABLE_VAL_FREE
+  PTABLE_VAL_FREE(ent->val);
+#endif
+  PerlMemShared_free(ent);
+ }
+}
+
+#endif /* PTABLE_NEED_DELETE */
+
+#if PTABLE_NEED_WALK && !defined(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))
  if (t && t->items) {
   register ptable_ent ** const array = t->ary;
-  UV i = t->max;
+  size_t i = t->max;
   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--);
  }
 }
-#endif /* !ptable_walk */
 
-STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
+#endif /* PTABLE_NEED_WALK && !defined(ptable_walk) */
+
+static void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
  if (t && t->items) {
   register ptable_ent ** const array = t->ary;
-  UV i = t->max;
+  size_t i = t->max;
 
   do {
    ptable_ent *entry = array[i];
    while (entry) {
-    ptable_ent * const oentry = entry;
-    void *val = oentry->val;
-    entry = entry->next;
-    PTABLE_VAL_FREE(val);
-    PerlMemShared_free(oentry);
+    ptable_ent * const nentry = entry->next;
+#ifdef PTABLE_VAL_FREE
+    PTABLE_VAL_FREE(entry->val);
+#endif
+    PerlMemShared_free(entry);
+    entry = nentry;
    }
    array[i] = NULL;
   } while (i--);
@@ -214,7 +249,7 @@ STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
  }
 }
 
-STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
+static void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
  if (!t)
   return;
  PTABLE_PREFIX(_clear)(aPTBL_ t);
@@ -229,3 +264,6 @@ STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
 
 #undef PTABLE_NAME
 #undef PTABLE_VAL_FREE
+
+#undef PTABLE_NEED_DELETE
+#undef PTABLE_NEED_WALK