This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Memory allocation Issue

Hi all!
When we used malloc of size 2**15 in C code, it does not allocate.

uint16_t *out = (uint16_t *)malloc(samples_size * sizeof(uint16_t));
if (!out) {
fprintf(stderr, "Memory allocation for out failed.\n");
exit(1);
}
unsigned int samples_size       = 1 << 15;

DTCM 256kb and also ITCM in our chip (ARM Cortex M7) is 256kb.

CM7_DTCMCR
x 0xE000EF94
0xE000EF94: 0x00000049

CM7_ITCMCR
x 0xE000EF90
0xE000EF90: 0x00000049

It seems that I cannot use all 256kb that I have in DTCM.
Can you give me insights through this?

Based on ARM support, I have to located this (via scatterloading region ARM_LIB_(STACK)HEAP), but I do not know how to do it?
Appreciate your kind support and assistance.

  • Hi

    When using the Arm Compiler for Embedded 6 toolchain, you can place a heap in the memory map using a ARM_LIB_HEAP or ARM_LIB_STACKHEAP region in a scatter-file at link-time.  Please see:

    https://developer.arm.com/documentation/100073/0623/The-Arm-C-and-C---Libraries/Stack-and-heap-memory-allocation-and-the-Arm-C-and-C---libraries/Stack-pointer-initialization-and-heap-bounds

    The heap may be in a separate region from the stack (the "two region memory model"), or the stack & heap may be in a single region ("one region memory model"), for example:

    #ifdef TWO_REGION
      ARM_LIB_STACK 0x20006000 EMPTY 0x0800
      {
      }
      ARM_LIB_HEAP  0x20006800 EMPTY 0x0800
      {
      }
    #else
      ARM_LIB_STACKHEAP 0x20006000 EMPTY 0x1000
      {
      }
    #endif

    Note that the heap grows up in memory from its base, and the stack grows down in memory from the initial stack pointer.

    Please take a look at the ready-made "startup_Cortex-M7_AC6" example that is provided in Arm DS.  Many of the other examples also show placement of stack & heap in the scatter-file.

    Hope this helps

    Stephen

  • Thanks Stephen
    It was so helpful.
    I will test it and get back to you soon!

  • Hi again!

    I Went through the description and wrote this scatter file

    /* Define the entire 256 kB DTCM region */
    LR_DTCM 0x20000000 0x00040000 {

    /* Execution region for code and read-only data.
    Here we reserve 128 kB from 0x20000000 to 0x2001FFFF. */
    ER_DTCM 0x20000000 0x00020000 {
    *(InRoot$$Sections)
    *(.text*)
    *(.rodata*)
    *(.constdata*)
    *(.ARM.Collect*)
    *(+RO)
    }

    /* Execution region for read-write data.
    Here we reserve 32 kB from 0x20020000 to 0x20027FFF. */
    RW_DTCM 0x20020000 0x00008000 {
    *(.data*)
    *(.bss*)
    *(+RW)
    }

    /* Define the heap region for dynamic allocations.
    This reserve is 64 kB from 0x20028000 to 0x20037FFF. */
    ARM_LIB_HEAP 0x20028000 EMPTY 0x00010000 {
    /* Heap region: 64 kB available for malloc() */
    }

    /* Define the stack region.
    Here we reserve 32 kB from 0x20038000 to 0x2003FFFF. */
    ARM_LIB_STACK 0x20038000 EMPTY 0x00008000 {
    /* Stack region: 32 kB */
    }
    }

    Any suggestions if it is the right one to dedicate 64kB of 256kB from DTCM?

    Warm regards


  • Hi again

    As a quick test, I added your scatter file above into the "startup_Cortex-M7_AC6" example.  With some tweaks, I was able to get the example basically working.

    Note that the default C library itself uses some heap space for things like printf, so your full "samples_size = 1 << 15" didn't quite fit in the heap space, but "samples_size = 1 << 14" did.

    I could see a malloc call executing & returning correctly, with "out" being 0x20028060, which looks correct.  
    That's 0x20028000 (your heap base) plus 0x60 bytes of C library private workspace.

    Stephen

  • Thank you Stephen
    It was a great help!
    The issue is resolved.

  • Hi again!

    I have sort of a small question.
    I have created this scatter file in my C project in ARM DS.

    The question is when I go to my CMSIS project, can I use the same scatter file, or any changes and modifications should be made on that?

    And in my file, both stack and heap are created in DTCM. Shall I define ITCM statically in the same scatter file as well?

    regards

  • CMSIS Packs typically provide referenc scatter files for the device used, but you can of course edit as appropriate, for example to locate the stack/heap in the DTCM rather than general memory.

  • Hi

    Thank you Ronan and Stephen for providing me the insights, now I changed the scatter file in a way that my code can use the resources in an optimized way!

    Warm regards
    Mehran