/** * Long path for mem_pools_alloc */ static void __attr_noinline___ mem_pools_alloc_longpath (void) { mem_check_pools (); JERRY_ASSERT (mem_free_chunk_p == NULL); JERRY_ASSERT (MEM_POOL_SIZE <= mem_heap_get_chunked_block_data_size ()); JERRY_ASSERT (MEM_POOL_CHUNKS_NUMBER >= 1); MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t*) mem_heap_alloc_chunked_block (MEM_HEAP_ALLOC_LONG_TERM); if (mem_free_chunk_p != NULL) { /* some chunks were freed due to GC invoked by heap allocator */ MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); mem_heap_free_block (pool_start_p); return; } #ifndef JERRY_NDEBUG mem_free_chunks_number += MEM_POOL_CHUNKS_NUMBER; #endif /* !JERRY_NDEBUG */ JERRY_STATIC_ASSERT (MEM_POOL_CHUNK_SIZE % MEM_ALIGNMENT == 0); JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_t) == MEM_POOL_CHUNK_SIZE); JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_index_t) <= MEM_POOL_CHUNK_SIZE); JERRY_ASSERT ((mem_pool_chunk_index_t) MEM_POOL_CHUNKS_NUMBER == MEM_POOL_CHUNKS_NUMBER); JERRY_ASSERT (MEM_POOL_SIZE == MEM_POOL_CHUNKS_NUMBER * MEM_POOL_CHUNK_SIZE); JERRY_ASSERT (((uintptr_t) pool_start_p) % MEM_ALIGNMENT == 0); mem_pool_chunk_t *prev_free_chunk_p = NULL; for (mem_pool_chunk_index_t chunk_index = 0; chunk_index < MEM_POOL_CHUNKS_NUMBER; chunk_index++) { mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; if (prev_free_chunk_p != NULL) { prev_free_chunk_p->u.free.next_p = chunk_p; } prev_free_chunk_p = chunk_p; } prev_free_chunk_p->u.free.next_p = NULL; #ifdef JERRY_VALGRIND for (mem_pool_chunk_index_t chunk_index = 0; chunk_index < MEM_POOL_CHUNKS_NUMBER; chunk_index++) { mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; VALGRIND_NOACCESS_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); } #endif /* JERRY_VALGRIND */ mem_free_chunk_p = pool_start_p; MEM_POOLS_STAT_ALLOC_POOL (); mem_check_pools (); } /* mem_pools_alloc_longpath */
int main (int __attr_unused___ argc, char __attr_unused___ **argv) { TEST_INIT (); mem_heap_init (); mem_register_a_try_give_memory_back_callback (test_heap_give_some_memory_back); mem_heap_print (true, false, true); for (uint32_t i = 0; i < test_iters; i++) { for (uint32_t j = 0; j < test_sub_iters; j++) { if (rand () % 2) { size_t size = (size_t) rand () % test_threshold_block_size; ptrs[j] = (uint8_t*) mem_heap_alloc_block (size, (rand () % 2) ? MEM_HEAP_ALLOC_LONG_TERM : MEM_HEAP_ALLOC_SHORT_TERM); sizes[j] = size; is_one_chunked[j] = false; } else { ptrs[j] = (uint8_t*) mem_heap_alloc_chunked_block ((rand () % 2) ? MEM_HEAP_ALLOC_LONG_TERM : MEM_HEAP_ALLOC_SHORT_TERM); sizes[j] = mem_heap_get_chunked_block_data_size (); is_one_chunked[j] = true; } JERRY_ASSERT (sizes[j] == 0 || ptrs[j] != NULL); memset (ptrs[j], 0, sizes[j]); if (is_one_chunked[j]) { JERRY_ASSERT (ptrs[j] != NULL && mem_heap_get_chunked_block_start (ptrs[j] + (size_t) rand () % sizes[j]) == ptrs[j]); } } // mem_heap_print (true); for (uint32_t j = 0; j < test_sub_iters; j++) { if (ptrs[j] != NULL) { for (size_t k = 0; k < sizes[j]; k++) { JERRY_ASSERT (ptrs[j][k] == 0); } if (is_one_chunked[j]) { JERRY_ASSERT (sizes[j] == 0 || mem_heap_get_chunked_block_start (ptrs[j] + (size_t) rand () % sizes[j]) == ptrs[j]); } mem_heap_free_block (ptrs[j]); ptrs[j] = NULL; } } } mem_heap_print (true, false, true); return 0; } /* main */