Blame ptable.h

Packit Service 7500fa
/* This file is part of the Variable::Magic Perl module.
Packit Service 7500fa
 * See http://search.cpan.org/dist/Variable-Magic/ */
Packit Service 7500fa
Packit Service 7500fa
/* This is a pointer table implementation essentially copied from the ptr_table
Packit Service 7500fa
 * implementation in perl's sv.c, except that it has been modified to use memory
Packit Service 7500fa
 * shared across threads.
Packit Service 7500fa
 * Copyright goes to the original authors, bug reports to me. */
Packit Service 7500fa
Packit Service 7500fa
/* This header is designed to be included several times with different
Packit Service 7500fa
 * definitions for PTABLE_NAME and PTABLE_VAL_FREE(). */
Packit Service 7500fa
Packit Service 7500fa
#undef VOID2
Packit Service 7500fa
#ifdef __cplusplus
Packit Service 7500fa
# define VOID2(T, P) static_cast<T>(P)
Packit Service 7500fa
#else
Packit Service 7500fa
# define VOID2(T, P) (P)
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#undef pPTBLMS
Packit Service 7500fa
#undef pPTBLMS_
Packit Service 7500fa
#undef aPTBLMS
Packit Service 7500fa
#undef aPTBLMS_
Packit Service 7500fa
Packit Service 7500fa
/* Context for PerlMemShared_* functions */
Packit Service 7500fa
Packit Service 7500fa
#ifdef PERL_IMPLICIT_SYS
Packit Service 7500fa
# define pPTBLMS  pTHX
Packit Service 7500fa
# define pPTBLMS_ pTHX_
Packit Service 7500fa
# define aPTBLMS  aTHX
Packit Service 7500fa
# define aPTBLMS_ aTHX_
Packit Service 7500fa
#else
Packit Service 7500fa
# define pPTBLMS  void
Packit Service 7500fa
# define pPTBLMS_
Packit Service 7500fa
# define aPTBLMS
Packit Service 7500fa
# define aPTBLMS_
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef pPTBL
Packit Service 7500fa
# define pPTBL  pPTBLMS
Packit Service 7500fa
#endif
Packit Service 7500fa
#ifndef pPTBL_
Packit Service 7500fa
# define pPTBL_ pPTBLMS_
Packit Service 7500fa
#endif
Packit Service 7500fa
#ifndef aPTBL
Packit Service 7500fa
# define aPTBL  aPTBLMS
Packit Service 7500fa
#endif
Packit Service 7500fa
#ifndef aPTBL_
Packit Service 7500fa
# define aPTBL_ aPTBLMS_
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef PTABLE_NAME
Packit Service 7500fa
# define PTABLE_NAME ptable
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef PTABLE_VAL_FREE
Packit Service 7500fa
# define PTABLE_VAL_FREE(V)
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef PTABLE_JOIN
Packit Service 7500fa
# define PTABLE_PASTE(A, B) A ## B
Packit Service 7500fa
# define PTABLE_JOIN(A, B)  PTABLE_PASTE(A, B)
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef PTABLE_PREFIX
Packit Service 7500fa
# define PTABLE_PREFIX(X) PTABLE_JOIN(PTABLE_NAME, X)
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_ent
Packit Service 7500fa
typedef struct ptable_ent {
Packit Service 7500fa
 struct ptable_ent *next;
Packit Service 7500fa
 const void *       key;
Packit Service 7500fa
 void *             val;
Packit Service 7500fa
} ptable_ent;
Packit Service 7500fa
#define ptable_ent ptable_ent
Packit Service 7500fa
#endif /* !ptable_ent */
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable
Packit Service 7500fa
typedef struct ptable {
Packit Service 7500fa
 ptable_ent **ary;
Packit Service 7500fa
 size_t       max;
Packit Service 7500fa
 size_t       items;
Packit Service 7500fa
} ptable;
Packit Service 7500fa
#define ptable ptable
Packit Service 7500fa
#endif /* !ptable */
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_new
Packit Service 7500fa
STATIC ptable *ptable_new(pPTBLMS) {
Packit Service 7500fa
#define ptable_new() ptable_new(aPTBLMS)
Packit Service 7500fa
 ptable *t = VOID2(ptable *, PerlMemShared_malloc(sizeof *t));
Packit Service 7500fa
 t->max    = 15;
Packit Service 7500fa
 t->items  = 0;
Packit Service 7500fa
 t->ary    = VOID2(ptable_ent **,
Packit Service 7500fa
                              PerlMemShared_calloc(t->max + 1, sizeof *t->ary));
Packit Service 7500fa
 return t;
Packit Service 7500fa
}
Packit Service 7500fa
#endif /* !ptable_new */
Packit Service 7500fa
Packit Service 7500fa
#ifndef PTABLE_HASH
Packit Service 7500fa
# define PTABLE_HASH(ptr) \
Packit Service 7500fa
     ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
Packit Service 7500fa
#endif
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_find
Packit Service 7500fa
STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
Packit Service 7500fa
#define ptable_find ptable_find
Packit Service 7500fa
 ptable_ent *ent;
Packit Service 7500fa
 const UV hash = PTABLE_HASH(key);
Packit Service 7500fa
Packit Service 7500fa
 ent = t->ary[hash & t->max];
Packit Service 7500fa
 for (; ent; ent = ent->next) {
Packit Service 7500fa
  if (ent->key == key)
Packit Service 7500fa
   return ent;
Packit Service 7500fa
 }
Packit Service 7500fa
Packit Service 7500fa
 return NULL;
Packit Service 7500fa
}
Packit Service 7500fa
#endif /* !ptable_find */
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_fetch
Packit Service 7500fa
STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
Packit Service 7500fa
#define ptable_fetch ptable_fetch
Packit Service 7500fa
 const ptable_ent *const ent = ptable_find(t, key);
Packit Service 7500fa
Packit Service 7500fa
 return ent ? ent->val : NULL;
Packit Service 7500fa
}
Packit Service 7500fa
#endif /* !ptable_fetch */
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_split
Packit Service 7500fa
STATIC void ptable_split(pPTBLMS_ ptable * const t) {
Packit Service 7500fa
#define ptable_split(T) ptable_split(aPTBLMS_ (T))
Packit Service 7500fa
 ptable_ent **ary = t->ary;
Packit Service 7500fa
 const size_t oldsize = t->max + 1;
Packit Service 7500fa
 size_t newsize = oldsize * 2;
Packit Service 7500fa
 size_t i;
Packit Service 7500fa
Packit Service 7500fa
 ary = VOID2(ptable_ent **, PerlMemShared_realloc(ary, newsize * sizeof(*ary)));
Packit Service 7500fa
 Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary));
Packit Service 7500fa
 t->max = --newsize;
Packit Service 7500fa
 t->ary = ary;
Packit Service 7500fa
Packit Service 7500fa
 for (i = 0; i < oldsize; i++, ary++) {
Packit Service 7500fa
  ptable_ent **curentp, **entp, *ent;
Packit Service 7500fa
  if (!*ary)
Packit Service 7500fa
   continue;
Packit Service 7500fa
  curentp = ary + oldsize;
Packit Service 7500fa
  for (entp = ary, ent = *ary; ent; ent = *entp) {
Packit Service 7500fa
   if ((newsize & PTABLE_HASH(ent->key)) != i) {
Packit Service 7500fa
    *entp     = ent->next;
Packit Service 7500fa
    ent->next = *curentp;
Packit Service 7500fa
    *curentp  = ent;
Packit Service 7500fa
    continue;
Packit Service 7500fa
   } else
Packit Service 7500fa
    entp = &ent->next;
Packit Service 7500fa
  }
Packit Service 7500fa
 }
Packit Service 7500fa
}
Packit Service 7500fa
#endif /* !ptable_split */
Packit Service 7500fa
Packit Service 7500fa
STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) {
Packit Service 7500fa
 ptable_ent *ent = ptable_find(t, key);
Packit Service 7500fa
Packit Service 7500fa
 if (ent) {
Packit Service 7500fa
  void *oldval = ent->val;
Packit Service 7500fa
  PTABLE_VAL_FREE(oldval);
Packit Service 7500fa
  ent->val = val;
Packit Service 7500fa
 } else if (val) {
Packit Service 7500fa
  const size_t i = PTABLE_HASH(key) & t->max;
Packit Service 7500fa
  ent = VOID2(ptable_ent *, PerlMemShared_malloc(sizeof *ent));
Packit Service 7500fa
  ent->key  = key;
Packit Service 7500fa
  ent->val  = val;
Packit Service 7500fa
  ent->next = t->ary[i];
Packit Service 7500fa
  t->ary[i] = ent;
Packit Service 7500fa
  t->items++;
Packit Service 7500fa
  if (ent->next && t->items > t->max)
Packit Service 7500fa
   ptable_split(t);
Packit Service 7500fa
 }
Packit Service 7500fa
}
Packit Service 7500fa
Packit Service 7500fa
#ifndef ptable_walk
Packit Service 7500fa
STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) {
Packit Service 7500fa
#define ptable_walk(T, CB, UD) ptable_walk(aTHX_ (T), (CB), (UD))
Packit Service 7500fa
 if (t && t->items) {
Packit Service 7500fa
  register ptable_ent ** const array = t->ary;
Packit Service 7500fa
  size_t i = t->max;
Packit Service 7500fa
  do {
Packit Service 7500fa
   ptable_ent *entry;
Packit Service 7500fa
   for (entry = array[i]; entry; entry = entry->next)
Packit Service 7500fa
    cb(aTHX_ entry, userdata);
Packit Service 7500fa
  } while (i--);
Packit Service 7500fa
 }
Packit Service 7500fa
}
Packit Service 7500fa
#endif /* !ptable_walk */
Packit Service 7500fa
Packit Service 7500fa
STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
Packit Service 7500fa
 if (t && t->items) {
Packit Service 7500fa
  register ptable_ent ** const array = t->ary;
Packit Service 7500fa
  size_t i = t->max;
Packit Service 7500fa
Packit Service 7500fa
  do {
Packit Service 7500fa
   ptable_ent *entry = array[i];
Packit Service 7500fa
   while (entry) {
Packit Service 7500fa
    ptable_ent * const oentry = entry;
Packit Service 7500fa
    void *val = oentry->val;
Packit Service 7500fa
    entry = entry->next;
Packit Service 7500fa
    PTABLE_VAL_FREE(val);
Packit Service 7500fa
    PerlMemShared_free(oentry);
Packit Service 7500fa
   }
Packit Service 7500fa
   array[i] = NULL;
Packit Service 7500fa
  } while (i--);
Packit Service 7500fa
Packit Service 7500fa
  t->items = 0;
Packit Service 7500fa
 }
Packit Service 7500fa
}
Packit Service 7500fa
Packit Service 7500fa
STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
Packit Service 7500fa
 if (!t)
Packit Service 7500fa
  return;
Packit Service 7500fa
 PTABLE_PREFIX(_clear)(aPTBL_ t);
Packit Service 7500fa
 PerlMemShared_free(t->ary);
Packit Service 7500fa
 PerlMemShared_free(t);
Packit Service 7500fa
}
Packit Service 7500fa
Packit Service 7500fa
#undef pPTBL
Packit Service 7500fa
#undef pPTBL_
Packit Service 7500fa
#undef aPTBL
Packit Service 7500fa
#undef aPTBL_
Packit Service 7500fa
Packit Service 7500fa
#undef PTABLE_NAME
Packit Service 7500fa
#undef PTABLE_VAL_FREE