This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2540: Memory full

Part Number: CC2540


I have a linked list that stores a uint32 value, and pointer. I am able to save 28 values, and then it reports no more memory available. Is this about normal?

I have also a routine that controls a strip of leds, and my routine takes the length(m) and drives those leds.
If I put 29m as parameter, it would hang.

I have looked into me code, and cannot see why 29 will hang the mcu.


Is there a way to read amount of bytes available in memory?

  • Where do you see it reports no more memory available? Can you elaborate?
  • I use malloc, and test if return value is NULL.
    If NULL received, I write to output characteristic array with message. In this case, "Memory Full".
    So, I fill my linked list till null happens. This happens when I store 28 * (uint32 of data + pointer).
    Try to store 29th, and get the NULL.
    The cc2540 has 8kbytes of memory, so surprised that it return NULL so soon.
  • 8K memory is shared by BLE Stack so it's not many for application usage. If you use a static array to store it, does it work?
  • I changed my linked list to use static array[100], and it is working.
    But, I would prefer to use a dynamic array rather than static size array.

    I usually test my code with gcc compiler, then copy over to mcu firmware. GCC compiler on windows, I can create list with 10000 entries.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dos.h>
    
    
    typedef signed   char   int8;     //!< Signed 8 bit integer
    typedef unsigned char   uint8;    //!< Unsigned 8 bit integer
    
    typedef signed   short  int16;    //!< Signed 16 bit integer
    typedef unsigned short  uint16;   //!< Unsigned 16 bit integer
    
    typedef signed   long   int32;    //!< Signed 32 bit integer
    typedef unsigned long   uint32;   //!< Unsigned 32 bit integer
    
    typedef unsigned char   bool;     //!< Boolean data type
    #define FALSE   0
    #define TRUE    1
    
    #define RESET_LIST              { temp = first; freeList(temp); first = 0; }
    
    struct node {
        uint32 laptime;
        struct node *next;
    };
    typedef struct node NODE;
    NODE *head, *first, *temp, *view;
    
    
    uint32 readValue(NODE* readptr, int index) {
        if ( readptr != NULL ) {
            while (index--) {
                readptr = readptr->next;
            }
            return readptr->laptime;
        }
        return 0;
    }
    
    bool appendValue(uint32 value) {
    
        head  = (NODE *)malloc(sizeof(NODE));
        if (head != NULL) {
            head->laptime = value;
            if (first != 0) {
                head->next = NULL;
                temp->next = head;
                temp = head;
            } else {
                first = temp = view = head;
                temp->next = NULL;
            }
            return TRUE;
        }
        return FALSE;
    }
    
    void freeList(NODE* curr) {
        NODE* freetemp;
    
        if (curr != NULL) {
            while(curr->next != NULL) {
                freetemp = curr->next;
                free(curr);
                curr = freetemp;
            }
            free(curr);
        }
    }
    
    int countList(NODE* curr) {
        int count;
        if (curr == NULL) {
            return 0;
        } else {
            count = 1;
            while(curr->next != NULL) {
                curr = curr->next;
                count++;
            }
            return count;
        }
    }
    
    int main(void)
    {
        int i;
    
        RESET_LIST
        for (i=0;i<10000;i++){
                appendValue(i);
        }
        view = first;
        for (i=0;i<10000;i++){
            printf("R%d: %lu\n", i, readValue(view, i) );
        }
        view = first;
        printf("Count:%d\n", countList(view));
        RESET_LIST
        view = first;
        printf("Count:%d\n", countList(view));
        return 0;
    }
    

  • If you have to use memory allocation, you can try to increase heap size.
  • INT_HEAP_LEN=3072
    Increase heap size? Is this correct parameter to set?
  • Changed to 4050 from 3072. Again it filled up on 29th entry.
    If I change to 5000, compiler complains about memory ranges.
  • Yes, there's still limitation to increase heap size. That is why I suggest using static array.
  • Thanks, I will use static array instead as it seems to work, compared to linked list.
  • You are welcome.