ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
common.c File Reference

🔧 Shared SIMD utilities: initialization, cleanup, and architecture-specific resource management More...

Go to the source code of this file.

Functions

void build_ramp64 (uint8_t ramp64[64], const char *ascii_chars)
 Build 64-entry character ramp cache.
 
uint64_t get_current_time_ns (void)
 Get current time in nanoseconds.
 
double calculate_cache_eviction_score (uint64_t last_access_time, uint32_t access_count, uint64_t creation_time, uint64_t current_time)
 Calculate cache eviction score.
 
 __attribute__ ((no_sanitize("integer")))
 Register a WebRTC client with the server.
 
void build_utf8_luminance_cache (const char *ascii_chars, utf8_char_t cache[256])
 Build UTF-8 luminance cache.
 
void build_utf8_ramp64_cache (const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[64])
 Build UTF-8 ramp64 cache.
 

Detailed Description

🔧 Shared SIMD utilities: initialization, cleanup, and architecture-specific resource management

Definition in file video/simd/common.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (no_sanitize("integer"))  )

Register a WebRTC client with the server.

Registers a client that connected via WebRTC data channel instead of TCP socket. This function reuses most of add_client() logic but skips:

  • Crypto handshake (already done via ACDS signaling)
  • Socket-specific configuration
  • TCP thread pool registration

DIFFERENCES FROM add_client():

  • Takes an already-created acip_transport_t* instead of socket
  • No crypto handshake (WebRTC signaling handled authentication)
  • No socket configuration (WebRTC handles buffering)
  • Uses generic thread spawning instead of tcp_server thread pool
Parameters
server_ctxServer context
transportWebRTC transport (already created and connected)
client_ipClient IP address for logging (may be empty for P2P)
Returns
Client ID on success, -1 on failure
Note
The transport must be fully initialized and ready to send/receive
Client capabilities are still expected as first packet

Definition at line 256 of file video/simd/common.c.

257 {
258 // Already holding write lock
259 // Note: key should already be set by caller, but ensure it's set
260 new_cache->key = hash;
261
262 // Check if hash table is near capacity and evict proactively (80% threshold)
263 size_t entry_count = HASH_COUNT(g_utf8_cache_table);
264 if (entry_count >= g_utf8_heap_capacity) {
265 // Proactive eviction: free space before attempting insertion
266 utf8_palette_cache_t *victim_cache = utf8_heap_extract_min();
267 if (victim_cache) {
268 uint32_t victim_key = victim_cache->key;
269
270 // Log clean eviction
271 uint32_t victim_access_count = atomic_load(&victim_cache->access_count);
272 uint64_t current_time = get_current_time_ns();
273 uint64_t victim_age = (current_time - atomic_load(&victim_cache->last_access_time)) / 1000000000ULL;
274
275 log_debug("UTF8_CACHE_EVICTION: Proactive min-heap eviction hash=0x%x (age=%lus, count=%u)", victim_key,
276 victim_age, victim_access_count);
277
278 // NOLINTNEXTLINE: uthash macros use void* casts internally (standard C practice, safe)
279 HASH_DEL(g_utf8_cache_table, victim_cache);
280 SAFE_FREE(victim_cache);
281 }
282 }
283
284 // Now attempt insertion (should succeed with freed space)
285 // Note: uthash doesn't have a failure path for insertion, so we handle eviction proactively
286 HASH_ADD_INT(g_utf8_cache_table, key, new_cache);
287
288 // Success: add to min-heap
289 uint64_t current_time = get_current_time_ns();
290 double initial_score = calculate_cache_eviction_score(current_time, 1, current_time, current_time);
291 utf8_heap_insert(new_cache, initial_score);
292 return true;
293}
unsigned int uint32_t
Definition common.h:58
#define SAFE_FREE(ptr)
Definition common.h:320
unsigned long long uint64_t
Definition common.h:59
#define log_debug(...)
Log a DEBUG message.
_Atomic uint64_t last_access_time
double calculate_cache_eviction_score(uint64_t last_access_time, uint32_t access_count, uint64_t creation_time, uint64_t current_time)
Calculate cache eviction score.
uint64_t get_current_time_ns(void)
Get current time in nanoseconds.
_Atomic uint32_t access_count
UTF-8 palette cache structure.

References utf8_palette_cache_s::access_count, calculate_cache_eviction_score(), get_current_time_ns(), utf8_palette_cache_s::key, utf8_palette_cache_s::last_access_time, log_debug, and SAFE_FREE.