From 06bd50ea83f43f30de9a27c0f41b001001fd49bd Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 25 Jul 2026 10:12:17 +0200 Subject: [PATCH] Implemented a "posix_memalign" shim on Support in the PlayDate binding target for Embedded Swift on device. --- Sources/PlayDate/Support.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sources/PlayDate/Support.swift b/Sources/PlayDate/Support.swift index 26f7dbf..623f87f 100644 --- a/Sources/PlayDate/Support.swift +++ b/Sources/PlayDate/Support.swift @@ -67,3 +67,22 @@ extension String { return buffer } } + +#if hasFeature(Embedded) && !os(macOS) +/// The Embedded Swift runtime allocates through `posix_memalign(3)`, which +/// the Playdate device C library does not provide. Memory comes from +/// `malloc`, which the SDK's setup code routes to the firmware allocator; +/// that allocator hands out sufficiently aligned blocks, so alignment is +/// only asserted, not adjusted. +@_cdecl("posix_memalign") +public func posix_memalign( + _ memptr: UnsafeMutablePointer, + _ alignment: Int, + _ size: Int +) -> CInt { + guard let allocation = malloc(size + alignment - 1) else { fatalError() } + precondition(Int(bitPattern: allocation) % alignment == 0) + memptr.pointee = allocation + return 0 +} +#endif