upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/compat.h
diff options
context:
space:
mode:
authorBitflash-sh <btf01@proton.me>2026-07-24 11:18:11 -0300
committerBitflash-sh <btf01@proton.me>2026-07-24 11:18:11 -0300
commit51d617e70f90a972185917ea0096c1ecef2bfa8b (patch)
tree9fbbcd867de9de9d0776afdd02f594ba312c46eb /src/compat.h
Bitflash: fair-launch release
Satoshi's bitcoin-0.1.0 reborn with RandomX CPU proof of work, anonymous .btf addressing over Nostr with CGNAT rendezvous, automatic peer discovery, a headless Linux node, and Windows GUI wallet. Fair launch, no premine, 21M cap.
Diffstat (limited to 'src/compat.h')
-rw-r--r--src/compat.h193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/compat.h b/src/compat.h
new file mode 100644
index 0000000..e3f76a1
--- /dev/null
+++ b/src/compat.h
@@ -0,0 +1,193 @@
1// Copyright (c) 2026 Bitflash developers
2// Distributed under the MIT/X11 software license.
3//
4// POSIX/Linux compatibility shim. Lets the Windows-flavored core (Winsock,
5// _beginthread, CRITICAL_SECTION, Sleep, ...) compile largely unchanged on
6// Linux for the headless node/miner `bitflash-node`. Included by headers.h on
7// non-Windows platforms only; the Windows build never sees this file.
8
9#ifndef BITFLASH_COMPAT_H
10#define BITFLASH_COMPAT_H
11#ifndef _WIN32
12
13#include <sys/types.h>
14#include <sys/socket.h>
15#include <sys/select.h>
16#include <sys/ioctl.h>
17#include <sys/stat.h>
18#include <sys/time.h>
19#include <netinet/in.h>
20#include <netinet/tcp.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <errno.h>
26#include <pthread.h>
27#include <time.h>
28#include <stdint.h>
29#include <stdarg.h>
30#include <cctype>
31#include <cstring>
32#include <cstdio>
33#include <cstdlib>
34#include <string>
35
36// ---- Windows integer/type aliases used across the core ----
37typedef unsigned int DWORD;
38typedef void* HANDLE;
39typedef int64_t LARGE_INTEGER;
40#ifndef TRUE
41#define TRUE 1
42#define FALSE 0
43#endif
44#ifndef _I64_MAX
45#define _I64_MAX 0x7fffffffffffffffLL
46#define _I64_MIN (-0x7fffffffffffffffLL - 1)
47#endif
48#ifndef _UI64_MAX
49#define _UI64_MAX 0xffffffffffffffffULL
50#endif
51
52// High-resolution counter (used only for optional timing in the core).
53inline void QueryPerformanceCounter(LARGE_INTEGER* p)
54{
55 struct timespec ts;
56 clock_gettime(CLOCK_MONOTONIC, &ts);
57 *p = (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
58}
59
60// ---- socket layer (Winsock -> BSD sockets) ----
61typedef int SOCKET;
62#ifndef INVALID_SOCKET
63#define INVALID_SOCKET (-1)
64#endif
65#ifndef SOCKET_ERROR
66#define SOCKET_ERROR (-1)
67#endif
68#ifndef NO_ERROR
69#define NO_ERROR 0
70#endif
71#define closesocket(s) ::close(s)
72inline int WSAGetLastError() { return errno; }
73
74// Winsock error codes mapped to POSIX errno values
75#define WSAEWOULDBLOCK EWOULDBLOCK
76#define WSAEMSGSIZE EMSGSIZE
77#define WSAEINTR EINTR
78#define WSAEINPROGRESS EINPROGRESS
79#define WSAEADDRINUSE EADDRINUSE
80#define WSAEALREADY EALREADY
81#define WSAECONNREFUSED ECONNREFUSED
82#define WSAENOTSOCK EBADF
83
84// Only FIONBIO (non-blocking toggle) is used; implement it robustly via fcntl.
85inline int ioctlsocket(SOCKET s, long cmd, u_long* argp)
86{
87 if (cmd == (long)FIONBIO)
88 {
89 int flags = fcntl(s, F_GETFL, 0);
90 if (flags < 0) return -1;
91 if (*argp) flags |= O_NONBLOCK; else flags &= ~O_NONBLOCK;
92 return fcntl(s, F_SETFL, flags);
93 }
94 return ioctl(s, cmd, argp);
95}
96
97// WSAStartup/WSACleanup are no-ops outside Windows.
98typedef int WSADATA;
99inline int WSAStartup(unsigned short, WSADATA*) { return 0; }
100inline int WSACleanup() { return 0; }
101#ifndef MAKEWORD
102#define MAKEWORD(a, b) 0
103#endif
104
105// ---- Sleep(ms) ----
106inline void Sleep(unsigned int ms)
107{
108 struct timespec ts;
109 ts.tv_sec = ms / 1000;
110 ts.tv_nsec = (long)(ms % 1000) * 1000000L;
111 nanosleep(&ts, NULL);
112}
113
114// ---- threads: _beginthread / _endthread over pthreads ----
115struct _bf_thread_ctx { void (*fn)(void*); void* arg; };
116inline void* _bf_thread_trampoline(void* p)
117{
118 _bf_thread_ctx* c = (_bf_thread_ctx*)p;
119 void (*fn)(void*) = c->fn;
120 void* arg = c->arg;
121 delete c;
122 fn(arg);
123 return NULL;
124}
125inline uintptr_t _beginthread(void (*start)(void*), unsigned, void* arg)
126{
127 _bf_thread_ctx* c = new _bf_thread_ctx;
128 c->fn = start; c->arg = arg;
129 pthread_t th;
130 if (pthread_create(&th, NULL, _bf_thread_trampoline, c) != 0)
131 {
132 delete c;
133 return (uintptr_t)-1;
134 }
135 pthread_detach(th);
136 return (uintptr_t)th;
137}
138inline void _endthread() { pthread_exit(NULL); }
139
140// ---- CRITICAL_SECTION over a recursive mutex (util.h's CCriticalSection
141// compiles unchanged on top of these) ----
142typedef pthread_mutex_t CRITICAL_SECTION;
143inline void InitializeCriticalSection(CRITICAL_SECTION* cs)
144{
145 pthread_mutexattr_t a;
146 pthread_mutexattr_init(&a);
147 pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
148 pthread_mutex_init(cs, &a);
149 pthread_mutexattr_destroy(&a);
150}
151inline void DeleteCriticalSection(CRITICAL_SECTION* cs) { pthread_mutex_destroy(cs); }
152inline void EnterCriticalSection(CRITICAL_SECTION* cs) { pthread_mutex_lock(cs); }
153inline void LeaveCriticalSection(CRITICAL_SECTION* cs) { pthread_mutex_unlock(cs); }
154inline bool TryEnterCriticalSection(CRITICAL_SECTION* cs) { return pthread_mutex_trylock(cs) == 0; }
155
156// ---- misc Windows CRT/debug helpers ----
157inline void OutputDebugStringA(const char*) {}
158inline void DebugBreak() {}
159#ifndef _HEAPOK
160#define _HEAPOK 0
161#endif
162inline int _heapchk() { return _HEAPOK; }
163#define _vsnprintf vsnprintf
164inline char* strlwr(char* s) { for (char* p = s; *p; ++p) *p = (char)tolower((unsigned char)*p); return s; }
165#define _strlwr strlwr
166inline int _mkdir(const char* p) { return ::mkdir(p, 0755); }
167
168// Thread priority is a Windows nicety; on Linux the message-handler thread just
169// runs at normal priority (harmless no-op).
170inline void* GetCurrentThread() { return (void*)0; }
171inline void SetThreadPriority(void*, int) {}
172#ifndef THREAD_PRIORITY_BELOW_NORMAL
173#define THREAD_PRIORITY_BELOW_NORMAL 0
174#define THREAD_PRIORITY_LOWEST 0
175#define THREAD_PRIORITY_NORMAL 0
176#endif
177
178// ---- wxWidgets shims (the headless build has no GUI; the few core call sites
179// degrade to console/no-op) ----
180#define wxTheApp ((void*)0)
181#ifndef wxOK
182#define wxOK 0
183#define wxICON_ERROR 0
184#define wxICON_EXCLAMATION 0
185#define wxICON_INFORMATION 0
186#endif
187inline void wxMessageBox(const std::string& msg, const std::string& caption = "", long = 0)
188{
189 fprintf(stderr, "%s%s%s\n", caption.c_str(), caption.empty() ? "" : ": ", msg.c_str());
190}
191
192#endif // !_WIN32
193#endif // BITFLASH_COMPAT_H