diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-10 14:27:31 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-10 14:27:31 +0000 |
commit | 58ba4c3b4c12e9bf6b8731fd26e0c9cac527122f (patch) | |
tree | a0fce39198f5a192e780016ef3608813a351573a /py/malloc.c | |
parent | ffa9bddfc4006aebb2689826f700ea167f4de54d (diff) |
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.
Diffstat (limited to 'py/malloc.c')
-rw-r--r-- | py/malloc.c | 20 |
1 files changed, 20 insertions, 0 deletions
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); |