Styles fixes in liballocator: indentation and braces rules.

This commit is contained in:
Ruben Ayrapetyan 2014-08-11 21:10:06 +04:00
parent 59940fb648
commit 38c6c2357d
7 changed files with 364 additions and 354 deletions

View File

@ -53,28 +53,28 @@ mem_finalize (bool is_show_mem_stats) /**< show heap memory stats
mem_pools_finalize ();
if (is_show_mem_stats)
{
mem_heap_print (false, false, true);
{
mem_heap_print (false, false, true);
#ifdef MEM_STATS
mem_pools_stats_t stats;
mem_pools_get_stats (&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);
__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 */
@ -94,20 +94,20 @@ mem_get_base_pointer (void)
uintptr_t
mem_compress_pointer (void *pointer) /**< pointer to compress */
{
JERRY_ASSERT(pointer != NULL);
JERRY_ASSERT(pointer != NULL);
uintptr_t int_ptr = (uintptr_t) pointer;
uintptr_t int_ptr = (uintptr_t) pointer;
JERRY_ASSERT(int_ptr % MEM_ALIGNMENT == 0);
JERRY_ASSERT(int_ptr % MEM_ALIGNMENT == 0);
int_ptr -= mem_get_base_pointer ();
int_ptr >>= MEM_ALIGNMENT_LOG;
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 & ~((1u << MEM_HEAP_OFFSET_LOG) - 1)) == 0);
JERRY_ASSERT(int_ptr != MEM_COMPRESSED_POINTER_NULL);
JERRY_ASSERT(int_ptr != MEM_COMPRESSED_POINTER_NULL);
return int_ptr;
return int_ptr;
} /* mem_compress_pointer */
/**
@ -116,12 +116,12 @@ mem_compress_pointer (void *pointer) /**< pointer to compress */
void*
mem_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decompress */
{
JERRY_ASSERT(compressed_pointer != MEM_COMPRESSED_POINTER_NULL);
JERRY_ASSERT(compressed_pointer != MEM_COMPRESSED_POINTER_NULL);
uintptr_t int_ptr = compressed_pointer;
uintptr_t int_ptr = compressed_pointer;
int_ptr <<= MEM_ALIGNMENT_LOG;
int_ptr += mem_get_base_pointer ();
int_ptr <<= MEM_ALIGNMENT_LOG;
int_ptr += mem_get_base_pointer ();
return (void*) int_ptr;
return (void*) int_ptr;
} /* mem_decompress_pointer */

View File

@ -87,10 +87,10 @@ typedef enum
typedef struct mem_block_header_t
{
mem_magic_num_of_block_t magic_num; /**< magic number - MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK for allocated block
and MEM_MAGIC_NUM_OF_FREE_BLOCK for free block */
and MEM_MAGIC_NUM_OF_FREE_BLOCK for free block */
struct mem_block_header_t *neighbours[ MEM_DIRECTION_COUNT ]; /**< neighbour blocks */
size_t allocated_bytes; /**< allocated area size - for allocated blocks;
0 - for free blocks */
size_t allocated_bytes; /**< allocated area size - for allocated blocks;
0 - for free blocks */
} mem_block_header_t;
/**
@ -108,8 +108,8 @@ JERRY_STATIC_ASSERT(MEM_HEAP_CHUNK_SIZE % MEM_ALIGNMENT == 0);
*/
typedef struct
{
uint8_t* heap_start; /**< first address of heap space */
size_t heap_size; /**< heap space size */
uint8_t* heap_start; /**< first address of heap space */
size_t heap_size; /**< heap space size */
mem_block_header_t* first_block_p; /**< first block of the heap */
mem_block_header_t* last_block_p; /**< last block of the heap */
} mem_heap_state_t;
@ -124,10 +124,10 @@ static size_t mem_get_block_data_space_size (const mem_block_header_t *block_hea
static size_t mem_get_block_chunks_count_from_data_size (size_t block_allocated_size);
static void mem_init_block_header (uint8_t *first_chunk_p,
size_t size_in_chunks,
mem_block_state_t block_state,
mem_block_header_t *prev_block_p,
mem_block_header_t *next_block_p);
size_t size_in_chunks,
mem_block_state_t block_state,
mem_block_header_t *prev_block_p,
mem_block_header_t *next_block_p);
static void mem_check_heap (void);
#ifdef MEM_STATS
@ -203,7 +203,7 @@ mem_get_block_chunks_count_from_data_size (size_t block_allocated_size) /**< siz
*/
void
mem_heap_init (uint8_t *heap_start, /**< first address of heap space */
size_t heap_size) /**< heap space size */
size_t heap_size) /**< heap space size */
{
JERRY_ASSERT(heap_start != NULL);
JERRY_ASSERT(heap_size != 0);
@ -217,10 +217,10 @@ mem_heap_init (uint8_t *heap_start, /**< first address of heap space */
VALGRIND_NOACCESS_SPACE(heap_start, heap_size);
mem_init_block_header (mem_heap.heap_start,
0,
MEM_BLOCK_FREE,
NULL,
NULL);
0,
MEM_BLOCK_FREE,
NULL,
NULL);
mem_heap.first_block_p = (mem_block_header_t*) mem_heap.heap_start;
mem_heap.last_block_p = mem_heap.first_block_p;
@ -249,10 +249,10 @@ mem_heap_finalize (void)
*/
static void
mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first chunk to use for the block */
size_t allocated_bytes, /**< size of block's allocated area */
mem_block_state_t block_state, /**< state of the block (allocated or free) */
mem_block_header_t *prev_block_p, /**< previous block */
mem_block_header_t *next_block_p) /**< next block */
size_t allocated_bytes, /**< size of block's allocated area */
mem_block_state_t block_state, /**< state of the block (allocated or free) */
mem_block_header_t *prev_block_p, /**< previous block */
mem_block_header_t *next_block_p) /**< next block */
{
mem_block_header_t *block_header_p = (mem_block_header_t*) first_chunk_p;
@ -292,7 +292,7 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first
*/
uint8_t*
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 */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
{
mem_block_header_t *block_p;
mem_direction_t direction;
@ -353,34 +353,34 @@ mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to all
uint8_t *new_free_block_first_chunk_p = (uint8_t*) block_p + new_block_size_in_chunks * MEM_HEAP_CHUNK_SIZE;
mem_init_block_header (new_free_block_first_chunk_p,
0,
MEM_BLOCK_FREE,
block_p /* there we will place new allocated block */,
next_block_p);
0,
MEM_BLOCK_FREE,
block_p /* there we will place new allocated block */,
next_block_p);
mem_block_header_t *new_free_block_p = (mem_block_header_t*) new_free_block_first_chunk_p;
if (next_block_p == NULL)
{
mem_heap.last_block_p = new_free_block_p;
}
{
mem_heap.last_block_p = new_free_block_p;
}
else
{
VALGRIND_DEFINED_STRUCT(next_block_p);
{
VALGRIND_DEFINED_STRUCT(next_block_p);
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = (mem_block_header_t*) new_free_block_first_chunk_p;
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = (mem_block_header_t*) new_free_block_first_chunk_p;
VALGRIND_NOACCESS_STRUCT(next_block_p);
}
VALGRIND_NOACCESS_STRUCT(next_block_p);
}
next_block_p = new_free_block_p;
}
mem_init_block_header ((uint8_t*) block_p,
size_in_bytes,
MEM_BLOCK_ALLOCATED,
prev_block_p,
next_block_p);
size_in_bytes,
MEM_BLOCK_ALLOCATED,
prev_block_p,
next_block_p);
VALGRIND_DEFINED_STRUCT(block_p);
@ -409,7 +409,7 @@ mem_heap_free_block (uint8_t *ptr) /**< pointer to beginning of data space of th
{
/* checking that ptr points to the heap */
JERRY_ASSERT(ptr >= mem_heap.heap_start
&& ptr <= mem_heap.heap_start + mem_heap.heap_size);
&& ptr <= mem_heap.heap_start + mem_heap.heap_size);
mem_check_heap ();
@ -439,63 +439,63 @@ mem_heap_free_block (uint8_t *ptr) /**< pointer to beginning of data space of th
block_p->magic_num = MEM_MAGIC_NUM_OF_FREE_BLOCK;
if (next_block_p != NULL)
{
VALGRIND_DEFINED_STRUCT(next_block_p);
if (next_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK)
{
VALGRIND_DEFINED_STRUCT(next_block_p);
/* merge with the next block */
mem_heap_stat_free_block_merge ();
if (next_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK)
{
/* merge with the next block */
mem_heap_stat_free_block_merge ();
mem_block_header_t *next_next_block_p = next_block_p->neighbours[ MEM_DIRECTION_NEXT ];
VALGRIND_NOACCESS_STRUCT(next_block_p);
next_block_p = next_next_block_p;
VALGRIND_DEFINED_STRUCT(next_block_p);
block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
if (next_block_p != NULL)
{
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p;
}
else
{
mem_heap.last_block_p = block_p;
}
}
mem_block_header_t *next_next_block_p = next_block_p->neighbours[ MEM_DIRECTION_NEXT ];
VALGRIND_NOACCESS_STRUCT(next_block_p);
next_block_p = next_next_block_p;
VALGRIND_DEFINED_STRUCT(next_block_p);
block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
if (next_block_p != NULL)
{
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p;
}
else
{
mem_heap.last_block_p = block_p;
}
}
VALGRIND_NOACCESS_STRUCT(next_block_p);
}
if (prev_block_p != NULL)
{
VALGRIND_DEFINED_STRUCT(prev_block_p);
if (prev_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK)
{
VALGRIND_DEFINED_STRUCT(prev_block_p);
/* merge with the previous block */
mem_heap_stat_free_block_merge ();
if (prev_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK)
{
/* merge with the previous block */
mem_heap_stat_free_block_merge ();
prev_block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
if (next_block_p != NULL)
{
VALGRIND_DEFINED_STRUCT(next_block_p);
prev_block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
if (next_block_p != NULL)
{
VALGRIND_DEFINED_STRUCT(next_block_p);
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p->neighbours[ MEM_DIRECTION_PREV ];
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p->neighbours[ MEM_DIRECTION_PREV ];
VALGRIND_NOACCESS_STRUCT(next_block_p);
}
else
{
mem_heap.last_block_p = prev_block_p;
}
}
VALGRIND_NOACCESS_STRUCT(prev_block_p);
VALGRIND_NOACCESS_STRUCT(next_block_p);
}
else
{
mem_heap.last_block_p = prev_block_p;
}
}
VALGRIND_NOACCESS_STRUCT(prev_block_p);
}
VALGRIND_NOACCESS_STRUCT(block_p);
mem_check_heap ();
@ -522,7 +522,7 @@ mem_heap_recommend_allocation_size (size_t minimum_allocation_size) /**< minimum
void
mem_heap_print (bool dump_block_headers, /**< print block headers */
bool dump_block_data, /**< print block with data (true)
or print only block header (false) */
or print only block header (false) */
bool dump_stats) /**< print heap stats */
{
mem_check_heap ();
@ -530,71 +530,71 @@ mem_heap_print (bool dump_block_headers, /**< print block headers */
JERRY_ASSERT(!dump_block_data || dump_block_headers);
if (dump_block_headers)
{
__printf ("Heap: start=%p size=%lu, first block->%p, last block->%p\n",
mem_heap.heap_start,
mem_heap.heap_size,
(void*) mem_heap.first_block_p,
(void*) mem_heap.last_block_p);
for (mem_block_header_t *block_p = mem_heap.first_block_p, *next_block_p;
block_p != NULL;
block_p = next_block_p)
{
__printf ("Heap: start=%p size=%lu, first block->%p, last block->%p\n",
mem_heap.heap_start,
mem_heap.heap_size,
(void*) mem_heap.first_block_p,
(void*) mem_heap.last_block_p);
VALGRIND_DEFINED_STRUCT(block_p);
for (mem_block_header_t *block_p = mem_heap.first_block_p, *next_block_p;
block_p != NULL;
block_p = next_block_p)
__printf ("Block (%p): magic num=0x%08x, size in chunks=%lu, previous block->%p next block->%p\n",
(void*) block_p,
block_p->magic_num,
mem_get_block_chunks_count (block_p),
(void*) block_p->neighbours[ MEM_DIRECTION_PREV ],
(void*) block_p->neighbours[ MEM_DIRECTION_NEXT ]);
if (dump_block_data)
{
uint8_t *block_data_p = (uint8_t*) (block_p + 1);
for (uint32_t offset = 0;
offset < mem_get_block_data_space_size (block_p);
offset++)
{
VALGRIND_DEFINED_STRUCT(block_p);
__printf ("Block (%p): magic num=0x%08x, size in chunks=%lu, previous block->%p next block->%p\n",
(void*) block_p,
block_p->magic_num,
mem_get_block_chunks_count (block_p),
(void*) block_p->neighbours[ MEM_DIRECTION_PREV ],
(void*) block_p->neighbours[ MEM_DIRECTION_NEXT ]);
if (dump_block_data)
{
uint8_t *block_data_p = (uint8_t*) (block_p + 1);
for (uint32_t offset = 0;
offset < mem_get_block_data_space_size (block_p);
offset++)
{
__printf ("%02x ", block_data_p[ offset ]);
}
__printf ("\n");
}
next_block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ];
VALGRIND_NOACCESS_STRUCT(block_p);
__printf ("%02x ", block_data_p[ offset ]);
}
__printf ("\n");
}
next_block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ];
VALGRIND_NOACCESS_STRUCT(block_p);
}
}
#ifdef MEM_STATS
if (dump_stats)
{
__printf ("Heap stats:\n");
__printf (" Heap size = %lu bytes\n"
" Chunk size = %lu bytes\n"
" Blocks count = %lu\n"
" Allocated blocks count = %lu\n"
" Allocated chunks count = %lu\n"
" Allocated = %lu bytes\n"
" Waste = %lu bytes\n"
" Peak allocated blocks count = %lu\n"
" Peak allocated chunks count = %lu\n"
" Peak allocated= %lu bytes\n"
" Peak waste = %lu bytes\n",
mem_heap_stats.size,
MEM_HEAP_CHUNK_SIZE,
mem_heap_stats.blocks,
mem_heap_stats.allocated_blocks,
mem_heap_stats.allocated_chunks,
mem_heap_stats.allocated_bytes,
mem_heap_stats.waste_bytes,
mem_heap_stats.peak_allocated_blocks,
mem_heap_stats.peak_allocated_chunks,
mem_heap_stats.peak_allocated_bytes,
mem_heap_stats.peak_waste_bytes);
}
{
__printf ("Heap stats:\n");
__printf (" Heap size = %lu bytes\n"
" Chunk size = %lu bytes\n"
" Blocks count = %lu\n"
" Allocated blocks count = %lu\n"
" Allocated chunks count = %lu\n"
" Allocated = %lu bytes\n"
" Waste = %lu bytes\n"
" Peak allocated blocks count = %lu\n"
" Peak allocated chunks count = %lu\n"
" Peak allocated= %lu bytes\n"
" Peak waste = %lu bytes\n",
mem_heap_stats.size,
MEM_HEAP_CHUNK_SIZE,
mem_heap_stats.blocks,
mem_heap_stats.allocated_blocks,
mem_heap_stats.allocated_chunks,
mem_heap_stats.allocated_bytes,
mem_heap_stats.waste_bytes,
mem_heap_stats.peak_allocated_blocks,
mem_heap_stats.peak_allocated_chunks,
mem_heap_stats.peak_allocated_bytes,
mem_heap_stats.peak_waste_bytes);
}
#endif /* MEM_STATS */
__printf ("\n");
@ -614,8 +614,8 @@ mem_check_heap (void)
size_t chunk_sizes_sum = 0;
for (mem_block_header_t *block_p = mem_heap.first_block_p, *next_block_p;
block_p != NULL;
block_p = next_block_p)
block_p != NULL;
block_p = next_block_p)
{
VALGRIND_DEFINED_STRUCT(block_p);
@ -636,7 +636,7 @@ mem_check_heap (void)
}
VALGRIND_NOACCESS_STRUCT(block_p);
}
}
JERRY_ASSERT(chunk_sizes_sum * MEM_HEAP_CHUNK_SIZE == mem_heap.heap_size);
JERRY_ASSERT(is_last_block_was_met);
@ -645,8 +645,8 @@ mem_check_heap (void)
chunk_sizes_sum = 0;
for (mem_block_header_t *block_p = mem_heap.last_block_p, *prev_block_p;
block_p != NULL;
block_p = prev_block_p)
block_p != NULL;
block_p = prev_block_p)
{
VALGRIND_DEFINED_STRUCT(block_p);
@ -667,7 +667,7 @@ mem_check_heap (void)
}
VALGRIND_NOACCESS_STRUCT(block_p);
}
}
JERRY_ASSERT(chunk_sizes_sum * MEM_HEAP_CHUNK_SIZE == mem_heap.heap_size);
JERRY_ASSERT(is_first_block_was_met);

View File

@ -33,9 +33,10 @@
*
* @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 */
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);
@ -49,22 +50,23 @@ extern void mem_heap_print (bool dump_block_headers, bool dump_block_data, bool
/**
* Heap memory usage statistics
*/
typedef struct {
size_t size; /**< size */
size_t blocks; /**< blocks count */
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 allocated_chunks; /**< currently allocated chunks */
size_t peak_allocated_chunks; /**< peak allocated chunks */
size_t allocated_blocks; /**< currently allocated blocks */
size_t peak_allocated_blocks; /**< peak allocated blocks */
size_t allocated_blocks; /**< currently allocated blocks */
size_t peak_allocated_blocks; /**< peak allocated blocks */
size_t allocated_bytes; /**< currently allocated bytes */
size_t peak_allocated_bytes; /**< peak allocated bytes */
size_t allocated_bytes; /**< currently allocated bytes */
size_t peak_allocated_bytes; /**< 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 waste_bytes; /**< bytes waste due to blocks filled partially
and due to block headers */
size_t peak_waste_bytes; /**< peak bytes waste */
} mem_heap_stats_t;
extern void mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p);

View File

@ -37,7 +37,7 @@ 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))
MEM_POOL_CHUNK_SIZE * chunk_index))
/**
* Initialization of memory pool.
@ -47,43 +47,43 @@ static void mem_check_pool (mem_pool_state_t *pool_p);
*/
void
mem_pool_init (mem_pool_state_t *pool_p, /**< pool */
size_t pool_size) /**< pool size */
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_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_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);
const size_t pool_space_size = pool_size - sizeof (mem_pool_state_t);
const size_t chunks_number = pool_space_size / MEM_POOL_CHUNK_SIZE;
const size_t pool_space_size = pool_size - sizeof (mem_pool_state_t);
const size_t chunks_number = pool_space_size / MEM_POOL_CHUNK_SIZE;
JERRY_ASSERT(((mem_pool_chunk_index_t) chunks_number) == chunks_number);
JERRY_ASSERT(((mem_pool_chunk_index_t) chunks_number) == chunks_number);
pool_p->chunks_number = (mem_pool_chunk_index_t) chunks_number;
pool_p->chunks_number = (mem_pool_chunk_index_t) chunks_number;
/*
* All chunks are free right after initialization
*/
pool_p->free_chunks_number = pool_p->chunks_number;
/*
* All chunks are free right after initialization
*/
pool_p->free_chunks_number = pool_p->chunks_number;
/*
* Chunk with zero index is first free chunk in the pool now
*/
pool_p->first_free_chunk = 0;
/*
* 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 < 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);
for (mem_pool_chunk_index_t chunk_index = 0;
chunk_index < 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);
}
*next_free_chunk_index_p = (mem_pool_chunk_index_t) (chunk_index + 1u);
}
mem_check_pool (pool_p);
mem_check_pool (pool_p);
} /* mem_pool_init */
/**
@ -95,11 +95,11 @@ mem_pool_alloc_chunk (mem_pool_state_t *pool_p) /**< pool */
mem_check_pool (pool_p);
if (unlikely (pool_p->free_chunks_number == 0))
{
JERRY_ASSERT(pool_p->first_free_chunk == pool_p->chunks_number);
{
JERRY_ASSERT(pool_p->first_free_chunk == pool_p->chunks_number);
return NULL;
}
return NULL;
}
JERRY_ASSERT(pool_p->first_free_chunk < pool_p->chunks_number);
@ -120,26 +120,26 @@ mem_pool_alloc_chunk (mem_pool_state_t *pool_p) /**< pool */
*/
void
mem_pool_free_chunk (mem_pool_state_t *pool_p, /**< pool */
uint8_t *chunk_p) /**< chunk pointer */
uint8_t *chunk_p) /**< chunk pointer */
{
JERRY_ASSERT(pool_p->free_chunks_number < pool_p->chunks_number);
JERRY_ASSERT(chunk_p >= MEM_POOL_SPACE_START(pool_p)
&& chunk_p <= MEM_POOL_SPACE_START(pool_p) + pool_p->chunks_number * MEM_POOL_CHUNK_SIZE);
JERRY_ASSERT(((uintptr_t) chunk_p - (uintptr_t) MEM_POOL_SPACE_START(pool_p)) % MEM_POOL_CHUNK_SIZE == 0);
JERRY_ASSERT(pool_p->free_chunks_number < pool_p->chunks_number);
JERRY_ASSERT(chunk_p >= MEM_POOL_SPACE_START(pool_p)
&& chunk_p <= MEM_POOL_SPACE_START(pool_p) + pool_p->chunks_number * MEM_POOL_CHUNK_SIZE);
JERRY_ASSERT(((uintptr_t) chunk_p - (uintptr_t) MEM_POOL_SPACE_START(pool_p)) % MEM_POOL_CHUNK_SIZE == 0);
mem_check_pool (pool_p);
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);
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;
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;
*next_free_chunk_index_p = pool_p->first_free_chunk;
pool_p->first_free_chunk = chunk_index;
pool_p->free_chunks_number++;
pool_p->first_free_chunk = chunk_index;
pool_p->free_chunks_number++;
mem_check_pool (pool_p);
mem_check_pool (pool_p);
} /* mem_pool_free_chunk */
/**
@ -149,23 +149,23 @@ static void
mem_check_pool (mem_pool_state_t __unused *pool_p) /**< pool (unused #ifdef JERRY_NDEBUG) */
{
#ifndef JERRY_NDEBUG
JERRY_ASSERT(pool_p->chunks_number != 0);
JERRY_ASSERT(pool_p->free_chunks_number <= pool_p->chunks_number);
JERRY_ASSERT(pool_p->chunks_number != 0);
JERRY_ASSERT(pool_p->free_chunks_number <= pool_p->chunks_number);
size_t met_free_chunks_number = 0;
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
size_t met_free_chunks_number = 0;
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
while (chunk_index != pool_p->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;
while (chunk_index != pool_p->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++;
met_free_chunks_number++;
chunk_index = *next_free_chunk_index_p;
}
chunk_index = *next_free_chunk_index_p;
}
JERRY_ASSERT(met_free_chunks_number == pool_p->free_chunks_number);
JERRY_ASSERT(met_free_chunks_number == pool_p->free_chunks_number);
#endif /* !JERRY_NDEBUG */
} /* mem_check_pool */

View File

@ -39,17 +39,18 @@ typedef uint16_t mem_pool_chunk_index_t;
/**
* State of a memory pool
*/
typedef struct mem_pool_state_t {
/** Number of chunks (mem_pool_chunk_index_t) */
unsigned int chunks_number : 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;
typedef struct mem_pool_state_t
{
/** Number of chunks (mem_pool_chunk_index_t) */
unsigned int chunks_number : 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;
/** 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;
/** 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;
/** Pointer to the next pool with same chunk size */
unsigned int next_pool_cp : MEM_HEAP_OFFSET_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);

View File

@ -45,7 +45,7 @@ size_t mem_free_chunks_number;
#ifdef MEM_STATS
/**
* Pools' memory usage statistics
* Pools' memory usage statistics
*/
mem_pools_stats_t mem_pools_stats;
@ -93,61 +93,68 @@ mem_pools_finalize (void)
uint8_t*
mem_pools_alloc (void)
{
/**
* If there are no free chunks, allocate new pool.
*/
if (mem_free_chunks_number == 0)
{
/**
* If there are no free chunks, allocate new pool.
*/
if (mem_free_chunks_number == 0)
{
/**
* Space, at least for header and eight chunks.
*
* TODO: Config.
*/
size_t pool_size = mem_heap_recommend_allocation_size (sizeof (mem_pool_state_t) + 8 * MEM_POOL_CHUNK_SIZE);
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_heap_alloc_block (pool_size, MEM_HEAP_ALLOC_LONG_TERM);
if (pool_state == NULL)
{
/**
* Not enough space for new pool.
*/
return NULL;
}
mem_pool_init (pool_state, pool_size);
pool_state->next_pool_cp = (mem_pools == NULL) ? MEM_COMPRESSED_POINTER_NULL
: (uint16_t) mem_compress_pointer (mem_pools);
mem_pools = pool_state;
mem_free_chunks_number += pool_state->chunks_number;
mem_pools_stat_alloc_pool ();
}
/**
* Now there is definitely at least one pool of specified type with at least one free chunk.
* Space, at least for header and eight chunks.
*
* Search for the pool.
* TODO: Config.
*/
mem_pool_state_t *pool_state = mem_pools;
size_t pool_size = mem_heap_recommend_allocation_size (sizeof (mem_pool_state_t) + 8 * MEM_POOL_CHUNK_SIZE);
while (pool_state->first_free_chunk == pool_state->chunks_number)
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_heap_alloc_block (pool_size, MEM_HEAP_ALLOC_LONG_TERM);
if (pool_state == NULL)
{
pool_state = mem_decompress_pointer (pool_state->next_pool_cp);
JERRY_ASSERT(pool_state != NULL);
/**
* Not enough space for new pool.
*/
return NULL;
}
/**
* And allocate chunk within it.
*/
mem_free_chunks_number--;
mem_pool_init (pool_state, pool_size);
mem_pools_stat_alloc_chunk ();
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);
}
return mem_pool_alloc_chunk (pool_state);
mem_pools = pool_state;
mem_free_chunks_number += pool_state->chunks_number;
mem_pools_stat_alloc_pool ();
}
/**
* Now 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;
while (pool_state->first_free_chunk == pool_state->chunks_number)
{
pool_state = mem_decompress_pointer (pool_state->next_pool_cp);
JERRY_ASSERT(pool_state != NULL);
}
/**
* And allocate chunk within it.
*/
mem_free_chunks_number--;
mem_pools_stat_alloc_chunk ();
return mem_pool_alloc_chunk (pool_state);
} /* mem_pools_alloc */
/**
@ -156,53 +163,53 @@ mem_pools_alloc (void)
void
mem_pools_free (uint8_t *chunk_p) /**< pointer to the chunk */
{
mem_pool_state_t *pool_state = mem_pools, *prev_pool_state = NULL;
mem_pool_state_t *pool_state = mem_pools, *prev_pool_state = NULL;
/**
* Search for the pool containing specified chunk.
*/
while (!(chunk_p >= MEM_POOL_SPACE_START(pool_state)
&& chunk_p <= MEM_POOL_SPACE_START(pool_state) + pool_state->chunks_number * MEM_POOL_CHUNK_SIZE))
/**
* Search for the pool containing specified chunk.
*/
while (!(chunk_p >= MEM_POOL_SPACE_START(pool_state)
&& chunk_p <= MEM_POOL_SPACE_START(pool_state) + pool_state->chunks_number * MEM_POOL_CHUNK_SIZE))
{
prev_pool_state = pool_state;
pool_state = 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 == pool_state->chunks_number)
{
if (prev_pool_state != NULL)
{
prev_pool_state = pool_state;
pool_state = mem_decompress_pointer (pool_state->next_pool_cp);
JERRY_ASSERT(pool_state != NULL);
prev_pool_state->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_decompress_pointer (pool_state->next_pool_cp);
}
}
/**
* Free the chunk
*/
mem_pool_free_chunk (pool_state, chunk_p);
mem_free_chunks_number++;
mem_free_chunks_number -= pool_state->chunks_number;
mem_pools_stat_free_chunk ();
mem_heap_free_block ((uint8_t*)pool_state);
/**
* If all chunks of the pool are free, free the pool itself.
*/
if (pool_state->free_chunks_number == pool_state->chunks_number)
{
if (prev_pool_state != NULL)
{
prev_pool_state->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_decompress_pointer (pool_state->next_pool_cp);
}
}
mem_free_chunks_number -= pool_state->chunks_number;
mem_heap_free_block ((uint8_t*)pool_state);
mem_pools_stat_free_pool ();
mem_pools_stat_free_pool ();
}
} /* mem_pools_free */

View File

@ -40,20 +40,20 @@ extern void mem_pools_free (uint8_t *chunk_p);
*/
typedef struct
{
/** pools' count */
size_t pools_count;
/** pools' count */
size_t pools_count;
/** peak pools' count */
size_t peak_pools_count;
/** peak pools' count */
size_t peak_pools_count;
/** allocated chunks count */
size_t allocated_chunks;
/** allocated chunks count */
size_t allocated_chunks;
/** peak allocated chunks count */
size_t peak_allocated_chunks;
/** peak allocated chunks count */
size_t peak_allocated_chunks;
/** free chunks count */
size_t free_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);