diff --git a/src/liballocator/mem-heap.c b/src/liballocator/mem-heap.c index a127f40b9..50a200680 100644 --- a/src/liballocator/mem-heap.c +++ b/src/liballocator/mem-heap.c @@ -409,8 +409,8 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first * It is supposed, that all short-term allocation is used during relatively short discrete sessions. * After end of the session all short-term allocated regions are supposed to be freed. * - * @return pointer to allocated memory block - if allocation is successful,\n - * NULL - if there is not enough memory. + * @return pointer to allocated memory block - if allocation is successful, + * NULL - if requested region size is zero or if there is not enough memory. */ void* mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to allocate in bytes */ @@ -421,6 +421,11 @@ mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to all mem_check_heap (); + if (size_in_bytes == 0) + { + return NULL; + } + if (alloc_term == MEM_HEAP_ALLOC_LONG_TERM) { block_p = mem_heap.first_block_p; diff --git a/src/liballocator/mem-heap.h b/src/liballocator/mem-heap.h index bcca4ba33..5db755c00 100644 --- a/src/liballocator/mem-heap.h +++ b/src/liballocator/mem-heap.h @@ -88,14 +88,14 @@ extern void mem_heap_stats_reset_peak (void); */ #define MEM_DEFINE_LOCAL_ARRAY(var_name, number, type) \ { \ - type *var_name = ((number > 0) \ - ? mem_heap_alloc_block ((number) * sizeof (type), MEM_HEAP_ALLOC_SHORT_TERM) \ - : NULL); \ + size_t var_name ## ___size = (size_t) (number) * sizeof (type); \ + type *var_name = mem_heap_alloc_block (var_name ## ___size, MEM_HEAP_ALLOC_SHORT_TERM); \ \ - if (var_name == NULL) \ + if (number > 0 \ + && var_name == NULL) \ { \ jerry_exit (ERR_OUT_OF_MEMORY); \ - } \ + } /** * Free the previously defined local array variable, freeing corresponding block on the heap, @@ -104,8 +104,14 @@ extern void mem_heap_stats_reset_peak (void); #define MEM_FINALIZE_LOCAL_ARRAY(var_name) \ if (var_name != NULL) \ { \ + JERRY_ASSERT (var_name ## ___size != 0); \ + \ mem_heap_free_block (var_name); \ } \ + else \ + { \ + JERRY_ASSERT (var_name ## ___size == 0); \ + } \ } /**