Skip to content

OS: allocate MEM1 on a 64 KiB boundary - #311

Open
StilesCrisis wants to merge 1 commit into
encounter:mainfrom
StilesCrisis:mem1-alignment
Open

StilesCrisis wants to merge 1 commit into
encounter:mainfrom
StilesCrisis:mem1-alignment

Conversation

@StilesCrisis

Copy link
Copy Markdown

The Win32 path allocated MEM1 using VirtualAlloc which natively gives a nice 64K alignment.

The platform-neutral path just used calloc, which has no special alignment guarantees. (In wasm, it might not even be 16-byte aligned!) Having the base of MEM1 be misaligned can break GameCube titles in all sorts of subtle ways.

To get a 64K-aligned block, we now overallocate with calloc and use std::align to round the pointer up to a 64K boundary. (It's never freed, so we don't even need to save off the base address.)

On hardware MEM1 starts at 0x80000000, so arena memory carved from it
has whatever large alignment the game rounds to. Outside 64-bit Windows
debug builds, AllocMEM1 used calloc, which only guarantees malloc
alignment (8 or 16 bytes), so games that align pointers to absolute
boundaries got addresses that weren't aligned the way they expect.
Over-allocate with calloc and use the 64 KiB-aligned part (MEM1 is never
freed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SjpWoYqNDKkH5Zpdbz3WaW
@PJB3005

PJB3005 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

In what ways do GC games expect MEM1 to be 64k aligned?

@StilesCrisis

StilesCrisis commented Nov 9, 2026 •

Copy link
Copy Markdown
Author

64K gives parity with the VirtualAlloc on the Windows side.

Concretely, everything fails if you don't have at least 16b alignment, and if we're going to bother to add alignment, it made sense to match the side that works.

(EDIT: I had put a comment about __attribute__((aligned(x))) here but I'm second guessing it now.)

@PJB3005

PJB3005 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

64K gives parity with the VirtualAlloc on the Windows side.

The Windows path doesn't VirtualAlloc for the sake of alignment, it's explicitly a debug-aiding feature to make sure MEM1 is allocated at 0x________80000000. This works together with us allocating 0x80000000 in the 32-bit address space so that any truncations explicitly hit that reserved space.

Also ew I just realized when replying to this on desktop
image

@StilesCrisis

StilesCrisis commented Nov 9, 2026 •

Copy link
Copy Markdown
Author

I checked all the docs and there wasn't any policy about AI-assisted contributions so feel free to have your opinions in private.

Aside from that, concretely we need some sort of alignment. 64K may well be too high but IMO we are splitting hairs--the only thing we are wasting is address space, and obviously no one is using 32-bit but me (the build was broken). The untouched pages cost nothing; they never hit RAM. Happy to dial it back to a smaller number if you feel more passionately about it than I do, just name the alignment that you think is reasonable. Based on the existence of dcbz I think 32 is an absolute minimum.

@Antidote

Antidote commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Don't talk to our contributors that way, she's right about why we do things the way we do, you must actually understand the code you're submitting.

@StilesCrisis

Copy link
Copy Markdown
Author

I do actually understand the code, ask me anything.

Sorry if I came off as brash. Just trying to contribute here. I'm not a vibe coder; I've been in the industry for a long time and I'm not pushing PRs I haven't personally read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants