Renaming rest camelCase-named identifiers according to underscore_named_value-naming.
This commit is contained in:
+135
-135
@@ -64,9 +64,9 @@ typedef enum
|
||||
*/
|
||||
typedef struct mem_block_header_t
|
||||
{
|
||||
mem_magic_num_of_block_t MagicNum; /**< magic number - MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK for allocated block
|
||||
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 */
|
||||
struct mem_block_header_t *Neighbours[ MEM_DIRECTION_COUNT ]; /**< neighbour blocks */
|
||||
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 */
|
||||
} mem_block_header_t;
|
||||
|
||||
@@ -85,10 +85,10 @@ JERRY_STATIC_ASSERT( MEM_HEAP_CHUNK_SIZE % MEM_ALIGNMENT == 0 );
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t* HeapStart; /**< first address of heap space */
|
||||
size_t HeapSize; /**< heap space size */
|
||||
mem_block_header_t* pFirstBlock; /**< first block of the heap */
|
||||
mem_block_header_t* pLastBlock; /**< last block of the heap */
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -100,11 +100,11 @@ static size_t mem_get_block_chunks_count( const mem_block_header_t *block_header
|
||||
static size_t mem_get_block_data_space_size( const mem_block_header_t *block_header_p);
|
||||
static size_t mem_get_block_chunks_count_from_data_size( size_t block_allocated_size);
|
||||
|
||||
static void mem_init_block_header( uint8_t *pFirstChunk,
|
||||
size_t sizeInChunks,
|
||||
mem_block_state_t blockState,
|
||||
mem_block_header_t *pPrevBlock,
|
||||
mem_block_header_t *pNextBlock);
|
||||
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);
|
||||
static void mem_check_heap( void);
|
||||
|
||||
#ifdef MEM_STATS
|
||||
@@ -136,18 +136,18 @@ mem_get_block_chunks_count( const mem_block_header_t *block_header_p) /**< block
|
||||
{
|
||||
JERRY_ASSERT( block_header_p != NULL );
|
||||
|
||||
const mem_block_header_t *next_block_p = block_header_p->Neighbours[ MEM_DIRECTION_NEXT ];
|
||||
const mem_block_header_t *next_block_p = block_header_p->neighbours[ MEM_DIRECTION_NEXT ];
|
||||
size_t dist_till_block_end;
|
||||
|
||||
if ( next_block_p == NULL )
|
||||
{
|
||||
dist_till_block_end = (size_t) ( mem_heap.HeapStart + mem_heap.HeapSize - (uint8_t*) block_header_p );
|
||||
dist_till_block_end = (size_t) ( mem_heap.heap_start + mem_heap.heap_size - (uint8_t*) block_header_p );
|
||||
} else
|
||||
{
|
||||
dist_till_block_end = (size_t) ( (uint8_t*) next_block_p - (uint8_t*) block_header_p );
|
||||
}
|
||||
|
||||
JERRY_ASSERT( dist_till_block_end <= mem_heap.HeapSize );
|
||||
JERRY_ASSERT( dist_till_block_end <= mem_heap.heap_size );
|
||||
JERRY_ASSERT( dist_till_block_end % MEM_HEAP_CHUNK_SIZE == 0 );
|
||||
|
||||
return dist_till_block_end / MEM_HEAP_CHUNK_SIZE;
|
||||
@@ -179,25 +179,25 @@ mem_get_block_chunks_count_from_data_size( size_t block_allocated_size) /**< siz
|
||||
* Startup initialization of heap
|
||||
*/
|
||||
void
|
||||
mem_heap_init(uint8_t *heapStart, /**< first address of heap space */
|
||||
size_t heapSize) /**< heap space size */
|
||||
mem_heap_init(uint8_t *heap_start, /**< first address of heap space */
|
||||
size_t heap_size) /**< heap space size */
|
||||
{
|
||||
JERRY_ASSERT( heapStart != NULL );
|
||||
JERRY_ASSERT( heapSize != 0 );
|
||||
JERRY_ASSERT( heapSize % MEM_HEAP_CHUNK_SIZE == 0 );
|
||||
JERRY_ASSERT( (uintptr_t) heapStart % MEM_ALIGNMENT == 0);
|
||||
JERRY_ASSERT( heap_start != NULL );
|
||||
JERRY_ASSERT( heap_size != 0 );
|
||||
JERRY_ASSERT( heap_size % MEM_HEAP_CHUNK_SIZE == 0 );
|
||||
JERRY_ASSERT( (uintptr_t) heap_start % MEM_ALIGNMENT == 0);
|
||||
|
||||
mem_heap.HeapStart = heapStart;
|
||||
mem_heap.HeapSize = heapSize;
|
||||
mem_heap.heap_start = heap_start;
|
||||
mem_heap.heap_size = heap_size;
|
||||
|
||||
mem_init_block_header(mem_heap.HeapStart,
|
||||
mem_init_block_header(mem_heap.heap_start,
|
||||
0,
|
||||
MEM_BLOCK_FREE,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
mem_heap.pFirstBlock = (mem_block_header_t*) mem_heap.HeapStart;
|
||||
mem_heap.pLastBlock = mem_heap.pFirstBlock;
|
||||
mem_heap.first_block_p = (mem_block_header_t*) mem_heap.heap_start;
|
||||
mem_heap.last_block_p = mem_heap.first_block_p;
|
||||
|
||||
mem_heap_stat_init();
|
||||
} /* mem_heap_init */
|
||||
@@ -206,29 +206,29 @@ mem_heap_init(uint8_t *heapStart, /**< first address of heap space */
|
||||
* Initialize block header
|
||||
*/
|
||||
static void
|
||||
mem_init_block_header( uint8_t *pFirstChunk, /**< address of the first chunk to use for the block */
|
||||
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 blockState, /**< state of the block (allocated or free) */
|
||||
mem_block_header_t *pPrevBlock, /**< previous block */
|
||||
mem_block_header_t *pNextBlock) /**< next block */
|
||||
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 *pBlockHeader = (mem_block_header_t*) pFirstChunk;
|
||||
mem_block_header_t *block_header_p = (mem_block_header_t*) first_chunk_p;
|
||||
|
||||
if ( blockState == MEM_BLOCK_FREE )
|
||||
if ( block_state == MEM_BLOCK_FREE )
|
||||
{
|
||||
pBlockHeader->MagicNum = MEM_MAGIC_NUM_OF_FREE_BLOCK;
|
||||
block_header_p->magic_num = MEM_MAGIC_NUM_OF_FREE_BLOCK;
|
||||
|
||||
JERRY_ASSERT( allocated_bytes == 0 );
|
||||
} else
|
||||
{
|
||||
pBlockHeader->MagicNum = MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK;
|
||||
block_header_p->magic_num = MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK;
|
||||
}
|
||||
|
||||
pBlockHeader->Neighbours[ MEM_DIRECTION_PREV ] = pPrevBlock;
|
||||
pBlockHeader->Neighbours[ MEM_DIRECTION_NEXT ] = pNextBlock;
|
||||
pBlockHeader->allocated_bytes = allocated_bytes;
|
||||
block_header_p->neighbours[ MEM_DIRECTION_PREV ] = prev_block_p;
|
||||
block_header_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
|
||||
block_header_p->allocated_bytes = allocated_bytes;
|
||||
|
||||
JERRY_ASSERT( allocated_bytes <= mem_get_block_data_space_size( pBlockHeader) );
|
||||
JERRY_ASSERT( allocated_bytes <= mem_get_block_data_space_size( block_header_p) );
|
||||
} /* mem_init_block_header */
|
||||
|
||||
/**
|
||||
@@ -245,94 +245,94 @@ mem_init_block_header( uint8_t *pFirstChunk, /**< address of the first c
|
||||
* NULL - if there is not enough memory.
|
||||
*/
|
||||
uint8_t*
|
||||
mem_heap_alloc_block( size_t sizeInBytes, /**< size of region to allocate in bytes */
|
||||
mem_heap_alloc_term_t allocTerm) /**< expected allocation term */
|
||||
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_block_header_t *pBlock;
|
||||
mem_block_header_t *block_p;
|
||||
mem_direction_t direction;
|
||||
|
||||
mem_check_heap();
|
||||
|
||||
if ( allocTerm == MEM_HEAP_ALLOC_SHORT_TERM )
|
||||
if ( alloc_term == MEM_HEAP_ALLOC_SHORT_TERM )
|
||||
{
|
||||
pBlock = mem_heap.pFirstBlock;
|
||||
block_p = mem_heap.first_block_p;
|
||||
direction = MEM_DIRECTION_NEXT;
|
||||
} else
|
||||
{
|
||||
pBlock = mem_heap.pLastBlock;
|
||||
block_p = mem_heap.last_block_p;
|
||||
direction = MEM_DIRECTION_PREV;
|
||||
}
|
||||
|
||||
/* searching for appropriate block */
|
||||
while ( pBlock != NULL )
|
||||
while ( block_p != NULL )
|
||||
{
|
||||
if ( pBlock->MagicNum == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
if ( block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
{
|
||||
if ( mem_get_block_data_space_size( pBlock) >= sizeInBytes )
|
||||
if ( mem_get_block_data_space_size( block_p) >= size_in_bytes )
|
||||
{
|
||||
break;
|
||||
}
|
||||
} else
|
||||
{
|
||||
JERRY_ASSERT( pBlock->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
JERRY_ASSERT( block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
}
|
||||
|
||||
pBlock = pBlock->Neighbours[ direction ];
|
||||
block_p = block_p->neighbours[ direction ];
|
||||
}
|
||||
|
||||
if ( pBlock == NULL )
|
||||
if ( block_p == NULL )
|
||||
{
|
||||
/* not enough free space */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* appropriate block found, allocating space */
|
||||
size_t newBlockSizeInChunks = mem_get_block_chunks_count_from_data_size( sizeInBytes);
|
||||
size_t foundBlockSizeInChunks = mem_get_block_chunks_count( pBlock);
|
||||
size_t new_block_size_in_chunks = mem_get_block_chunks_count_from_data_size( size_in_bytes);
|
||||
size_t found_block_size_in_chunks = mem_get_block_chunks_count( block_p);
|
||||
|
||||
JERRY_ASSERT( newBlockSizeInChunks <= foundBlockSizeInChunks );
|
||||
JERRY_ASSERT( new_block_size_in_chunks <= found_block_size_in_chunks );
|
||||
|
||||
mem_block_header_t *pPrevBlock = pBlock->Neighbours[ MEM_DIRECTION_PREV ];
|
||||
mem_block_header_t *pNextBlock = pBlock->Neighbours[ MEM_DIRECTION_NEXT ];
|
||||
mem_block_header_t *prev_block_p = block_p->neighbours[ MEM_DIRECTION_PREV ];
|
||||
mem_block_header_t *next_block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ];
|
||||
|
||||
if ( newBlockSizeInChunks < foundBlockSizeInChunks )
|
||||
if ( new_block_size_in_chunks < found_block_size_in_chunks )
|
||||
{
|
||||
mem_heap_stat_free_block_split();
|
||||
|
||||
uint8_t *pNewFreeBlockFirstChunk = (uint8_t*) pBlock + newBlockSizeInChunks * MEM_HEAP_CHUNK_SIZE;
|
||||
mem_init_block_header(pNewFreeBlockFirstChunk,
|
||||
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,
|
||||
pBlock /* there we will place new allocated block */,
|
||||
pNextBlock);
|
||||
block_p /* there we will place new allocated block */,
|
||||
next_block_p);
|
||||
|
||||
mem_block_header_t *pNewFreeBlock = (mem_block_header_t*) pNewFreeBlockFirstChunk;
|
||||
mem_block_header_t *new_free_block_p = (mem_block_header_t*) new_free_block_first_chunk_p;
|
||||
|
||||
if ( pNextBlock == NULL )
|
||||
if ( next_block_p == NULL )
|
||||
{
|
||||
mem_heap.pLastBlock = pNewFreeBlock;
|
||||
mem_heap.last_block_p = new_free_block_p;
|
||||
}
|
||||
|
||||
pNextBlock = pNewFreeBlock;
|
||||
next_block_p = new_free_block_p;
|
||||
}
|
||||
|
||||
mem_init_block_header((uint8_t*) pBlock,
|
||||
sizeInBytes,
|
||||
mem_init_block_header((uint8_t*) block_p,
|
||||
size_in_bytes,
|
||||
MEM_BLOCK_ALLOCATED,
|
||||
pPrevBlock,
|
||||
pNextBlock);
|
||||
prev_block_p,
|
||||
next_block_p);
|
||||
|
||||
mem_heap_stat_alloc_block( pBlock);
|
||||
mem_heap_stat_alloc_block( block_p);
|
||||
|
||||
JERRY_ASSERT( mem_get_block_data_space_size( pBlock) >= sizeInBytes );
|
||||
JERRY_ASSERT( mem_get_block_data_space_size( block_p) >= size_in_bytes );
|
||||
|
||||
mem_check_heap();
|
||||
|
||||
/* return data space beginning address */
|
||||
uint8_t *pDataSpace = (uint8_t*) (pBlock + 1);
|
||||
JERRY_ASSERT( (uintptr_t) pDataSpace % MEM_ALIGNMENT == 0);
|
||||
uint8_t *data_space_p = (uint8_t*) (block_p + 1);
|
||||
JERRY_ASSERT( (uintptr_t) data_space_p % MEM_ALIGNMENT == 0);
|
||||
|
||||
return pDataSpace;
|
||||
return data_space_p;
|
||||
} /* mem_heap_alloc_block */
|
||||
|
||||
/**
|
||||
@@ -342,57 +342,57 @@ void
|
||||
mem_heap_free_block( uint8_t *ptr) /**< pointer to beginning of data space of the block */
|
||||
{
|
||||
/* checking that ptr points to the heap */
|
||||
JERRY_ASSERT( ptr >= mem_heap.HeapStart
|
||||
&& ptr <= mem_heap.HeapStart + mem_heap.HeapSize );
|
||||
JERRY_ASSERT( ptr >= mem_heap.heap_start
|
||||
&& ptr <= mem_heap.heap_start + mem_heap.heap_size );
|
||||
|
||||
mem_check_heap();
|
||||
|
||||
mem_block_header_t *pBlock = (mem_block_header_t*) ptr - 1;
|
||||
mem_block_header_t *pPrevBlock = pBlock->Neighbours[ MEM_DIRECTION_PREV ];
|
||||
mem_block_header_t *pNextBlock = pBlock->Neighbours[ MEM_DIRECTION_NEXT ];
|
||||
mem_block_header_t *block_p = (mem_block_header_t*) ptr - 1;
|
||||
mem_block_header_t *prev_block_p = block_p->neighbours[ MEM_DIRECTION_PREV ];
|
||||
mem_block_header_t *next_block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ];
|
||||
|
||||
mem_heap_stat_free_block( pBlock);
|
||||
mem_heap_stat_free_block( block_p);
|
||||
|
||||
/* checking magic nums that are neighbour to data space */
|
||||
JERRY_ASSERT( pBlock->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
if ( pNextBlock != NULL )
|
||||
JERRY_ASSERT( block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
if ( next_block_p != NULL )
|
||||
{
|
||||
JERRY_ASSERT( pNextBlock->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK
|
||||
|| pNextBlock->MagicNum == MEM_MAGIC_NUM_OF_FREE_BLOCK );
|
||||
JERRY_ASSERT( next_block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK
|
||||
|| next_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK );
|
||||
}
|
||||
|
||||
pBlock->MagicNum = MEM_MAGIC_NUM_OF_FREE_BLOCK;
|
||||
block_p->magic_num = MEM_MAGIC_NUM_OF_FREE_BLOCK;
|
||||
|
||||
if ( pNextBlock != NULL
|
||||
&& pNextBlock->MagicNum == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
if ( next_block_p != NULL
|
||||
&& next_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
{
|
||||
/* merge with the next block */
|
||||
mem_heap_stat_free_block_merge();
|
||||
|
||||
pNextBlock = pNextBlock->Neighbours[ MEM_DIRECTION_NEXT ];
|
||||
pBlock->Neighbours[ MEM_DIRECTION_NEXT ] = pNextBlock;
|
||||
if ( pNextBlock != NULL )
|
||||
next_block_p = next_block_p->neighbours[ MEM_DIRECTION_NEXT ];
|
||||
block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
|
||||
if ( next_block_p != NULL )
|
||||
{
|
||||
pNextBlock->Neighbours[ MEM_DIRECTION_PREV ] = pBlock;
|
||||
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p;
|
||||
} else
|
||||
{
|
||||
mem_heap.pLastBlock = pBlock;
|
||||
mem_heap.last_block_p = block_p;
|
||||
}
|
||||
}
|
||||
|
||||
if ( pPrevBlock != NULL
|
||||
&& pPrevBlock->MagicNum == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
if ( prev_block_p != NULL
|
||||
&& prev_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
|
||||
{
|
||||
/* merge with the previous block */
|
||||
mem_heap_stat_free_block_merge();
|
||||
|
||||
pPrevBlock->Neighbours[ MEM_DIRECTION_NEXT ] = pNextBlock;
|
||||
if ( pNextBlock != NULL )
|
||||
prev_block_p->neighbours[ MEM_DIRECTION_NEXT ] = next_block_p;
|
||||
if ( next_block_p != NULL )
|
||||
{
|
||||
pNextBlock->Neighbours[ MEM_DIRECTION_PREV ] = pBlock->Neighbours[ MEM_DIRECTION_PREV ];
|
||||
next_block_p->neighbours[ MEM_DIRECTION_PREV ] = block_p->neighbours[ MEM_DIRECTION_PREV ];
|
||||
} else
|
||||
{
|
||||
mem_heap.pLastBlock = pPrevBlock;
|
||||
mem_heap.last_block_p = prev_block_p;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,48 +405,48 @@ mem_heap_free_block( uint8_t *ptr) /**< pointer to beginning of data space of th
|
||||
* @return recommended allocation size
|
||||
*/
|
||||
size_t
|
||||
mem_heap_recommend_allocation_size( size_t minimumAllocationSize) /**< minimum allocation size */
|
||||
mem_heap_recommend_allocation_size( size_t minimum_allocation_size) /**< minimum allocation size */
|
||||
{
|
||||
size_t minimumAllocationSizeWithBlockHeader = minimumAllocationSize + sizeof (mem_block_header_t);
|
||||
size_t heapChunkAlignedAllocationSize = JERRY_ALIGNUP( minimumAllocationSizeWithBlockHeader, MEM_HEAP_CHUNK_SIZE);
|
||||
size_t minimum_allocation_size_with_block_header = minimum_allocation_size + sizeof (mem_block_header_t);
|
||||
size_t heap_chunk_aligned_allocation_size = JERRY_ALIGNUP( minimum_allocation_size_with_block_header, MEM_HEAP_CHUNK_SIZE);
|
||||
|
||||
return heapChunkAlignedAllocationSize - sizeof (mem_block_header_t);
|
||||
return heap_chunk_aligned_allocation_size - sizeof (mem_block_header_t);
|
||||
} /* mem_heap_recommend_allocation_size */
|
||||
|
||||
/**
|
||||
* Print heap
|
||||
*/
|
||||
void
|
||||
mem_heap_print( bool dumpBlockData) /**< print block with data (true)
|
||||
mem_heap_print( bool dump_block_data) /**< print block with data (true)
|
||||
or print only block header (false) */
|
||||
{
|
||||
mem_check_heap();
|
||||
|
||||
__printf("Heap: start=%p size=%lu, first block->%p, last block->%p\n",
|
||||
mem_heap.HeapStart,
|
||||
mem_heap.HeapSize,
|
||||
(void*) mem_heap.pFirstBlock,
|
||||
(void*) mem_heap.pLastBlock);
|
||||
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 *pBlock = mem_heap.pFirstBlock;
|
||||
pBlock != NULL;
|
||||
pBlock = pBlock->Neighbours[ MEM_DIRECTION_NEXT ] )
|
||||
for ( mem_block_header_t *block_p = mem_heap.first_block_p;
|
||||
block_p != NULL;
|
||||
block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ] )
|
||||
{
|
||||
__printf("Block (%p): magic num=0x%08x, size in chunks=%lu, previous block->%p next block->%p\n",
|
||||
(void*) pBlock,
|
||||
pBlock->MagicNum,
|
||||
mem_get_block_chunks_count( pBlock),
|
||||
(void*) pBlock->Neighbours[ MEM_DIRECTION_PREV ],
|
||||
(void*) pBlock->Neighbours[ MEM_DIRECTION_NEXT ]);
|
||||
(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 ( dumpBlockData )
|
||||
if ( dump_block_data )
|
||||
{
|
||||
uint8_t *pBlockData = (uint8_t*) (pBlock + 1);
|
||||
uint8_t *block_data_p = (uint8_t*) (block_p + 1);
|
||||
for ( uint32_t offset = 0;
|
||||
offset < mem_get_block_data_space_size( pBlock);
|
||||
offset < mem_get_block_data_space_size( block_p);
|
||||
offset++ )
|
||||
{
|
||||
__printf("%02x ", pBlockData[ offset ]);
|
||||
__printf("%02x ", block_data_p[ offset ]);
|
||||
}
|
||||
__printf("\n");
|
||||
}
|
||||
@@ -486,31 +486,31 @@ static void
|
||||
mem_check_heap( void)
|
||||
{
|
||||
#ifndef JERRY_NDEBUG
|
||||
JERRY_ASSERT( (uint8_t*) mem_heap.pFirstBlock == mem_heap.HeapStart );
|
||||
JERRY_ASSERT( mem_heap.HeapSize % MEM_HEAP_CHUNK_SIZE == 0 );
|
||||
JERRY_ASSERT( (uint8_t*) mem_heap.first_block_p == mem_heap.heap_start );
|
||||
JERRY_ASSERT( mem_heap.heap_size % MEM_HEAP_CHUNK_SIZE == 0 );
|
||||
|
||||
bool isLastBlockWasMet = false;
|
||||
for ( mem_block_header_t *pBlock = mem_heap.pFirstBlock;
|
||||
pBlock != NULL;
|
||||
pBlock = pBlock->Neighbours[ MEM_DIRECTION_NEXT ] )
|
||||
bool is_last_block_was_met = false;
|
||||
for ( mem_block_header_t *block_p = mem_heap.first_block_p;
|
||||
block_p != NULL;
|
||||
block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ] )
|
||||
{
|
||||
JERRY_ASSERT( pBlock != NULL );
|
||||
JERRY_ASSERT( pBlock->MagicNum == MEM_MAGIC_NUM_OF_FREE_BLOCK
|
||||
|| pBlock->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
JERRY_ASSERT( block_p != NULL );
|
||||
JERRY_ASSERT( block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK
|
||||
|| block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
|
||||
mem_block_header_t *pNextBlock = pBlock->Neighbours[ MEM_DIRECTION_NEXT ];
|
||||
if ( pBlock == mem_heap.pLastBlock )
|
||||
mem_block_header_t *next_block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ];
|
||||
if ( block_p == mem_heap.last_block_p )
|
||||
{
|
||||
isLastBlockWasMet = true;
|
||||
is_last_block_was_met = true;
|
||||
|
||||
JERRY_ASSERT( pNextBlock == NULL );
|
||||
JERRY_ASSERT( next_block_p == NULL );
|
||||
} else
|
||||
{
|
||||
JERRY_ASSERT( pNextBlock != NULL );
|
||||
JERRY_ASSERT( next_block_p != NULL );
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT( isLastBlockWasMet );
|
||||
JERRY_ASSERT( is_last_block_was_met );
|
||||
#endif /* !JERRY_NDEBUG */
|
||||
} /* mem_check_heap */
|
||||
|
||||
@@ -532,7 +532,7 @@ mem_heap_stat_init()
|
||||
{
|
||||
__memset( &mem_heap_stats, 0, sizeof (mem_heap_stats));
|
||||
|
||||
mem_heap_stats.size = mem_heap.HeapSize;
|
||||
mem_heap_stats.size = mem_heap.heap_size;
|
||||
mem_heap_stats.blocks = 1;
|
||||
} /* mem_InitStats */
|
||||
|
||||
@@ -542,7 +542,7 @@ mem_heap_stat_init()
|
||||
static void
|
||||
mem_heap_stat_alloc_block( mem_block_header_t *block_header_p) /**< allocated block */
|
||||
{
|
||||
JERRY_ASSERT( block_header_p->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
JERRY_ASSERT( block_header_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
|
||||
const size_t chunks = mem_get_block_chunks_count( block_header_p);
|
||||
const size_t bytes = block_header_p->allocated_bytes;
|
||||
@@ -584,7 +584,7 @@ mem_heap_stat_alloc_block( mem_block_header_t *block_header_p) /**< allocated bl
|
||||
static void
|
||||
mem_heap_stat_free_block( mem_block_header_t *block_header_p) /**< block to be freed */
|
||||
{
|
||||
JERRY_ASSERT( block_header_p->MagicNum == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
JERRY_ASSERT( block_header_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
|
||||
|
||||
const size_t chunks = mem_get_block_chunks_count( block_header_p);
|
||||
const size_t bytes = block_header_p->allocated_bytes;
|
||||
|
||||
@@ -38,11 +38,11 @@ typedef enum {
|
||||
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 *heapStart, size_t heapSize);
|
||||
extern uint8_t* mem_heap_alloc_block(size_t sizeInBytes, mem_heap_alloc_term_t allocTerm);
|
||||
extern void mem_heap_init(uint8_t *heap_start, size_t heap_size);
|
||||
extern uint8_t* mem_heap_alloc_block(size_t size_in_bytes, mem_heap_alloc_term_t alloc_term);
|
||||
extern void mem_heap_free_block(uint8_t *ptr);
|
||||
extern size_t mem_heap_recommend_allocation_size(size_t minimumAllocationSize);
|
||||
extern void mem_heap_print(bool dumpBlockData);
|
||||
extern size_t mem_heap_recommend_allocation_size(size_t minimum_allocation_size);
|
||||
extern void mem_heap_print(bool dump_block_data);
|
||||
|
||||
#ifdef MEM_STATS
|
||||
/**
|
||||
|
||||
+92
-92
@@ -41,114 +41,114 @@ static const uint8_t mem_pool_free_chunk_magic_num = 0x71;
|
||||
*/
|
||||
static const mword_t mem_bitmap_bits_in_block = sizeof (mword_t) * JERRY_BITSINBYTE;
|
||||
|
||||
static void mem_check_pool( mem_pool_state_t *pPool);
|
||||
static void mem_check_pool( mem_pool_state_t *pool_p);
|
||||
|
||||
/**
|
||||
* Initialization of memory pool.
|
||||
*
|
||||
* Pool will be located in the segment [poolStart; poolStart + poolSize).
|
||||
* 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.
|
||||
*
|
||||
* Warning:
|
||||
* it is incorrect to suppose, that chunk number = poolSize / chunkSize.
|
||||
* it is incorrect to suppose, that chunk number = pool_size / chunk_size.
|
||||
*/
|
||||
void
|
||||
mem_pool_init(mem_pool_state_t *pPool, /**< pool */
|
||||
size_t chunkSize, /**< size of one chunk */
|
||||
uint8_t *poolStart, /**< start of pool space */
|
||||
size_t poolSize) /**< pool space size */
|
||||
mem_pool_init(mem_pool_state_t *pool_p, /**< pool */
|
||||
size_t chunk_size, /**< size of one chunk */
|
||||
uint8_t *pool_start, /**< start of pool space */
|
||||
size_t pool_size) /**< pool space size */
|
||||
{
|
||||
JERRY_ASSERT( pPool != NULL );
|
||||
JERRY_ASSERT( (uintptr_t) poolStart % MEM_ALIGNMENT == 0);
|
||||
JERRY_ASSERT( chunkSize % MEM_ALIGNMENT == 0 );
|
||||
JERRY_ASSERT( pool_p != NULL );
|
||||
JERRY_ASSERT( (uintptr_t) pool_start % MEM_ALIGNMENT == 0);
|
||||
JERRY_ASSERT( chunk_size % MEM_ALIGNMENT == 0 );
|
||||
|
||||
pPool->pPoolStart = poolStart;
|
||||
pPool->PoolSize = poolSize;
|
||||
pPool->ChunkSize = chunkSize;
|
||||
pool_p->pool_start_p = pool_start;
|
||||
pool_p->pool_size = pool_size;
|
||||
pool_p->chunk_size = chunk_size;
|
||||
|
||||
const size_t bitsInByte = JERRY_BITSINBYTE;
|
||||
const size_t bitmapAreaSizeAlignment = JERRY_MAX( sizeof (mword_t), MEM_ALIGNMENT);
|
||||
const size_t bits_in_byte = JERRY_BITSINBYTE;
|
||||
const size_t bitmap_area_size_alignment = JERRY_MAX( sizeof (mword_t), MEM_ALIGNMENT);
|
||||
|
||||
/*
|
||||
* Calculation chunks number
|
||||
*/
|
||||
|
||||
size_t bitmapAreaSize = 0;
|
||||
size_t chunksAreaSize = JERRY_ALIGNDOWN( poolSize - bitmapAreaSize, chunkSize);
|
||||
size_t chunksNumber = chunksAreaSize / chunkSize;
|
||||
size_t bitmap_area_size = 0;
|
||||
size_t chunks_area_size = JERRY_ALIGNDOWN( pool_size - bitmap_area_size, chunk_size);
|
||||
size_t chunks_number = chunks_area_size / chunk_size;
|
||||
|
||||
/* while there is not enough area to hold state of all chunks*/
|
||||
while ( bitmapAreaSize * bitsInByte < chunksNumber )
|
||||
while ( bitmap_area_size * bits_in_byte < chunks_number )
|
||||
{
|
||||
JERRY_ASSERT( bitmapAreaSize + chunksAreaSize <= poolSize );
|
||||
JERRY_ASSERT( bitmap_area_size + chunks_area_size <= pool_size );
|
||||
|
||||
/* correct bitmap area's size and, accordingly, chunks' area's size*/
|
||||
|
||||
size_t newBitmapAreaSize = bitmapAreaSize + bitmapAreaSizeAlignment;
|
||||
size_t newChunksAreaSize = JERRY_ALIGNDOWN( poolSize - newBitmapAreaSize, chunkSize);
|
||||
size_t newChunksNumber = newChunksAreaSize / chunkSize;
|
||||
size_t new_bitmap_area_size = bitmap_area_size + bitmap_area_size_alignment;
|
||||
size_t new_chunks_area_size = JERRY_ALIGNDOWN( pool_size - new_bitmap_area_size, chunk_size);
|
||||
size_t new_chunks_number = new_chunks_area_size / chunk_size;
|
||||
|
||||
bitmapAreaSize = newBitmapAreaSize;
|
||||
chunksAreaSize = newChunksAreaSize;
|
||||
chunksNumber = newChunksNumber;
|
||||
bitmap_area_size = new_bitmap_area_size;
|
||||
chunks_area_size = new_chunks_area_size;
|
||||
chunks_number = new_chunks_number;
|
||||
}
|
||||
|
||||
/*
|
||||
* Final calculation checks
|
||||
*/
|
||||
JERRY_ASSERT( bitmapAreaSize * bitsInByte >= chunksNumber );
|
||||
JERRY_ASSERT( chunksAreaSize >= chunksNumber * chunkSize );
|
||||
JERRY_ASSERT( bitmapAreaSize + chunksAreaSize <= poolSize );
|
||||
JERRY_ASSERT( bitmap_area_size * bits_in_byte >= chunks_number );
|
||||
JERRY_ASSERT( chunks_area_size >= chunks_number * chunk_size );
|
||||
JERRY_ASSERT( bitmap_area_size + chunks_area_size <= pool_size );
|
||||
|
||||
pPool->pBitmap = (mword_t*) poolStart;
|
||||
pPool->pChunks = poolStart + bitmapAreaSize;
|
||||
pool_p->bitmap_p = (mword_t*) pool_start;
|
||||
pool_p->chunks_p = pool_start + bitmap_area_size;
|
||||
|
||||
JERRY_ASSERT( (uintptr_t) pPool->pChunks % MEM_ALIGNMENT == 0 );
|
||||
JERRY_ASSERT( (uintptr_t) pool_p->chunks_p % MEM_ALIGNMENT == 0 );
|
||||
|
||||
pPool->ChunksNumber = chunksNumber;
|
||||
pool_p->chunks_number = chunks_number;
|
||||
|
||||
/*
|
||||
* All chunks are free right after initialization
|
||||
*/
|
||||
pPool->FreeChunksNumber = chunksNumber;
|
||||
__memset( pPool->pBitmap, 0, bitmapAreaSize);
|
||||
pool_p->free_chunks_number = chunks_number;
|
||||
__memset( pool_p->bitmap_p, 0, bitmap_area_size);
|
||||
|
||||
#ifndef JERRY_NDEBUG
|
||||
__memset( pPool->pChunks, mem_pool_free_chunk_magic_num, chunksAreaSize);
|
||||
__memset( pool_p->chunks_p, mem_pool_free_chunk_magic_num, chunks_area_size);
|
||||
#endif /* JERRY_NDEBUG */
|
||||
|
||||
mem_check_pool( pPool);
|
||||
mem_check_pool( pool_p);
|
||||
} /* mem_pool_init */
|
||||
|
||||
/**
|
||||
* Allocate a chunk in the pool
|
||||
*/
|
||||
uint8_t*
|
||||
mem_pool_alloc_chunk(mem_pool_state_t *pPool) /**< pool */
|
||||
mem_pool_alloc_chunk(mem_pool_state_t *pool_p) /**< pool */
|
||||
{
|
||||
mem_check_pool( pPool);
|
||||
mem_check_pool( pool_p);
|
||||
|
||||
if ( pPool->FreeChunksNumber == 0 )
|
||||
if ( pool_p->free_chunks_number == 0 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t chunkIndex = 0;
|
||||
size_t bitmapBlockIndex = 0;
|
||||
size_t chunk_index = 0;
|
||||
size_t bitmap_block_index = 0;
|
||||
|
||||
while ( chunkIndex < pPool->ChunksNumber )
|
||||
while ( chunk_index < pool_p->chunks_number )
|
||||
{
|
||||
if ( ~pPool->pBitmap[ bitmapBlockIndex ] != 0 )
|
||||
if ( ~pool_p->bitmap_p[ bitmap_block_index ] != 0 )
|
||||
{
|
||||
break;
|
||||
} else
|
||||
{
|
||||
bitmapBlockIndex++;
|
||||
chunkIndex += mem_bitmap_bits_in_block;
|
||||
bitmap_block_index++;
|
||||
chunk_index += mem_bitmap_bits_in_block;
|
||||
}
|
||||
}
|
||||
|
||||
if ( chunkIndex >= pPool->ChunksNumber )
|
||||
if ( chunk_index >= pool_p->chunks_number )
|
||||
{
|
||||
/* no free chunks */
|
||||
return NULL;
|
||||
@@ -157,21 +157,21 @@ mem_pool_alloc_chunk(mem_pool_state_t *pPool) /**< pool */
|
||||
/* found bitmap block with a zero bit */
|
||||
|
||||
mword_t bit = 1;
|
||||
for ( size_t bitIndex = 0;
|
||||
bitIndex < mem_bitmap_bits_in_block && chunkIndex < pPool->ChunksNumber;
|
||||
bitIndex++, chunkIndex++, bit <<= 1 )
|
||||
for ( size_t bit_index = 0;
|
||||
bit_index < mem_bitmap_bits_in_block && chunk_index < pool_p->chunks_number;
|
||||
bit_index++, chunk_index++, bit <<= 1 )
|
||||
{
|
||||
if ( ~pPool->pBitmap[ bitmapBlockIndex ] & bit )
|
||||
if ( ~pool_p->bitmap_p[ bitmap_block_index ] & bit )
|
||||
{
|
||||
/* found free chunk */
|
||||
pPool->pBitmap[ bitmapBlockIndex ] |= bit;
|
||||
pool_p->bitmap_p[ bitmap_block_index ] |= bit;
|
||||
|
||||
uint8_t *pChunk = &pPool->pChunks[ chunkIndex * pPool->ChunkSize ];
|
||||
pPool->FreeChunksNumber--;
|
||||
uint8_t *chunk_p = &pool_p->chunks_p[ chunk_index * pool_p->chunk_size ];
|
||||
pool_p->free_chunks_number--;
|
||||
|
||||
mem_check_pool( pPool);
|
||||
mem_check_pool( pool_p);
|
||||
|
||||
return pChunk;
|
||||
return chunk_p;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,71 +183,71 @@ mem_pool_alloc_chunk(mem_pool_state_t *pPool) /**< pool */
|
||||
* Free the chunk in the pool
|
||||
*/
|
||||
void
|
||||
mem_pool_free_chunk(mem_pool_state_t *pPool, /**< pool */
|
||||
uint8_t *pChunk) /**< chunk pointer */
|
||||
mem_pool_free_chunk(mem_pool_state_t *pool_p, /**< pool */
|
||||
uint8_t *chunk_p) /**< chunk pointer */
|
||||
{
|
||||
JERRY_ASSERT( pPool->FreeChunksNumber < pPool->ChunksNumber );
|
||||
JERRY_ASSERT( pChunk >= pPool->pChunks && pChunk <= pPool->pChunks + pPool->ChunksNumber * pPool->ChunkSize );
|
||||
JERRY_ASSERT( ( (uintptr_t) pChunk - (uintptr_t) pPool->pChunks ) % pPool->ChunkSize == 0 );
|
||||
JERRY_ASSERT( pool_p->free_chunks_number < pool_p->chunks_number );
|
||||
JERRY_ASSERT( chunk_p >= pool_p->chunks_p && chunk_p <= pool_p->chunks_p + pool_p->chunks_number * pool_p->chunk_size );
|
||||
JERRY_ASSERT( ( (uintptr_t) chunk_p - (uintptr_t) pool_p->chunks_p ) % pool_p->chunk_size == 0 );
|
||||
|
||||
mem_check_pool( pPool);
|
||||
mem_check_pool( pool_p);
|
||||
|
||||
size_t chunkIndex = (size_t) (pChunk - pPool->pChunks) / pPool->ChunkSize;
|
||||
size_t bitmapBlockIndex = chunkIndex / mem_bitmap_bits_in_block;
|
||||
size_t bitmapBitInBlock = chunkIndex % mem_bitmap_bits_in_block;
|
||||
mword_t bitMask = ( 1lu << bitmapBitInBlock );
|
||||
size_t chunk_index = (size_t) (chunk_p - pool_p->chunks_p) / pool_p->chunk_size;
|
||||
size_t bitmap_block_index = chunk_index / mem_bitmap_bits_in_block;
|
||||
size_t bitmap_bit_in_block = chunk_index % mem_bitmap_bits_in_block;
|
||||
mword_t bit_mask = ( 1lu << bitmap_bit_in_block );
|
||||
|
||||
#ifndef JERRY_NDEBUG
|
||||
__memset( (uint8_t*) pChunk, mem_pool_free_chunk_magic_num, pPool->ChunkSize);
|
||||
__memset( (uint8_t*) chunk_p, mem_pool_free_chunk_magic_num, pool_p->chunk_size);
|
||||
#endif /* JERRY_NDEBUG */
|
||||
JERRY_ASSERT( pPool->pBitmap[ bitmapBlockIndex ] & bitMask );
|
||||
JERRY_ASSERT( pool_p->bitmap_p[ bitmap_block_index ] & bit_mask );
|
||||
|
||||
pPool->pBitmap[ bitmapBlockIndex ] &= ~bitMask;
|
||||
pPool->FreeChunksNumber++;
|
||||
pool_p->bitmap_p[ bitmap_block_index ] &= ~bit_mask;
|
||||
pool_p->free_chunks_number++;
|
||||
|
||||
mem_check_pool( pPool);
|
||||
mem_check_pool( pool_p);
|
||||
} /* mem_pool_free_chunk */
|
||||
|
||||
/**
|
||||
* Check pool state consistency
|
||||
*/
|
||||
static void
|
||||
mem_check_pool( mem_pool_state_t __unused *pPool) /**< pool (unused #ifdef JERRY_NDEBUG) */
|
||||
mem_check_pool( mem_pool_state_t __unused *pool_p) /**< pool (unused #ifdef JERRY_NDEBUG) */
|
||||
{
|
||||
#ifndef JERRY_NDEBUG
|
||||
JERRY_ASSERT( pPool->ChunksNumber != 0 );
|
||||
JERRY_ASSERT( pPool->FreeChunksNumber <= pPool->ChunksNumber );
|
||||
JERRY_ASSERT( (uint8_t*) pPool->pBitmap == pPool->pPoolStart );
|
||||
JERRY_ASSERT( (uint8_t*) pPool->pChunks > pPool->pPoolStart );
|
||||
JERRY_ASSERT( pool_p->chunks_number != 0 );
|
||||
JERRY_ASSERT( pool_p->free_chunks_number <= pool_p->chunks_number );
|
||||
JERRY_ASSERT( (uint8_t*) pool_p->bitmap_p == pool_p->pool_start_p );
|
||||
JERRY_ASSERT( (uint8_t*) pool_p->chunks_p > pool_p->pool_start_p );
|
||||
|
||||
uint8_t freeChunkTemplate[ pPool->ChunkSize ];
|
||||
__memset( &freeChunkTemplate, mem_pool_free_chunk_magic_num, sizeof (freeChunkTemplate));
|
||||
uint8_t free_chunk_template[ pool_p->chunk_size ];
|
||||
__memset( &free_chunk_template, mem_pool_free_chunk_magic_num, sizeof (free_chunk_template));
|
||||
|
||||
size_t metFreeChunksNumber = 0;
|
||||
size_t met_free_chunks_number = 0;
|
||||
|
||||
for ( size_t chunkIndex = 0, bitmapBlockIndex = 0;
|
||||
chunkIndex < pPool->ChunksNumber;
|
||||
bitmapBlockIndex++ )
|
||||
for ( size_t chunk_index = 0, bitmap_block_index = 0;
|
||||
chunk_index < pool_p->chunks_number;
|
||||
bitmap_block_index++ )
|
||||
{
|
||||
JERRY_ASSERT( (uint8_t*) & pPool->pBitmap[ bitmapBlockIndex ] < pPool->pChunks );
|
||||
JERRY_ASSERT( (uint8_t*) & pool_p->bitmap_p[ bitmap_block_index ] < pool_p->chunks_p );
|
||||
|
||||
mword_t bitmapBlock = pPool->pBitmap[ bitmapBlockIndex ];
|
||||
mword_t bitmap_block = pool_p->bitmap_p[ bitmap_block_index ];
|
||||
|
||||
mword_t bitMask = 1;
|
||||
for ( size_t bitmapBitInBlock = 0;
|
||||
chunkIndex < pPool->ChunksNumber && bitmapBitInBlock < mem_bitmap_bits_in_block;
|
||||
bitmapBitInBlock++, bitMask <<= 1, chunkIndex++ )
|
||||
mword_t bit_mask = 1;
|
||||
for ( size_t bitmap_bit_in_block = 0;
|
||||
chunk_index < pool_p->chunks_number && bitmap_bit_in_block < mem_bitmap_bits_in_block;
|
||||
bitmap_bit_in_block++, bit_mask <<= 1, chunk_index++ )
|
||||
{
|
||||
if ( ~bitmapBlock & bitMask )
|
||||
if ( ~bitmap_block & bit_mask )
|
||||
{
|
||||
metFreeChunksNumber++;
|
||||
met_free_chunks_number++;
|
||||
|
||||
JERRY_ASSERT( __memcmp( &pPool->pChunks[ chunkIndex * pPool->ChunkSize ], freeChunkTemplate, pPool->ChunkSize) == 0 );
|
||||
JERRY_ASSERT( __memcmp( &pool_p->chunks_p[ chunk_index * pool_p->chunk_size ], free_chunk_template, pool_p->chunk_size) == 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT( metFreeChunksNumber == pPool->FreeChunksNumber );
|
||||
JERRY_ASSERT( met_free_chunks_number == pool_p->free_chunks_number );
|
||||
#endif /* !JERRY_NDEBUG */
|
||||
} /* mem_check_pool */
|
||||
|
||||
|
||||
+11
-11
@@ -31,23 +31,23 @@
|
||||
* Compact the struct
|
||||
*/
|
||||
typedef struct mem_pool_state_t {
|
||||
uint8_t *pPoolStart; /**< first address of pool space */
|
||||
size_t PoolSize; /**< pool space size */
|
||||
uint8_t *pool_start_p; /**< first address of pool space */
|
||||
size_t pool_size; /**< pool space size */
|
||||
|
||||
size_t ChunkSize; /**< size of one chunk */
|
||||
size_t chunk_size; /**< size of one chunk */
|
||||
|
||||
mword_t *pBitmap; /**< bitmap - pool chunks' state */
|
||||
uint8_t *pChunks; /**< chunks with data */
|
||||
mword_t *bitmap_p; /**< bitmap - pool chunks' state */
|
||||
uint8_t *chunks_p; /**< chunks with data */
|
||||
|
||||
size_t ChunksNumber; /**< number of chunks */
|
||||
size_t FreeChunksNumber; /**< number of free chunks */
|
||||
size_t chunks_number; /**< number of chunks */
|
||||
size_t free_chunks_number; /**< number of free chunks */
|
||||
|
||||
struct mem_pool_state_t *pNextPool; /**< pointer to the next pool with same chunk size */
|
||||
struct mem_pool_state_t *next_pool_p; /**< pointer to the next pool with same chunk size */
|
||||
} mem_pool_state_t;
|
||||
|
||||
extern void mem_pool_init(mem_pool_state_t *pPool, size_t chunkSize, uint8_t *poolStart, size_t poolSize);
|
||||
extern uint8_t* mem_pool_alloc_chunk(mem_pool_state_t *pPool);
|
||||
extern void mem_pool_free_chunk(mem_pool_state_t *pPool, uint8_t *pChunk);
|
||||
extern void mem_pool_init(mem_pool_state_t *pool_p, size_t chunk_size, uint8_t *pool_start, 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);
|
||||
|
||||
#endif /* JERRY_MEM_POOL_H */
|
||||
|
||||
|
||||
@@ -78,13 +78,13 @@ static void mem_pools_stat_free_chunk( mem_pool_chunk_type_t);
|
||||
* @return size (in bytes) of chunk of specified type
|
||||
*/
|
||||
size_t
|
||||
mem_get_chunk_size( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_get_chunk_size( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
uint32_t chunkTypeId = (uint32_t) chunkType;
|
||||
uint32_t chunk_type_id = (uint32_t) chunk_type;
|
||||
|
||||
JERRY_ASSERT( chunkTypeId < MEM_POOL_CHUNK_TYPE__COUNT );
|
||||
JERRY_ASSERT( chunk_type_id < MEM_POOL_CHUNK_TYPE__COUNT );
|
||||
|
||||
return ( 1u << ( chunkTypeId + 2 ) );
|
||||
return ( 1u << ( chunk_type_id + 2 ) );
|
||||
} /* mem_get_chunk_size */
|
||||
|
||||
/**
|
||||
@@ -104,20 +104,20 @@ mem_pools_init(void)
|
||||
*
|
||||
* TODO: Research.
|
||||
*/
|
||||
size_t poolSpaceSize = mem_heap_recommend_allocation_size( 4 * sizeof (mem_pool_state_t) + sizeof (mword_t) );
|
||||
size_t pool_space_size = mem_heap_recommend_allocation_size( 4 * sizeof (mem_pool_state_t) + sizeof (mword_t) );
|
||||
|
||||
mem_space_for_pool_for_pool_headers = mem_heap_alloc_block(poolSpaceSize,
|
||||
mem_space_for_pool_for_pool_headers = mem_heap_alloc_block(pool_space_size,
|
||||
MEM_HEAP_ALLOC_LONG_TERM);
|
||||
|
||||
/*
|
||||
* Get chunk type, checking that there is a type corresponding to specified size.
|
||||
*/
|
||||
const mem_pool_chunk_type_t chunkType = mem_size_to_pool_chunk_type( sizeof(mem_pool_state_t));
|
||||
const mem_pool_chunk_type_t chunk_type = mem_size_to_pool_chunk_type( sizeof(mem_pool_state_t));
|
||||
|
||||
mem_pool_init(&mem_pool_for_pool_headers,
|
||||
mem_get_chunk_size( chunkType),
|
||||
mem_get_chunk_size( chunk_type),
|
||||
mem_space_for_pool_for_pool_headers,
|
||||
poolSpaceSize);
|
||||
pool_space_size);
|
||||
|
||||
mem_pools_stat_init();
|
||||
} /* mem_pools_init */
|
||||
@@ -129,18 +129,18 @@ mem_pools_init(void)
|
||||
* or NULL - if not enough memory.
|
||||
*/
|
||||
uint8_t*
|
||||
mem_pools_alloc( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_pools_alloc( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
size_t chunkSize = mem_get_chunk_size( chunkType);
|
||||
size_t chunk_size = mem_get_chunk_size( chunk_type);
|
||||
|
||||
/**
|
||||
* If there are no free chunks, allocate new pool.
|
||||
*/
|
||||
if ( mem_free_chunks_number[ chunkType ] == 0 )
|
||||
if ( mem_free_chunks_number[ chunk_type ] == 0 )
|
||||
{
|
||||
mem_pool_state_t *poolState = (mem_pool_state_t*) mem_pool_alloc_chunk( &mem_pool_for_pool_headers);
|
||||
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_pool_alloc_chunk( &mem_pool_for_pool_headers);
|
||||
|
||||
if ( poolState == NULL )
|
||||
if ( pool_state == NULL )
|
||||
{
|
||||
/**
|
||||
* Not enough space for new pool' header.
|
||||
@@ -153,12 +153,12 @@ mem_pools_alloc( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
*
|
||||
* TODO: Research.
|
||||
*/
|
||||
size_t poolSpaceSize = mem_heap_recommend_allocation_size( 8 * chunkSize + sizeof (mword_t) );
|
||||
size_t pool_space_size = mem_heap_recommend_allocation_size( 8 * chunk_size + sizeof (mword_t) );
|
||||
|
||||
uint8_t *poolSpace = mem_heap_alloc_block( poolSpaceSize,
|
||||
uint8_t *pool_space = mem_heap_alloc_block( pool_space_size,
|
||||
MEM_HEAP_ALLOC_LONG_TERM);
|
||||
|
||||
if ( poolSpace == NULL )
|
||||
if ( pool_space == NULL )
|
||||
{
|
||||
/**
|
||||
* Not enough memory.
|
||||
@@ -166,17 +166,17 @@ mem_pools_alloc( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mem_pool_init( poolState,
|
||||
chunkSize,
|
||||
poolSpace,
|
||||
poolSpaceSize);
|
||||
mem_pool_init( pool_state,
|
||||
chunk_size,
|
||||
pool_space,
|
||||
pool_space_size);
|
||||
|
||||
poolState->pNextPool = mem_pools[ chunkType ];
|
||||
mem_pools[ chunkType ] = poolState;
|
||||
pool_state->next_pool_p = mem_pools[ chunk_type ];
|
||||
mem_pools[ chunk_type ] = pool_state;
|
||||
|
||||
mem_free_chunks_number[ chunkType ] += poolState->FreeChunksNumber;
|
||||
mem_free_chunks_number[ chunk_type ] += pool_state->free_chunks_number;
|
||||
|
||||
mem_pools_stat_alloc_pool( chunkType);
|
||||
mem_pools_stat_alloc_pool( chunk_type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,74 +184,74 @@ mem_pools_alloc( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
*
|
||||
* Search for the pool.
|
||||
*/
|
||||
mem_pool_state_t *poolState = mem_pools[ chunkType ];
|
||||
mem_pool_state_t *pool_state = mem_pools[ chunk_type ];
|
||||
|
||||
while ( poolState->FreeChunksNumber == 0 )
|
||||
while ( pool_state->free_chunks_number == 0 )
|
||||
{
|
||||
poolState = poolState->pNextPool;
|
||||
pool_state = pool_state->next_pool_p;
|
||||
|
||||
JERRY_ASSERT( poolState != NULL );
|
||||
JERRY_ASSERT( pool_state != NULL );
|
||||
}
|
||||
|
||||
/**
|
||||
* And allocate chunk within it.
|
||||
*/
|
||||
mem_free_chunks_number[ chunkType ]--;
|
||||
mem_free_chunks_number[ chunk_type ]--;
|
||||
|
||||
mem_pools_stat_alloc_chunk( chunkType);
|
||||
mem_pools_stat_alloc_chunk( chunk_type);
|
||||
|
||||
return mem_pool_alloc_chunk( poolState);
|
||||
return mem_pool_alloc_chunk( pool_state);
|
||||
} /* mem_pools_alloc */
|
||||
|
||||
/**
|
||||
* Free the chunk
|
||||
*/
|
||||
void
|
||||
mem_pools_free( mem_pool_chunk_type_t chunkType, /**< the chunk type */
|
||||
uint8_t *pChunk) /**< pointer to the chunk */
|
||||
mem_pools_free(mem_pool_chunk_type_t chunk_type, /**< the chunk type */
|
||||
uint8_t *chunk_p) /**< pointer to the chunk */
|
||||
{
|
||||
mem_pool_state_t *poolState = mem_pools[ chunkType ], *prevPoolState = NULL;
|
||||
mem_pool_state_t *pool_state = mem_pools[ chunk_type ], *prev_pool_state = NULL;
|
||||
|
||||
/**
|
||||
* Search for the pool containing specified chunk.
|
||||
*/
|
||||
while ( !( pChunk >= poolState->pChunks
|
||||
&& pChunk <= poolState->pPoolStart + poolState->PoolSize ) )
|
||||
while ( !( chunk_p >= pool_state->chunks_p
|
||||
&& chunk_p <= pool_state->pool_start_p + pool_state->pool_size ) )
|
||||
{
|
||||
prevPoolState = poolState;
|
||||
poolState = poolState->pNextPool;
|
||||
prev_pool_state = pool_state;
|
||||
pool_state = pool_state->next_pool_p;
|
||||
|
||||
JERRY_ASSERT( poolState != NULL );
|
||||
JERRY_ASSERT( pool_state != NULL );
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the chunk
|
||||
*/
|
||||
mem_pool_free_chunk( poolState, pChunk);
|
||||
mem_free_chunks_number[ chunkType ]++;
|
||||
mem_pool_free_chunk( pool_state, chunk_p);
|
||||
mem_free_chunks_number[ chunk_type ]++;
|
||||
|
||||
mem_pools_stat_free_chunk( chunkType);
|
||||
mem_pools_stat_free_chunk( chunk_type);
|
||||
|
||||
/**
|
||||
* If all chunks of the pool are free, free the pool itself.
|
||||
*/
|
||||
if ( poolState->FreeChunksNumber == poolState->ChunksNumber )
|
||||
if ( pool_state->free_chunks_number == pool_state->chunks_number )
|
||||
{
|
||||
if ( prevPoolState != NULL )
|
||||
if ( prev_pool_state != NULL )
|
||||
{
|
||||
prevPoolState->pNextPool = poolState->pNextPool;
|
||||
prev_pool_state->next_pool_p = pool_state->next_pool_p;
|
||||
} else
|
||||
{
|
||||
mem_pools[ chunkType ] = poolState->pNextPool;
|
||||
mem_pools[ chunk_type ] = pool_state->next_pool_p;
|
||||
}
|
||||
|
||||
mem_free_chunks_number[ chunkType ] -= poolState->ChunksNumber;
|
||||
mem_free_chunks_number[ chunk_type ] -= pool_state->chunks_number;
|
||||
|
||||
mem_heap_free_block( poolState->pPoolStart);
|
||||
mem_heap_free_block( pool_state->pool_start_p);
|
||||
|
||||
mem_pool_free_chunk( &mem_pool_for_pool_headers, (uint8_t*) poolState);
|
||||
mem_pool_free_chunk( &mem_pool_for_pool_headers, (uint8_t*) pool_state);
|
||||
|
||||
mem_pools_stat_free_pool( chunkType);
|
||||
mem_pools_stat_free_pool( chunk_type);
|
||||
}
|
||||
} /* mem_pools_free */
|
||||
|
||||
@@ -280,14 +280,14 @@ mem_pools_stat_init( void)
|
||||
* Account allocation of a pool
|
||||
*/
|
||||
static void
|
||||
mem_pools_stat_alloc_pool( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_pools_stat_alloc_pool( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
mem_pools_stats.pools_count[ chunkType ]++;
|
||||
mem_pools_stats.free_chunks[ chunkType ] = mem_free_chunks_number[ chunkType ];
|
||||
mem_pools_stats.pools_count[ chunk_type ]++;
|
||||
mem_pools_stats.free_chunks[ chunk_type ] = mem_free_chunks_number[ chunk_type ];
|
||||
|
||||
if ( mem_pools_stats.pools_count[ chunkType ] > mem_pools_stats.peak_pools_count[ chunkType ] )
|
||||
if ( mem_pools_stats.pools_count[ chunk_type ] > mem_pools_stats.peak_pools_count[ chunk_type ] )
|
||||
{
|
||||
mem_pools_stats.peak_pools_count[ chunkType ] = mem_pools_stats.pools_count[ chunkType ];
|
||||
mem_pools_stats.peak_pools_count[ chunk_type ] = mem_pools_stats.pools_count[ chunk_type ];
|
||||
}
|
||||
} /* mem_pools_stat_alloc_pool */
|
||||
|
||||
@@ -295,28 +295,28 @@ mem_pools_stat_alloc_pool( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
* Account freeing of a pool
|
||||
*/
|
||||
static void
|
||||
mem_pools_stat_free_pool( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_pools_stat_free_pool( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
JERRY_ASSERT( mem_pools_stats.pools_count[ chunkType ] > 0 );
|
||||
JERRY_ASSERT( mem_pools_stats.pools_count[ chunk_type ] > 0 );
|
||||
|
||||
mem_pools_stats.pools_count[ chunkType ]--;
|
||||
mem_pools_stats.free_chunks[ chunkType ] = mem_free_chunks_number[ chunkType ];
|
||||
mem_pools_stats.pools_count[ chunk_type ]--;
|
||||
mem_pools_stats.free_chunks[ chunk_type ] = mem_free_chunks_number[ chunk_type ];
|
||||
} /* mem_pools_stat_free_pool */
|
||||
|
||||
/**
|
||||
* Account allocation of chunk in a pool
|
||||
*/
|
||||
static void
|
||||
mem_pools_stat_alloc_chunk( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_pools_stat_alloc_chunk( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
JERRY_ASSERT( mem_pools_stats.free_chunks[ chunkType ] > 0 );
|
||||
JERRY_ASSERT( mem_pools_stats.free_chunks[ chunk_type ] > 0 );
|
||||
|
||||
mem_pools_stats.allocated_chunks[ chunkType ]++;
|
||||
mem_pools_stats.free_chunks[ chunkType ]--;
|
||||
mem_pools_stats.allocated_chunks[ chunk_type ]++;
|
||||
mem_pools_stats.free_chunks[ chunk_type ]--;
|
||||
|
||||
if ( mem_pools_stats.allocated_chunks[ chunkType ] > mem_pools_stats.peak_allocated_chunks[ chunkType ] )
|
||||
if ( mem_pools_stats.allocated_chunks[ chunk_type ] > mem_pools_stats.peak_allocated_chunks[ chunk_type ] )
|
||||
{
|
||||
mem_pools_stats.peak_allocated_chunks[ chunkType ] = mem_pools_stats.allocated_chunks[ chunkType ];
|
||||
mem_pools_stats.peak_allocated_chunks[ chunk_type ] = mem_pools_stats.allocated_chunks[ chunk_type ];
|
||||
}
|
||||
} /* mem_pools_stat_alloc_chunk */
|
||||
|
||||
@@ -324,12 +324,12 @@ mem_pools_stat_alloc_chunk( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
* Account freeing of chunk in a pool
|
||||
*/
|
||||
static void
|
||||
mem_pools_stat_free_chunk( mem_pool_chunk_type_t chunkType) /**< chunk type */
|
||||
mem_pools_stat_free_chunk( mem_pool_chunk_type_t chunk_type) /**< chunk type */
|
||||
{
|
||||
JERRY_ASSERT( mem_pools_stats.allocated_chunks[ chunkType ] > 0 );
|
||||
JERRY_ASSERT( mem_pools_stats.allocated_chunks[ chunk_type ] > 0 );
|
||||
|
||||
mem_pools_stats.allocated_chunks[ chunkType ]--;
|
||||
mem_pools_stats.free_chunks[ chunkType ]++;
|
||||
mem_pools_stats.allocated_chunks[ chunk_type ]--;
|
||||
mem_pools_stats.free_chunks[ chunk_type ]++;
|
||||
} /* mem_pools_stat_free_chunk */
|
||||
#endif /* MEM_STATS */
|
||||
|
||||
|
||||
@@ -51,11 +51,11 @@ typedef enum {
|
||||
((size) == 64 ? MEM_POOL_CHUNK_TYPE_64 : \
|
||||
jerry_unreferenced_expression)))))
|
||||
|
||||
extern size_t mem_get_chunk_size( mem_pool_chunk_type_t chunkType);
|
||||
extern size_t mem_get_chunk_size( mem_pool_chunk_type_t chunk_type);
|
||||
|
||||
extern void mem_pools_init(void);
|
||||
extern uint8_t* mem_pools_alloc(mem_pool_chunk_type_t chunkType);
|
||||
extern void mem_pools_free(mem_pool_chunk_type_t chunkType, uint8_t *pChunk);
|
||||
extern uint8_t* mem_pools_alloc(mem_pool_chunk_type_t chunk_type);
|
||||
extern void mem_pools_free(mem_pool_chunk_type_t chunk_type, uint8_t *chunk_p);
|
||||
|
||||
#ifdef MEM_STATS
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user