@@ -364,18 +364,13 @@ void *jmem_heap_alloc_block_internal (const size_t size)
364364 * Allocation of memory block, running 'try to give memory back' callbacks, if there is not enough memory.
365365 *
366366 * Note:
367- * if after running the callbacks, there is still not enough memory, engine is terminated with ERR_OUT_OF_MEMORY.
367+ * if after running the callbacks, there is still not enough memory, NULL value will be returned
368368 *
369- * @return pointer to allocated memory block
369+ * @return pointer to allocated memory block or NULL in case of unsuccessful allocation
370370 */
371- void * __attribute__ ( ( hot ))
372- jmem_heap_alloc_block (const size_t size )
371+ static void *
372+ jmem_heap_gc_and_alloc_block (const size_t size ) /**< required memory size */
373373{
374- if (unlikely (size == 0 ))
375- {
376- return NULL ;
377- }
378-
379374 VALGRIND_FREYA_CHECK_MEMPOOL_REQUEST ;
380375
381376#ifdef JMEM_GC_BEFORE_EACH_ALLOC
@@ -411,10 +406,59 @@ jmem_heap_alloc_block (const size_t size)
411406 }
412407
413408 JERRY_ASSERT (data_space_p == NULL );
409+ return data_space_p ;
410+ } /* jmem_heap_gc_and_alloc_block */
411+
412+
413+ /**
414+ * Allocation of memory block, running 'try to give memory back' callbacks, if there is not enough memory.
415+ *
416+ * Note:
417+ * if after running the callbacks, there is still not enough memory, engine is terminated with ERR_OUT_OF_MEMORY.
418+ *
419+ * @return NULL, if the required memory is 0
420+ * pointer to allocated memory block, otherwise
421+ */
422+ void * __attribute__ ( (hot ))
423+ jmem_heap_alloc_block (const size_t size ) /**< required memory size */
424+ {
425+ if (unlikely (size == 0 ))
426+ {
427+ return NULL ;
428+ }
429+
430+ void * data_space_p = jmem_heap_gc_and_alloc_block (size );
431+
432+ if (likely (data_space_p != NULL ))
433+ {
434+ return data_space_p ;
435+ }
414436
415437 jerry_fatal (ERR_OUT_OF_MEMORY );
416438} /* jmem_heap_alloc_block */
417439
440+ /**
441+ * Allocation of memory block, running 'try to give memory back' callbacks, if there is not enough memory.
442+ *
443+ * Note:
444+ * if after running the callbacks, there is still not enough memory, NULL will be returned
445+ *
446+ * @return NULL, if the required memory size is 0
447+ * also NULL, if the allocation has failed
448+ * pointer to allocated memory block, otherwise
449+ */
450+ void * __attribute__ ( (hot ))
451+ jmem_heap_alloc_block_null_on_error (const size_t size ) /**< required memory size */
452+ {
453+ if (unlikely (size == 0 ))
454+ {
455+ return NULL ;
456+ }
457+
458+ return jmem_heap_gc_and_alloc_block (size );
459+ } /* jmem_heap_alloc_block_null_on_error */
460+
461+
418462/**
419463 * Allocate block and store block size.
420464 *
0 commit comments