chipKIT® Development Platform

Inspired by Arduino™

Max32 Memory Allocation Revisited

Created Tue, 31 Jan 2012 00:39:55 +0000 by username


username

Tue, 31 Jan 2012 00:39:55 +0000

Like Ben had reported, I'm having malloc issues, but without his success!

First, I am (attempting?) to set the _min_heap_size via:

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

But when I run:

void setup()
{
  Serial.begin(115200);
  Serial.println("Hello");
}

#define ALLOCS 100
#define ALLOCSIZE 0x100

void loop()
{
  int scnt = 0;
  int fcnt = 0;
  
  for (int i = 0; i < ALLOCS; i++)
  {
    byte* pb = (byte*)malloc(ALLOCSIZE);
    if (pb == NULL)
    {
      Serial.print("F");
      fcnt++;
    }
    else
    {
      Serial.print("S");
      scnt++;
    }
  }
  
  Serial.print("Using an ALLOCSIZE of ");
  Serial.println(ALLOCSIZE);
  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);
}

I get:

Hello
SSSSSSSFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Using an ALLOCSIZE of 256
Successfully allocated 7 times. Total memory allocated 1792 bytes.
93 Failures

What am I doing wrong?

The call to the linker looks like:

/Applications/mpide.app/Contents/Resources/Java/hardware/pic32/compiler/pic32-tools/bin/pic32-g++  -Os  -Wl,--gc-sections  -mdebugger  --defsym,_min_heap_size=32768  -mprocessor=32MX795F512L  -o  /var/folders/1L/1L0xkmmtEJeeHvrzE56y9E+++TI/-Tmp-/build808180045614654187.tmp/mallocTest2.cpp.elf  /var/folders/1L/1L0xkmmtEJeeHvrzE56y9E+++TI/-Tmp-/build808180045614654187.tmp/mallocTest2.cpp.o    /var/folders/1L/1L0xkmmtEJeeHvrzE56y9E+++TI/-Tmp-/build808180045614654187.tmp/core.a  -L/var/folders/1L/1L0xkmmtEJeeHvrzE56y9E+++TI/-Tmp-/build808180045614654187.tmp  -lm  -T  /Applications/Mpide.app/Contents/Resources/Java/hardware/pic32/cores/pic32/chipKIT-MAX32-application-32MX795F512L.ld

I was getting this result with 0022 build, but have switch to the 0023 - but same results. Ideas?

Thanks in advance, -Scott


username

Tue, 31 Jan 2012 01:05:56 +0000

Self Answered! After posting the above and a little RTFMing... I found:

-Wa,option... described in the C32 manual, which passes options to the ASSEMBLER, and wondered if -Wl,option... passes options to the linker, in which case what I needed was a link line that looked like:

-Wl,--gc-sections,--defsym,_min_heap_size=32768

That is, there can be no spaces... so if you want a 32k heap, your preferences file should look like:

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

-Wl is documented on page 37 of "MPLAB C32 User Guide.pdf"

And as a side note, I'm running on OSX... and (though beggars can't be choosers) I think it would be awesome to have some "per project" preferences, say for things like Optimization, Debug level and Heap Size!