Introducing 'try to give memory back' callback for heap allocator to use upon allocation request that can not be satisfied by the allocator.

This commit is contained in:
Ruben Ayrapetyan
2014-12-18 21:20:28 +03:00
parent 8febd2bae8
commit 6bb39bb8ea
14 changed files with 308 additions and 105 deletions
+33
View File
@@ -0,0 +1,33 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MEM_ALLOCATOR_INTERNAL_H
#define MEM_ALLOCATOR_INTERNAL_H
#ifndef MEM_ALLOCATOR_INTERNAL
# error "The header is for internal routines of memory allocator component. Please, don't use the routines directly."
#endif /* !MEM_ALLOCATOR_INTERNAL */
/** \addtogroup mem Memory allocation
* @{
*/
extern void mem_run_try_to_give_memory_back_callbacks (mem_try_give_memory_back_severity_t severity);
/**
* @}
*/
#endif /* MEM_ALLOCATOR_INTERNAL_H */
+47 -2
View File
@@ -23,15 +23,24 @@
#include "mem-heap.h"
#include "mem-poolman.h"
#define MEM_ALLOCATOR_INTERNAL
#include "mem-allocator-internal.h"
/**
* Check that heap area is less or equal than 64K.
*/
JERRY_STATIC_ASSERT(MEM_HEAP_AREA_SIZE <= 64 * 1024);
/**
* Area for heap
*/
static uint8_t mem_heap_area[ MEM_HEAP_AREA_SIZE ] __attribute__ ((aligned (MEM_ALIGNMENT)));
/**
* Check that heap area is less or equal than 64K.
* The 'try to give memory back' callback
*/
JERRY_STATIC_ASSERT(MEM_HEAP_AREA_SIZE <= 64 * 1024);
static mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback = NULL;
/**
* Initialize memory allocators.
@@ -126,6 +135,42 @@ mem_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decompress
return (void*) int_ptr;
} /* mem_decompress_pointer */
/**
* Register specified 'try to give memory back' callback routine
*/
void
mem_register_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (mem_try_give_memory_back_callback == NULL);
mem_try_give_memory_back_callback = callback;
} /* mem_register_a_try_give_memory_back_callback */
/**
* Unregister specified 'try to give memory back' callback routine
*/
void
mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (mem_try_give_memory_back_callback == callback);
mem_try_give_memory_back_callback = NULL;
} /* mem_unregister_a_try_give_memory_back_callback */
/**
* Run 'try to give memory back' callbacks with specified severity
*/
void
mem_run_try_to_give_memory_back_callbacks (mem_try_give_memory_back_severity_t severity) /**< severity of
the request */
{
JERRY_ASSERT (mem_try_give_memory_back_callback != NULL);
mem_try_give_memory_back_callback (severity);
} /* mem_run_try_to_give_memory_back_callbacks */
#ifndef JERRY_NDEBUG
/**
* Check whether the pointer points to the heap
+25
View File
@@ -43,12 +43,37 @@
*/
#define MEM_COMPRESSED_POINTER_WIDTH (MEM_HEAP_OFFSET_LOG - MEM_ALIGNMENT_LOG)
/**
* Severity of a 'try give memory back' request
*
* The request are posted sequentially beginning from
* low to critical until enough memory is freed.
*
* If not enough memory is freed upon a critical request
* then the engine is shut down with ERR_OUT_OF_MEMORY.
*/
typedef enum
{
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW, /* 'low' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_MEDIUM, /* 'medium' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH, /* 'high' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL /* 'critical' severity */
} mem_try_give_memory_back_severity_t;
/**
* A 'try give memory back' callback routine type.
*/
typedef void (*mem_try_give_memory_back_callback_t) (mem_try_give_memory_back_severity_t);
extern void mem_init (void);
extern void mem_finalize (bool is_show_mem_stats);
extern uintptr_t mem_compress_pointer (void *pointer);
extern void* mem_decompress_pointer (uintptr_t compressed_pointer);
extern void mem_register_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback);
extern void mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback);
#ifndef JERRY_NDEBUG
extern bool mem_is_heap_pointer (void *pointer);
#endif /* !JERRY_NDEBUG */
+64 -17
View File
@@ -30,6 +30,10 @@
#include "mem-config.h"
#include "mem-heap.h"
#define MEM_ALLOCATOR_INTERNAL
#include "mem-allocator-internal.h"
/*
* Valgrind-related options and headers
*/
@@ -402,29 +406,22 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first
/**
* Allocation of memory region.
*
* To reduce heap fragmentation there are two allocation modes - short-term and long-term.
*
* If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap.
*
* 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.
* See also:
* mem_heap_alloc_block
*
* @return pointer to allocated memory block - if allocation is successful,
* NULL - if requested region size is zero or if there is not enough memory.
* NULL - if there is not enough memory.
*/
void*
mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to allocate in bytes */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
static
void* mem_heap_alloc_block_internal (size_t size_in_bytes, /**< size of region to allocate in bytes */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
{
mem_block_header_t *block_p;
mem_direction_t direction;
mem_check_heap ();
JERRY_ASSERT (size_in_bytes != 0);
if (size_in_bytes == 0)
{
return NULL;
}
mem_check_heap ();
if (alloc_term == MEM_HEAP_ALLOC_LONG_TERM)
{
@@ -553,15 +550,65 @@ mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to all
VALGRIND_NOACCESS_STRUCT(block_p);
mem_check_heap ();
/* return data space beginning address */
uint8_t *data_space_p = (uint8_t*) (block_p + 1);
JERRY_ASSERT((uintptr_t) data_space_p % MEM_ALIGNMENT == 0);
VALGRIND_UNDEFINED_SPACE(data_space_p, size_in_bytes);
mem_check_heap ();
return data_space_p;
} /* mem_heap_alloc_block_internal */
/**
* Allocation of memory region.
*
* To reduce heap fragmentation there are two allocation modes - short-term and long-term.
*
* If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap.
*
* 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,
* 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 */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
{
if (unlikely (size_in_bytes == 0))
{
return NULL;
}
else
{
void *data_space_p = mem_heap_alloc_block_internal (size_in_bytes, alloc_term);
if (likely (data_space_p != NULL))
{
return data_space_p;
}
for (mem_try_give_memory_back_severity_t severity = MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW;
severity <= MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL;
severity++)
{
mem_run_try_to_give_memory_back_callbacks (severity);
data_space_p = mem_heap_alloc_block_internal (size_in_bytes, alloc_term);
if (data_space_p != NULL)
{
return data_space_p;
}
}
JERRY_ASSERT (data_space_p == NULL);
jerry_exit (ERR_OUT_OF_MEMORY);
}
} /* mem_heap_alloc_block */
/**
+1 -7
View File
@@ -89,13 +89,7 @@ extern void mem_heap_stats_reset_peak (void);
#define MEM_DEFINE_LOCAL_ARRAY(var_name, number, type) \
{ \
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 (number > 0 \
&& var_name == NULL) \
{ \
jerry_exit (ERR_OUT_OF_MEMORY); \
}
type *var_name = mem_heap_alloc_block (var_name ## ___size, MEM_HEAP_ALLOC_SHORT_TERM);
/**
* Free the previously defined local array variable, freeing corresponding block on the heap,
+1 -7
View File
@@ -106,13 +106,7 @@ mem_pools_alloc_longpath (void)
{
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_heap_alloc_block (MEM_POOL_SIZE, MEM_HEAP_ALLOC_LONG_TERM);
if (pool_state == NULL)
{
/**
* Not enough space for new pool.
*/
return NULL;
}
JERRY_ASSERT (pool_state != NULL);
mem_pool_init (pool_state, MEM_POOL_SIZE);