Prevent malloc / free compilation for embedded projects

Reference Information. We use Keil to compile our NXP LPC2458 project. There are many tasks that are performed on Keils RealView RTOS. A stack space has been created that is allocated for each task. There is no default HEAP, and I want to avoid it, because we cannot afford the overhead of code and the cost of "garbage collection"

Goal: use C ++ in embedded code without using heap. Keil provides #pragma (__use_no_heap), which prohibits the binding of malloc () and free () calls.

Solution: I tried creating a Singleton with a private static pointer. My hopes were that new () would not be called, as I declared dlmData static in getDLMData (). For some reason, the linker still claims that malloc () and free () are being called. I have the thoughts of the private operator new () and the private operator delete (), and then declaring dlmData as static in an overloaded function. For some reason it does not work. WHAT IS I DEFAULT?

//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 put this together is to create a bunch of space and then allow compilation of malloc () and free () into the project. As expected, the “static” associated object, dlmData, is located in the RAM space allocated for the dataLog.o module (that is, it does not live in HEAP).

I can’t understand, and I checked Google, what am I missing? Is it possible in C ++ to bypass malloc () and free () when compiling clean objects? I know that I can replace the RTOSs implementation of malloc () and free () to do nothing, but I want to avoid compiling in code that I do not use.

+4
source share
3 answers

Probably some code that we don’t see is calling a function that calls malloc backstage.

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

+2
source

From the code you posted, I don't see anything that would like to allocate memory on the heap. Are there any implicit conversions happening somewhere? What if you compile this class at all?

What can you do:

1) Run under the debugger (provided that you can create an executable image, possibly on an emulator), set a breakpoint in malloc and examine the stack

2) Provide your own malloc and free the linker, then repeat step 1.

You may find that you need a link to a different version of launch C. In the worst case, if the number of calls in malloc / free is limited, you can deploy your own version, which will give callers some predefined memory, but hopefully this will not be necessary .

0
source

Keil comes with a set of PDF files ... one of the documents (document identifier DUI0475A) is called "Using ARM C and C ++ libraries and floating point support." It discusses the use of the heap (and the prevention of its use) in several places.

In particular, check out Section 2.64, “Avoid the Heap and Heap Library Functions Provided by ARM,” there is a lot of good information. Interesting text in this section:

You can refer to __use_no_heap or __use_no_heap_region . your code to ensure that no heap functions related to the ARM library.

__use_no_heap protects against the use of malloc (), realloc (), free (), and any function that uses these functions. For example, calloc () and other stdio functions.

__use_no_heap_region has the same properties as __ use_no_heap , but in addition, it protects against other things that use the heap memory area. For example, if you declare main () as a function that takes arguments, the heap area is used to collect argc and argv.

Since your question is about how you cannot call / use malloc() , this may lead you to the right track.

0
source

All Articles