Page 1 of 1

alloc from mem2?

Posted: Thu Jun 04, 2009 7:01 pm
by ozzyyzzo
Hi,

While malloc() seems to use mem1 .. how to properly allocate memory from mem2?
thx

Re: alloc from mem2?

Posted: Thu Jun 04, 2009 7:12 pm
by ozzyyzzo
right now i'm assuming that SYS_GetArena2Lo() is returning usable memory. ^^
maybe we've got something a bit better? :)

Re: alloc from mem2?

Posted: Fri Jun 05, 2009 10:12 pm
by ozzyyzzo
Ok then, using SYS_GetArena2Lo() base addy just works well..
it doesn't seem that there is any kind of memory manager for mem2..
and yes we don't need that! (much) ^^

Re: alloc from mem2?

Posted: Sat Jun 06, 2009 9:02 am
by shagkur
Hi,

latest SVN already contains a patch for malloc to handle mem2 aswell.
ofcourse mem2 will only be taken if we run out of mem1.

Also there's the lwp_heap API for custom memory management.

regards
shagkur

Re: alloc from mem2?

Posted: Thu Sep 10, 2009 11:54 pm
by Beau
Becareful using this, a lot of memory goes to waste in certain instances due to internal fragmentation since MEM1 and MEM2 are not contiguous memory.

For example, the way memory is allocated with sbrk and the way Wii has 23 MB and 64 MB seperately leads to major internal fragmentation. If you were to allocate say 11 MB, and you only have 10 MB in MEM1, 11 MB will be allocated out of MEM2. You'd think that you would still have 10 MB in MEM1, however, you would be wrong. The 10 MB in MEM1 is treated as fragmented within sbrk/newlib malloc implemenations. MEM1 is not able to be used until all memory in MEM2 has been free'd (every last byte).

If you want the best possible use of memory, configure libogc to not malloc out of MEM2 (just MEM1) and write your own heap for MEM2 or use LWP_HEAP. This makes LibOGC have 2 heaps (which reflects the reality of the hardware design Nintendo decided) and avoids fragmentation easily. However, with this approach you typically have to hook malloc via -wrap in GCC and call __real_malloc yourself and if that fails, then allocate out of the LWP_HEAP (MEM2).