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.

C/C++ > Checking memory

#9684 - DiscoStew - Wed Aug 13, 2003 8:52 pm

Is there a way that I can check to see what is the first unused section of RAM at runtime on the GBA? Allocating a dynamic array of variables is supposedly slow, so I just want to get the address of the first unused section of memory. Is this possible?
_________________
DS - It's all about DiscoStew

#9689 - tepples - Wed Aug 13, 2003 11:19 pm

Depending on your link script, your elf file should have a symbol with a name like 'ewram heap start'. To read the symbols in your elf file, provided you didn't link it with -s (which tells the linker to omit symbols), do nm -n your elf file. Look for the highest address matching 0x020##### to see which symbol your link script uses.

Dynamic memory allocation can be fast depending on the algorithm. The general-purpose malloc() included with newlib may be too slow or have too much fragmentation, but given the address of the start of the heap in EWRAM, you can write your own allocator.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#9697 - Touchstone - Thu Aug 14, 2003 11:51 am

Like tepples said, it's very much up to your linkerscript, but there is often a symbol called __bss_end__ and the address of that symbol is the first byte of RAM that is not allocated for variables.

When linking you can generate a map-file by passing -Map mapfile name to the linker. Find the BSS section in the generated mapfile (by looking for the line that starts with .bss) and then look for the last symbol before the next segment section.

I don't know if there is an all-purpose solution to this problem so how you should solve it depends on if you are using linkerscripts and in that case, what they look like.
_________________
You can't beat our meat

#9699 - Sweex - Thu Aug 14, 2003 2:27 pm

Just thought of the following, in line with this topic: Is it possible to detect the amount of free work memory (EWRAM?).

I currently subtract the number of bytes allocated by my own "new" function from 256k, but obviously this isn't too accurate.
_________________
If everything fails, read the manual: If even that fails, post on forum!

#9704 - tepples - Thu Aug 14, 2003 4:45 pm

Yes, you have the right idea: if you want to know how much free memory is left, keep track of free memory yourself.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#9711 - DiscoStew - Thu Aug 14, 2003 6:19 pm

Thx for the answer to my question, but now I've got another question.
I check my small program, and it appears that if you don't set it, the 1st 32k of variables, arrays, etc. is set into IWRAM, since my ewram_heap_start starts at 0x02000000. How can I prevent anything from going into IWRAM, and setting specific things into IWRAM?
_________________
DS - It's all about DiscoStew

#9720 - tepples - Fri Aug 15, 2003 3:27 am

If you're using Jeff F's link script (found in DevKit Advance), you can use attributes to put code and data in various sections. See the relevant part of Jeff F's GBA FAQ.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.