Override new and delete operators to trap errors

When new or new[] fails to allocate space trigger an error.
pull/2499/head
Russ Butler 2016-08-01 09:07:35 -05:00 committed by 0xc0170
parent 91a48a8cfc
commit 4ce977ee88
1 changed files with 33 additions and 0 deletions

View File

@ -23,6 +23,8 @@
#include "mbed_interface.h" #include "mbed_interface.h"
#include "SingletonPtr.h" #include "SingletonPtr.h"
#include "PlatformMutex.h" #include "PlatformMutex.h"
#include "mbed_error.h"
#include <stdlib.h>
#if DEVICE_STDIO_MESSAGES #if DEVICE_STDIO_MESSAGES
#include <stdio.h> #include <stdio.h>
#endif #endif
@ -724,3 +726,34 @@ extern "C" void __env_unlock( struct _reent *_r )
#endif #endif
} // namespace mbed } // namespace mbed
void *operator new(std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new out of memory\r\n");
}
return buffer;
}
void *operator new[](std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
}
return buffer;
}
void operator delete(void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
}
void operator delete[](void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
}