ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
buffer_pool.h
Go to the documentation of this file.
1#pragma once
2
28#include <stdbool.h>
29#include <stddef.h>
30#include <stdint.h>
31#include <ascii-chat/atomic.h>
32#include "platform/mutex.h"
33#include "util/magic.h"
34
35/* ============================================================================
36 * Configuration Constants
37 * ============================================================================
38 */
39
41#define BUFFER_POOL_MAX_BYTES (337 * 1024 * 1024)
42
44#define BUFFER_POOL_SHRINK_DELAY_NS 5000000000ULL
45
47#define BUFFER_POOL_MIN_SIZE 64
48
50#define BUFFER_POOL_MAX_SINGLE_SIZE (4 * 1024 * 1024)
51
53#define BUFFER_POOL_MAGIC MAGIC_BUFFER_POOL_VALID
55#define BUFFER_POOL_MAGIC_FALLBACK MAGIC_BUFFER_POOL_FALLBACK
56
57/* ============================================================================
58 * Data Structures
59 * ============================================================================
60 */
61
79
105
106/* ============================================================================
107 * Buffer Pool API
108 * ============================================================================
109 */
110
117buffer_pool_t *buffer_pool_create(size_t max_bytes, uint64_t shrink_delay_ns);
118
124
131void *buffer_pool_alloc(buffer_pool_t *pool, size_t size);
132
139void buffer_pool_free(buffer_pool_t *pool, const void *data, size_t size);
140
147
151void buffer_pool_get_stats(buffer_pool_t *pool, size_t *current_bytes, size_t *used_bytes, size_t *free_bytes);
152
156void buffer_pool_log_stats(buffer_pool_t *pool, const char *name);
157
158/* ============================================================================
159 * Global Buffer Pool
160 * ============================================================================
161 */
162
163void buffer_pool_init_global(void);
166
167/* ============================================================================
168 * Convenience Macros
169 * ============================================================================
170 */
171
172#define POOL_ALLOC(size) buffer_pool_alloc(NULL, (size))
173#define POOL_FREE(data, size) buffer_pool_free(NULL, (data), (size))
174
⚛️ Atomic operations abstraction layer with debug tracking
buffer_pool_t * pool
buffer_pool_t * buffer_pool_get_global(void)
struct buffer_pool buffer_pool_t
Unified buffer pool with lock-free fast path.
void buffer_pool_free(buffer_pool_t *pool, const void *data, size_t size)
Free a buffer back to the pool (lock-free)
void buffer_pool_shrink(buffer_pool_t *pool)
Force shrink the pool (free old unused buffers)
void buffer_pool_destroy(buffer_pool_t *pool)
Destroy a buffer pool and free all memory.
void buffer_pool_cleanup_global(void)
void buffer_pool_log_stats(buffer_pool_t *pool, const char *name)
Log pool statistics.
void buffer_pool_get_stats(buffer_pool_t *pool, size_t *current_bytes, size_t *used_bytes, size_t *free_bytes)
Get pool statistics (atomic reads)
void * buffer_pool_alloc(buffer_pool_t *pool, size_t size)
Allocate a buffer from the pool (lock-free fast path)
void buffer_pool_init_global(void)
buffer_pool_t * buffer_pool_create(size_t max_bytes, uint64_t shrink_delay_ns)
Create a new buffer pool.
Definition buffer_pool.c:51
struct buffer_node buffer_node_t
Node header embedded before user data.
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
Magic number validation constants and macros.
Cross-platform mutex interface for ascii-chat.
ClangTool/LibTooling compatibility shim for stdbool.h.
Atomic pointer wrapper.
Definition atomic.h:106
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
Node header embedded before user data.
Definition buffer_pool.h:71
atomic_t returned_at_ns
Timestamp when returned to free list (nanoseconds)
Definition buffer_pool.h:76
atomic_ptr_t next
Next in free list (lock-free)
Definition buffer_pool.h:75
uint32_t _pad
Padding for alignment.
Definition buffer_pool.h:73
uint32_t magic
Magic to identify pooled buffers.
Definition buffer_pool.h:72
size_t size
Size of user data portion.
Definition buffer_pool.h:74
struct buffer_pool * pool
Owning pool (for free)
Definition buffer_pool.h:77
Unified buffer pool with lock-free fast path.
Definition buffer_pool.h:85
size_t max_bytes
Maximum total bytes allowed.
Definition buffer_pool.h:89
atomic_t malloc_fallbacks
Allocations that bypassed pool.
uint64_t shrink_delay_ns
Time before unused buffers freed (nanoseconds)
Definition buffer_pool.h:90
atomic_t hits
Allocations from free list.
Definition buffer_pool.h:98
mutex_t shrink_mutex
Only used for shrinking.
Definition buffer_pool.h:87
atomic_ptr_t free_list
Lock-free stack of available buffers (void* cast from buffer_node_t*)
Definition buffer_pool.h:86
atomic_t used_bytes
Bytes currently in use (stored as uint64_t)
Definition buffer_pool.h:94
atomic_t peak_bytes
Peak bytes in use (stored as uint64_t)
Definition buffer_pool.h:95
atomic_t current_bytes
Total bytes in pool (stored as uint64_t)
Definition buffer_pool.h:93
atomic_t shrink_freed
Buffers freed by shrink.
atomic_t returns
Buffers returned to free list.
atomic_t allocs
New buffer allocations.
Definition buffer_pool.h:99
atomic_t peak_pool_bytes
Peak bytes in pool (stored as uint64_t)
Definition buffer_pool.h:96
Mutex type (POSIX: pthread_mutex_t with debug tracking)