From 018cb59d98a711d815511a70e685a2b086e6d48d Mon Sep 17 00:00:00 2001
From: ISSOtm <me@eldred.fr>
Date: Sat, 21 Mar 2026 22:51:59 +0100
Subject: [PATCH] Fix building with MinGW
---
src/newstruct.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/src/newstruct.c b/src/newstruct.c
index 98cc86b..40b008e 100644
@@ -65,6 +65,20 @@ void plum_destroy_image (struct plum_image * image) {
struct context * create_context (void) {
struct allocator_node * allocator = NULL;
struct context * context = NULL;
+#if defined(_MSC_VER) || defined(__MINGW32__)
+ // Windows does not provide `aligned_alloc` [1]; it *does* provide `_aligned_malloc`,
+ // but that *must* be paired with `_aligned_free` and not with `free` [2]!
+ // Fortunately, at least on 64-bit Windows, `alignof(jmp_buf)` is equal to `alignof(max_align_t)`!
+ // Thus, the branch on which `aligned_alloc` is used is dead code, which we can simply comment out;
+ // the following assertion checks so nonetheless.
+ //
+ // Note that `aligned_alloc` requires system support due to `free` awareness, thus MinGW doesn't
+ // provide a shim for it either.
+ //
+ // [1]: https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-180#note_M
+ // [2]: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/aligned-malloc?view=msvc-180#remarks
+ _Static_assert(!(alignof(jmp_buf) > alignof(max_align_t)), "Cannot safely allocate `jmp_buf` with `malloc`!");
+#else
if (alignof(jmp_buf) > alignof(max_align_t)) {
// this is the odd case where jmp_buf requires a stricter alignment than malloc is guaranteed to enforce
size_t skip = (alignof(jmp_buf) - 1) / sizeof *allocator + 1;
@@ -75,6 +89,7 @@ struct context * create_context (void) {
context = (struct context *) (allocator -> data + (skip - 1) * sizeof *allocator);
}
} else
+#endif
// normal case: malloc already returns a suitably-aligned pointer
context = allocate(&allocator, sizeof *context);
if (context) *context = (struct context) {.allocator = allocator};