HAL libraries... no C++ or heap

Development of new Cores using the STMCubeMX and HAL
User avatar
wallaceowen
Posts: 2
Joined: Thu Feb 09, 2017 6:36 pm
Location: San Diego

Re: HAL libraries... no C++ or heap

Post by wallaceowen » Mon Jul 17, 2017 8:55 pm

No arduino C++ code uses the heap, AFAIK.

The ATMega328 has only 2k of RAM, and there's no malloc() let alone new(). All ram not allocated to named vars is used for the stack.

The ATMega2560 only has 8k, but can use up to 64K of external RAM for data (Harvard arch. means all code lives in the Flash)

So, all those C++ libs are written with the expectation that create objects are declared as static file-scope vars.
Your eyes are weary from staring at the CRT. You feel sleepy. Notice how restful it is to watch the cursor blink. Close your eyes. The opinions stated above are yours. You cannot imagine why you ever felt otherwise.

ChrisMicro
Posts: 293
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: HAL libraries... no C++ or heap

Post by ChrisMicro » Tue Jul 18, 2017 7:48 am

The ATMega328 has only 2k of RAM, and there's no malloc() let alone new(). All ram not allocated to named vars is used for the stack.
You can add your own new and delete:

Code: Select all

#include <stdlib.h> 

__extension__ typedef int __guard __attribute__((mode (__DI__)));

extern "C" int __cxa_guard_acquire(__guard *);
extern "C" void __cxa_guard_release (__guard *);
extern "C" void __cxa_guard_abort (__guard *); 

int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard *) {}; 

void * operator new(size_t size)
{
  return malloc(size);
}

void operator delete(void * ptr)
{
  free(ptr);
} 

Post Reply