forked from samdze/playdate-nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use playdate realloc for memory management
- Loading branch information
Showing
8 changed files
with
87 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
include playdate/build/config | ||
|
||
switch("d", "memtrace") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
## | ||
## This file is a re-implementation of memalloc.nim in the Nim standard library. It is intended to be used by patching | ||
## it in as a replacement in your configs.nim file, like this: | ||
## | ||
## ```nim | ||
## patchFile("stdlib", "malloc", nimblePlaydatePath / "src/playdate/bindings/malloc") | ||
## ``` | ||
## | ||
## This file exists to ensure that Nim itself is using the memory allocators provided by the playdate SDK | ||
## | ||
|
||
{.push stackTrace: off.} | ||
|
||
import system/ansi_c | ||
|
||
type PDRealloc = proc (p: pointer; size: csize_t): pointer {.tags: [], raises: [], cdecl, gcsafe.} | ||
|
||
var pdrealloc: PDRealloc | ||
|
||
proc setupRealloc*(allocator: PDRealloc) = | ||
when defined(memtrace): | ||
cfprintf(cstderr, "Setting up playdate allocator") | ||
pdrealloc = allocator | ||
|
||
proc allocImpl(size: Natural): pointer = | ||
when defined(memtrace): | ||
cfprintf(cstderr, "Allocating %d\n", size) | ||
result = pdrealloc(nil, size.csize_t) | ||
when defined(memtrace): | ||
cfprintf(cstderr, " At %p\n", result) | ||
|
||
proc alloc0Impl(size: Natural): pointer = | ||
result = allocImpl(size) | ||
zeroMem(result, size) | ||
|
||
proc reallocImpl(p: pointer, newSize: Natural): pointer = | ||
when defined(memtrace): | ||
cfprintf(cstderr, "Reallocating %p with size %d\n", p, newSize) | ||
return pdrealloc(p, newSize.csize_t) | ||
|
||
proc realloc0Impl(p: pointer, oldsize, newSize: Natural): pointer = | ||
result = realloc(p, newSize.csize_t) | ||
if newSize > oldSize: | ||
zeroMem(cast[pointer](cast[uint](result) + uint(oldSize)), newSize - oldSize) | ||
|
||
proc deallocImpl(p: pointer) = | ||
when defined(memtrace): | ||
cfprintf(cstderr, "Freeing %p\n", p) | ||
discard pdrealloc(p, 0) | ||
|
||
# The shared allocators map on the regular ones | ||
|
||
proc allocSharedImpl(size: Natural): pointer {.used.} = allocImpl(size) | ||
|
||
proc allocShared0Impl(size: Natural): pointer {.used.} = alloc0Impl(size) | ||
|
||
proc reallocSharedImpl(p: pointer, newSize: Natural): pointer {.used.} = reallocImpl(p, newSize) | ||
|
||
proc reallocShared0Impl(p: pointer, oldsize, newSize: Natural): pointer {.used.} = realloc0Impl(p, oldSize, newSize) | ||
|
||
proc deallocSharedImpl(p: pointer) {.used.} = deallocImpl(p) | ||
|
||
proc getOccupiedMem(): int {.used.} = discard | ||
proc getFreeMem(): int {.used.} = discard | ||
proc getTotalMem(): int {.used.} = discard | ||
proc deallocOsPages() {.used.} = discard | ||
|
||
{.pop.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters