Add basic C++ new/delete operators, as contributed by @mikejac in #24
This commit is contained in:
parent
424dc0f3b0
commit
7d480876fa
2 changed files with 41 additions and 4 deletions
25
core/cplusplus_operators.cpp
Normal file
25
core/cplusplus_operators.cpp
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/* Part of esp-open-rtos
|
||||||
|
* BSD Licensed as described in the file LICENSE
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -35,11 +35,23 @@ static Counter static_counter(99);
|
||||||
void task1(void *pvParameters)
|
void task1(void *pvParameters)
|
||||||
{
|
{
|
||||||
Counter local_counter = Counter(12);
|
Counter local_counter = Counter(12);
|
||||||
|
Counter *new_counter = new Counter(24);
|
||||||
while(1) {
|
while(1) {
|
||||||
Counter &counter = rand() % 2 ? static_counter : local_counter;
|
Counter *counter = NULL;
|
||||||
counter.Increment();
|
switch(rand() % 3) {
|
||||||
printf("local counter %ld static counter %ld\r\n", local_counter.getCount(),
|
case 0:
|
||||||
static_counter.getCount());
|
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);
|
vTaskDelay(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue