Merge pull request #1118 from openmv/xalloc_update

Make xalloc exception more useful.
This commit is contained in:
Ibrahim Abd Elkader 2021-01-11 19:41:05 +02:00 committed by GitHub
commit f1046af606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,10 +14,10 @@
#include "py/mphal.h" #include "py/mphal.h"
#include "xalloc.h" #include "xalloc.h"
NORETURN static void xalloc_fail() NORETURN static void xalloc_fail(uint32_t size)
{ {
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("Out of normal MicroPython Heap Memory!" mp_raise_msg_varg(&mp_type_MemoryError,
" Please reduce the resolution of the image you are running this algorithm on to bypass this issue!")); MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)size);
} }
// returns null pointer without error if size==0 // returns null pointer without error if size==0
@ -25,7 +25,7 @@ void *xalloc(uint32_t size)
{ {
void *mem = gc_alloc(size, false); void *mem = gc_alloc(size, false);
if (size && (mem == NULL)) { if (size && (mem == NULL)) {
xalloc_fail(); xalloc_fail(size);
} }
return mem; return mem;
} }
@ -41,7 +41,7 @@ void *xalloc0(uint32_t size)
{ {
void *mem = gc_alloc(size, false); void *mem = gc_alloc(size, false);
if (size && (mem == NULL)) { if (size && (mem == NULL)) {
xalloc_fail(); xalloc_fail(size);
} }
memset(mem, 0, size); memset(mem, 0, size);
return mem; return mem;
@ -60,7 +60,7 @@ void *xrealloc(void *mem, uint32_t size)
{ {
mem = gc_realloc(mem, size, true); mem = gc_realloc(mem, size, true);
if (size && (mem == NULL)) { if (size && (mem == NULL)) {
xalloc_fail(); xalloc_fail(size);
} }
return mem; return mem;
} }