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     2            /* v2: reader-slot table for dead-reader rwlock recovery */
#define BUF_ERR_BUFLEN  256
#define BUF_READER_SLOTS 1024         /* per-process reader-counter mirror */

/* ---- Per-process reader-slot table (in shared memory) ----
 * Mirrors each process's contribution to the global rwlock counters so a
 * dead reader's contribution can be reclaimed on writer-lock timeout. */
typedef struct {
    uint32_t pid;             /* owning PID, 0 = free */
    uint32_t subcount;        /* this process's rwlock reader contribution */
    uint32_t waiters_parked;  /* this process's contribution to rwlock_waiters */
    uint32_t writers_parked;  /* this process's contribution to rwlock_writers_waiting */
} BufReaderSlot;

/* ---- Shared memory header (128 bytes, 2 cache lines, in mmap) ---- */

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define BUF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
#else
#define BUF_STATIC_ASSERT(cond, msg)



( run in 2.597 seconds using v1.01-cache-2.11-cpan-483215c6ad5 )