I need it for two reasons:
- 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 else i can't use dynamic memory, libc, libfat and lots of cool things i'd like to use...
I did try to intercept _malloc_r and _free_r too. I just wasn't able because there was soooo much shit:
Code: Select all
#if __STD_C
Void_t* mALLOc(RARG size_t bytes)
#else
Void_t* mALLOc(RARG bytes) RDECL size_t bytes;
#endif
{
#ifdef MALLOC_PROVIDED
return malloc (bytes); // Make sure that the pointer returned by malloc is returned back.
#else
mchunkptr victim; /* inspected/selected chunk */
INTERNAL_SIZE_T victim_size; /* its size */
int idx; /* index for bin traversal */
mbinptr bin; /* associated bin */
mchunkptr remainder; /* remainder from a split */
long remainder_size; /* its size */
int remainder_index; /* its bin index */
unsigned long block; /* block traverser bit */
int startidx; /* first bin of a traversed block */
mchunkptr fwd; /* misc temp for linking */
mchunkptr bck; /* misc temp for linking */
(blah)
Right now, i've been able to get something working by copying the entire libc source code into my project, modifying malloc.c and compiling everything together.
However, that gives lots of problems. For example, when you just link to libc.a, the linker is smart and only links the .o's from libc.a that are needed. But if I put all the libc source code, everything is compiled and linked no matter if they are used or not

so i get more or less 100kb more of code. That's only a temporary solution...
So, I'm still interested in more "elegant" ways for doing this

Thanks!