ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
buffer.h File Reference

Go to the source code of this file.

Data Structures

struct  buffer_domain_config_t
struct  buffer_stats_t
struct  buffer_workspace_t
 Owns the temporary storage used by buffer workspace. The data pointers and size or cursor fields describe the live portion of the allocation; callers must use the type's cleanup path when the buffer or queue is no longer needed. More...

Macros

#define BUFFER_DEFAULT_ALIGNMENT   64u
#define BUFFER_PROCESS_LIMIT   ((size_t)512 * 1024 * 1024)
#define BUFFER_CONTROL_RESERVE   ((size_t)8 * 1024 * 1024)
#define BUFFER_DOMAIN_LIMIT   (BUFFER_PROCESS_LIMIT - BUFFER_CONTROL_RESERVE)
#define buffer_release(data)

Typedefs

typedef struct buffer_domain buffer_domain_t

Functions

asciichat_error_t buffer_domain_create (const buffer_domain_config_t *config, buffer_domain_t **out)
void buffer_domain_close (buffer_domain_t **domain)
void buffer_domain_trim (buffer_domain_t *domain)
void buffer_domain_stats (buffer_domain_t *domain, buffer_stats_t *out)
void buffer_domain_log_stats (buffer_domain_t *domain, const char *name)
size_t buffer_process_reserved (void)
asciichat_error_t buffer_budget_reserve (buffer_budget_t *budget, size_t bytes)
void buffer_budget_release (buffer_budget_t *budget)
asciichat_error_t buffer_acquire (buffer_domain_t *domain, size_t size, size_t alignment, void **out)
void * buffer_alloc (buffer_domain_t *domain, size_t size)
size_t buffer_capacity (const void *data)
size_t buffer_size (const void *data)
asciichat_error_t buffer_set_size (void *data, size_t size)
const void * buffer_share (const void *data)
bool buffer_is_unique (const void *data)
void buffer_unref (const void *data)
asciichat_error_t buffer_workspace_reserve (buffer_workspace_t *workspace, buffer_domain_t *domain, size_t capacity)
void buffer_workspace_destroy (buffer_workspace_t *workspace)
void buffer_system_init (void)
void buffer_system_cleanup (void)
buffer_domain_t * buffer_default_domain (void)
buffer_domain_t * buffer_control_domain (void)

Macro Definition Documentation

◆ BUFFER_CONTROL_RESERVE

#define BUFFER_CONTROL_RESERVE   ((size_t)8 * 1024 * 1024)

Definition at line 32 of file buffer.h.

Referenced by buffer_system_init().

◆ BUFFER_DEFAULT_ALIGNMENT

#define BUFFER_DEFAULT_ALIGNMENT   64u

Definition at line 30 of file buffer.h.

Referenced by buffer_acquire(), buffer_alloc(), buffer_workspace_reserve(), and image_new().

◆ BUFFER_DOMAIN_LIMIT

#define BUFFER_DOMAIN_LIMIT   (BUFFER_PROCESS_LIMIT - BUFFER_CONTROL_RESERVE)

Definition at line 33 of file buffer.h.

Referenced by buffer_domain_create().

◆ BUFFER_PROCESS_LIMIT

#define BUFFER_PROCESS_LIMIT   ((size_t)512 * 1024 * 1024)

Definition at line 31 of file buffer.h.

◆ buffer_release

#define buffer_release ( data)
Value:
do { \
__typeof__(&(data)) buffer_slot__ = &(data); \
buffer_unref(*buffer_slot__); \
*buffer_slot__ = NULL; \
} while (asciichat_macro_block_continue())

Release and clear a typed pointer lvalue; evaluates the lvalue once.

Definition at line 108 of file buffer.h.

108#define buffer_release(data) \
109 do { \
110 __typeof__(&(data)) buffer_slot__ = &(data); \
111 buffer_unref(*buffer_slot__); \
112 *buffer_slot__ = NULL; \
113 } while (asciichat_macro_block_continue())

Referenced by acip_async_tcp_client_create(), acip_packet_encrypt(), acip_packet_seal(), acip_security_decrypt(), acip_security_flush(), acip_send_audio_batch(), acip_send_audio_opus_batch(), acip_send_error(), acip_send_image_frame_h265(), acip_send_remote_log(), acip_server_receive_and_dispatch(), audio_ring_buffer_destroy(), av_send_audio_opus_batch(), browser_client_decrypt_packet(), browser_client_encrypt_packet(), browser_session_destroy(), browser_session_receive(), browser_transport_security_output(), browser_transport_security_packet(), buffer_workspace_destroy(), buffer_workspace_reserve(), client_decrypt_packet(), client_encrypt_packet(), client_handle_crypto_parameters(), compress_data(), crypto_record_open_sequence(), crypto_record_seal_into_domain(), framebuffer_clear(), framebuffer_read_frame(), framebuffer_write_frame(), framebuffer_write_multi_frame(), image_destroy(), mixer_create_channels(), mixer_destroy(), packet_decoder_reset(), packet_decrypt_envelope(), packet_envelope_release(), packet_queue_free_packet(), packet_queue_push_copy(), packet_queue_push_shared(), packet_receive(), packet_receive_via_transport(), quic_connection_destroy(), quic_connection_enable_security(), quic_connection_submit(), quic_connection_submit_datagram(), quic_datagram_read(), receive_packet_secure_with_timeout(), send_image_frame_packet(), send_packet_secure(), video_frame_buffer_create(), video_frame_buffer_destroy(), websocket_transport_send(), and websocket_transport_write_pending().

Typedef Documentation

◆ buffer_domain_t

Owned, aligned payloads for synchronous and asynchronous media pipelines.

A returned payload pointer is an ownership token. Only pointers returned by this API may be released or queried; interior pointers are borrowed views. Capacity, allocation origin and domain lifetime are private implementation details. Ordinary objects continue to use SAFE_MALLOC/SAFE_FREE.

Buffers are exclusive until buffer_share() is called. Shared buffers must be treated as immutable. Each owner releases exactly once. Transfer a pointer by clearing the source after a successful handoff; failure preserves it.

Domains serialize allocation, return and trim. No allocator operation is suitable for a real-time audio callback: reserve and touch storage first.

Definition at line 28 of file buffer.h.

Function Documentation

◆ buffer_acquire()

asciichat_error_t buffer_acquire ( buffer_domain_t * domain,
size_t size,
size_t alignment,
void ** out )

NULL domain selects the default process domain. Failure leaves *out NULL.

Definition at line 189 of file buffer.c.

190 {
191 if (!out)
192 return SET_ERRNO(ERROR_INVALID_PARAM, "Buffer output is NULL");
193 *out = NULL;
194 if (!alignment)
195 alignment = BUFFER_DEFAULT_ALIGNMENT;
196 if (alignment < sizeof(void *) || (alignment & (alignment - 1)) ||
197 alignment > 4096)
198 return SET_ERRNO(ERROR_INVALID_PARAM, "Buffer alignment must be a power of "
199 "two between pointer size and 4096");
200 if (!domain)
201 domain = buffer_default_domain();
202 if (!domain)
203 return SET_ERRNO(ERROR_INVALID_STATE, "Buffer system is unavailable");
204
205 size_t capacity = BUFFER_MIN_CAPACITY;
206 unsigned bin = 0;
207 while (capacity < size && bin + 1 < BUFFER_BINS) {
208 capacity *= 2;
209 bin++;
210 }
211 if (capacity < size)
212 capacity = size;
213 size_t overhead =
214 sizeof(buffer_header_t) + sizeof(buffer_header_t *) + alignment - 1;
215 if (capacity > SIZE_MAX - overhead)
216 return SET_ERRNO(ERROR_INVALID_PARAM, "Buffer capacity overflow");
217 size_t charge = capacity + overhead;
218
219 mutex_lock(&domain->mutex);
220 if (domain->closing) {
221 mutex_unlock(&domain->mutex);
222 return SET_ERRNO(ERROR_INVALID_STATE, "Buffer domain is closing");
223 }
224 buffer_header_t **link = &domain->bins[bin];
225 buffer_header_t *node = NULL;
226 if (capacity <= BUFFER_MAX_CACHED) {
227 while (*link) {
228 if ((*link)->alignment >= alignment) {
229 node = *link;
230 *link = node->next;
231 domain->stats.cached_bytes -= node->charge;
232 domain->stats.hits++;
233 break;
234 }
235 link = &(*link)->next;
236 }
237 }
238 if (!node) {
239 if (charge > domain->config.limit_bytes ||
240 domain->stats.reserved_bytes > domain->config.limit_bytes - charge) {
241 /* A miss may reclaim idle capacity, but a release never scans a pool. */
242 trim_locked(domain, true);
243 }
244 bool fits =
245 charge <= domain->config.limit_bytes &&
246 domain->stats.reserved_bytes <= domain->config.limit_bytes - charge;
247 bool reserved = fits && process_reserve(charge, domain->config.control);
248 if (fits && !reserved && domain->stats.cached_bytes) {
249 trim_locked(domain, true);
250 reserved = process_reserve(charge, domain->config.control);
251 }
252 if (!reserved) {
253 domain->stats.failures++;
254 mutex_unlock(&domain->mutex);
255 return SET_ERRNO(ERROR_MEMORY,
256 "Buffer budget exhausted requesting %zu bytes", size);
257 }
258 node = ALLOC_MALLOC(charge);
259 if (!node) {
260 atomic_fetch_sub_u64(&process_bytes, charge);
261 domain->stats.failures++;
262 mutex_unlock(&domain->mutex);
263 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate %zu buffer bytes",
264 charge);
265 }
266 memset(node, 0, sizeof(*node));
267 node->allocation = node;
268 uintptr_t start = (uintptr_t)(node + 1) + sizeof(buffer_header_t *);
269 node->data =
270 (void *)((start + alignment - 1) & ~(uintptr_t)(alignment - 1));
271 ((buffer_header_t **)node->data)[-1] = node;
272 node->domain = domain;
273 node->capacity = capacity;
274 node->charge = charge;
275 node->alignment = alignment;
276 node->bin = bin;
277 domain->stats.reserved_bytes += charge;
278 domain->stats.allocations++;
279 }
280 node->next = NULL;
281 BUFFER_UNPOISON(node->data, node->capacity);
282 atomic_store_u64(&node->references, 1);
283 node->length = size;
284 domain->stats.outstanding++;
285 domain->stats.live_bytes += node->charge;
286 domain->stats.requested_bytes += size;
287 if (domain->stats.reserved_bytes > domain->stats.peak_bytes)
288 domain->stats.peak_bytes = domain->stats.reserved_bytes;
289 *out = node->data;
290 mutex_unlock(&domain->mutex);
291 return ASCIICHAT_OK;
292}
#define SET_ERRNO(code,...)
uint64_t atomic_fetch_sub_u64(atomic_t *a, uint64_t delta)
Atomically subtract from a uint64_t and return the previous value.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
#define BUFFER_MIN_CAPACITY
Definition buffer.c:26
buffer_domain_t * buffer_default_domain(void)
Definition buffer.c:452
#define BUFFER_BINS
Definition buffer.c:25
#define BUFFER_MAX_CACHED
Definition buffer.c:27
struct buffer_header buffer_header_t
#define BUFFER_UNPOISON(p, n)
Definition buffer.c:20
#define BUFFER_DEFAULT_ALIGNMENT
Definition buffer.h:30
#define ALLOC_MALLOC(size)
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
buffer_stats_t stats
Definition buffer.c:58
bool closing
Definition buffer.c:60
mutex_t mutex
Definition buffer.c:56
buffer_header_t * bins[17]
Definition buffer.c:59
buffer_domain_config_t config
Definition buffer.c:57
size_t capacity
Definition buffer.c:36
void * allocation
Definition buffer.c:34
size_t charge
Definition buffer.c:38
struct buffer_header * next
Definition buffer.c:32
buffer_domain_t * domain
Definition buffer.c:33
unsigned bin
Definition buffer.c:42
size_t alignment
Definition buffer.c:39
size_t length
Definition buffer.c:37
void * data
Definition buffer.c:35
atomic_t references
Definition buffer.c:40
size_t live_bytes
Definition buffer.h:47
uint64_t hits
Definition buffer.h:52
size_t outstanding
Definition buffer.h:51
size_t reserved_bytes
Definition buffer.h:46
size_t peak_bytes
Definition buffer.h:50
size_t requested_bytes
Definition buffer.h:48
size_t cached_bytes
Definition buffer.h:49
uint64_t failures
Definition buffer.h:54
uint64_t allocations
Definition buffer.h:53

References buffer_header::alignment, ALLOC_MALLOC, buffer_header::allocation, buffer_stats_t::allocations, ASCIICHAT_OK, atomic_fetch_sub_u64(), atomic_store_u64(), buffer_header::bin, buffer_domain::bins, BUFFER_BINS, BUFFER_DEFAULT_ALIGNMENT, buffer_default_domain(), BUFFER_MAX_CACHED, BUFFER_MIN_CAPACITY, BUFFER_UNPOISON, buffer_stats_t::cached_bytes, buffer_header::capacity, buffer_header::charge, buffer_domain::closing, buffer_domain::config, buffer_domain_config_t::control, buffer_header::data, buffer_header::domain, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, buffer_stats_t::failures, buffer_stats_t::hits, buffer_header::length, buffer_domain_config_t::limit_bytes, buffer_stats_t::live_bytes, buffer_domain::mutex, buffer_header::next, out, buffer_stats_t::outstanding, buffer_stats_t::peak_bytes, buffer_header::references, buffer_stats_t::requested_bytes, buffer_stats_t::reserved_bytes, SET_ERRNO, and buffer_domain::stats.

Referenced by buffer_alloc(), and buffer_workspace_reserve().

◆ buffer_alloc()

◆ buffer_budget_release()

void buffer_budget_release ( buffer_budget_t * budget)

Definition at line 95 of file buffer.c.

95 {
96 if (budget && budget->bytes) {
97 atomic_fetch_sub_u64(&process_bytes, budget->bytes);
98 budget->bytes = 0;
99 }
100}
size_t bytes
Definition budget.h:16

References atomic_fetch_sub_u64(), and buffer_budget_t::bytes.

◆ buffer_budget_reserve()

asciichat_error_t buffer_budget_reserve ( buffer_budget_t * budget,
size_t bytes )

Budget lease for platform storage that cannot be recycled (e.g. pinned mappings). Zero-initialize, reserve before allocation, release only after disposing storage. A failed reservation preserves the lease. Never copy a live lease.

Definition at line 83 of file buffer.c.

83 {
84 if (!budget || budget->bytes || !bytes)
86 "Expected an empty buffer budget lease");
87 if (!process_reserve(bytes, false))
88 return SET_ERRNO(
90 "Process buffer budget exhausted reserving platform storage");
91 budget->bytes = bytes;
92 return ASCIICHAT_OK;
93}

References ASCIICHAT_OK, buffer_budget_t::bytes, ERROR_INVALID_PARAM, ERROR_MEMORY, and SET_ERRNO.

◆ buffer_capacity()

◆ buffer_control_domain()

buffer_domain_t * buffer_control_domain ( void )

Small, bounded control messages may use the reserved process allowance.

Definition at line 465 of file buffer.c.

465 {
467 return control_domain;
468}
void buffer_system_init(void)
Definition buffer.c:426

References buffer_system_init().

Referenced by acip_packet_plan(), acip_send_error(), crypto_record_seal_into_domain(), packet_receive(), stats_logger_thread(), and websocket_transport_send().

◆ buffer_default_domain()

buffer_domain_t * buffer_default_domain ( void )

Definition at line 452 of file buffer.c.

452 {
454 return default_domain;
455}

References buffer_system_init().

Referenced by buffer_acquire(), and stats_logger_thread().

◆ buffer_domain_close()

void buffer_domain_close ( buffer_domain_t ** domain)

Stop admissions and trim cached storage. Outstanding buffers keep the domain alive. Join users of the domain pointer before closing; outstanding payloads may be released afterwards, from any thread. Clears the caller's domain pointer.

Definition at line 167 of file buffer.c.

167 {
168 if (!slot || !*slot)
169 return;
170 buffer_domain_t *domain = *slot;
171 *slot = NULL;
172 mutex_lock(&domain->mutex);
173 domain->closing = true;
174 trim_locked(domain, true);
175 bool done = domain->stats.outstanding == 0;
176 mutex_unlock(&domain->mutex);
177 if (done)
178 domain_dispose(domain);
179}
struct buffer_domain buffer_domain_t
Definition buffer.h:28

References buffer_domain::closing, buffer_domain::mutex, buffer_stats_t::outstanding, and buffer_domain::stats.

Referenced by buffer_system_cleanup(), buffer_system_init(), and packet_queue_destroy().

◆ buffer_domain_create()

asciichat_error_t buffer_domain_create ( const buffer_domain_config_t * config,
buffer_domain_t ** out )

Create a domain under the shared process budget.

Definition at line 132 of file buffer.c.

133 {
134 if (!out)
135 return SET_ERRNO(ERROR_INVALID_PARAM, "Buffer domain output is NULL");
136 *out = NULL;
137 buffer_domain_config_t defaults = {BUFFER_DOMAIN_LIMIT, 32 * 1024 * 1024,
138 5000000000ULL, false};
139 if (!process_reserve(sizeof(buffer_domain_t), config && config->control))
140 return SET_ERRNO(ERROR_MEMORY,
141 "Process buffer budget exhausted creating domain");
142 buffer_domain_t *domain = ALLOC_CALLOC(1, sizeof(*domain));
143 if (!domain) {
144 atomic_fetch_sub_u64(&process_bytes, sizeof(*domain));
145 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate buffer domain");
146 }
147 domain->config = config ? *config : defaults;
148 if (!domain->config.limit_bytes)
150 if (mutex_init(&domain->mutex, domain->config.control
151 ? "reserved_control_buffer_pool"
152 : "shared_media_buffer_pool") != 0) {
153 atomic_fetch_sub_u64(&process_bytes, sizeof(*domain));
154 SAFE_FREE(domain);
155 return SET_ERRNO(ERROR_THREAD, "Cannot initialize buffer domain mutex");
156 }
157 *out = domain;
158 return ASCIICHAT_OK;
159}
#define BUFFER_DOMAIN_LIMIT
Definition buffer.h:33
#define SAFE_FREE(ptr)
#define ALLOC_CALLOC(count, size)
@ ERROR_THREAD

References ALLOC_CALLOC, ASCIICHAT_OK, atomic_fetch_sub_u64(), BUFFER_DOMAIN_LIMIT, buffer_domain::config, buffer_domain_config_t::control, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_THREAD, buffer_domain_config_t::limit_bytes, buffer_domain::mutex, out, SAFE_FREE, and SET_ERRNO.

Referenced by buffer_system_init(), and packet_queue_create_config().

◆ buffer_domain_log_stats()

void buffer_domain_log_stats ( buffer_domain_t * domain,
const char * name )

Definition at line 416 of file buffer.c.

416 {
417 buffer_stats_t stats;
418 buffer_domain_stats(domain, &stats);
419 log_debug("Buffers %s: reserved=%zu live=%zu requested=%zu cached=%zu "
420 "outstanding=%zu hits=%llu failures=%llu",
421 name ? name : "default", stats.reserved_bytes, stats.live_bytes,
422 stats.requested_bytes, stats.cached_bytes, stats.outstanding,
423 (unsigned long long)stats.hits, (unsigned long long)stats.failures);
424}
void buffer_domain_stats(buffer_domain_t *domain, buffer_stats_t *out)
Definition buffer.c:405
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565

References buffer_domain_stats(), buffer_stats_t::cached_bytes, buffer_stats_t::failures, buffer_stats_t::hits, buffer_stats_t::live_bytes, log_debug, buffer_stats_t::outstanding, buffer_stats_t::requested_bytes, and buffer_stats_t::reserved_bytes.

Referenced by stats_logger_thread().

◆ buffer_domain_stats()

void buffer_domain_stats ( buffer_domain_t * domain,
buffer_stats_t * out )

Definition at line 405 of file buffer.c.

405 {
406 if (!out)
407 return;
408 memset(out, 0, sizeof(*out));
409 if (!domain)
410 return;
411 mutex_lock(&domain->mutex);
412 *out = domain->stats;
413 mutex_unlock(&domain->mutex);
414}

References buffer_domain::mutex, out, and buffer_domain::stats.

Referenced by buffer_domain_log_stats().

◆ buffer_domain_trim()

void buffer_domain_trim ( buffer_domain_t * domain)

Maintenance operation; never called implicitly from the steady-state release path.

Definition at line 181 of file buffer.c.

181 {
182 if (!domain)
183 return;
184 mutex_lock(&domain->mutex);
185 trim_locked(domain, false);
186 mutex_unlock(&domain->mutex);
187}

References buffer_domain::mutex.

Referenced by stats_logger_thread().

◆ buffer_is_unique()

bool buffer_is_unique ( const void * data)

Test exclusive ownership while retaining a reference. Only the owner may publish new references while using this result to modify/recycle storage.

Definition at line 335 of file buffer.c.

335 {
336 return data && atomic_load_u64(&header(data)->references) == 1;
337}
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.

References atomic_load_u64().

Referenced by packet_decrypt_envelope(), and packet_envelope_release().

◆ buffer_process_reserved()

size_t buffer_process_reserved ( void )

Definition at line 79 of file buffer.c.

79 {
80 return (size_t)atomic_load_u64(&process_bytes);
81}

References atomic_load_u64().

◆ buffer_set_size()

asciichat_error_t buffer_set_size ( void * data,
size_t size )

Change logical length within capacity. Requires exclusive ownership.

Definition at line 305 of file buffer.c.

305 {
306 if (!data || size > buffer_capacity(data))
307 return SET_ERRNO(ERROR_INVALID_PARAM, "Buffer length exceeds capacity");
308 buffer_header_t *node = header(data);
309 buffer_domain_t *domain = node->domain;
310 mutex_lock(&domain->mutex);
311 if (atomic_load_u64(&node->references) != 1) {
312 mutex_unlock(&domain->mutex);
313 return SET_ERRNO(ERROR_INVALID_STATE, "Cannot resize shared buffer");
314 }
315 domain->stats.requested_bytes -= node->length;
316 node->length = size;
317 domain->stats.requested_bytes += size;
318 mutex_unlock(&domain->mutex);
319 return ASCIICHAT_OK;
320}
size_t buffer_capacity(const void *data)
Definition buffer.c:300

References ASCIICHAT_OK, atomic_load_u64(), buffer_capacity(), buffer_header::domain, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, buffer_header::length, buffer_domain::mutex, buffer_header::references, buffer_stats_t::requested_bytes, SET_ERRNO, and buffer_domain::stats.

Referenced by compress_workspace().

◆ buffer_share()

const void * buffer_share ( const void * data)

Acquire another immutable reference while an existing reference is held.

Definition at line 322 of file buffer.c.

322 {
323 if (!data)
324 return NULL;
325 buffer_header_t *node = header(data);
326 uint64_t references = atomic_load_u64(&node->references);
327 while (references && references != SIZE_MAX) {
328 if (atomic_cas_u64(&node->references, &references, references + 1))
329 return data;
330 }
331 SET_ERRNO(ERROR_INVALID_STATE, "Invalid buffer reference count");
332 return NULL;
333}
bool atomic_cas_u64(atomic_t *a, uint64_t *expected, uint64_t new_value)
Atomically compare-and-swap a uint64_t.
unsigned long long uint64_t

References atomic_cas_u64(), atomic_load_u64(), ERROR_INVALID_STATE, buffer_header::references, and SET_ERRNO.

Referenced by packet_queue_push_shared().

◆ buffer_size()

size_t buffer_size ( const void * data)

Definition at line 303 of file buffer.c.

303{ return data ? header(data)->length : 0; }

◆ buffer_system_cleanup()

void buffer_system_cleanup ( void )

Definition at line 457 of file buffer.c.

457 {
458 if (lifecycle_shutdown(&default_lifecycle)) {
459 NAMED_UNREGISTER(&process_bytes);
460 buffer_domain_close(&default_domain);
461 buffer_domain_close(&control_domain);
462 }
463}
void buffer_domain_close(buffer_domain_t **slot)
Definition buffer.c:167
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.

References buffer_domain_close(), and NAMED_UNREGISTER.

Referenced by asciichat_shared_destroy(), and mirror_cleanup().

◆ buffer_system_init()

void buffer_system_init ( void )

Application startup/shutdown, serialized against starting/stopping workers.

Definition at line 426 of file buffer.c.

426 {
427 for (;;) {
428 if (lifecycle_init_once(&default_lifecycle))
429 break;
430 if (lifecycle_is_initialized(&default_lifecycle) ||
431 lifecycle_is_dead(&default_lifecycle))
432 return;
433 // Another caller must finish publishing both domains before allocations use
434 // them.
435 platform_sleep_us(100);
436 }
437 if (buffer_domain_create(NULL, &default_domain) != ASCIICHAT_OK) {
438 lifecycle_init_abort(&default_lifecycle);
439 return;
440 }
441 buffer_domain_config_t control = {BUFFER_CONTROL_RESERVE, 1024 * 1024,
442 5000000000ULL, true};
443 if (buffer_domain_create(&control, &control_domain) != ASCIICHAT_OK) {
444 buffer_domain_close(&default_domain);
445 lifecycle_init_abort(&default_lifecycle);
446 return;
447 }
448 NAMED_REGISTER_ATOMIC(&process_bytes, "buffer.process_reserved_bytes", NULL);
449 lifecycle_init_commit(&default_lifecycle);
450}
asciichat_error_t buffer_domain_create(const buffer_domain_config_t *config, buffer_domain_t **out)
Definition buffer.c:132
#define BUFFER_CONTROL_RESERVE
Definition buffer.h:32
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.

References ASCIICHAT_OK, BUFFER_CONTROL_RESERVE, buffer_domain_close(), buffer_domain_create(), and NAMED_REGISTER_ATOMIC.

Referenced by asciichat_shared_init(), buffer_control_domain(), and buffer_default_domain().

◆ buffer_unref()

void buffer_unref ( const void * data)

Low-level release for ownership passed by value. Prefer buffer_release().

Definition at line 339 of file buffer.c.

339 {
340 if (!data)
341 return;
342 buffer_header_t *node = header(data);
343 buffer_domain_t *domain = node->domain;
344 uint64_t references = atomic_load_u64(&node->references);
345 while (references &&
346 !atomic_cas_u64(&node->references, &references, references - 1)) {
347 }
348 if (!references) {
349 SET_ERRNO(ERROR_INVALID_STATE, "Buffer has already been released");
350 return;
351 }
352 if (references > 1) {
353 return;
354 }
355 mutex_lock(&domain->mutex);
356 domain->stats.outstanding--;
357 domain->stats.live_bytes -= node->charge;
358 domain->stats.requested_bytes -= node->length;
359 bool done = domain->closing && !domain->stats.outstanding;
360 if (!domain->closing && node->capacity <= BUFFER_MAX_CACHED &&
361 node->charge <= domain->config.cache_bytes &&
362 domain->stats.cached_bytes <= domain->config.cache_bytes - node->charge) {
363 node->returned_at = time_get_ns();
364 BUFFER_POISON(node->data, node->capacity);
365 node->next = domain->bins[node->bin];
366 domain->bins[node->bin] = node;
367 domain->stats.cached_bytes += node->charge;
368 } else {
369 dispose(domain, node);
370 }
371 mutex_unlock(&domain->mutex);
372 if (done)
373 domain_dispose(domain);
374}
#define BUFFER_POISON(p, n)
Definition buffer.c:19
uint64_t returned_at
Definition buffer.c:41

References atomic_cas_u64(), atomic_load_u64(), buffer_header::bin, buffer_domain::bins, BUFFER_MAX_CACHED, BUFFER_POISON, buffer_domain_config_t::cache_bytes, buffer_stats_t::cached_bytes, buffer_header::capacity, buffer_header::charge, buffer_domain::closing, buffer_domain::config, buffer_header::data, buffer_header::domain, ERROR_INVALID_STATE, buffer_header::length, buffer_stats_t::live_bytes, buffer_domain::mutex, buffer_header::next, buffer_stats_t::outstanding, buffer_header::references, buffer_stats_t::requested_bytes, buffer_header::returned_at, SET_ERRNO, and buffer_domain::stats.

◆ buffer_workspace_destroy()

void buffer_workspace_destroy ( buffer_workspace_t * workspace)

Definition at line 398 of file buffer.c.

398 {
399 if (workspace) {
400 buffer_release(workspace->data);
401 workspace->length = 0;
402 }
403}
#define buffer_release(data)
Definition buffer.h:108

References buffer_release, buffer_workspace_t::data, and buffer_workspace_t::length.

Referenced by compression_workspace_destroy().

◆ buffer_workspace_reserve()

asciichat_error_t buffer_workspace_reserve ( buffer_workspace_t * workspace,
buffer_domain_t * domain,
size_t capacity )

Preserves the previous workspace on failure; retains capacity between frames.

Definition at line 376 of file buffer.c.

378 {
379 if (!workspace)
380 return SET_ERRNO(ERROR_INVALID_PARAM, "Workspace is NULL");
381 if (workspace->length > buffer_capacity(workspace->data))
383 "Workspace length exceeds its storage");
384 if (capacity <= buffer_capacity(workspace->data))
385 return ASCIICHAT_OK;
386 void *next = NULL;
387 asciichat_error_t result =
388 buffer_acquire(domain, capacity, BUFFER_DEFAULT_ALIGNMENT, &next);
389 if (result != ASCIICHAT_OK)
390 return result;
391 if (workspace->data && workspace->length)
392 memcpy(next, workspace->data, workspace->length);
393 buffer_release(workspace->data);
394 workspace->data = next;
395 return ASCIICHAT_OK;
396}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48

References ASCIICHAT_OK, buffer_acquire(), buffer_capacity(), BUFFER_DEFAULT_ALIGNMENT, buffer_release, buffer_workspace_t::data, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, buffer_workspace_t::length, and SET_ERRNO.

Referenced by compress_workspace(), packet_decode_frame_data_workspace(), packet_decoder_feed(), and threaded_send_image_frame_encoded().