Data-DisjointSet-Shared

 view release on metacpan or  search on metacpan

dsu.h  view on Meta::CPAN

    /* Explicitly zero the header + reader-slot region (lock-recovery state);
       the parent/size arrays are initialized explicitly below. */
    memset(base, 0, (size_t)L.parent);
    hdr->magic            = DSU_MAGIC;
    hdr->version          = DSU_VERSION;
    hdr->n                = n;
    hdr->num_sets         = n;
    hdr->total_size       = total_size;
    hdr->reader_slots_off = L.reader_slots;
    hdr->parent_off       = L.parent;
    hdr->size_off         = L.size;
    {
        uint32_t *p  = (uint32_t *)((char *)base + L.parent);
        uint32_t *sz = (uint32_t *)((char *)base + L.size);
        for (uint32_t i = 0; i < n; i++) { p[i] = i; sz[i] = 1; }
    }
    __atomic_thread_fence(__ATOMIC_SEQ_CST);
}

static inline DsuHandle *dsu_setup(void *base, size_t map_size,
                                   const char *path, int backing_fd) {
    DsuHeader *hdr = (DsuHeader *)base;
    DsuHandle *h = (DsuHandle *)calloc(1, sizeof(DsuHandle));
    if (!h) {
        munmap(base, map_size);
        if (backing_fd >= 0) close(backing_fd);
        return NULL;
    }
    h->hdr          = hdr;
    h->base         = base;
    h->reader_slots = (DsuReaderSlot *)((uint8_t *)base + hdr->reader_slots_off);
    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 dsu_create reopen and dsu_open_fd).
 * Stored n wins on reopen; require total_size == the size n implies and ==
 * the actual file size, and all offsets to match the canonical layout. */
static inline int dsu_validate_header(const DsuHeader *hdr, uint64_t file_size) {
    if (hdr->magic != DSU_MAGIC) return 0;
    if (hdr->version != DSU_VERSION) return 0;
    if (hdr->n < 1 || hdr->n > DSU_MAX_N) return 0;
    if (hdr->num_sets < 1 || hdr->num_sets > hdr->n) return 0;
    if (hdr->total_size != file_size) return 0;
    if (hdr->total_size != dsu_total_size(hdr->n)) return 0;
    DsuLayout L = dsu_layout(hdr->n);
    if (hdr->reader_slots_off != L.reader_slots) return 0;
    if (hdr->parent_off != L.parent) return 0;
    if (hdr->size_off != L.size) return 0;
    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 dsu_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) { DSU_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 */
        DSU_ERR("open %s: %s", path, strerror(errno));  /* ELOOP => symlink rejected */
        return -1;
    }
    DSU_ERR("open %s: create/attach kept racing", path);
    return -1;
}

static DsuHandle *dsu_create(const char *path, uint64_t n_in, mode_t mode, char *errbuf) {
    if (!dsu_validate_create_args(n_in, errbuf)) return NULL;
    uint32_t n = (uint32_t)n_in;

    uint64_t total = dsu_total_size(n);
    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) { DSU_ERR("mmap: %s", strerror(errno)); return NULL; }
    } else {
        fd = dsu_secure_open(path, mode, errbuf);
        if (fd < 0) return NULL;
        if (flock(fd, LOCK_EX) < 0) { DSU_ERR("flock: %s", strerror(errno)); close(fd); return NULL; }
        struct stat st;
        if (fstat(fd, &st) < 0) { DSU_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(DsuHeader)) {
            DSU_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) {
            DSU_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) { DSU_ERR("mmap: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
        if (!is_new) {
            if (!dsu_validate_header((DsuHeader *)base, (uint64_t)st.st_size)) {
                DSU_ERR("invalid disjoint-set file"); munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL;
            }
            flock(fd, LOCK_UN); close(fd);
            return dsu_setup(base, map_size, path, -1);
        }
    }
    dsu_init_header(base, n, total);
    if (fd >= 0) { flock(fd, LOCK_UN); close(fd); }
    return dsu_setup(base, map_size, path, -1);
}

static DsuHandle *dsu_create_memfd(const char *name, uint64_t n_in, char *errbuf) {
    if (!dsu_validate_create_args(n_in, errbuf)) return NULL;
    uint32_t n = (uint32_t)n_in;

    uint64_t total = dsu_total_size(n);



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