ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
abstraction.h
Go to the documentation of this file.
1#pragma once
2
54// ============================================================================
55// Platform Detection
56// ============================================================================
57
63#ifdef _WIN32
64// Suppress Microsoft deprecation warnings for POSIX functions
65#ifndef _CRT_SECURE_NO_WARNINGS
66#define _CRT_SECURE_NO_WARNINGS
67#endif
68#ifndef _CRT_NONSTDC_NO_WARNINGS
69#define _CRT_NONSTDC_NO_WARNINGS
70#endif
71
80#define PLATFORM_WINDOWS 1
81
90#define PLATFORM_POSIX 0
91#else
93#define PLATFORM_WINDOWS 0
95#define PLATFORM_POSIX 1
96#endif
97
100// ============================================================================
101// Compiler Attributes
102// ============================================================================
103
109#ifdef _WIN32
110// MSVC doesn't support __attribute__((packed)) - use #pragma pack instead
111
130#define PACKED_STRUCT_BEGIN __pragma(pack(push, 1))
131
139#define PACKED_STRUCT_END __pragma(pack(pop))
140
148#define PACKED_ATTR
149
162#define ALIGNED_ATTR(x) __declspec(align(x))
163#else
164// GCC/Clang packed struct support
165
167#define PACKED_STRUCT_BEGIN
169#define PACKED_STRUCT_END
170
185#define PACKED_ATTR __attribute__((packed))
186
192#define ALIGNED_ATTR(x) __attribute__((aligned(x)))
193#endif
194
197// ============================================================================
198// Standard Headers
199// ============================================================================
200
201#include <stdio.h>
202#include <stdlib.h>
203#include <string.h>
204#include <stdint.h>
205#include <stdbool.h>
206#include <time.h>
207
208// ============================================================================
209// Platform Abstraction Modules
210// ============================================================================
211
212#include "../platform/thread.h"
213#include "../platform/mutex.h"
214#include "../platform/rwlock.h"
215#include "../platform/cond.h"
216// Windows socket shutdown constants - define before socket.h includes winsock2.h
217#ifdef _WIN32
218#ifndef SHUT_RDWR
220#define SHUT_RD 0
222#define SHUT_WR 1
224#define SHUT_RDWR 2
225#endif
226#endif
227
228#include "../platform/socket.h"
229#include "../platform/terminal.h"
230#include "../platform/system.h"
231#include "../platform/string.h"
232#include "../platform/memory.h"
233#include "../platform/process.h"
234#include "../platform/filesystem.h"
235// NOTE: uthash/uthash.h is NOT included here to avoid circular dependencies.
236// Files that need uthash should include it directly AFTER common.h.
237#include "../platform/pipe.h"
238#ifndef __cplusplus
239#include "../debug/sync.h"
240#endif
241
242// ============================================================================
243// Thread-Local Storage and Alignment Macros
244// ============================================================================
245
251#ifdef _WIN32
252// Windows-specific thread-local storage and alignment
253#ifdef _MSC_VER
269#define THREAD_LOCAL __declspec(thread)
270
278#define ALIGNED_32 __declspec(align(32))
279
287#define ALIGNED_16 __declspec(align(16))
288#else
289// Clang on Windows
294#define THREAD_LOCAL __thread
299#define ALIGNED_32 __attribute__((aligned(32)))
304#define ALIGNED_16 __attribute__((aligned(16)))
305#endif
306#else
307// POSIX thread-local storage and alignment
308#ifndef THREAD_LOCAL
313#define THREAD_LOCAL __thread
318#define ALIGNED_32 __attribute__((aligned(32)))
323#define ALIGNED_16 __attribute__((aligned(16)))
324#endif
325#endif
326
329// ============================================================================
330// Platform-Specific Compatibility
331// ============================================================================
332
333#ifdef _WIN32
334// Windows-specific compatibility
335
341#ifndef PATH_MAX
353#define PATH_MAX 260
354#endif
355
356// POSIX-style file permissions (not used on Windows, provided for compatibility)
357#ifndef S_IRUSR
359#define S_IRUSR 0400
361#define S_IWUSR 0200
363#define S_IXUSR 0100
365#define S_IRGRP 0040
367#define S_IWGRP 0020
369#define S_IXGRP 0010
371#define S_IROTH 0004
373#define S_IWOTH 0002
375#define S_IXOTH 0001
376#endif
377
378// Signal constants
379#ifndef SIGPIPE
387#define SIGPIPE 13
388#endif
389
390// Standard file descriptor constants
391#ifndef STDIN_FILENO
393#define STDIN_FILENO 0
395#define STDOUT_FILENO 1
397#define STDERR_FILENO 2
398#endif
399
400// Windows socket types
407typedef unsigned long nfds_t;
408
409// Windows socket control codes
410#ifndef SIO_KEEPALIVE_VALS
417#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4)
418#endif
419
427// Use platform-safe write wrapper
428#ifndef write
436#define write _write
437#endif
438
439// Windows headers for POSIX-like functions
440#include <io.h>
441#include <fcntl.h>
442#include <process.h>
443
444// POSIX function aliases for Windows
449#define close _close
454#define lseek _lseek
459#define read _read
464#define unlink _unlink
465
466// Additional POSIX function aliases for Windows
471#define isatty _isatty
476#define getpid _getpid
477
498void *aligned_alloc(size_t alignment, size_t size);
499
511int clock_gettime(int clk_id, struct timespec *tp);
512
524struct tm *gmtime_r(const time_t *timep, struct tm *result);
525
536typedef unsigned int useconds_t;
537
543// Missing POSIX file flags for Windows
544#ifndef O_CLOEXEC
553#define O_CLOEXEC 0
554#endif
555
556// Missing POSIX time functions and constants for Windows
557#include <time.h>
558#ifndef CLOCK_REALTIME
566#define CLOCK_REALTIME 0
567#ifndef CLOCK_MONOTONIC
575#define CLOCK_MONOTONIC 1
576#endif
577#endif
578
581#else
582// POSIX-specific includes
583#include <unistd.h>
584#include <limits.h>
585#endif
586
587// ============================================================================
588// Cross-Platform Sleep Functions
589// ============================================================================
590
614void platform_sleep_us(unsigned int usec);
615
634
637// ============================================================================
638// Utility Macros
639// ============================================================================
640
666#ifndef UNUSED
667#define UNUSED(x) ((void)(x))
668#endif
669
684#ifdef _WIN32
685#define PLATFORM_BINARY_NAME "ascii-chat.exe"
686#else
687#define PLATFORM_BINARY_NAME "ascii-chat"
688#endif
689
706#ifdef _WIN32
707#define PLATFORM_SHELL_NULL_REDIRECT "2>nul"
708#else
709#define PLATFORM_SHELL_NULL_REDIRECT "2>/dev/null"
710#endif
711
714// ============================================================================
715// Backtrace Symbol Resolution
716// ============================================================================
717
734// ============================================================================
735// Restore Default Packing for Application Code
736// ============================================================================
737// NOTE: windows_compat.h now handles pack(pop) automatically
unsigned long long uint64_t
Definition common.h:59
void platform_sleep_ns(uint64_t ns)
Platform-safe sleep function with nanosecond precision.
void platform_sleep_us(unsigned int usec)
High-precision sleep function with microsecond precision.
Capture stdout/stderr and redirect to logging system.
Application limits and constraints.
Cross-platform process types and execution utilities.
ClangTool/LibTooling compatibility shim for stdbool.h.
⏱️ High-precision timing utilities using sokol_time.h and uthash
🔤 String Manipulation and Shell Escaping Utilities