pd-rs / crankstart

A barely functional, wildly incomplete and basically undocumented Rust crate whose aim is to let you write Games for the Playdate handheld gaming system in Rust.
MIT License
235 stars 24 forks source link

Which memory allocator is used? #24

Closed laplab closed 2 years ago

laplab commented 2 years ago

Hi!

First of all, thank you for all the effort you put into this crate! It is wonderful to have an option of writing performance-sensitive games for PlayDate in Rust.

I have a question regarding memory allocations. If my understanding is correct, certain PlayDate SDK functions can allocate memory. My guess would be that either standard C allocator is used or some other allocator coming from the SDK.

What allocator is used for the Rust part of the game? Basically, what exactly happens when one writes Box::new in the game's code?

From what I gather, the options are:

  1. The same allocator as PlayDate SDK. There is a global allocator defined somewhere in crankstart, which calls respective malloc/free functions from C
  2. There is a Rust allocator, which has its own separate heap. In this case, the question is how C allocator and Rust allocator share the RAM
  3. Maybe something else I missed!

Would appreciate any pointers/hints to the answer. Thanks!

rtsuk commented 2 years ago

It is a wrapper around the allocator provided by the PlayDate SDK, implemented here: https://github.com/pd-rs/crankstart/blob/main/src/lib.rs#L325.

laplab commented 2 years ago

Makes sense, thank you!