From d308687782c8fb72e10f2015d960a1dc9372f9d1 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 13 Aug 2015 08:32:34 +1000 Subject: [PATCH] Add basic C++ new/delete operators, as contributed by @mikejac in #24 --- core/cplusplus_operators.cpp | 25 +++++++++++++++++++++++++ examples/simple_cplusplus/simple.cpp | 20 ++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 core/cplusplus_operators.cpp diff --git a/core/cplusplus_operators.cpp b/core/cplusplus_operators.cpp new file mode 100644 index 0000000..86f4477 --- /dev/null +++ b/core/cplusplus_operators.cpp @@ -0,0 +1,25 @@ +/* Part of esp-open-rtos + * BSD Licensed as described in the file LICENSE + */ +#include +#include + +void *operator new(size_t size) +{ + return malloc(size); +} + +void *operator new[](size_t size) +{ + return malloc(size); +} + +void operator delete(void * ptr) +{ + free(ptr); +} + +void operator delete[](void * ptr) +{ + free(ptr); +} diff --git a/examples/simple_cplusplus/simple.cpp b/examples/simple_cplusplus/simple.cpp index dada441..8d964fa 100644 --- a/examples/simple_cplusplus/simple.cpp +++ b/examples/simple_cplusplus/simple.cpp @@ -35,11 +35,23 @@ static Counter static_counter(99); void task1(void *pvParameters) { Counter local_counter = Counter(12); + Counter *new_counter = new Counter(24); while(1) { - Counter &counter = rand() % 2 ? static_counter : local_counter; - counter.Increment(); - printf("local counter %ld static counter %ld\r\n", local_counter.getCount(), - static_counter.getCount()); + Counter *counter = NULL; + switch(rand() % 3) { + case 0: + counter = &local_counter; + break; + case 1: + counter = &static_counter; + break; + default: + counter = new_counter; + break; + } + counter->Increment(); + printf("local counter %ld static counter %ld newly allocated counter %ld\r\n", local_counter.getCount(), + static_counter.getCount(), new_counter->getCount()); vTaskDelay(100); } }