Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Is it possible to change the memory bank of the Teensy 4 for malloc/heap?

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Location
    Irvine California
    Posts
    2

    Is it possible to change the memory bank of the Teensy 4 for malloc/heap?

    Hello. I was wondering if anyone could help me figure out how to change the memory bank for dealing with malloc/heap operations. I see it's on memory bank two, but that runs at 1/4th the speed of the primary memory bank. If possible I'd like to change up my memory map to have more heap space on RAM bank 1.

    Best,

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,671
    currently there is no malloc/free operations on T4.x that allow you to allocate unused memory in the DTCM area of memory.

    Note: especially when you add a T4.1 into the mix with the possibility of RAM added, there are three places one may want to allocate and free memory.

    As mentioned currently you can only allocate memory from the DMAMEM segement of memory. This is setup by the linker script, which has two addresses added:
    And the main allocate function for new blocks, which is in startup.c...


    Code:
    // from the linker script
    extern unsigned long _heap_start;
    extern unsigned long _heap_end;
    
    char *__brkval = (char *)&_heap_start;
    
    void * _sbrk(int incr)
    {
            char *prev = __brkval;
            if (incr != 0) {
                    if (prev + incr > (char *)&_heap_end) {
                            errno = ENOMEM;
                            return (void *)-1;
                    }
                    __brkval = prev + incr;
            }
            return prev;
    }
    So you could hack up the linker script to setup the _heap_start and _heap_end values to be at the end of dtcm.
    You can setup the start to be some where after the last variables are defined. As for end, the heap wants to grow up and the stack grow down (which is at the end of DTCM). So they then can collide.

    Longer term I am hoping that we can come up with something like a heapAlloc that you can pass in which heap you want and/or parameters on memory usage and have it allocate from the appropriate memory pool. But so far that does not exist.

  3. #3
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    646
    Possibly just by changing _heap_start and _heap_end values to what would normally be unused stack space (above zeroed variables)? For some users, I'm sure the speed is more important than the heap size. Interesting optimization would be to allocate first from a small amount of fast ram and then slower ram when that runs out.

    edit: typing at the same time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •