From b4c1cb7fa98a89a4ef0d1c15984aa68f8fc3e86a Mon Sep 17 00:00:00 2001 From: Larry Doolittle Date: Sat, 31 Oct 2026 13:40:46 -0700 Subject: [PATCH 1/3] fix building on OpenBSD Add brain-dead support for __OpenBSD__ in the tangle of ifdefs surrounding memalign(). --- context/cocore.c | 2 ++ context/platform.h | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/context/cocore.c b/context/cocore.c index bc78452f..46dbb454 100644 --- a/context/cocore.c +++ b/context/cocore.c @@ -34,8 +34,10 @@ #include #include #if !defined(__APPLE__) +#if !defined(__OpenBSD__) #include #endif +#endif #include #include #include diff --git a/context/platform.h b/context/platform.h index dae78229..946beadf 100644 --- a/context/platform.h +++ b/context/platform.h @@ -62,6 +62,11 @@ #define MALLOC_ALIGNED(alignment, size) valloc(size) #define FREE_ALIGNED(mem) free(mem) +#elif defined(__OpenBSD__) +/* See above -- should also apply to at least FreeBSD, right? */ +#define MALLOC_ALIGNED(alignment, size) valloc(size) +#define FREE_ALIGNED(mem) free(mem) + #elif defined(WIN32) /* This is what we have to use on 32-bit Windows. */ #define MALLOC_ALIGNED(alignment, size) \ From 6418bfc2fc60e0f30245d42a1bf77f74ac29bfe6 Mon Sep 17 00:00:00 2001 From: Larry Doolittle Date: Mon, 2 Nov 2026 00:30:35 -0800 Subject: [PATCH 2/3] use logical or in ifdef rather than repeating stanzas --- context/cocore.c | 4 +--- context/platform.h | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/context/cocore.c b/context/cocore.c index 46dbb454..570f87be 100644 --- a/context/cocore.c +++ b/context/cocore.c @@ -33,11 +33,9 @@ #include #include #include -#if !defined(__APPLE__) -#if !defined(__OpenBSD__) +#if !(defined(__APPLE__) || defined(__OpenBSD__)) #include #endif -#endif #include #include #include diff --git a/context/platform.h b/context/platform.h index 946beadf..f373ee63 100644 --- a/context/platform.h +++ b/context/platform.h @@ -57,13 +57,9 @@ /* Allocating stack aligned memory. Again this is very platform dependent. */ -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__OpenBSD__) /* On OSX there is no memalign, and valloc is mostly what we want anyway. */ -#define MALLOC_ALIGNED(alignment, size) valloc(size) -#define FREE_ALIGNED(mem) free(mem) - -#elif defined(__OpenBSD__) -/* See above -- should also apply to at least FreeBSD, right? */ +/* Same for OpenBSD */ #define MALLOC_ALIGNED(alignment, size) valloc(size) #define FREE_ALIGNED(mem) free(mem) From 9e58d11623b7b5f8a530718cc2e3d64499734153 Mon Sep 17 00:00:00 2001 From: Larry Doolittle Date: Mon, 2 Nov 2026 23:54:43 -0800 Subject: [PATCH 3/3] Use mmap() instead of valloc() on OpenBSD for MALLOC_ALIGNED Less than perfect -- can't easily free() this memory --- context/platform.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/context/platform.h b/context/platform.h index f373ee63..04ddbc33 100644 --- a/context/platform.h +++ b/context/platform.h @@ -57,12 +57,19 @@ /* Allocating stack aligned memory. Again this is very platform dependent. */ -#if defined(__APPLE__) || defined(__OpenBSD__) +#if defined(__APPLE__) /* On OSX there is no memalign, and valloc is mostly what we want anyway. */ -/* Same for OpenBSD */ #define MALLOC_ALIGNED(alignment, size) valloc(size) #define FREE_ALIGNED(mem) free(mem) +#elif defined(__OpenBSD__) +/* OpenBSD stack memory has to be specially allocated with mmap() */ +#define MALLOC_ALIGNED(alignment, size) \ + mmap(NULL, size, PROT_READ | PROT_WRITE, \ + MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, -1, 0) +#define FREE_ALIGNED(mem) ((void) 0) +/* XXX will leak memory; maybe won't crash with short-lived processes */ + #elif defined(WIN32) /* This is what we have to use on 32-bit Windows. */ #define MALLOC_ALIGNED(alignment, size) \