Data-HashMap-Shared

 view release on metacpan or  search on metacpan

Shared.xs  view on Meta::CPAN


#define RDLOCK_GUARD(handle) \
    shm_rwlock_rdlock(handle); \
    (handle)->lock_depth++; \
    SAVEDESTRUCTOR_X(shm_rdunlock_cleanup, (void*)(handle))

/* ---- The wrlock + seqlock write section of a batch write ----
 * No destructor releases it: the only croak possible inside, Perl's own
 * pending-signals croak, must leave the lock held and seq odd for stale-lock
 * recovery, never publish a half-written table.  shm_sig_check_every keeps that
 * croak out. */
#define WRSEQ_BEGIN(handle) \
    shm_rwlock_wrlock(handle); \
    shm_seqlock_write_begin(&(handle)->hdr->seq)

#define WRSEQ_END(handle) \
    shm_seqlock_write_end(&(handle)->hdr->seq); \
    shm_rwlock_wrunlock(handle)

/* free(), not Safefree: the buffer comes from shm_grow_buf's realloc(). */
static void shm_free_cleanup(pTHX_ void *ptr) {
    free(ptr);
}

/* An embedded NUL would truncate the path: unlink could remove another file.
 * The caller has run get-magic. */
static const char *shm_path_arg(pTHX_ SV *sv, const char *what, const char *classname) {
    STRLEN len;
    const char *p = SvPV_nomg(sv, len);
    if (memchr(p, '\0', len))
        croak("%s: %s contains an embedded NUL byte", classname, what);
    return p;
}

/* As core open: tainted data anywhere in the statement is fatal for a call that
 * writes, creates or removes a file.  Use it once every argument has been read;
 * opening read-only by a tainted name is allowed. */
#define SHM_TAINT_CHECK(op) STMT_START { TAINT_PROPER(op); } STMT_END

static const char *shm_write_path_arg(pTHX_ SV *sv, const char *what,
                                      const char *classname, const char *op) {
    const char *p = shm_path_arg(aTHX_ sv, what, classname);
    if (TAINT_get) {
        /* a -t warning runs Perl code, which may free sv's buffer */
        p = SvPVX(sv_2mortal(newSVpv(p, 0)));
        SHM_TAINT_CHECK(op);
    }
    return p;
}

/* A UV parameter would wrap -1 to "everything". */
static UV shm_count_arg(pTHX_ SV *sv, const char *what, const char *classname) {
    NV nv;
    SvGETMAGIC(sv);
    if (SvIOK(sv)) {
        if (!SvIsUV(sv) && SvIVX(sv) < 0) croak("%s: %s must not be negative", classname, what);
        return SvUVX(sv);
    }
    nv = SvNV_nomg(sv);
    if (nv < 0) croak("%s: %s must not be negative", classname, what);
    if (!(nv >= 1)) return 0;   /* also NaN */
    return nv >= (NV)UV_MAX ? UV_MAX : (UV)nv;
}

static inline uint32_t shm_u32_arg(pTHX_ SV *sv, const char *what, const char *classname, uint32_t dflt) {
    UV v;
    if (!sv) return dflt;
    if ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == SVf_IOK && (UV)SvIVX(sv) <= 0xFFFFFFFFu)
        return (uint32_t)SvIVX(sv);
    v = shm_count_arg(aTHX_ sv, what, classname);
    if (v > 0xFFFFFFFFu)
        croak("%s: %s %" UVuf " exceeds the maximum of %u",
              classname, what, v, 0xFFFFFFFFU);
    return (uint32_t)v;
}

/* An object invocant would stringify to "Class=SCALAR(0x...)". */
static const char *shm_invocant_class(pTHX_ SV *sv, const char *classname) {
    if (sv_isobject(sv))
        return HvNAME_get(SvSTASH(SvRV(sv)));
    if (SvROK(sv))
        croak("%s: constructor invocant must be a class name or object", classname);
    return SvPV_nolen(sv);
}

/* to_hash copies entries under the read lock (integers raw into `raw`, string
 * keys packed into `keys`, string values as SVs into `vals`, NULL for an integer
 * side) and builds the hash here, after the lock is released. */
#define SHM_KEY_UTF8_BIT 0x80000000u
static STRLEN shm_pack_key(pTHX_ SV *keys, STRLEN used, const char *p, uint32_t len, int utf8) {
    STRLEN need = used + sizeof(uint32_t) + len;
    uint32_t word = len | (utf8 ? SHM_KEY_UTF8_BIT : 0);
    char *d;
    if (need > SvLEN(keys)) SvGROW(keys, need + need / 4);
    d = SvPVX(keys) + used;
    memcpy(d, &word, sizeof word);
    memcpy(d + sizeof word, p, len);
    return need;
}

static SV *shm_copies_to_hashref(pTHX_ SV *raw, SV *keys, AV *vals, SSize_t n) {
    HV *hv = newHV();
    const IV *r = raw ? (const IV *)SvPVX(raw) : NULL;
    const char *kb = keys ? SvPVX(keys) : NULL;
    SV **s = vals ? AvARRAY(vals) : NULL;
    SSize_t ri = 0, vi = 0, j;
    STRLEN ko = 0;
    hv_ksplit(hv, n);
    for (j = 0; j < n; j++) {
        char kbuf[24];
        const char *kp = kbuf;
        I32 klen;
        SV *val;
        if (!keys) {
            klen = my_snprintf(kbuf, sizeof(kbuf), "%" IVdf, r[ri++]);
        } else {
            uint32_t word;
            memcpy(&word, kb + ko, sizeof word);
            kp = kb + ko + sizeof word;
            klen = (I32)(word & ~SHM_KEY_UTF8_BIT);
            ko += sizeof word + (STRLEN)klen;



( run in 1.724 second using v1.01-cache-2.11-cpan-60884a8df76 )