Data-HyperLogLog-Shared

 view release on metacpan or  search on metacpan

hll.h  view on Meta::CPAN

    uint32_t p   = h->hdr->precision;
    uint32_t m   = h->hdr->m;
    uint64_t off = h->hdr->regs_off;
    if (p < HLL_MIN_PRECISION || p > HLL_MAX_PRECISION) return NULL;
    if (m != (1u << p)) return NULL;                              /* ties m to p */
    if (off > h->mmap_size || (uint64_t)m > (uint64_t)h->mmap_size - off) return NULL;
    if (m_out) *m_out = m;
    if (p_out) *p_out = p;
    return (uint8_t *)((char *)h->base + off);
}

static inline HllHandle *hll_setup(void *base, size_t map_size,
                                   const char *path, int backing_fd) {
    HllHeader *hdr = (HllHeader *)base;
    HllHandle *h = (HllHandle *)calloc(1, sizeof(HllHandle));
    if (!h) {
        munmap(base, map_size);
        if (backing_fd >= 0) close(backing_fd);
        return NULL;
    }
    h->hdr          = hdr;
    h->base         = base;
    h->reader_slots = (HllReaderSlot *)((uint8_t *)base + sizeof(HllHeader));  /* trusted layout, not the peer-writable header offset */
    h->mmap_size    = map_size;
    h->path         = path ? strdup(path) : NULL;
    h->backing_fd   = backing_fd;
    h->my_slot_idx  = UINT32_MAX;
    return h;
}

/* Validate a mapped header (shared by hll_create reopen and hll_open_fd). */
static inline int hll_validate_header(const HllHeader *hdr, uint64_t file_size) {
    if (hdr->magic != HLL_MAGIC) return 0;
    if (hdr->version != HLL_VERSION) return 0;
    if (hdr->precision < HLL_MIN_PRECISION || hdr->precision > HLL_MAX_PRECISION) return 0;
    if (hdr->m != (1u << hdr->precision)) return 0;
    if (hdr->total_size != file_size) return 0;
    if (hdr->total_size != hll_total_size(hdr->m)) return 0;
    HllLayout L = hll_layout();
    if (hdr->reader_slots_off != L.reader_slots) return 0;
    if (hdr->regs_off != L.regs) return 0;
    return 1;
}

/* validate the precision argument */
static int hll_validate_create_args(uint32_t precision, uint32_t *m_out, char *errbuf) {
    if (errbuf) errbuf[0] = '\0';
    if (precision < HLL_MIN_PRECISION || precision > HLL_MAX_PRECISION) {
        HLL_ERR("precision must be between %d and %d", HLL_MIN_PRECISION, HLL_MAX_PRECISION);
        return 0;
    }
    *m_out = 1u << precision;
    return 1;
}

/* Securely obtain a fd: create exclusively (O_CREAT|O_EXCL|O_NOFOLLOW at mode,
 * default 0600), or attach an existing file (O_RDWR|O_NOFOLLOW, no O_CREAT). */
static int hll_secure_open(const char *path, mode_t mode, char *errbuf) {
    for (int attempt = 0; attempt < 100; attempt++) {
        int fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, mode);
        if (fd >= 0) { (void)fchmod(fd, mode); return fd; }   /* exact mode: umask narrowed the O_EXCL create */
        if (errno != EEXIST) { HLL_ERR("create %s: %s", path, strerror(errno)); return -1; }
        fd = open(path, O_RDWR|O_NOFOLLOW|O_CLOEXEC);
        if (fd >= 0) return fd;
        if (errno == ENOENT) continue;   /* creator unlinked between our two opens; retry */
        HLL_ERR("open %s: %s", path, strerror(errno));  /* ELOOP => symlink rejected */
        return -1;
    }
    HLL_ERR("open %s: create/attach kept racing", path);
    return -1;
}

static HllHandle *hll_create(const char *path, uint32_t precision, mode_t mode, char *errbuf) {
    uint32_t m;
    if (!hll_validate_create_args(precision, &m, errbuf)) return NULL;

    uint64_t total = hll_total_size(m);
    int anonymous = (path == NULL);
    int fd = -1;
    size_t map_size;
    void *base;

    if (anonymous) {
        map_size = (size_t)total;
        base = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
        if (base == MAP_FAILED) { HLL_ERR("mmap: %s", strerror(errno)); return NULL; }
    } else {
        fd = hll_secure_open(path, mode, errbuf);
        if (fd < 0) return NULL;
        if (flock(fd, LOCK_EX) < 0) { HLL_ERR("flock: %s", strerror(errno)); close(fd); return NULL; }
        struct stat st;
        if (fstat(fd, &st) < 0) { HLL_ERR("fstat: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
        int is_new = (st.st_size == 0);
        if (!is_new && (uint64_t)st.st_size < sizeof(HllHeader)) {
            HLL_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
            flock(fd, LOCK_UN); close(fd); return NULL;
        }
        if (is_new && ftruncate(fd, (off_t)total) < 0) {
            HLL_ERR("ftruncate: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL;
        }
        map_size = is_new ? (size_t)total : (size_t)st.st_size;
        base = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
        if (base == MAP_FAILED) { HLL_ERR("mmap: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
        if (!is_new) {
            if (!hll_validate_header((HllHeader *)base, (uint64_t)st.st_size)) {
                HLL_ERR("invalid HyperLogLog file"); munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL;
            }
            flock(fd, LOCK_UN); close(fd);
            return hll_setup(base, map_size, path, -1);
        }
    }
    hll_init_header(base, precision, m, total);
    if (fd >= 0) { flock(fd, LOCK_UN); close(fd); }
    return hll_setup(base, map_size, path, -1);
}

static HllHandle *hll_create_memfd(const char *name, uint32_t precision, char *errbuf) {
    uint32_t m;
    if (!hll_validate_create_args(precision, &m, errbuf)) return NULL;

    uint64_t total = hll_total_size(m);



( run in 1.983 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )