Skip to content

Commit 3e7fbb3

Browse files
Fix --fstdalloc on OpenBSD
1 parent a9a8405 commit 3e7fbb3

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

kklib/include/kklib.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,49 @@ static inline void kk_free_local(const void* p, kk_context_t* ctx) {
534534

535535
#define kk_malloc_usable_size(p) mi_usable_size(p)
536536

537+
#elif defined(__OpenBSD__)
538+
539+
// On OpenBSD, libc's malloc implementation doesn't allow checking the
540+
// size of an allocation. Here we prefix each allocation with a `size_t`
541+
// that contains its size (excluding the prefix itself, so that
542+
// `kk_malloc_usable_size` works the same as on other platforms).
543+
544+
static inline void* kk_malloc(kk_ssize_t sz, kk_context_t* ctx) {
545+
kk_unused(ctx);
546+
size_t* p = malloc(sizeof(size_t) + (size_t)sz);
547+
*p = (size_t)sz;
548+
return (void*)(p + 1);
549+
}
550+
551+
static inline void* kk_malloc_small(kk_ssize_t sz, kk_context_t* ctx) {
552+
return kk_malloc(sz,ctx);
553+
}
554+
555+
static inline void* kk_zalloc(kk_ssize_t sz, kk_context_t* ctx) {
556+
kk_unused(ctx);
557+
size_t* p = calloc(1, sizeof(size_t) + (size_t)sz);
558+
*p = (size_t)sz;
559+
return (void*)(p + 1);
560+
}
561+
562+
static inline void* kk_realloc(void* p, kk_ssize_t sz, kk_context_t* ctx) {
563+
kk_unused(ctx);
564+
size_t* q = realloc((size_t*)p - 1, sizeof(size_t) + (size_t)sz);
565+
*q = (size_t)sz;
566+
return (void*)(q + 1);
567+
}
568+
569+
static inline void kk_free(const void* p, kk_context_t* ctx) {
570+
kk_unused(ctx);
571+
free((size_t*)p - 1);
572+
}
573+
574+
static inline void kk_free_local(const void* p, kk_context_t* ctx) {
575+
kk_free(p,ctx);
576+
}
577+
578+
#define kk_malloc_usable_size(p) *((size_t*)p - 1)
579+
537580
#else
538581
static inline void* kk_malloc(kk_ssize_t sz, kk_context_t* ctx) {
539582
kk_unused(ctx);

0 commit comments

Comments
 (0)