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 > malloc, EWRAM and ld problems

#53791 - xflash - Sun Sep 11, 2005 11:58 pm

Hello,

I'm currently porting some existing win32 code on the DS. I first try to compile my C++ code "as is".
After 2 or 3 liters of coffee, and a sleepless night, I succeed in it.
But my code crashs on some NULL return memory allocations.

While having read on this site all the concern met with malloc, I choose to replace thoses malloc with some static arrays.
Before:
Code:
uint8* _itemArray;
(...)
_itemArray = (uint8*)malloc(MAX_ITEMS);


After:
Code:
uint8 _itemArray[MAX_ITEMS];


The problem is on the link phase. The linker couldn't place my code in Ewram I think. I've got this error :
Code:
ld.exe: address 0x28006e8 of Test_08.arm9.elf section .bss is not within region ewram


What can I do? What is the limit of the binaries size to be loaded in EWram ?
I already use GBFS (great thanks to Damian Yerrick!!!) to include some heavy datas (near 6MB).
Isn't there other memory range where I could place some big temp arrays ? iwram ? the internal DS cache range ?

Thanks for your help !

xFlasH

#53797 - tepples - Mon Sep 12, 2005 1:41 am

xflash wrote:
What can I do? What is the limit of the binaries size to be loaded in EWram ?

GBA EWRAM is 0.25 MiB. Nintendo DS EWRAM is 4 MiB.
(1 MiB == 1,048,576 bytes)
This has to cover your program, const data (such as a GBFS file inserted with bin2s), global variables, and the heap.

Quote:
I already use GBFS (great thanks to Damian Yerrick!!!)

You're welcome.

Quote:
to include some heavy datas (near 6MB).
Isn't there other memory range where I could place some big temp arrays ? iwram ? the internal DS cache range ?

You could use the GBA ROM space (up to 32 MB), or you could use the CF of a GBA Movie Player (up to 2 GB).
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#53824 - xflash - Mon Sep 12, 2005 9:01 am

Hi,

tepples wrote:
GBA EWRAM is 0.25 MiB. Nintendo DS EWRAM is 4 MiB.
(1 MiB == 1,048,576 bytes)
This has to cover your program, const data (such as a GBFS file inserted with bin2s), global variables, and the heap.

The maximum memory allocation is about 1.5MBytes (detected under win32), my program is under 250kBytes, I've got some little global variables, but indeed the problem is about the heap which seems to be overfilled.
This is why I try to replace malloc with static arrays, whith the issue I've described ...
I couldn't understand what is so heavy with my code.
I've tried to interpret the memory map generated with -map gcc option
But it's a real pain :)
Isn't there some docs to describe linkscript sections .bss .sbss .ewram .iwram ?
I'm afraid that my code overlaps some Linker sections whitout to be so heavy.

thanks for your help

xf

#53838 - tepples - Mon Sep 12, 2005 2:54 pm

If you have an ELF file for your arm9 segment, try the 'nm' command to see if anything is overflowing RAM that ends at 0x02400000.

arm-elf-nm -n kitten.elf > kitten.map.txt
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#53883 - xflash - Mon Sep 12, 2005 11:10 pm

Thanks for your help.

Apparently the result seems to be good :
Code:
(...)
0208ebb4 B stub
0208ebb8 B gGlobalMemoryLength
0208ebbc B _currentTimeMillis
0208ebc0 B _frameCount
0208ebc4 B _pal
0208edc4 B buf
0208f1c4 B gNbLines
0208f1c8 B g_debugMask
0208f1cc b keysold
0208f1ce b keys
0208f1d0 b oldy
0208f1d2 b oldx
0208f1d4 b _ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating
0208f1d8 b emergency_used
0208f1dc b emergency_buffer
0208f9dc b globals_static
0208f9e4 B __new_handler
0208f9e8 b fc_static
0208f9ec B fake_heap_start
0208f9f0 B fake_heap_end
0208f9f4 B __malloc_current_mallinfo
0208fa1c B __malloc_max_total_mem
0208fa20 B __malloc_max_sbrked_mem
0208fa24 B __malloc_top_pad
0208fa28 b heap_end.2060
0208fa2c b openfiles
0208facc b monitor_stderr
0208fad0 b monitor_stdout
0208fad4 b monitor_stdin
0208fad8 b std_files_checked
0208fadc B _PathLocale
0208fae0 B __mlocale_changed
0208fae4 B __nlocale_changed
0208fae8 A __end__
0208fae8 A _end
0208fae8 A end
023ff000 A __eheap_end
02400000 A __ewram_end


But it doesn't show the heap allocation (malloc/free) which I was obliged to leave for some really dynamic arrays, unless the .elf is not produced.

#53896 - tepples - Tue Sep 13, 2005 3:20 am

For that, you'll have to monitor the variables whose names start with "__malloc_" at run time.

But either way, you won't be able to put 6 MB of stuff in a valid .nds file. You'll have to either use a *ds.gba file (if using a flash cart) or load one part at a time from the CF card (if using a GBA Movie Player) or win the Wi-Fi bounty and load one part at a time from a SMB or HTTP server (if using WMB with FlashMe).
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#53916 - xflash - Tue Sep 13, 2005 9:09 am

tepples wrote:
For that, you'll have to monitor the variables whose names start with "__malloc_" at run time.

Do you mean those one ? :
Code:
__malloc_current_mallinfo
__malloc_max_total_mem
__malloc_max_sbrked_mem
__malloc_top_pad


tepples wrote:
But either way, you won't be able to put 6 MB of stuff in a valid .nds file.

Yes I know. At present, my 6MBytes datas are stored on my EZII 256
I just upload the nds code with wmb. The datas which I mentionned is some internal engine datas .

tepples wrote:
win the Wi-Fi bounty and load one part at a time from a SMB or HTTP server (if using WMB with FlashMe).
I'm not John Carmack:p

xf

#53949 - tepples - Tue Sep 13, 2005 8:01 pm

xflash wrote:
tepples wrote:
For that, you'll have to monitor the variables whose names start with "__malloc_" at run time.

Do you mean those one ? :
Code:
__malloc_current_mallinfo
__malloc_max_total_mem
__malloc_max_sbrked_mem
__malloc_top_pad

Exacticalmente. Put some code in your program to print the values of those variables at various points, and you'll understand how much you're trying to allocate.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.