From 58ba4c3b4c12e9bf6b8731fd26e0c9cac527122f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 10 Apr 2014 14:27:31 +0000 Subject: py: Check explicitly for memory allocation failure in parser. Previously, a failed malloc/realloc would throw an exception, which was not caught. I think it's better to keep the parser free from NLR (exception throwing), hence this patch. --- py/malloc.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'py/malloc.c') diff --git a/py/malloc.c b/py/malloc.c index 1d18a9a64..db2578d9a 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -118,6 +118,26 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { return new_ptr; } +void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) { + void *new_ptr = realloc(ptr, new_num_bytes); + if (new_ptr == NULL) { + return NULL; + } +#if MICROPY_MEM_STATS + // At first thought, "Total bytes allocated" should only grow, + // after all, it's *total*. But consider for example 2K block + // shrunk to 1K and then grown to 2K again. It's still 2K + // allocated total. If we process only positive increments, + // we'll count 3K. + int diff = new_num_bytes - old_num_bytes; + total_bytes_allocated += diff; + current_bytes_allocated += diff; + UPDATE_PEAK(); +#endif + DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); + return new_ptr; +} + void m_free(void *ptr, int num_bytes) { if (ptr != NULL) { free(ptr); -- cgit v1.2.3