chipKIT® Development Platform

Inspired by Arduino™

MAX32 Memory allocations

Created Wed, 20 Jul 2011 00:31:49 +0000 by benwoodcock


benwoodcock

Wed, 20 Jul 2011 00:31:49 +0000

Hey all,

I did write up a long post explaining this situation only to hit a wrong key and loose it. All the better for you guys cause this one is much shorter.

Below is a test program that when run on the MAX32 results in successful allocation of 85 blocks of 16 bytes or 1360 bytes. This is less than the 128KB I was expecting (allowing for fragmentation etc obviously).

Can anyone point me in the direction of what it is that I am missing?

Thanks in advance,

Ben

void setup()
{
  Serial.begin(115200);
}

#define ALLOCS 200
#define ALLOCSIZE 0x02

void loop()
{
  int scnt = 0;
  int fcnt = 0;
  
  for (int i = 0; i < ALLOCS; i++)
  {
    byte* pb = (byte*)malloc(ALLOCSIZE);
    if (pb == NULL)
    {
      Serial.println("Failed");
      fcnt++;
    }
    else
    {
      Serial.println("Success");
      scnt++;
    }
  }
  
  Serial.print("Successfully allocated ");
  Serial.print(scnt, DEC);
  Serial.print(" times. Total memory allocated ");
  Serial.print(scnt * ALLOCSIZE);
  Serial.println(" bytes.");
  
  Serial.print(fcnt, DEC);
  Serial.println(" Failures");
  
  delay(1000000);
}

SERIAL OUTPUT:

Successfully allocated 85 times. Total memory allocated 1360 bytes. 115 Failures


ron.dunn

Wed, 20 Jul 2011 00:43:56 +0000

You need to adjust the heap size in the linker.

Here are a couple of good discussions on the subject:

http://www.microchip.com/forums/m512887-print.aspx

http://www.microchip.com/forums/m483626-print.aspx

One further point. There is an overhead associated with each malloc() entry. I've seen it suggested as around 15 bytes, but I haven't verified that in person. you might want to revisit the idea of allocating a large number of small entries.


benwoodcock

Wed, 20 Jul 2011 00:57:02 +0000

Hey,

Thanks for the push in the right direction. I knew I was just missing something.

Now I just need to figure out where to do this.

I am using the MPIDE IDE. Do you know if the heap options are available though this or do I need to find a config file or similar.

Also in regard to alloc overheads, this test is purely that, a test. I am actually looking at allocing around 1k blocks, but it kept failing and this is what testing ended up with.

Again thanks for your help.

Ben


ron.dunn

Wed, 20 Jul 2011 01:04:12 +0000

There is a file named PLATFORMS.TXT which you can modify to include linker settings.

I'm sorry, but I don't use the Arduino IDE, and I don't know where to find this file or how to modify it. Perhaps someone else will jump in with this information.


benwoodcock

Wed, 20 Jul 2011 02:05:16 +0000

I am looking around for information on how to increase the heap. Have not had any luck other than a lot of references to "I increased the heap size".

Does anyone know how to increase the heap for the MAX32? I would have thought this would be simple.


ron.dunn

Wed, 20 Jul 2011 06:24:15 +0000

Ben, it is somewhere here:

http://chipkit.org/forum/viewtopic.php?f=6&t=3&p=52&hilit=platforms.txt#p134

I've scratched my head over how this works, but I'm lost between the numeric parameters of the type {1} and the lines of what look like substitution values.

If I had to make a guess it would be to modify this line:

pic32.compiler.c.elf.flags=-Os::-Wl,--gc-sections

and possibly make it look like this:

pic32.compiler.c.elf.flags=-Os::-Wl,--gc-sections::--defsym,_min_heap_size=32768

If it works the way I think it does, that would give you a 32k heap.

Let's hope someone more knowledgeable can add some advice.


benwoodcock

Wed, 20 Jul 2011 23:31:01 +0000

Well I have it working. I am not 100% on the exact numbers I can use but I am now able to allocate about 3/4 of the heap in 1k blocks (seems about right with overhead etc).

As Ron Dunn said the change I made was to the line in platforms.txt

Forum thinks the line I changed is too spamy and contains offsite URLs. Please refer to Ron Dunn's port above for the change I made.

Thanks for your input

Ben


username

Mon, 30 Jan 2012 23:31:30 +0000

I'm having a similar problem, but adding the --defsym,_min_heap_size=32768 isn't helping. I'm able to allocate 254 bytes using your test program above.

I'm loving this chip, but the malloc issues are going to make me a) either do a rewrite of a large library, or write my own malloc/free, etc. neither of which make me happy!

-Scott