X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=reap.h;fp=reap.h;h=9073899088bc07fc2993ed92e1a3656a1af67adf;hb=daea523653756bb45d89c7b17bbc6ac70f37019d;hp=0000000000000000000000000000000000000000;hpb=120d12f635bf0ef0f334f463c455a0d2082db4e9;p=perl%2Fmodules%2Fautovivification.git diff --git a/reap.h b/reap.h new file mode 100644 index 0000000..9073899 --- /dev/null +++ b/reap.h @@ -0,0 +1,81 @@ +/* This file is part of the autovivification Perl module. + * See http://search.cpan.org/dist/autovivification/ */ + +/* This header provides a specialized version of Scope::Upper::reap that can be + * called directly from XS. + * See http://search.cpan.org/dist/Scope-Upper/ for details. */ + +#ifndef REAP_H +#define REAP_H 1 + +#define REAP_DESTRUCTOR_SIZE 3 + +typedef struct { + I32 depth; + I32 *origin; + void (*cb)(pTHX_ void *); + void *ud; + char *dummy; +} reap_ud; + +STATIC void reap_pop(pTHX_ void *); + +STATIC void reap_pop(pTHX_ void *ud_) { + reap_ud *ud = ud_; + I32 depth, *origin, mark, base; + + depth = ud->depth; + origin = ud->origin; + mark = origin[depth]; + base = origin[depth - 1]; + + if (base < mark) { + PL_savestack_ix = mark; + leave_scope(base); + } + PL_savestack_ix = base; + + if ((ud->depth = --depth) > 0) { + SAVEDESTRUCTOR_X(reap_pop, ud); + } else { + void (*cb)(pTHX_ void *) = ud->cb; + void *cb_ud = ud->ud; + + PerlMemShared_free(ud->origin); + PerlMemShared_free(ud); + + SAVEDESTRUCTOR_X(cb, cb_ud); + } +} + +STATIC void reap(pTHX_ I32 depth, void (*cb)(pTHX_ void *), void *cb_ud) { +#define reap(D, CB, UD) reap(aTHX_ (D), (CB), (UD)) + reap_ud *ud; + I32 i; + + if (depth > PL_scopestack_ix) + depth = PL_scopestack_ix; + + ud = PerlMemShared_malloc(sizeof *ud); + ud->depth = depth; + ud->origin = PerlMemShared_malloc((depth + 1) * sizeof *ud->origin); + ud->cb = cb; + ud->ud = cb_ud; + ud->dummy = NULL; + + for (i = depth; i >= 1; --i) { + I32 j = PL_scopestack_ix - i; + ud->origin[depth - i] = PL_scopestack[j]; + PL_scopestack[j] += REAP_DESTRUCTOR_SIZE; + } + ud->origin[depth] = PL_savestack_ix; + + while (PL_savestack_ix + REAP_DESTRUCTOR_SIZE + <= PL_scopestack[PL_scopestack_ix - 1]) { + save_pptr(&ud->dummy); + } + + SAVEDESTRUCTOR_X(reap_pop, ud); +} + +#endif /* REAP_H */