How can I make custom malloc / free?
How can I make custom malloc / free?
So, basically I want to implement my custom malloc and free, overriding libc ones.
I've tried the following without success:
- Malloc hooks: they don't exist in devkitarm
- Defining my own malloc / free functions: Works, but some functions from libc (printf related) call _malloc_r and _free_r, which i'm not intercepting.
How could I do it?
Thanks in advance!
I've tried the following without success:
- Malloc hooks: they don't exist in devkitarm
- Defining my own malloc / free functions: Works, but some functions from libc (printf related) call _malloc_r and _free_r, which i'm not intercepting.
How could I do it?
Thanks in advance!
-
- Site Admin
- Posts: 1986
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: How can I make custom malloc / free?
I'm really not sure how easy this will be to do, I'll need to do some digging in newlib.
malloc and free call _malloc_r and _free_r respectively so intercepting those might work.
What's this for anyway?
malloc and free call _malloc_r and _free_r respectively so intercepting those might work.
What's this for anyway?
Re: How can I make custom malloc / free?
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:
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!
- 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!
Re: How can I make custom malloc / free?
are you thinking that there are memory issues outside of your code - in libnds or newlib?
Re: How can I make custom malloc / free?
Nope
I dont want custom mallocs because I think that the default ones are bugged.
With custom mallocs you can do very good debugging.
For instance, what i have now is this: http://dirbaio.pastebin.com/UaHqxWfa
What it does is allocate the memory from the beginning of RAM, and each allocated block has around it a reserved area filled with constant data. When you do a free(), it checks if the data is still there, so it can see if you're writing out of the bounds of an array or similar things.
It can catch silly errors like
u8* ptr = new u8[100];
delete[] ptr;
delete[] ptr; //Twice!!
and like
u8* ptr = new u8[10];
ptr[11]=1;
delete[] ptr;
I dont want custom mallocs because I think that the default ones are bugged.
With custom mallocs you can do very good debugging.
For instance, what i have now is this: http://dirbaio.pastebin.com/UaHqxWfa
What it does is allocate the memory from the beginning of RAM, and each allocated block has around it a reserved area filled with constant data. When you do a free(), it checks if the data is still there, so it can see if you're writing out of the bounds of an array or similar things.
It can catch silly errors like
u8* ptr = new u8[100];
delete[] ptr;
delete[] ptr; //Twice!!
and like
u8* ptr = new u8[10];
ptr[11]=1;
delete[] ptr;
Re: How can I make custom malloc / free?
if you just want to catch your own silly errors then only use new and delete and override them
Re: How can I make custom malloc / free?
Umm... could you please elaborate a little more on this thing about default malloc / free being "bugged"?
Re: How can I make custom malloc / free?
its not the default malloc/free that is bugged, but by overriding malloc and free, you can keep better track of memory allocation/freeing to find memory leaks and stuff.
Re: How can I make custom malloc / free?
The GNU linker allows you to override functions. It can be useful for globally disabling printf()s in crappy middleware and libraries in release builds.
Here's a forum post explaining how to use it with malloc. The same thing would apply for free/realloc.
http://www.linuxquestions.org/questions ... nt-790444/
Here's a forum post explaining how to use it with malloc. The same thing would apply for free/realloc.
http://www.linuxquestions.org/questions ... nt-790444/
-
- Site Admin
- Posts: 1986
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: How can I make custom malloc / free?
That's awesome, just tested with devkitARM, thanks cdmac.
and add -Wl,-wrap,_malloc_r to your LDFLAGS in the makefile. The linker then prefixes the real function with __real_ and your override with __wrap_
Code: Select all
void *__real__malloc_r(struct _reent*r, size_t size);
void *__wrap__malloc_r(struct _reent*r, size_t size) {
iprintf("my malloc\n");
/* do this or write your own allocator */
return __real_malloc_r(r,size);
}
Code: Select all
LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-wrap,_malloc_r -Wl,-Map,$(notdir $*.map)
Who is online
Users browsing this forum: No registered users and 4 guests