Renaming core -> jerry-core.

This commit is contained in:
Ruben Ayrapetyan
2015-02-17 19:00:34 +03:00
parent b6d018d019
commit 88353e93cf
183 changed files with 14 additions and 14 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 */
+192
View File
@@ -0,0 +1,192 @@
/* Copyright 2014-2015 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.
*/
/**
* Allocator implementation
*/
#include "jrt.h"
#include "jrt-libc-includes.h"
#include "mem-allocator.h"
#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)));
/**
* The 'try to give memory back' callback
*/
static mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback = NULL;
/**
* Initialize memory allocators.
*/
void
mem_init (void)
{
mem_heap_init (mem_heap_area, sizeof (mem_heap_area));
mem_pools_init ();
} /* mem_init */
/**
* Finalize memory allocators.
*/
void
mem_finalize (bool is_show_mem_stats) /**< show heap memory stats
before finalization? */
{
mem_pools_finalize ();
if (is_show_mem_stats)
{
mem_heap_print (false, false, true);
#ifdef MEM_STATS
mem_pools_stats_t stats;
mem_pools_get_stats (&stats);
printf ("Pools stats:\n");
printf (" Chunk size: %u\n"
" Pools: %lu\n"
" Allocated chunks: %lu\n"
" Free chunks: %lu\n"
" Peak pools: %lu\n"
" Peak allocated chunks: %lu\n\n",
MEM_POOL_CHUNK_SIZE,
stats.pools_count,
stats.allocated_chunks,
stats.free_chunks,
stats.peak_pools_count,
stats.peak_allocated_chunks);
#endif /* MEM_STATS */
}
mem_heap_finalize ();
} /* mem_finalize */
/**
* Get base pointer for allocation area.
*/
static uintptr_t
mem_get_base_pointer (void)
{
return (uintptr_t) mem_heap_area;
} /* mem_get_base_pointer */
/**
* Compress pointer.
*/
uintptr_t
mem_compress_pointer (void *pointer) /**< pointer to compress */
{
JERRY_ASSERT(pointer != NULL);
uintptr_t int_ptr = (uintptr_t) pointer;
JERRY_ASSERT(int_ptr % MEM_ALIGNMENT == 0);
int_ptr -= mem_get_base_pointer ();
int_ptr >>= MEM_ALIGNMENT_LOG;
JERRY_ASSERT((int_ptr & ~((1u << MEM_HEAP_OFFSET_LOG) - 1)) == 0);
JERRY_ASSERT(int_ptr != MEM_COMPRESSED_POINTER_NULL);
return int_ptr;
} /* mem_compress_pointer */
/**
* Decompress pointer.
*/
void*
mem_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decompress */
{
JERRY_ASSERT(compressed_pointer != MEM_COMPRESSED_POINTER_NULL);
uintptr_t int_ptr = compressed_pointer;
int_ptr <<= MEM_ALIGNMENT_LOG;
int_ptr += mem_get_base_pointer ();
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 */
{
if (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
*
* Note:
* the routine should be used only for assertion checks
*
* @return true - if pointer points to the heap,
* false - otherwise
*/
bool
mem_is_heap_pointer (void *pointer) /**< pointer */
{
uint8_t *uint8_pointer = (uint8_t*) pointer;
return (uint8_pointer >= mem_heap_area && uint8_pointer <= (mem_heap_area + MEM_HEAP_AREA_SIZE));
} /* mem_is_heap_pointer */
#endif /* !JERRY_NDEBUG */
+85
View File
@@ -0,0 +1,85 @@
/* Copyright 2014-2015 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.
*/
/** \addtogroup mem Memory allocation
* @{
*/
/**
* Allocator interface
*/
#ifndef JERRY_MEM_ALLOCATOR_H
#define JERRY_MEM_ALLOCATOR_H
#include "jrt.h"
#include "mem-config.h"
#include "mem-heap.h"
#include "mem-poolman.h"
/**
* Representation of NULL value for compressed pointers
*/
#define MEM_COMPRESSED_POINTER_NULL 0
/**
* Required alignment for allocated units/blocks
*/
#define MEM_ALIGNMENT (1 << MEM_ALIGNMENT_LOG)
/**
* Width of compressed memory pointer
*/
#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 */
#endif /* !JERRY_MEM_ALLOCATOR_H */
/**
* @}
*/
+51
View File
@@ -0,0 +1,51 @@
/* 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_CONFIG_H
#define MEM_CONFIG_H
#include "config.h"
/**
* Log2 of maximum possible offset in the heap
*/
#define MEM_HEAP_OFFSET_LOG (CONFIG_MEM_HEAP_OFFSET_LOG)
/**
* Size of heap
*/
#define MEM_HEAP_AREA_SIZE (CONFIG_MEM_HEAP_AREA_SIZE)
/**
* Size of heap chunk
*/
#define MEM_HEAP_CHUNK_SIZE (CONFIG_MEM_HEAP_CHUNK_SIZE)
/**
* Size of pool chunk
*/
#define MEM_POOL_CHUNK_SIZE (CONFIG_MEM_POOL_CHUNK_SIZE)
/**
* Log2 of maximum number of chunks in a pool
*/
#define MEM_POOL_MAX_CHUNKS_NUMBER_LOG (CONFIG_MEM_POOL_MAX_CHUNKS_NUMBER_LOG)
/**
* Logarithm of required alignment for allocated units/blocks
*/
#define MEM_ALIGNMENT_LOG 2
#endif /* MEM_CONFIG_H */
File diff suppressed because it is too large Load Diff
+116
View File
@@ -0,0 +1,116 @@
/* Copyright 2014-2015 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.
*/
/** \addtogroup mem Memory allocation
* @{
*
* \addtogroup heap Heap
* @{
*/
/**
* Heap allocator interface
*/
#ifndef JERRY_MEM_HEAP_H
#define JERRY_MEM_HEAP_H
#include "jrt.h"
/**
* Type of allocation (argument of mem_Alloc)
*
* @see mem_heap_alloc_block
*/
typedef enum
{
MEM_HEAP_ALLOC_SHORT_TERM, /**< allocated region will be freed soon */
MEM_HEAP_ALLOC_LONG_TERM /**< allocated region most likely will not be freed soon */
} mem_heap_alloc_term_t;
extern void mem_heap_init (uint8_t *heap_start, size_t heap_size);
extern void mem_heap_finalize (void);
extern void* mem_heap_alloc_block (size_t size_in_bytes, mem_heap_alloc_term_t alloc_term);
extern bool mem_heap_try_resize_block (void *ptr, size_t size_in_bytes);
extern void mem_heap_free_block (void *ptr);
extern size_t __attr_pure___ mem_heap_recommend_allocation_size (size_t minimum_allocation_size);
extern void mem_heap_print (bool dump_block_headers, bool dump_block_data, bool dump_stats);
#ifdef MEM_STATS
/**
* Heap memory usage statistics
*/
typedef struct
{
size_t size; /**< size */
size_t blocks; /**< blocks count */
size_t allocated_chunks; /**< currently allocated chunks */
size_t peak_allocated_chunks; /**< peak allocated chunks */
size_t global_peak_allocated_chunks; /**< non-resettable peak allocated chunks */
size_t allocated_blocks; /**< currently allocated blocks */
size_t peak_allocated_blocks; /**< peak allocated blocks */
size_t global_peak_allocated_blocks; /**< non-resettable peak allocated blocks */
size_t allocated_bytes; /**< currently allocated bytes */
size_t peak_allocated_bytes; /**< peak allocated bytes */
size_t global_peak_allocated_bytes; /**< non-resettable peak allocated bytes */
size_t waste_bytes; /**< bytes waste due to blocks filled partially
and due to block headers */
size_t peak_waste_bytes; /**< peak bytes waste */
size_t global_peak_waste_bytes; /**< non-resettable peak bytes waste */
} mem_heap_stats_t;
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) \
{ \
size_t var_name ## ___size = (size_t) (number) * sizeof (type); \
type *var_name = static_cast <type *> (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,
* 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) \
{ \
JERRY_ASSERT (var_name ## ___size != 0); \
\
mem_heap_free_block (var_name); \
} \
else \
{ \
JERRY_ASSERT (var_name ## ___size == 0); \
} \
}
/**
* @}
* @}
*/
#endif /* !JERRY_MEM_HEAP_H */
+213
View File
@@ -0,0 +1,213 @@
/* Copyright 2014-2015 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.
*/
/** \addtogroup mem Memory allocation
* @{
*
* \addtogroup pool Memory pool
* @{
*/
/**
* Memory pool implementation
*/
#define JERRY_MEM_POOL_INTERNAL
#include "jrt.h"
#include "jrt-libc-includes.h"
#include "mem-allocator.h"
#include "mem-pool.h"
/*
* Valgrind-related options and headers
*/
#ifdef JERRY_VALGRIND
# include "memcheck.h"
# define VALGRIND_NOACCESS_SPACE(p, s) (void)VALGRIND_MAKE_MEM_NOACCESS((p), (s))
# define VALGRIND_UNDEFINED_SPACE(p, s) (void)VALGRIND_MAKE_MEM_UNDEFINED((p), (s))
# define VALGRIND_DEFINED_SPACE(p, s) (void)VALGRIND_MAKE_MEM_DEFINED((p), (s))
#else /* JERRY_VALGRIND */
# define VALGRIND_NOACCESS_SPACE(p, s)
# define VALGRIND_UNDEFINED_SPACE(p, s)
# define VALGRIND_DEFINED_SPACE(p, s)
#endif /* JERRY_VALGRIND */
static void mem_check_pool (mem_pool_state_t *pool_p);
/**
* Get address of pool chunk with specified index
*/
#define MEM_POOL_CHUNK_ADDRESS(pool_header_p, chunk_index) ((uint8_t*) (MEM_POOL_SPACE_START(pool_p) + \
MEM_POOL_CHUNK_SIZE * chunk_index))
/**
* Is the chunk is inside of the pool?
*
* @return true / false
*/
bool __attr_const___
mem_pool_is_chunk_inside (mem_pool_state_t *pool_p, /**< pool */
uint8_t *chunk_p) /**< chunk */
{
if (chunk_p >= (uint8_t*) pool_p && chunk_p < (uint8_t*) pool_p + MEM_POOL_SIZE)
{
JERRY_ASSERT (chunk_p >= MEM_POOL_SPACE_START(pool_p)
&& chunk_p <= MEM_POOL_SPACE_START(pool_p) + MEM_POOL_CHUNKS_NUMBER * MEM_POOL_CHUNK_SIZE);
return true;
}
return false;
} /* mem_pool_is_chunk_inside */
/**
* Initialization of memory pool.
*
* Pool will be located in the segment [pool_start; pool_start + pool_size).
* Part of pool space will be used for bitmap and the rest will store chunks.
*/
void
mem_pool_init (mem_pool_state_t *pool_p, /**< pool */
size_t pool_size) /**< pool size */
{
JERRY_ASSERT(pool_p != NULL);
JERRY_ASSERT((size_t)MEM_POOL_SPACE_START(pool_p) % MEM_ALIGNMENT == 0);
JERRY_STATIC_ASSERT(MEM_POOL_CHUNK_SIZE % MEM_ALIGNMENT == 0);
JERRY_STATIC_ASSERT(MEM_POOL_MAX_CHUNKS_NUMBER_LOG <= sizeof (mem_pool_chunk_index_t) * JERRY_BITSINBYTE);
JERRY_ASSERT(sizeof (mem_pool_chunk_index_t) <= MEM_POOL_CHUNK_SIZE);
JERRY_ASSERT (MEM_POOL_SIZE == sizeof (mem_pool_state_t) + MEM_POOL_CHUNKS_NUMBER * MEM_POOL_CHUNK_SIZE);
JERRY_ASSERT (MEM_POOL_CHUNKS_NUMBER >= CONFIG_MEM_LEAST_CHUNK_NUMBER_IN_POOL);
JERRY_ASSERT (pool_size == MEM_POOL_SIZE);
/*
* All chunks are free right after initialization
*/
pool_p->free_chunks_number = (mem_pool_chunk_index_t) MEM_POOL_CHUNKS_NUMBER;
JERRY_ASSERT (pool_p->free_chunks_number == MEM_POOL_CHUNKS_NUMBER);
/*
* Chunk with zero index is first free chunk in the pool now
*/
pool_p->first_free_chunk = 0;
for (mem_pool_chunk_index_t chunk_index = 0;
chunk_index < MEM_POOL_CHUNKS_NUMBER;
chunk_index++)
{
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) MEM_POOL_CHUNK_ADDRESS(pool_p,
chunk_index);
*next_free_chunk_index_p = (mem_pool_chunk_index_t) (chunk_index + 1u);
VALGRIND_NOACCESS_SPACE (next_free_chunk_index_p, MEM_POOL_CHUNK_SIZE);
}
mem_check_pool (pool_p);
} /* mem_pool_init */
/**
* Allocate a chunk in the pool
*/
uint8_t*
mem_pool_alloc_chunk (mem_pool_state_t *pool_p) /**< pool */
{
mem_check_pool (pool_p);
JERRY_ASSERT (pool_p->free_chunks_number != 0);
JERRY_ASSERT (pool_p->first_free_chunk < MEM_POOL_CHUNKS_NUMBER);
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
uint8_t *chunk_p = MEM_POOL_CHUNK_ADDRESS(pool_p, chunk_index);
VALGRIND_DEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE);
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
pool_p->first_free_chunk = *next_free_chunk_index_p;
pool_p->free_chunks_number--;
VALGRIND_UNDEFINED_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE);
mem_check_pool (pool_p);
return chunk_p;
} /* mem_pool_alloc_chunk */
/**
* Free the chunk in the pool
*/
void
mem_pool_free_chunk (mem_pool_state_t *pool_p, /**< pool */
uint8_t *chunk_p) /**< chunk pointer */
{
JERRY_ASSERT(pool_p->free_chunks_number < MEM_POOL_CHUNKS_NUMBER);
JERRY_ASSERT(mem_pool_is_chunk_inside (pool_p, chunk_p));
JERRY_ASSERT(((uintptr_t) chunk_p - (uintptr_t) MEM_POOL_SPACE_START(pool_p)) % MEM_POOL_CHUNK_SIZE == 0);
mem_check_pool (pool_p);
const size_t chunk_byte_offset = (size_t) (chunk_p - MEM_POOL_SPACE_START(pool_p));
const mem_pool_chunk_index_t chunk_index = (mem_pool_chunk_index_t) (chunk_byte_offset / MEM_POOL_CHUNK_SIZE);
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
*next_free_chunk_index_p = pool_p->first_free_chunk;
pool_p->first_free_chunk = chunk_index;
pool_p->free_chunks_number++;
VALGRIND_NOACCESS_SPACE (next_free_chunk_index_p, MEM_POOL_CHUNK_SIZE);
mem_check_pool (pool_p);
} /* mem_pool_free_chunk */
/**
* Check pool state consistency
*/
static void
mem_check_pool (mem_pool_state_t __attr_unused___ *pool_p) /**< pool (unused #ifdef JERRY_NDEBUG) */
{
#ifndef JERRY_NDEBUG
JERRY_ASSERT(pool_p->free_chunks_number <= MEM_POOL_CHUNKS_NUMBER);
size_t met_free_chunks_number = 0;
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
while (chunk_index != MEM_POOL_CHUNKS_NUMBER)
{
uint8_t *chunk_p = MEM_POOL_CHUNK_ADDRESS(pool_p, chunk_index);
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
met_free_chunks_number++;
VALGRIND_DEFINED_SPACE (next_free_chunk_index_p, MEM_POOL_CHUNK_SIZE);
chunk_index = *next_free_chunk_index_p;
VALGRIND_NOACCESS_SPACE (next_free_chunk_index_p, MEM_POOL_CHUNK_SIZE);
}
JERRY_ASSERT(met_free_chunks_number == pool_p->free_chunks_number);
#endif /* !JERRY_NDEBUG */
} /* mem_check_pool */
/**
* @}
* @}
*/
+80
View File
@@ -0,0 +1,80 @@
/* Copyright 2014-2015 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 JERRY_MEM_POOL_INTERNAL
#error "Please, use mem_poolman.h instead of mem_pool.h"
#endif
#ifndef JERRY_MEM_POOL_H
#define JERRY_MEM_POOL_H
#include "mem-config.h"
#include "mem-heap.h"
/** \addtogroup pool Memory pool
* @{
*/
/**
* Size of a pool (header + chunks)
*/
#define MEM_POOL_SIZE \
((size_t) JERRY_MIN (sizeof (mem_pool_state_t) + (1ull << MEM_POOL_MAX_CHUNKS_NUMBER_LOG) * MEM_POOL_CHUNK_SIZE, \
JERRY_ALIGNDOWN (mem_heap_recommend_allocation_size (sizeof (mem_pool_state_t) + \
CONFIG_MEM_LEAST_CHUNK_NUMBER_IN_POOL * \
MEM_POOL_CHUNK_SIZE) \
- sizeof (mem_pool_state_t), \
MEM_POOL_CHUNK_SIZE)) + sizeof (mem_pool_state_t))
/**
* Number of chunks in a pool
*/
#define MEM_POOL_CHUNKS_NUMBER ((MEM_POOL_SIZE - sizeof (mem_pool_state_t)) / MEM_POOL_CHUNK_SIZE)
/**
* Get pool's space size
*/
#define MEM_POOL_SPACE_START(pool_header_p) ((uint8_t*) ((mem_pool_state_t*) pool_header_p + 1))
/**
* Index of chunk in a pool
*/
typedef uint8_t mem_pool_chunk_index_t;
/**
* State of a memory pool
*/
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) mem_pool_state_t
{
/** Offset of first free chunk from the beginning of the pool (mem_pool_chunk_index_t) */
unsigned int first_free_chunk : MEM_POOL_MAX_CHUNKS_NUMBER_LOG;
/** Number of free chunks (mem_pool_chunk_index_t) */
unsigned int free_chunks_number : MEM_POOL_MAX_CHUNKS_NUMBER_LOG;
/** Pointer to the next pool with same chunk size */
unsigned int next_pool_cp : MEM_HEAP_OFFSET_LOG;
} mem_pool_state_t;
extern void mem_pool_init (mem_pool_state_t *pool_p, size_t pool_size);
extern uint8_t* mem_pool_alloc_chunk (mem_pool_state_t *pool_p);
extern void mem_pool_free_chunk (mem_pool_state_t *pool_p, uint8_t *chunk_p);
extern bool __attr_const___ mem_pool_is_chunk_inside (mem_pool_state_t *pool_p, uint8_t *chunk_p);
/**
* @}
*/
#endif /* JERRY_MEM_POOL_H */
+349
View File
@@ -0,0 +1,349 @@
/* Copyright 2014-2015 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.
*/
/** \addtogroup mem Memory allocation
* @{
*
* \addtogroup poolman Memory pool manager
* @{
*/
/**
* Memory pool manager implementation
*/
#define JERRY_MEM_POOL_INTERNAL
#include "jrt.h"
#include "jrt-libc-includes.h"
#include "mem-allocator.h"
#include "mem-heap.h"
#include "mem-pool.h"
#include "mem-poolman.h"
/**
* Lists of pools
*/
mem_pool_state_t *mem_pools;
/**
* Number of free chunks
*/
size_t mem_free_chunks_number;
#ifdef MEM_STATS
/**
* Pools' memory usage statistics
*/
mem_pools_stats_t mem_pools_stats;
static void mem_pools_stat_init (void);
static void mem_pools_stat_alloc_pool (void);
static void mem_pools_stat_free_pool (void);
static void mem_pools_stat_alloc_chunk (void);
static void mem_pools_stat_free_chunk (void);
# define MEM_POOLS_STAT_INIT() mem_pools_stat_init ()
# define MEM_POOLS_STAT_ALLOC_POOL() mem_pools_stat_alloc_pool ()
# define MEM_POOLS_STAT_FREE_POOL() mem_pools_stat_free_pool ()
# define MEM_POOLS_STAT_ALLOC_CHUNK() mem_pools_stat_alloc_chunk ()
# define MEM_POOLS_STAT_FREE_CHUNK() mem_pools_stat_free_chunk ()
#else /* !MEM_STATS */
# define MEM_POOLS_STAT_INIT()
# define MEM_POOLS_STAT_ALLOC_POOL()
# define MEM_POOLS_STAT_FREE_POOL()
# define MEM_POOLS_STAT_ALLOC_CHUNK()
# define MEM_POOLS_STAT_FREE_CHUNK()
#endif /* !MEM_STATS */
/**
* Initialize pool manager
*/
void
mem_pools_init (void)
{
mem_pools = NULL;
mem_free_chunks_number = 0;
MEM_POOLS_STAT_INIT ();
} /* mem_pools_init */
/**
* Finalize pool manager
*/
void
mem_pools_finalize (void)
{
JERRY_ASSERT(mem_pools == NULL);
JERRY_ASSERT(mem_free_chunks_number == 0);
} /* mem_pools_finalize */
/**
* Long path for mem_pools_alloc
*
* @return true - if there is a free chunk in mem_pools,
* false - otherwise (not enough memory).
*/
static bool __attr_noinline___
mem_pools_alloc_longpath (void)
{
/**
* If there are no free chunks, allocate new pool.
*/
if (mem_free_chunks_number == 0)
{
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_heap_alloc_block (MEM_POOL_SIZE, MEM_HEAP_ALLOC_LONG_TERM);
JERRY_ASSERT (pool_state != NULL);
mem_pool_init (pool_state, MEM_POOL_SIZE);
if (mem_pools == NULL)
{
pool_state->next_pool_cp = MEM_COMPRESSED_POINTER_NULL;
}
else
{
pool_state->next_pool_cp = (uint16_t) mem_compress_pointer (mem_pools);
}
mem_pools = pool_state;
mem_free_chunks_number += MEM_POOL_CHUNKS_NUMBER;
MEM_POOLS_STAT_ALLOC_POOL ();
}
else
{
/**
* There is definitely at least one pool of specified type with at least one free chunk.
*
* Search for the pool.
*/
mem_pool_state_t *pool_state = mem_pools, *prev_pool_state_p = NULL;
while (pool_state->first_free_chunk == MEM_POOL_CHUNKS_NUMBER)
{
prev_pool_state_p = pool_state;
pool_state = (mem_pool_state_t*) mem_decompress_pointer (pool_state->next_pool_cp);
JERRY_ASSERT(pool_state != NULL);
}
JERRY_ASSERT (prev_pool_state_p != NULL && pool_state != mem_pools);
prev_pool_state_p->next_pool_cp = pool_state->next_pool_cp;
pool_state->next_pool_cp = (uint16_t) mem_compress_pointer (mem_pools);
mem_pools = pool_state;
}
return true;
} /* mem_pools_alloc_longpath */
/**
* Allocate a chunk of specified size
*
* @return pointer to allocated chunk, if allocation was successful,
* or NULL - if not enough memory.
*/
uint8_t*
mem_pools_alloc (void)
{
if (mem_pools == NULL || mem_pools->first_free_chunk == MEM_POOL_CHUNKS_NUMBER)
{
if (!mem_pools_alloc_longpath ())
{
return NULL;
}
}
JERRY_ASSERT (mem_pools != NULL && mem_pools->first_free_chunk != MEM_POOL_CHUNKS_NUMBER);
/**
* And allocate chunk within it.
*/
mem_free_chunks_number--;
MEM_POOLS_STAT_ALLOC_CHUNK ();
return mem_pool_alloc_chunk (mem_pools);
} /* mem_pools_alloc */
/**
* Free the chunk
*/
void
mem_pools_free (uint8_t *chunk_p) /**< pointer to the chunk */
{
mem_pool_state_t *pool_state = mem_pools, *prev_pool_state_p = NULL;
/**
* Search for the pool containing specified chunk.
*/
while (!mem_pool_is_chunk_inside (pool_state, chunk_p))
{
prev_pool_state_p = pool_state;
pool_state = (mem_pool_state_t*) mem_decompress_pointer (pool_state->next_pool_cp);
JERRY_ASSERT(pool_state != NULL);
}
/**
* Free the chunk
*/
mem_pool_free_chunk (pool_state, chunk_p);
mem_free_chunks_number++;
MEM_POOLS_STAT_FREE_CHUNK ();
/**
* If all chunks of the pool are free, free the pool itself.
*/
if (pool_state->free_chunks_number == MEM_POOL_CHUNKS_NUMBER)
{
if (prev_pool_state_p != NULL)
{
prev_pool_state_p->next_pool_cp = pool_state->next_pool_cp;
}
else
{
if (pool_state->next_pool_cp == MEM_COMPRESSED_POINTER_NULL)
{
mem_pools = NULL;
}
else
{
mem_pools = (mem_pool_state_t*) mem_decompress_pointer (pool_state->next_pool_cp);
}
}
mem_free_chunks_number -= MEM_POOL_CHUNKS_NUMBER;
mem_heap_free_block ((uint8_t*)pool_state);
MEM_POOLS_STAT_FREE_POOL ();
}
else if (mem_pools != pool_state)
{
JERRY_ASSERT (prev_pool_state_p != NULL);
prev_pool_state_p->next_pool_cp = pool_state->next_pool_cp;
pool_state->next_pool_cp = (uint16_t) mem_compress_pointer (mem_pools);
mem_pools = pool_state;
}
} /* mem_pools_free */
#ifdef MEM_STATS
/**
* Get pools memory usage statistics
*/
void
mem_pools_get_stats (mem_pools_stats_t *out_pools_stats_p) /**< out: pools' stats */
{
JERRY_ASSERT(out_pools_stats_p != NULL);
*out_pools_stats_p = mem_pools_stats;
} /* mem_pools_get_stats */
/**
* Reset peak values in memory usage statistics
*/
void
mem_pools_stats_reset_peak (void)
{
mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count;
mem_pools_stats.peak_allocated_chunks = mem_pools_stats.allocated_chunks;
} /* mem_pools_stats_reset_peak */
/**
* Initalize pools' memory usage statistics account structure
*/
static void
mem_pools_stat_init (void)
{
memset (&mem_pools_stats, 0, sizeof (mem_pools_stats));
} /* mem_pools_stat_init */
/**
* Account allocation of a pool
*/
static void
mem_pools_stat_alloc_pool (void)
{
mem_pools_stats.pools_count++;
mem_pools_stats.free_chunks = mem_free_chunks_number;
if (mem_pools_stats.pools_count > mem_pools_stats.peak_pools_count)
{
mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count;
}
if (mem_pools_stats.pools_count > mem_pools_stats.global_peak_pools_count)
{
mem_pools_stats.global_peak_pools_count = mem_pools_stats.pools_count;
}
} /* mem_pools_stat_alloc_pool */
/**
* Account freeing of a pool
*/
static void
mem_pools_stat_free_pool (void)
{
JERRY_ASSERT(mem_pools_stats.pools_count > 0);
mem_pools_stats.pools_count--;
mem_pools_stats.free_chunks = mem_free_chunks_number;
} /* mem_pools_stat_free_pool */
/**
* Account allocation of chunk in a pool
*/
static void
mem_pools_stat_alloc_chunk (void)
{
JERRY_ASSERT(mem_pools_stats.free_chunks > 0);
mem_pools_stats.allocated_chunks++;
mem_pools_stats.free_chunks--;
if (mem_pools_stats.allocated_chunks > mem_pools_stats.peak_allocated_chunks)
{
mem_pools_stats.peak_allocated_chunks = mem_pools_stats.allocated_chunks;
}
if (mem_pools_stats.allocated_chunks > mem_pools_stats.global_peak_allocated_chunks)
{
mem_pools_stats.global_peak_allocated_chunks = mem_pools_stats.allocated_chunks;
}
} /* mem_pools_stat_alloc_chunk */
/**
* Account freeing of chunk in a pool
*/
static void
mem_pools_stat_free_chunk (void)
{
JERRY_ASSERT(mem_pools_stats.allocated_chunks > 0);
mem_pools_stats.allocated_chunks--;
mem_pools_stats.free_chunks++;
} /* mem_pools_stat_free_chunk */
#endif /* MEM_STATS */
/**
* @}
*/
/**
* @}
*/
+77
View File
@@ -0,0 +1,77 @@
/* Copyright 2014-2015 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.
*/
/** \addtogroup mem Memory allocation
* @{
*/
/** \addtogroup poolman Memory pool manager
* @{
*/
/**
* Pool manager interface
*/
#ifndef JERRY_MEM_POOLMAN_H
#define JERRY_MEM_POOLMAN_H
#include "jrt.h"
extern void mem_pools_init (void);
extern void mem_pools_finalize (void);
extern uint8_t* mem_pools_alloc (void);
extern void mem_pools_free (uint8_t *chunk_p);
#ifdef MEM_STATS
/**
* Pools' memory usage statistics
*/
typedef struct
{
/** pools' count */
size_t pools_count;
/** peak pools' count */
size_t peak_pools_count;
/** non-resettable peak pools' count */
size_t global_peak_pools_count;
/** allocated chunks count */
size_t allocated_chunks;
/** peak allocated chunks count */
size_t peak_allocated_chunks;
/** non-resettable peak allocated chunks count */
size_t global_peak_allocated_chunks;
/** free chunks count */
size_t free_chunks;
} mem_pools_stats_t;
extern void mem_pools_get_stats (mem_pools_stats_t *out_pools_stats_p);
extern void mem_pools_stats_reset_peak (void);
#endif /* MEM_STATS */
#endif /* JERRY_MEM_POOLMAN_H */
/**
* @}
*/
/**
* @}
*/