Page 1 of 1

Dynamically allocating SRAM memory

Posted: Fri Sep 18, 2009 7:58 am
by pwhiteis
Hello,

I currently have a need for fast buffers like those used in the Ethernet/TCP stack. Is there a way I can use malloc() or remalloc() equivalent functions using my SRAM memory area?

Re: Dynamically allocating SRAM memory

Posted: Fri Sep 18, 2009 12:55 pm
by lgitlitz
Hi,

The way the system is configured you can not use normal malloc() in SRAM space. The system heap is located in SDRAM so that is where malloc calls will allocate memory.

As with any ram limited embedded system, it is probably not in your best interest to use malloc. Due to 8MB or less of SDRAM and 63K of space in SRAM there is a much greater chance of malloc failing then on a normal PC application. You must make sure that you check the return of every malloc call and properly handle the condition where it fails. It is a much safer approach to use static memory, this will ensure that memory will be available at run-time.

One alternative is to use our buffer system instead of malloc. In the newer builds there will be a buffer pool in both SDRAM and SRAM. At the buffer initialization the system checks how much unused space is in SRAM and fills this space with buffers which are used for network and serial communication. I think the default SRAM allocation will leave about 17 or 18 buffers in the SRAM buffer pool, but then 10 are allocated for Ethernet RX. This leaves the system with 7 or 8 fast SRAM buffers for other network or serial data. When the system allocates a buffer it first will try to get a buffer from SRAM, if there are non available then it gets one from the much larger pool in SDRAM.

So in your application you can call "GetBuffer();". This will return a pointer to a buffer if successful and 0 if it fails. The size of each buffer is ETHER_BUFFER_SIZE or 1548 bytes, if you need more space then this may not work for you. You can then check the address returned to see if it is located in SRAM... if the address is greater then 0x20000000 it is in SRAM. When you are done with the buffer you must send it back to the pool by calling FreeBuffer().

Re: Dynamically allocating SRAM memory

Posted: Fri Sep 18, 2009 2:46 pm
by pwhiteis
Hello,

I should've phrased my question a little differently. I am aware of the potential problems using malloc on a memory limited system, however, I have a need for a small dynamically allocated high speed RAM space. I have some code inherited from a vendor that liberally mallocs (never frees) small chunks of memory while it is building a DPM memory map. Total memory consumption is probably no more that 2Kb. So, I the interest of keeping re-write of the vendors code to a minimum, I was wondering if there was an alternate malloc in the NDDK (like fast_malloc()) which gives me control over the size of the chunk I request.
I can probably accomplish what I need with the fixed chunk allocator GetBuffer() you mention, then divide the big chunk(s) into smaller bits as needed.

Cheers,

-Pete-