gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > How to implement custom malloc / free?

#174860 - Dirbaio - Mon Jul 26, 2010 4:13 pm

So, basically I want to replace libc's malloc and free functions with mine.
(I'm using latest devkitpro and libnds)
I've had no success no far.

Googling around i got these two solutions, which i tried without success:

"Malloc hooks": They don't seem to exist in DevkitARM...
Redefining malloc/free and making them link before the lib ones: Works for my code, but other parts of libc (printf-related) call weird functions called _malloc_r or _free_r which i guess are part of the internal logic for malloc and free... I'm not catching those calls, so it doesnt work...


BTW, you're probably asking why in the world I want that. I want it for two different things:

- For debugging: I have weird memory corruption issues :(
- For ROM Hacking (in particular NSMB Hacking): I've set up some linker scripts that allow me to write some asm/c/c++ code and insert it into the game. I need malloc/free to call the actuall malloc/free from the game, or i can't use dynamic memory, libc, libfat and lots of cool things i'd like to use...

What would you suggest? Ive read blogs of people that have mentioned that they have done it, so it must be possible ...
Thanks in advance!

#174872 - LOst? - Tue Jul 27, 2010 3:28 pm

Dirbaio wrote:
- For ROM Hacking (in particular NSMB Hacking): I've set up some linker scripts that allow me to write some asm/c/c++ code and insert it into the game. I need malloc/free to call the actuall malloc/free from the game, or i can't use dynamic memory, libc, libfat and lots of cool things i'd like to use...

You know this is not a hacking forum.
I can tell you that NSMB isn't using malloc/free from devkitpro. But it does use some kind of dynamic size allocation. When I found out about that, I got kinda sad, because I am a fan of fixed size pools for games (for speed and debugging of course).
_________________
Exceptions are fun

#174873 - Dirbaio - Tue Jul 27, 2010 6:05 pm

LOst? wrote:
Dirbaio wrote:
- For ROM Hacking (in particular NSMB Hacking): I've set up some linker scripts that allow me to write some asm/c/c++ code and insert it into the game. I need malloc/free to call the actuall malloc/free from the game, or i can't use dynamic memory, libc, libfat and lots of cool things i'd like to use...

You know this is not a hacking forum.
I can tell you that NSMB isn't using malloc/free from devkitpro. But it does use some kind of dynamic size allocation. When I found out about that, I got kinda sad, because I am a fan of fixed size pools for games (for speed and debugging of course).


I know that NSMB isn't using libc heaps. I have located the functions that do the equivalent of malloc/free, and I know how to call them from my c++ code.

I just want to be able to define two functions in my code that will be called instead of devkitpro's libc malloc/free. Independantly of NSMB hacking.

#174875 - Dwedit - Wed Jul 28, 2010 4:22 am

You can look at a disassembly of NewLib to see what is calling the parts of malloc.
Use arm-eabi-ar to extract libg.a into its .o files, then arm-eabi-objdump to disassemble the .o files. The old batch command "for %F in (*.o) do ..." command might help.
Then use a good text editor with a "Find in Files" feature.

Also check what extra calls GCC is introducing into the code by building with the -S option so you can see the asm code.

Don't forget to kill crap like __aeabi_atexit, I think that might call malloc_r somewhere.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."