Skip to content

Commit 87f92b1

Browse files
authored
cgen: skip emitting mman.h and pthreads related code, for freestanding builds (#24118)
1 parent 9b6ccb3 commit 87f92b1

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎vlib/v/gen/c/cheaders.v‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static inline void __sort_ptr(uintptr_t a[], bool b[], int l) {
5656

5757
fn c_closure_helpers(pref_ &pref.Preferences) string {
5858
mut builder := strings.new_builder(2048)
59-
if pref_.os != .windows {
59+
if pref_.os != .windows && pref_.is_bare == false {
6060
builder.writeln('#include <sys/mman.h>')
6161
}
6262

@@ -174,6 +174,10 @@ static SRWLOCK _closure_mtx;
174174
#define _closure_mtx_init() InitializeSRWLock(&_closure_mtx)
175175
#define _closure_mtx_lock() AcquireSRWLockExclusive(&_closure_mtx)
176176
#define _closure_mtx_unlock() ReleaseSRWLockExclusive(&_closure_mtx)
177+
#elif defined(_VFREESTANDING)
178+
#define _closure_mtx_init()
179+
#define _closure_mtx_lock()
180+
#define _closure_mtx_unlock()
177181
#else
178182
static pthread_mutex_t _closure_mtx;
179183
#define _closure_mtx_init() pthread_mutex_init(&_closure_mtx, 0)
@@ -196,6 +200,9 @@ static void __closure_alloc(void) {
196200
#ifdef _WIN32
197201
char* p = VirtualAlloc(NULL, _V_page_size * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
198202
if (p == NULL) return;
203+
#elif defined(_VFREESTANDING)
204+
char *p = malloc(_V_page_size * 2);
205+
if (p == NULL) return;
199206
#else
200207
char* p = mmap(0, _V_page_size * 2, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
201208
if (p == MAP_FAILED) return;
@@ -212,6 +219,7 @@ static void __closure_alloc(void) {
212219
#ifdef _WIN32
213220
DWORD _tmp;
214221
VirtualProtect(_closure_ptr, _V_page_size, PAGE_EXECUTE_READ, &_tmp);
222+
#elif defined(_VFREESTANDING)
215223
#else
216224
mprotect(_closure_ptr, _V_page_size, PROT_READ | PROT_EXEC);
217225
#endif
@@ -234,13 +242,21 @@ void __closure_init() {
234242
}
235243
#else
236244
${static_non_parallel}void __closure_init() {
245+
#ifndef _VFREESTANDING
237246
uint32_t page_size = sysconf(_SC_PAGESIZE);
247+
#else
248+
uint32_t page_size = 0x4000;
249+
#endif
238250
page_size = page_size * (((ASSUMED_PAGE_SIZE - 1) / page_size) + 1);
239251
_V_page_size = page_size;
240252
__closure_alloc();
253+
#ifndef _VFREESTANDING
241254
mprotect(_closure_ptr, page_size, PROT_READ | PROT_WRITE);
255+
#endif
242256
memcpy(_closure_ptr, __CLOSURE_GET_DATA_BYTES, sizeof(__CLOSURE_GET_DATA_BYTES));
257+
#ifndef _VFREESTANDING
243258
mprotect(_closure_ptr, page_size, PROT_READ | PROT_EXEC);
259+
#endif
244260
__CLOSURE_GET_DATA = (void*)_closure_ptr;
245261
_closure_ptr += _CLOSURE_SIZE;
246262
_closure_cap--;

0 commit comments

Comments
 (0)