Home arrow Support arrow Forums

Luminary Micro Forums

<< Start < Prev 1 2 Next > End >>

dereksoftstuff

Expert Boarder
Click here to see the profile of this user

2008/09/23 02:34

Re:HowTo : get malloc to work

Hello AN

Yes I used to use PIC's as well (PIC18) - great stuff, but those days are gone now. It had 3 or 4k RAM and not much point in using dynamic memory.
The current range of LMI M3 chips have 64k RAM - lets just say that again "64k RAM" for small embedded systems - new world ...

You probably will not be interested in my upcoming posts regading utilities and dynamic memory, but if you get the chance give it a try, you might be surprised.

login or register to reply

jrmymllr

Platinum Boarder
Click here to see the profile of this user

2008/09/24 05:56

Re:HowTo : get malloc to work

I've been meaning to post this for awhile, but I am having an odd problem with the _sbrk function. It is a problem with the part of the code that checks if I'm exceeding available RAM.

With this code in place, I was having a problem where malloc was not allocating RAM, even though I knew I had enough. When I removed this code, everything worked fine. So I did some debugging....Turns out, even though I was requesting 20-some KB from malloc, the number passed to the _sbrk function was over 23 or 24K!

For now I just removed the check and make sure I have enough before asking for memory. But any ideas what is going on with this?

login or register to reply

dereksoftstuff

Expert Boarder
Click here to see the profile of this user

2008/09/24 09:25

Re:HowTo : get malloc to work

Nice spot, the last chunk of RAM isn't allocated.

The RAM check is OK - infact only needed if your stack starts at top of heap.

The problem is that the end of heap is at 2000fff8 and malloc wants to go to 20010000.

Therefore 2 mods :-
a) linker (remove -8)

Code:

  PROVIDE _HEAP_END ALIGN(ORIGIN(SRAM) + LENGTH(SRAM) ,8) );



b) sbrk
>= to >
Code:

  ... (nextHeap > (caddr_t)&_HEAP_END)){         return NULL// error - no more memory 



I think that there is not necessarily a 1:1 relationship between malloc calls and sbrk calls, that depends on the malloc algorithm. It might get a bit more than you requested and try and service your next request with what's left ...

Also the malloc algorithm itself will use some of the RAM to link the chunks etc.


I use the following simple utility :-
systemRAM.h & .c



Code:

  #ifndef SYSTEM_RAM_H_ #define SYSTEM_RAM_H_ #include <systemDefs.h> #ifdef __cplusplus extern "C" { #endif typedef struct dynamicRAMInfoType {     UINT size;     UINT startAddr;     UINT endAddr; }; typedef voidRAM_ADDRESS; struct dynamicRAMInfoType sizeofDynamicRAM(void); boolean validHeapAddress(RAM_ADDRESS addr); RAM_ADDRESS freeMemory(RAM_ADDRESS addr); #ifdef __cplusplus } #endif #endif // SYSTEM_RAM_H_





Code:

  #include <systemRAM.h> #include <string.h> #include <malloc.h> // linker (standalone.ld) sets heap start and end extern unsigned int  _HEAP_START; extern unsigned int  _HEAP_END; #ifdef __cplusplus extern "C" { #endif struct dynamicRAMInfoType sizeofDynamicRAM(void) {     struct dynamicRAMInfoType info;          info.startAddr = (UINT)&_HEAP_START;     info.endAddr = (UINT)&_HEAP_END;     info.size info.endAddr info.startAddr;          return info; } boolean validHeapAddress(RAM_ADDRESS addr) {     if (((UINT)addr >= (UINT)&_HEAP_START) &&          ((UINT)addr <= (UINT)&_HEAP_END)) {         return TRUE;     } else {         return FALSE;     } } RAM_ADDRESS freeMemory(RAM_ADDRESS addr) {     free(addr);     return NULL; } #ifdef __cplusplus } #endif

login or register to reply
<< Start < Prev 1 2 Next > End >>