ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
debug/named.h
Go to the documentation of this file.
1#pragma once
2
45
46/* Storage capacities in elements; text buffers include the terminating NUL. */
47#define DEBUG_NAMED_NAME_BUFFER_SIZE 256
48#define DEBUG_NAMED_TYPE_BUFFER_SIZE 64
49#define DEBUG_NAMED_FORMAT_BUFFER_SIZE 64
50
51#include <stdint.h>
52#include <stdbool.h>
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59#include <libplatform/thread.h>
60
61// ============================================================================
62// Core Registry API
63// ============================================================================
64
74
82void named_destroy(void);
83
105const char *named_register(uintptr_t key, const char *base_name, const char *type, const char *format_spec,
106 const char *file, int line, const char *func, uintptr_t parent_key);
107
129const char *named_register_fmt(uintptr_t key, const char *type, const char *format_spec, const char *file, int line,
130 const char *func, const char *fmt, ...);
131
140void named_unregister(uintptr_t key);
141
155const char *named_update_name(uintptr_t key, const char *new_base_name);
156
168const char *named_get(uintptr_t key);
169
179
186bool named_metadata_copy(uintptr_t key, named_metadata_t *output);
187bool named_fd_metadata_copy(int fd, named_metadata_t *output);
188bool named_packet_metadata_copy(int packet_type, named_metadata_t *output);
189
201const char *named_get_type(uintptr_t key);
202
204bool named_registry_has_type(const char *type, size_t length);
205
217const char *named_get_format_spec(uintptr_t key);
218
233const char *named_register_fd(int fd, const char *name, const char *file, int line, const char *func);
234
244const char *named_get_fd(int fd);
245
252const char *named_get_fd_format_spec(int fd);
253
267const char *named_register_packet_type(int pkt_type, const char *file, int line, const char *func);
268
278const char *named_get_packet_type(int pkt_type);
279
286const char *named_get_packet_type_format_spec(int pkt_type);
287
300const char *named_get_by_type_and_id(const char *type_name, size_t type_len, int id);
301
316const char *named_describe(uintptr_t key, const char *type_hint);
317
329uintptr_t thread_to_key(platform_thread_t thread);
330
331// ============================================================================
332// Convenience Macros — Pointer-based Registration
333// ============================================================================
334
346#define NAMED_REGISTER(ptr, name, type, fmt, parent_ptr) \
347 named_register((uintptr_t)(const void *)(ptr), (name), (type), (fmt), __FILE__, __LINE__, __func__, \
348 (uintptr_t)(const void *)(parent_ptr))
349
362#define NAMED_REGISTER_FMT(ptr, type, fmt_spec, fmt, ...) \
363 named_register_fmt((uintptr_t)(const void *)(ptr), (type), (fmt_spec), __FILE__, __LINE__, __func__, (fmt), \
364 __VA_ARGS__)
365
371#define NAMED_UNREGISTER(ptr) named_unregister((uintptr_t)(const void *)(ptr))
372
378#define NAMED_GET(ptr) named_get((uintptr_t)(const void *)(ptr))
379
386#define NAMED_DESCRIBE(ptr, hint) named_describe((uintptr_t)(const void *)(ptr), (hint))
387
388// ============================================================================
389// Convenience Macros — Integer Handle Registration
390// ============================================================================
391
402#define NAMED_REGISTER_ID(id, name, type, fmt, parent_ptr) \
403 named_register((uintptr_t)(intptr_t)(id), (name), (type), (fmt), __FILE__, __LINE__, __func__, \
404 (uintptr_t)(const void *)(parent_ptr))
405
411#define NAMED_UNREGISTER_ID(id) named_unregister((uintptr_t)(intptr_t)(id))
412
419#define NAMED_DESCRIBE_ID(id, hint) named_describe((uintptr_t)(intptr_t)(id), (hint))
420
421// ============================================================================
422// Type-Specific Convenience Macros (Mutex, RWLock, Cond, Thread)
423// ============================================================================
424
433#define NAMED_REGISTER_MUTEX(mutex, name, parent_ptr) \
434 named_register((uintptr_t)(const void *)(mutex), (name), "mutex", "0x%tx", __FILE__, __LINE__, __func__, \
435 (uintptr_t)(const void *)(parent_ptr))
436
445#ifndef NDEBUG
446#define NAMED_REGISTER_RWLOCK(lock, name, parent_ptr) \
447 named_register((uintptr_t)(const void *)(lock), (name), "rwlock", "0x%tx", __FILE__, __LINE__, __func__, \
448 (uintptr_t)(const void *)(parent_ptr))
449#else
450#define NAMED_REGISTER_RWLOCK(lock, name, parent_ptr) ((void)(lock), (void)(parent_ptr), (name))
451#endif
452
461#define NAMED_REGISTER_COND(cond, name, parent_ptr) \
462 named_register((uintptr_t)(const void *)(cond), (name), "cond", "0x%tx", __FILE__, __LINE__, __func__, \
463 (uintptr_t)(const void *)(parent_ptr))
464
473#define NAMED_REGISTER_SOCKET(socket, name, parent_ptr) \
474 named_register((uintptr_t)(intptr_t)(socket), (name), "socket", "0x%tx", __FILE__, __LINE__, __func__, \
475 (uintptr_t)(const void *)(parent_ptr))
476#define NAMED_UNREGISTER_SOCKET(socket) NAMED_UNREGISTER_ID((socket))
477
486#define NAMED_REGISTER_WEBSOCKET(websocket, name, parent_ptr) \
487 named_register((uintptr_t)(const void *)(websocket), (name), "websocket", "0x%tx", __FILE__, __LINE__, __func__, \
488 (uintptr_t)(const void *)(parent_ptr))
489
498#define NAMED_REGISTER_DATACHANNEL(datachannel, name, parent_ptr) \
499 named_register((uintptr_t)(const void *)(datachannel), (name), "datachannel", "0x%tx", __FILE__, __LINE__, __func__, \
500 (uintptr_t)(const void *)(parent_ptr))
501
510#define NAMED_REGISTER_THREADPOOL_WORK(work, name, parent_ptr) \
511 named_register((uintptr_t)(const void *)(work), (name), "work", "0x%tx", __FILE__, __LINE__, __func__, \
512 (uintptr_t)(const void *)(parent_ptr))
513
522#define NAMED_REGISTER_CLIENT(client, name, parent_ptr) \
523 named_register((uintptr_t)(const void *)(client), (name), "client", "0x%tx", __FILE__, __LINE__, __func__, \
524 (uintptr_t)(const void *)(parent_ptr))
525
534#define NAMED_REGISTER_CRYPTO_CONTEXT(ctx, name, parent_ptr) \
535 named_register((uintptr_t)(const void *)(ctx), (name), "crypto", "0x%tx", __FILE__, __LINE__, __func__, \
536 (uintptr_t)(const void *)(parent_ptr))
537
546#define NAMED_REGISTER_TRANSPORT(transport, name, parent_ptr) \
547 named_register((uintptr_t)(const void *)(transport), (name), "transport", "0x%tx", __FILE__, __LINE__, __func__, \
548 (uintptr_t)(const void *)(parent_ptr))
549
558#define NAMED_REGISTER_FRAME_BUFFER(buf, name, parent_ptr) \
559 named_register((uintptr_t)(const void *)(buf), (name), "frame_buffer", "0x%tx", __FILE__, __LINE__, __func__, \
560 (uintptr_t)(const void *)(parent_ptr))
561
570#define NAMED_REGISTER_PACKET_QUEUE(queue, name, parent_ptr) \
571 named_register((uintptr_t)(const void *)(queue), (name), "packet_queue", "0x%tx", __FILE__, __LINE__, __func__, \
572 (uintptr_t)(const void *)(parent_ptr))
573
582#define NAMED_REGISTER_AUDIO_RINGBUF(buf, name, parent_ptr) \
583 named_register((uintptr_t)(const void *)(buf), (name), "audio_ringbuf", "0x%tx", __FILE__, __LINE__, __func__, \
584 (uintptr_t)(const void *)(parent_ptr))
585
594#define NAMED_REGISTER_MIXER(mixer, name, parent_ptr) \
595 named_register((uintptr_t)(const void *)(mixer), (name), "mixer", "0x%tx", __FILE__, __LINE__, __func__, \
596 (uintptr_t)(const void *)(parent_ptr))
597
606#define NAMED_REGISTER_AUDIO_CODEC(codec, name, parent_ptr) \
607 named_register((uintptr_t)(const void *)(codec), (name), "audio_codec", "0x%tx", __FILE__, __LINE__, __func__, \
608 (uintptr_t)(const void *)(parent_ptr))
609
618#define NAMED_REGISTER_VIDEO_ENCODER(encoder, name, parent_ptr) \
619 named_register((uintptr_t)(const void *)(encoder), (name), "video_encoder", "0x%tx", __FILE__, __LINE__, __func__, \
620 (uintptr_t)(const void *)(parent_ptr))
621
630#define NAMED_REGISTER_BUFFER_DOMAIN(pool, name, parent_ptr) \
631 named_register((uintptr_t)(const void *)(pool), (name), "buffer_domain", "0x%tx", __FILE__, __LINE__, __func__, \
632 (uintptr_t)(const void *)(parent_ptr))
633
642#define NAMED_REGISTER_FFMPEG_DECODER(decoder, name, parent_ptr) \
643 named_register((uintptr_t)(const void *)(decoder), (name), "ffmpeg_decoder", "0x%tx", __FILE__, __LINE__, __func__, \
644 (uintptr_t)(const void *)(parent_ptr))
645
655#define NAMED_REGISTER_FFMPEG_ENCODER(encoder, name, parent_ptr) \
656 named_register((uintptr_t)(const void *)(encoder), (name), "ffmpeg_encoder", "0x%tx", __FILE__, __LINE__, __func__, \
657 (uintptr_t)(const void *)(parent_ptr))
658
668#define NAMED_REGISTER_H265_ENCODER(encoder, name, parent_ptr) \
669 named_register((uintptr_t)(const void *)(encoder), (name), "h265_encoder", "0x%tx", __FILE__, __LINE__, __func__, \
670 (uintptr_t)(const void *)(parent_ptr))
671
681#define NAMED_REGISTER_H265_DECODER(decoder, name, parent_ptr) \
682 named_register((uintptr_t)(const void *)(decoder), (name), "h265_decoder", "0x%tx", __FILE__, __LINE__, __func__, \
683 (uintptr_t)(const void *)(parent_ptr))
684
693#define NAMED_REGISTER_AUDIO_CONTEXT(ctx, name, parent_ptr) \
694 named_register((uintptr_t)(const void *)(ctx), (name), "audio_context", "0x%tx", __FILE__, __LINE__, __func__, \
695 (uintptr_t)(const void *)(parent_ptr))
696
705#define NAMED_REGISTER_RATE_LIMITER(limiter, name, parent_ptr) \
706 named_register((uintptr_t)(const void *)(limiter), (name), "rate_limiter", "0x%tx", __FILE__, __LINE__, __func__, \
707 (uintptr_t)(const void *)(parent_ptr))
708
717#define NAMED_REGISTER_WAV_WRITER(writer, name, parent_ptr) \
718 named_register((uintptr_t)(const void *)(writer), (name), "wav_writer", "0x%tx", __FILE__, __LINE__, __func__, \
719 (uintptr_t)(const void *)(parent_ptr))
720
729#define NAMED_REGISTER_MEDIA_SOURCE(source, name, parent_ptr) \
730 named_register((uintptr_t)(const void *)(source), (name), "media_source", "0x%tx", __FILE__, __LINE__, __func__, \
731 (uintptr_t)(const void *)(parent_ptr))
732
748#define NAMED_OPEN(pathname, name, flags, ...) \
749 ({ \
750 int _fd; \
751 if ((flags) & O_CREAT) { \
752 _fd = platform_open((pathname), (flags), __VA_ARGS__); \
753 } else { \
754 _fd = platform_open((pathname), (flags)); \
755 } \
756 if (_fd >= 0) { \
757 named_register_fd(_fd, NULL, __FILE__, __LINE__, __func__); \
758 } \
759 _fd; \
760 })
761
769#define NAMED_UNREGISTER_FD(fd) NAMED_UNREGISTER_ID((fd))
770
781#define NAMED_REGISTER_FD(fd, name) named_register_fd((fd), (name), __FILE__, __LINE__, __func__)
782
791#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr) \
792 named_register((uintptr_t)(const void *)(a), (name), "atomic", "0x%tx", __FILE__, __LINE__, __func__, \
793 (uintptr_t)(const void *)(parent_ptr))
794
803#define NAMED_REGISTER_ATOMIC_PTR(a, name, parent_ptr) \
804 named_register((uintptr_t)(const void *)(a), (name), "atomic_ptr", "0x%tx", __FILE__, __LINE__, __func__, \
805 (uintptr_t)(const void *)(parent_ptr))
806
807// ============================================================================
808// Thread-Specific Registration Macros
809// ============================================================================
810
822#define NAMED_REGISTER_THREAD(thread, name, parent_ptr) \
823 named_register(thread_to_key((thread)), (name), "thread", "0x%tx", __FILE__, __LINE__, __func__, \
824 (uintptr_t)(const void *)(parent_ptr))
825
831#define NAMED_UNREGISTER_THREAD(thread) named_unregister(thread_to_key((thread)))
832
833// ============================================================================
834// Type-Specific Description Macros
835// ============================================================================
836
844
850#define NAME_DESCRIBE_MUTEX(m) named_describe((uintptr_t)(const void *)(m), "mutex")
851
857#define NAME_DESCRIBE_RWLOCK(l) named_describe((uintptr_t)(const void *)(l), "rwlock")
858
864#define NAME_DESCRIBE_COND(c) named_describe((uintptr_t)(const void *)(c), "cond")
865
873#define NAME_DESCRIBE_THREAD(t) named_describe_thread(t)
874
880#define NAME_DESCRIBE_TRANSPORT(tr) named_describe((uintptr_t)(const void *)(tr), "transport")
881
887#define NAME_DESCRIBE_SOCKET(fd) named_describe((uintptr_t)(intptr_t)(fd), "socket")
888
894#define NAME_DESCRIBE_RINGBUF(rb) named_describe((uintptr_t)(const void *)(rb), "ringbuf")
895 /* Type-Specific Description Macros */
897
898// ============================================================================
899// Thread-Specific Description Function
900// ============================================================================
901
911const char *named_describe_thread(void *thread);
912
913// ============================================================================
914// Registry Iteration
915// ============================================================================
916
927typedef void (*named_iter_callback_t)(uintptr_t key, const char *name, void *user_data);
928
935#define NAMED_REGISTER_TCP_CLIENT(client, name, parent_ptr) \
936 named_register((uintptr_t)(const void *)(client), (name), "tcp_client", "0x%tx", __FILE__, __LINE__, __func__, \
937 (uintptr_t)(const void *)(parent_ptr))
938
945#define NAMED_REGISTER_WEBSOCKET_CLIENT(client, name, parent_ptr) \
946 named_register((uintptr_t)(const void *)(client), (name), "websocket_client", "0x%tx", __FILE__, __LINE__, __func__, \
947 (uintptr_t)(const void *)(parent_ptr))
948
955#define NAMED_REGISTER_APP_CLIENT(client, name, parent_ptr) \
956 named_register((uintptr_t)(const void *)(client), (name), "app_client", "0x%tx", __FILE__, __LINE__, __func__, \
957 (uintptr_t)(const void *)(parent_ptr))
958
965#define NAMED_REGISTER_VIDEO_FRAME_BUFFER(buf, name, parent_ptr) \
966 named_register((uintptr_t)(const void *)(buf), (name), "video_frame_buffer", "0x%tx", __FILE__, __LINE__, __func__, \
967 (uintptr_t)(const void *)(parent_ptr))
968
975#define NAMED_REGISTER_THREAD_POOL(pool, name, parent_ptr) \
976 named_register((uintptr_t)(const void *)(pool), (name), "thread_pool", "0x%tx", __FILE__, __LINE__, __func__, \
977 (uintptr_t)(const void *)(parent_ptr))
978
985#define NAMED_REGISTER_OPTIONS_BUILDER(builder, name, parent_ptr) \
986 named_register((uintptr_t)(const void *)(builder), (name), "options_builder", "0x%tx", __FILE__, __LINE__, __func__, \
987 (uintptr_t)(const void *)(parent_ptr))
988
995#define NAMED_REGISTER_SIMPLE_FRAME_SWAP(swap, name, parent_ptr) \
996 named_register((uintptr_t)(const void *)(swap), (name), "simple_frame_swap", "0x%tx", __FILE__, __LINE__, __func__, \
997 (uintptr_t)(const void *)(parent_ptr))
998
1005#define NAMED_REGISTER_CLIENT_AUDIO_PIPELINE(pipeline, name, parent_ptr) \
1006 named_register((uintptr_t)(const void *)(pipeline), (name), "client_audio_pipeline", "0x%tx", __FILE__, __LINE__, \
1007 __func__, (uintptr_t)(const void *)(parent_ptr))
1008
1009#ifdef DEBUG_MEMORY
1010#define NAMED_REGISTER_WEBSOCKET_IMPL(data, name, parent_ptr) \
1011 named_register((uintptr_t)(const void *)(data), (name), "websocket_impl", "0x%tx", __FILE__, __LINE__, __func__, \
1012 (uintptr_t)(const void *)(parent_ptr))
1013#else
1014#define NAMED_REGISTER_WEBSOCKET_IMPL(data, name, parent_ptr) ((void)(data), (void)(name), (void)(parent_ptr))
1015#endif
1016
1027
1042asciichat_error_t named_registry_for_each(named_iter_callback_t callback, void *user_data, bool *completed);
1043
1050bool named_registry_read(uintptr_t key, const char *type, void (*read_object)(uintptr_t, void *), void *user_data);
1051
1056typedef void (*named_snapshot_fn)(uintptr_t key, uint64_t generation, const char *name, const char *type,
1057 const char *file, int line, void *data);
1058asciichat_error_t named_registry_try_snapshot(size_t capacity, size_t *required, bool *completed,
1059 named_snapshot_fn copy, void *data);
1060
1070#define NAMED_REGISTER_LWS_CONTEXT(context, name, parent_ptr) \
1071 named_register((uintptr_t)(const void *)(context), (name), "lws_context", "0x%tx", __FILE__, __LINE__, __func__, \
1072 (uintptr_t)(const void *)(parent_ptr))
1073
1083#define NAMED_REGISTER_FFMPEG_CONTEXT(context, name, parent_ptr) \
1084 named_register((uintptr_t)(const void *)(context), (name), "ffmpeg_context", "0x%tx", __FILE__, __LINE__, __func__, \
1085 (uintptr_t)(const void *)(parent_ptr))
1086
1097#define NAMED_REGISTER_CONTEXT(context, context_type, name, parent_ptr) \
1098 named_register((uintptr_t)(const void *)(context), (name), (context_type), "0x%tx", __FILE__, __LINE__, __func__, \
1099 (uintptr_t)(const void *)(parent_ptr))
1100
1116#define NAMED_GET_BY_PTR(key, buffer, size) \
1117 do { \
1118 const char *_name = named_get(key); \
1119 if (_name) { \
1120 size_t _len = strlen(_name); \
1121 if (_len >= (size)) \
1122 _len = (size) - 1; \
1123 memcpy((buffer), (_name), _len); \
1124 (buffer)[_len] = '\0'; \
1125 } else { \
1126 snprintf((buffer), (size), "0x%tx", (ptrdiff_t)(key)); \
1127 } \
1128 } while (0)
1129
1143#define NAMED_GET_BY_INT(id, buffer, size) \
1144 do { \
1145 const char *_name = named_get((uintptr_t)(id)); \
1146 if (_name) { \
1147 size_t _len = strlen(_name); \
1148 if (_len >= (size)) \
1149 _len = (size) - 1; \
1150 memcpy((buffer), (_name), _len); \
1151 (buffer)[_len] = '\0'; \
1152 } else { \
1153 snprintf((buffer), (size), "0x%lx", (unsigned long)(id)); \
1154 } \
1155 } while (0)
1156
1169#define NAMED_GET_BY_STR(str, buffer, size) \
1170 do { \
1171 if ((str)) { \
1172 strncpy((buffer), (str), (size) - 1); \
1173 (buffer)[(size) - 1] = '\0'; \
1174 } else { \
1175 (buffer)[0] = '\0'; \
1176 } \
1177 } while (0)
1178
1180void named_print_hash_stats(void);
1181
1182#ifdef __cplusplus
1183}
1184#endif
1185 /* debug_named */
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
unsigned long long uint64_t
Definition common.h:63
void named_print_hash_stats(void)
const char * named_get_fd(int fd)
Look up a registered file descriptor.
bool named_registry_has_type(const char *type, size_t length)
const char * named_get_packet_type_format_spec(int pkt_type)
Get the format specifier for a registered packet type.
uintptr_t thread_to_key(platform_thread_t thread)
Convert a thread handle to a registry key.
bool named_packet_metadata_copy(int packet_type, named_metadata_t *output)
bool named_registry_read(uintptr_t key, const char *type, void(*read_object)(uintptr_t, void *), void *user_data)
void named_destroy(void)
Destroy the named object registry.
const char * named_describe(uintptr_t key, const char *type_hint)
Format a description string for logging.
void(* named_snapshot_fn)(uintptr_t key, uint64_t generation, const char *name, const char *type, const char *file, int line, void *data)
#define DEBUG_NAMED_FORMAT_BUFFER_SIZE
Definition debug/named.h:49
bool named_metadata_copy(uintptr_t key, named_metadata_t *output)
const char * named_get_packet_type(int pkt_type)
Look up a registered packet type.
const char * named_register_packet_type(int pkt_type, const char *file, int line, const char *func)
Register a packet type with namespace encoding.
const char * named_register_fd(int fd, const char *name, const char *file, int line, const char *func)
Register a file descriptor with namespace encoding.
const char * named_get_type(uintptr_t key)
Look up the registered type for a resource.
void named_unregister(uintptr_t key)
Unregister a resource by key.
void(* named_iter_callback_t)(uintptr_t key, const char *name, void *user_data)
Callback function for iterating registered entries.
const char * named_get_format_spec(uintptr_t key)
Get the format specifier for a registered named object.
asciichat_error_t named_registry_try_snapshot(size_t capacity, size_t *required, bool *completed, named_snapshot_fn copy, void *data)
void named_registry_register_packet_types(void)
Register all packet types from packet_type_t enum.
const char * named_get(uintptr_t key)
Look up the registered name for a resource.
#define DEBUG_NAMED_TYPE_BUFFER_SIZE
Definition debug/named.h:48
const char * named_get_fd_format_spec(int fd)
Get the format specifier for a registered file descriptor.
bool named_fd_metadata_copy(int fd, named_metadata_t *output)
asciichat_error_t named_registry_for_each(named_iter_callback_t callback, void *user_data, bool *completed)
Iterate through all registered entries.
#define DEBUG_NAMED_NAME_BUFFER_SIZE
Definition debug/named.h:47
asciichat_error_t named_init(void)
Initialize the named object registry.
const char * named_register(uintptr_t key, const char *base_name, const char *type, const char *format_spec, const char *file, int line, const char *func, uintptr_t parent_key)
Register a resource with an auto-suffixed name, type, and location info.
const char * named_get_by_type_and_id(const char *type_name, size_t type_len, int id)
Look up an integer ID by type name and value.
const char * named_describe_thread(void *thread)
Describe a thread for logging (function wrapper).
const char * named_register_fmt(uintptr_t key, const char *type, const char *format_spec, const char *file, int line, const char *func, const char *fmt,...)
Register a resource with a formatted name, type, and location info (no auto-suffix).
const char * named_update_name(uintptr_t key, const char *new_base_name)
Update the registered name for a resource with a new base name.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
The useful part of named metadata is in its fields: together they describe what is pending,...