Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent malloc/free to be compiled for embedded projects

Background: We are using Keil to compile our NXP LPC2458 project. There are numerous tasks that are being run on Keil’s RealView RTOS. There is stack space created, which is being allocated to each task. There is no HEAP created by default, and I want to avoid it since we can't afford the code-space overhead and the cost of "garbage collecting"

Objective: Use C++ in the embedded code without using the heap. Keil provides the #pragma (__use_no_heap) which prevents malloc() and free() calls to be linked.

Solution: I tried creating a Singleton with a private static pointer. My hopes were that the new() would not be called since I declared dlmData as static in the getDLMData(). For some reason, the linker still states that malloc() and free() are being called. I have thoughts of a private operator new () and a private operator delete() , and then declaring the dlmData as static within the overloaded function. It is not working for some reason. WHAT AM I DOING WRONG?

    //class declaration
    class DataLogMaintenanceData
    {
    public:
      static DataLogMaintenanceData* getDLMData();
      ~DataLogMaintenanceData()
      { instanceFlag = FALSE; }
    protected:
      DataLogMaintenaceData(); //constructor declared protected to avoid poly
    private:
      static Boolean instanceFlag;
      static DataLogMaintenceData *DLMData;
    }

    //set these to NULL when the code is first started
    Boolean DataLogMaintenanceData::instanceFlag = FALSE;
    DataLogMaintenanceData *DataLogMaintenaceData::DLMData = NULL;    

    //class functions
    DataLogMaintenanceData *DataLogMaintenanceData::getDLMData()
    {
        if (FALSE == instanceFlag)
        {
            static DataLogMaintenanceData dlmData;
            DLMData = &dlmData;
            instanceFlag = TRUE;
            return DLMData;
        }
        else
        {
            return DLMData;
        }
    }

    void InitDataLog ( void )
    {
        DataLogMaintenanceData *dlmData;
        dlmData = DataLogMaintenanceData::getDLMData();
        // to avoid dlmData warning
        dlmData = dlmData;
    }

    //ACTUAL TASK
    __task DataLog()
    {
      .. .. .. code to initialize stuff

      InitDataLog();

      .. .. ..more stuff
    }

For some reason, the only way I can get this to compile, is to create a heap space and then allow the malloc() and free() calls to be compiled into the project. As expected, the “static”ally defined object, dlmData, resides in the RAM space allocated to the dataLog.o module (i.e. it doesn’t live in the HEAP).

I can’t figure out, and I have checked Google, what am I missing? Is it possible in C++ to bypass malloc() and free() when compiling pure objects? I know I can replace the RTOS’s implementation of malloc() and free() to do nothing, but I want to avoid compiling in code that I won’t use.

like image 451
embedded_parag Avatar asked Oct 10 '22 08:10

embedded_parag


1 Answers

Probably some of the code we aren't seeing calls a function that calls malloc behind the scenes.

From http://www.keil.com/support/man/docs/armlib/armlib_CJAIJCJI.htm you can use --verbose --list=out.txt on the link line to get details about the malloc caller.

like image 151
Mark B Avatar answered Oct 18 '22 13:10

Mark B