Introducing MEM_DEFINE_LOCAL_ARRAY and MEM_FINALIZE_LOCAL_ARRAY to replace on-stack allocation of dynamic sized arrays.

This commit is contained in:
Ruben Ayrapetyan
2014-12-17 22:18:32 +03:00
parent 005fcb008e
commit 9b1fff1d8b
+30
View File
@@ -78,6 +78,36 @@ extern void mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p);
extern void mem_heap_stats_reset_peak (void);
#endif /* MEM_STATS */
/**
* Define a local array variable and allocate memory for the array on the heap.
*
* If requested number of elements is zero, assign NULL to the variable.
*
* Warning:
* if there is not enough memory on the heap, shutdown engine with ERR_OUT_OF_MEMORY.
*/
#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); \
\
if (var_name == NULL) \
{ \
jerry_exit (ERR_OUT_OF_MEMORY); \
} \
/**
* Free the previously defined local array variable, freeing corresponding block on the heap,
* if it was allocated (i.e. if the array's size was non-zero).
*/
#define MEM_FINALIZE_LOCAL_ARRAY(var_name) \
if (var_name != NULL) \
{ \
mem_heap_free_block (var_name); \
} \
}
/**
* @}
* @}