Data-Buffer-Shared
view release on metacpan or search on metacpan
buf_generic.h view on Meta::CPAN
/*
* buf_generic.h -- Macro-template for shared-memory typed buffers.
*
* Before including, define:
* BUF_PREFIX -- function prefix (e.g., buf_i64)
* BUF_ELEM_TYPE -- C element type (e.g., int64_t)
* BUF_VARIANT_ID -- unique integer for header validation
* BUF_ELEM_SIZE -- sizeof(BUF_ELEM_TYPE) or fixed string length
*
* Optional:
* BUF_HAS_COUNTERS -- generate incr/decr/cas (integer types only)
* BUF_IS_FLOAT -- element is float/double (affects SV conversion)
* BUF_IS_FIXEDSTR -- element is fixed-length char array
*/
/* ================================================================
* Part 1: Shared definitions (included once)
* ================================================================ */
#ifndef BUF_DEFS_H
#define BUF_DEFS_H
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/syscall.h>
#include <limits.h>
#include <signal.h>
#include <errno.h>
#include <linux/futex.h>
#include <sys/eventfd.h>
#include <pthread.h>
/* ---- Constants ---- */
#define BUF_MAGIC 0x42554631U /* "BUF1" */
#define BUF_VERSION 3 /* v3: added occupancy bitmap region (layout change) */
#define BUF_ERR_BUFLEN 256
#define BUF_READER_SLOTS 1024 /* per-process reader-counter mirror */
/* Occupancy bitmap: one bit per reader slot, set when a process claims a slot and
* cleared on clean release. A writer scans these BUF_OCC_WORDS words to visit
* only OCCUPIED slots (O(words + live readers)) instead of all BUF_READER_SLOTS. */
#define BUF_OCC_WORDS (((BUF_READER_SLOTS) + 63) / 64) /* 16 for 1024 slots */
#define BUF_OCC_BYTES ((uint64_t)BUF_OCC_WORDS * 8) /* 128 bytes */
/* ---- Per-process reader-slot table (in shared memory) ----
* In the reader-slots-only rwlock a reader's ENTIRE contribution to the shared
* lock is `rdepth` in its OWN slot -- there is no separate shared reader counter
* to fall out of sync with it -- so a dead reader's contribution is exactly this
* one word, which a draining writer neutralises by clearing the slot's pid (the
* scan then ignores the slot). No orphaned counter can exist, so there is no
* quiescent force-reset and sustained readers cannot starve a writer.
* _rsv1/_rsv2 are kept only to preserve the 16-byte slot size across the
* already-released builds. */
typedef struct {
( run in 2.445 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )