static ulong_t mem_scan_region(ulong_t start, ulong_t end, frame_state_t state, void *data_) { struct scan_region_data *data = data_; /* try to create kernel heap, if appropriate */ if (!data->heap_created && state == FRAME_AVAIL && (end - start) >= HEAP_SIZE) { mem_set_region_state(start, start + HEAP_SIZE, FRAME_HEAP); mem_heap_init(start, start + HEAP_SIZE); start += HEAP_SIZE; data->heap_created = true; data->heap_size = HEAP_SIZE; } /* set state of all frames in region, and add to freelist if appropriate */ mem_set_region_state(start, end, state); if (state == FRAME_AVAIL) { data->avail_pages += (end - start) / PAGE_SIZE; } return end; }
/** * Initialize memory allocators. */ void mem_init (void) { mem_heap_init (); mem_pools_init (); } /* mem_init */
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 */
void mem_init(struct ls_state *ls) { mem_heap_init(&ls->kern_mem); mem_heap_init(&ls->user_mem); }