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 > mallinfo() returns wrong values

#71384 - HyperHacker - Sun Feb 12, 2006 8:35 am

I just came across mallinfo(), which is nice, but it seems to be a bit confused as to how much memory is actually free. While uordblks correctly specifies how much I've allocated, fordblks specifies a very small number (~1000, when I've allocated 96K) that actually goes up as more memory gets used (~5000 when allocating an additional 4K). O_o

I imagine this is related to another problem; malloc() is failing trying to allocate another 96K, even though there's nearly 4MB available and it used to work before. (Just kinda died all of a sudden...)

[edit] Odd... malloc() fails the first time trying to allocate 96K, but then succeeds doing the same a few milliseconds later.

#71901 - HyperHacker - Wed Feb 15, 2006 6:20 am

Hmm, weird. I fixed a stupid allocation bug, and now mallinfo() at least reports a bigger number (~300,000). I guess it's number of blocks, not number of bytes; how big are these blocks? malloc() still fails (as does memalign(), which is like malloc() but aligns blocks to a specified boundary) but just calling it in a loop works around this and it eventually succeeds. O_o (Of course if it fails 10 times in a row the loop quits and I assume it really is out of memory.)

So it works, but I can't shake the feeling that something must be going wrong for it to need that...

#78811 - wintermute - Sun Apr 09, 2006 11:39 pm

That behaviour sounds a bit odd. Could you send me some test code that I can reproduce the problem with.

In regard to mallinfo you'll find some further information here

http://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html

arena contains the total size of memory allocated with sbrk by malloc, in bytes. This is the total amount of memory that the malloc functions have requested from the underlying heap allocator via sbrk. Malloc simply requests a block of memory which is then divided up by it's own internal routines.

ordblks are chunks in mallocs internal tables which are currently free and can be allocated. This is unrelated to the actual amount of free RAM in the system unless all available memory has been allocated at some point.

keepcost may possibly be useful in determining the memory yet to be allocated. If you can determine the base address of this chunk and add keepcost then you should obtain the base address of memory still not obtained from sbrk. In devkitARM there is a variable called fake_head_end which contains the end address of memory which will be allocated by sbrk. You can access this by using this declaration

Code:

extern char *fake_heap_end;

_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#80761 - HyperHacker - Tue Apr 25, 2006 8:31 am

Alright, just wanted to confirm that I saw your post. I'll do it next time I do DS dev, I'm just too busy at the moment. (Reinstalling Windows is fun! -_-)

#80769 - tepples - Tue Apr 25, 2006 12:49 pm

wintermute wrote:
In regard to mallinfo you'll find some further information here

http://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html

That manual covers GNU libc. Does devkitARM's newlib provide the same extensions?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#80805 - wintermute - Tue Apr 25, 2006 6:50 pm

tepples wrote:
wintermute wrote:
In regard to mallinfo you'll find some further information here

http://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html

That manual covers GNU libc. Does devkitARM's newlib provide the same extensions?


That page says nothing about extensions, it merely describes the mallinfo struct. The struct defined in malloc.h contains those elements but the newlib documentation doesn't have as much detail as that page.

http://sourceware.org/newlib/libc.html#SEC22
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#80918 - tepples - Wed Apr 26, 2006 3:09 am

wintermute wrote:
tepples wrote:
devkitARM's newlib provide the same extensions?

That page says nothing about extensions, it merely describes the mallinfo struct.

mallinfo itself is an extension to the C standard library.

Quote:
The struct defined in malloc.h contains those elements

Thanks. It's a pet peeve of mine when people talk about functions in one implementation of a C library and treat them as if they're in the ANSI standard.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#81574 - HyperHacker - Sun Apr 30, 2006 4:25 am

Alright, well the program that was having this problem doesn't even really exist anymore, but I wrote a quick test app. malloc() doesn't fail, but fordblks jumps around a lot.

Test app here