• AVR Freaks

Hot!XC32 dont allocate heap memory

Author
rangel
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2015/02/24 08:21:23
  • Location: 0
  • Status: offline
2019/07/04 17:54:56 (permalink)
0

XC32 dont allocate heap memory

Hi,
 
Why "XC32" linker not allocate memory to heap  ?
 
Under project properties -> XC32 -> XC32-ld -> "Options for xc32-ld (v2.20) -> option categories: General -> Heap size (bytes)  i specified 32768 bytes for the heap.
The project compile OK, but after compiling, in dashboard "Data Memory" by my calculations surely the memory heap was not allocated.
 
I programming the PIC32MK and the "real prog" dont work too.
If i allocate a static array, the code works.
 
Below is the partial code:

uint16_t *temp_buf = ( uint16_t * ) malloc( 7056 * sizeof(uint16_t) );

if( temp_buf == NULL )
{
    led = ~led;
    return;
}
 
cont = 0;

for( i = Top_Position ; i <= endLin ; i++ )
{
      for( j = Left_Position ; j <= endCol ; j++ )
      {
            temp_buf[cont] = frameBuffer[i][j];   //  OR without variable cont.   (*temp_buf++) = frameBuffer[i][j];
            cont++;
      }
}
 
 
 
// do something.
 
 
if( temp_buf )    // If temp_buf != NULL
    free(temp_buf);
 

PIC32MK1024MCF064
XC32 v2.20
MPLABX v5.20 
 
 
Thank's.
 
#1

18 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3607
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/04 19:45:02 (permalink)
    0 (2)
    Not an answer to your question but why are you wanting such a HUGE heap?
    I'm guessing its because you want to allocate lots of large blocks of memory dynamically. In an embedded system that is intended to run 'forever', this can be a recipe for disaster.
    Unless the malloc/free functions are *VERY* well written and can make sure that you NEVER fragment the heap, this this might work. However most implementations are not so well written and you will eventually run into trouble.
    The general rule for embedded apps is that if you think you need malloc/free then you are probably not writing your code the right way. Think very hard before going down this road.
    Susan
    #2
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/04 21:57:00 (permalink)
    0
    Hi Aussie,

    My intention is to learn at the moment.

    I know that dinamic allocation can be "dangerous" for the embedded program if not well thought.

    Could you tell me why the compiler did not allocate the heap?

    Thank's for the help.
    #3
    muellernick
    Super Member
    • Total Posts : 472
    • Reward points : 0
    • Joined: 2015/01/06 23:58:23
    • Location: Germany
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/04 23:15:44 (permalink)
    0
     Not an answer to your question but why are you wanting such a HUGE heap?

     
    So 32 k is HUGE!?
    I use 105 k.
    Make millions of allocs and frees a day. No problem with fragmentation. About 30 k are left free.
    Old granny tales from the 8 bit era that alloc is bad.
     
    Nick
    #4
    andersm
    Super Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: XC32 dont allocate heap memory 2019/07/05 02:36:04 (permalink)
    +1 (1)
    I don't think the heap reservation is included in the data memory count shown by the IDE. The linker will verify that there is at least that much space available, and generate some symbols to indicate where the heap should go, but it doesn't alter the .data, .bss or .sbss sections.
     
    muellernickMake millions of allocs and frees a day. No problem with fragmentation. About 30 k are left free.
    Old granny tales from the 8 bit era that alloc is bad.

    That depends very much on your allocation patterns.
    #5
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/05 16:02:38 (permalink)
    0
    No one.   :(
    #6
    ric
    Super Member
    • Total Posts : 23163
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/05 16:03:50 (permalink)
    0
    rangel
    No one.   :(

    Did you read post#5?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/05 16:42:34 (permalink)
    0
    Hi ric,
     
    I read and from what i understood andersm not sure that the allocated memory does not appear in the ide. In my opinion should appear, because it is a memory consumed by the system.
     
    I'm not sure if the memory is being allocated or not.
     
    As i said, if i allocate a static array, the code works and with dynamic allocated array not working.
     
    I think that the compiler dont allocate memory to heap, but my code may have another problem.
     
    Any suggestion ??? 
     
     
    Note:
    The compiler is strange too, it seems that it is optimizing the code. If I declare and do not use a global statically allocated array, the compiler does not reserve memory for this array.
    I also found a bug, I had forgotten to declare a variable within a function (variable with local scope) and the program compiled normally.
     
     
    PIC32MK1024MCF064 with predictive prefetch cache disabled.
    Affected Silicon Revisions A1.
    My pic32mk are revision A1.
     
    Thank's.
    #8
    aschen0866
    Super Member
    • Total Posts : 4478
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/05 16:58:06 (permalink)
    +1 (1)
    Go to Project Properties -> xc32-ld. From "Option categories" pull-down menu, choose Diagnostics, check "Display memory usage". You should see the memory summary after build. Of course, you can just open the .map file in the folder that corresponds to your build configuration in the .X folder.
    #9
    andersm
    Super Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: XC32 dont allocate heap memory 2019/07/05 17:29:31 (permalink)
    +1 (1)
    In the map file, the heap is listed under the heading "Dynamic Data-Memory Reservation":
    Dynamic Data-Memory Reservation
    section                    address  length [bytes]      (dec)  Description
    -------                 ----------  -------------------------  -----------
    heap                    0x80000008           0x10          16  Reserved for heap
    stack                   0x80000030         0x7fc8       32712  Reserved for stack

    If you examine the object file, you can see the symbols generated by the linker:
    $ xc32-nm mm_isr_test.X.production.elf | grep -e heap -e stack -e splim | sort
    00000010 W _min_heap_size
    00000400 A _min_stack_size
    80000008 W _heap
    80000018 W _eheap
    80000030 W _splim
    80007ff8 W _stack

    _min_heap_size and _min_stack_size corresponds to the values set in the project options, but the linker places them in the largest free contiguous chunk of RAM it can find. You can see in the example above that the space allocated for the stack (_stack - _splim) is much greater than the requested minimum.
     
    rangelI also found a bug, I had forgotten to declare a variable within a function (variable with local scope) and the program compiled normally.

    Are you sure you did not have a variable with the same name in an outer scope? Anyway, when you say that your program "don't work" when using malloc, what do you mean? Does malloc return NULL? Can you provide a complete example that exhibits the problem?
    #10
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/07 15:38:23 (permalink)
    0
    Hi andersm,
     
    "Are you sure you did not have a variable with the same name in an outer scope ?"
    My mistake. There is a variable with the same name in another part of the program(outer scope) in another file(global variable).  I will start declaring variables with store class static. 
     
    "Anyway, when you say that your program "don't work" when using malloc, what do you mean? Does malloc return NULL? Can you provide a complete example that exhibits the problem?"
    In my code in the first post i put to toggle an i/o led if the malloc return a NULL pointer. The led dont toggle, so i presume that malloc dont return a NULL.  
     
    Thank's.
    #11
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/07 16:40:28 (permalink)
    0
    Hi,
     
    I enabled "Display memory usage" how aschen0866 said and i can see in the file "Project name(Clean, Build, ...)"
     
    Dynamic Data-Memory Reservation
    section    address            length      [bytes] (dec)      Description
    ------- ---------- ------------------------- -----------
    heap       0x80025bd0     0x8000     32768                Reserved for heap
    stack       0x8002dbe8     0x12410   74768                Reserved for stack
     
     
    I also opened the map file(.map) and saw that information.
     
    I will post the code that works with statically allocated array and the code that dont works with dynamically allocated array. 
     
    Thank's.
    #12
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/07 17:18:42 (permalink)
    0
    Hi,
     
    Below is the code that works:

    void ili9341GFX_Framed_Mode_Send_Parcial_Frame_To_Display( uint16_t Left_Position, uint16_t Top_Position, uint16_t Pix_Width, uint16_t Pix_Height )
    {
          static uint16_t i, j;
          static SetColumnAddress px;
          static SetLineAddress py;  
          static uint16_t endLin;
          static uint16_t endCol;
          static uint32_t pixNumber;
          static uint32_t index;
          static uint16_t temp_buf[16384];  // 32768 bytes


          pixNumber = (uint32_t) ( Pix_Height ) * ( Pix_Width );

    ////////////////////////////////////////////////////////////////////////////////

          px.STARTCOL = Left_Position;
          endCol = ( Left_Position ) + ( Pix_Width ) - 1;

          if ( endCol > 319 )
          {
                px.ENDCOL = 319;
          }
          else
          {
               px.ENDCOL = endCol;
          }

          py.STARTLIN = Top_Position;
          endLin = ( Top_Position ) + ( Pix_Height ) - 1;

          if ( endLin > 239 )
         {
               py.ENDLIN = 239;
         }
         else
        {
                py.ENDLIN = endLin;
        }

    ////////////////////////////////////////////////////////////////////////////////

          index = 0;

          for( i = Top_Position ; i <= endLin ; i++ )
         {
                for( j = Left_Position ; j <= endCol ; j++ )
               {
                     temp_buf[index] = frameBuffer[i][j];
                     index++;
               }  
          }

    ////////////////////////////////////////////////////////////////////////////////

          ili9341Drv_Set_Column( &px );
          ili9341Drv_Set_Line( &py );
          ili9341Drv_Command_Write( ramWr );

          ili9341Drv_Data_Write_16bits( &temp_buf, pixNumber );
    }

     
    Below is the code that don't works:
     

    void ili9341GFX_Framed_Mode_Send_Parcial_Frame_To_Display( uint16_t Left_Position, uint16_t Top_Position, uint16_t Pix_Width, uint16_t Pix_Height )
    {
          static uint16_t i, j;
          static SetColumnAddress px;
          static SetLineAddress py;
          static uint16_t endLin;
          static uint16_t endCol;
          static uint32_t pixNumber;
          static uint32_t index;
          static uint16_t *temp_buf;

          pixNumber = (uint32_t) ( Pix_Height ) * ( Pix_Width );
     
    //if( ( pixNumber * sizeof(uint16_t) ) > heap_size )
    // return;

          temp_buf = ( uint16_t * ) malloc( pixNumber * sizeof(uint16_t) );

          if( temp_buf == NULL )
          {
                led = ~led;
                return;
          }

    ////////////////////////////////////////////////////////////////////////////////

           px.STARTCOL = Left_Position;
           endCol = ( Left_Position ) + ( Pix_Width ) - 1;

           if ( endCol > 319 )
           {
                px.ENDCOL = 319;
           }
           else
          {  
                px.ENDCOL = endCol;
          }

          py.STARTLIN = Top_Position;
          endLin = ( Top_Position ) + ( Pix_Height ) - 1;

          if ( endLin > 239 )
          {
                py.ENDLIN = 239;
          }
          else
         {
               py.ENDLIN = endLin;
         }   

    ////////////////////////////////////////////////////////////////////////////////

          index = 0;

          for( i = Top_Position ; i <= endLin ; i++ )
         {
                for( j = Left_Position ; j <= endCol ; j++ )
               {
                      temp_buf[index] = frameBuffer[i][j];
                      index++;
               }
          }

    ////////////////////////////////////////////////////////////////////////////////

          ili9341Drv_Set_Column( &px );
          ili9341Drv_Set_Line( &py );
          ili9341Drv_Command_Write( ramWr );

          ili9341Drv_Data_Write_16bits( &temp_buf, pixNumber );


          if( temp_buf ) // Se temp_buf != NULL
                free(temp_buf);
    }

     
     
    #13
    andersm
    Super Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: XC32 dont allocate heap memory 2019/07/08 05:15:46 (permalink)
    0
    rangelMy mistake. There is a variable with the same name in another part of the program(outer scope) in another file(global variable).

    As long as the global variable wasn't declared in a header, enabling compiler warnings should have warned about that.
     
    With "complete example", I meant a project that someone else can build and run. However, since each malloc allocation includes some overhead for internal bookkeeping, you can't allocate the full amount of memory reserved for the heap. If you want to allocate 32768 bytes, then the heap must be larger than 32768 bytes. Since the XC32 default malloc routine is a binary-only black box I can't tell how much overhead there is, but my guess is that it is 8 bytes or, maybe 16 per allocation.
    post edited by andersm - 2019/07/08 06:20:46
    #14
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/08 08:07:31 (permalink)
    0
    Hi andersm,

    The global variable was declared in a header file how "extern" :(

    About the problem between statically and dinamically allocated array, the functions that i post are complete.
    How i said: statically function works and dinamically function dont works.
    I dont change anything in the program to test the two functions, just comment the unused function.

    What part of the program would you consider relevant for me to post, not the entire program ?


    Thank's.
    #15
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/08 08:17:39 (permalink)
    0
    About the size of heap, i made a test allocating 32768 bytes, full heap size, and the malloc return a null pointer in this case.

    But in another test, i allocate only 14112 bytes to test my malloc function and the malloc dont return null.

    And again with 14112 bytes the statically allocated array function works and dinamically allocated array dont works.
    #16
    andersm
    Super Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: XC32 dont allocate heap memory 2019/07/08 08:59:48 (permalink)
    0
    rangelAbout the problem between statically and dinamically allocated array, the functions that i post are complete.

    I can't compile and run a function. The best thing to post is a complete, reduced example that shows the problem. Complete means all source, project files, and anything else needed to build and run the example. Reduced means that anything not related to the problem is removed. It doesn't help to post 10000 lines of source, but if you manage to reproduce the problem in a 100 line program, it is much easier to pinpoint the issue.
     
    If you only post a function, or part of a function, that doesn't include project settings, function parameters, and other important pieces of context.
    #17
    NKurzman
    A Guy on the Net
    • Total Posts : 17595
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/08 11:55:52 (permalink)
    0
    I am using XC32 V1.42  (with Harmony V1.11)  The Heap Works fine.
     
    The Question is what are you doing wrong?
    #18
    rangel
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2015/02/24 08:21:23
    • Location: 0
    • Status: offline
    Re: XC32 dont allocate heap memory 2019/07/08 12:31:25 (permalink)
    +2 (2)
    Hi,
     
    I simulated in the mplab simulator and i found my mistake.
     
    I was passing the address of pointer (address of temp_buf), and not the value inside the pointer temp_buf (address returned by malloc function).
     
    It's not the first time i do it :( .
    I had done this with the dma start address in a xc16 application.

    wrong code:
    ili9341Drv_Data_Write_16bits( &temp_buf, pixNumber );

    correct code:
    ili9341Drv_Data_Write_16bits( temp_buf, pixNumber );
     

    Code that receives the pointer:
    void ili9341Drv_Data_Write_16bits( uint16_t *objAddress, uint32_t size )
     

     
    Thank's for the help.
    post edited by rangel - 2019/07/08 13:03:37
    #19
    Jump to:
    © 2019 APG vNext Commercial Version 4.5