ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
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// C11 stdatomic.h conflicts with MSVC's C++ <atomic> header on Windows.
32#if defined(__cplusplus) && defined(_WIN32)
33#include <atomic>
34using std::atomic_size_t;
35#else
36#include <stdatomic.h>
37#endif
38#include "platform/mutex.h"
39
40/* ============================================================================
41 * Configuration Constants
42 * ============================================================================
43 */
44
46#define BUFFER_POOL_MAX_BYTES (337 * 1024 * 1024)
47
49#define BUFFER_POOL_SHRINK_DELAY_MS 5000
50
52#define BUFFER_POOL_MIN_SIZE 64
53
55#define BUFFER_POOL_MAX_SINGLE_SIZE (4 * 1024 * 1024)
56
58#define BUFFER_POOL_MAGIC 0xBF00B001U
60#define BUFFER_POOL_MAGIC_FALLBACK 0xBF00FA11U
61
62/* ============================================================================
63 * Data Structures
64 * ============================================================================
65 */
66
76typedef struct buffer_node {
79 size_t size;
80 _Atomic(struct buffer_node *) next;
81 _Atomic(uint64_t) returned_at_ms;
82 struct buffer_pool *pool;
84
90typedef struct buffer_pool {
91 _Atomic(buffer_node_t *) free_list;
93
94 size_t max_bytes;
96
98 _Atomic(size_t) current_bytes;
99 _Atomic(size_t) used_bytes;
100 _Atomic(size_t) peak_bytes;
101 _Atomic(size_t) peak_pool_bytes;
102
105 _Atomic(uint64_t) returns;
106 _Atomic(uint64_t) shrink_freed;
107 _Atomic(uint64_t) malloc_fallbacks;
110
111/* ============================================================================
112 * Buffer Pool API
113 * ============================================================================
114 */
115
122buffer_pool_t *buffer_pool_create(size_t max_bytes, uint64_t shrink_delay_ms);
123
129
136void *buffer_pool_alloc(buffer_pool_t *pool, size_t size);
137
144void buffer_pool_free(buffer_pool_t *pool, void *data, size_t size);
145
152
156void buffer_pool_get_stats(buffer_pool_t *pool, size_t *current_bytes, size_t *used_bytes, size_t *free_bytes);
157
161void buffer_pool_log_stats(buffer_pool_t *pool, const char *name);
162
163/* ============================================================================
164 * Global Buffer Pool
165 * ============================================================================
166 */
167
168void buffer_pool_init_global(void);
171
172/* ============================================================================
173 * Convenience Macros
174 * ============================================================================
175 */
176
177#define POOL_ALLOC(size) buffer_pool_alloc(NULL, (size))
178#define POOL_FREE(data, size) buffer_pool_free(NULL, (data), (size))
179
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_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_free(buffer_pool_t *pool, void *data, size_t size)
Free a buffer back to the pool (lock-free)
buffer_pool_t * buffer_pool_create(size_t max_bytes, uint64_t shrink_delay_ms)
Create a new buffer pool.
Definition buffer_pool.c:70
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)
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
pthread_mutex_t mutex_t
Mutex type (POSIX: pthread_mutex_t)
Definition mutex.h:38
Cross-platform mutex interface for ascii-chat.
Node header embedded before user data.
Definition buffer_pool.h:76
uint32_t _pad
Padding for alignment.
Definition buffer_pool.h:78
uint32_t magic
Magic to identify pooled buffers.
Definition buffer_pool.h:77
size_t size
Size of user data portion.
Definition buffer_pool.h:79
_Atomic(uint64_t) returned_at_ms
Timestamp when returned to free list.
struct buffer_pool * pool
Owning pool (for free)
Definition buffer_pool.h:82
_Atomic(struct buffer_node *) next
Next in free list (atomic for lock-free)
Unified buffer pool with lock-free fast path.
Definition buffer_pool.h:90
size_t max_bytes
Maximum total bytes allowed.
Definition buffer_pool.h:94
_Atomic(uint64_t) hits
Allocations from free list.
_Atomic(size_t) peak_pool_bytes
Peak bytes in pool.
_Atomic(size_t) peak_bytes
Peak bytes in use.
mutex_t shrink_mutex
Only used for shrinking.
Definition buffer_pool.h:92
uint64_t shrink_delay_ms
Time before unused buffers freed.
Definition buffer_pool.h:95
_Atomic(size_t) current_bytes
Total bytes in pool.
_Atomic(size_t) used_bytes
Bytes currently in use.
_Atomic(buffer_node_t *) free_list
Lock-free stack of available buffers.
_Atomic(uint64_t) shrink_freed
Buffers freed by shrink.
_Atomic(uint64_t) returns
Buffers returned to free list.
_Atomic(uint64_t) malloc_fallbacks
Allocations that bypassed pool.
_Atomic(uint64_t) allocs
New buffer allocations.