static void calloc_func() { size_t pool_size = 0x40000; size_t malloc_size; /* element size 1 */ for(size_t i = 1; i < pool_size - 0x3e001; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); char* mem = __calloc(malloc_size, 1, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, (int)mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } /* element size 4 */ for(size_t i = 1; i < (pool_size - 0x3e001) / 4; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); int* mem = __calloc(malloc_size, 4, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, (int)mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } /* element size 8 */ for(size_t i = 1; i < (pool_size - 0x3e001) / 8; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); uint64_t* mem = __calloc(malloc_size, 8, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } }
void init_memory_utilities (agent* thisAgent) { int i; init_memory_pool (thisAgent, &thisAgent->cons_cell_pool, sizeof(cons), "cons cell"); init_memory_pool (thisAgent, &thisAgent->dl_cons_pool, sizeof(dl_cons), "dl cons"); for (i=0; i<NUM_MEM_USAGE_CODES; i++) thisAgent->memory_for_usage[i] = 0; }
static void malloc_func(void** state) { size_t pool_size = 0x40000; /* void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); size_t mem_size = 0x3e000; void* mem = __malloc(mem_size, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); printf("extra malloc\n"); void* mem2 = __malloc(1800, malloc_pool); assert_in_range(mem2, malloc_pool, malloc_pool + pool_size); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; return; */ /* * 0x3e000 is max size that won't make failure of malloc * when pool size is set to 0x4000 * If malloc size over the 0x3e000(may be 0x3e001), malloc failed. * more problems is descirbed in Packetngin App Test Sheet. */ /* use private malloc_pool */ for(size_t i = 1; i < pool_size - 0x3e001; i++) { void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); size_t mem_size = i; void* mem = __malloc(mem_size, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } /* use __malloc_pool */ for(size_t i = 1; i < pool_size - 0x3e001; i++) { __malloc_pool = malloc(pool_size); init_memory_pool(pool_size, __malloc_pool, 0); size_t mem_size = i; void* mem = __malloc(mem_size, NULL); assert_in_range(mem, __malloc_pool, __malloc_pool + pool_size); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; } }
void HeapTlsf::TLSF_INIT() { if (mTlsfIndex >= MAX_TLSF_POOLS) { LOGE("No more TLSF POOLS!!!"); STOP; } if (mTlsfPools[mTlsfIndex] == NULL) { mTotalSize += DEFAULT_TLSF_SIZE; mTlsfPools[mTlsfIndex] = (char*)malloc(DEFAULT_TLSF_SIZE); if (mTlsfPools[mTlsfIndex] == NULL) { LOGE("Failed to malloc in TLSF_INIT"); STOP; } mTlsfPoolsMem[mTlsfIndex] = init_memory_pool(DEFAULT_TLSF_SIZE, mTlsfPools[mTlsfIndex]); LOGI("TLSF_INIT: %s (%x), Size: %d, Count: %d\n", mUserName, (unsigned int)this, DEFAULT_TLSF_SIZE, mTlsfIndex); } else { LOGE("Bad tlsf index"); STOP; } }
static void init_ulibc (void) { if (!(_dyn_mem_pool = (char *) ualloc (CONFIG_UDYNAMIC_MEMORY))) _exit (-1); if (!init_memory_pool (CONFIG_UDYNAMIC_MEMORY, _dyn_mem_pool)) _exit (-1); init_lang_supp (); }
static void arp_pack_func(void** state) { // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x10c37b9309d1; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); uint32_t spa = 0xc0a80a6f; uint32_t dpa = 0xc0a80a90; arp_request(__nics[0], dpa, spa); Packet* packet = fifo_pop(__nics[0]->output_buffer); uint32_t comp_size = packet->end; packet->end = 0; arp_pack(packet); // Checking packet->end assert_int_equal(comp_size, packet->end); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
static void arp_announce_func(void** state) { // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x10c37b9309d1; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); uint32_t spa = 0xc0a80a6f; arp_announce(__nics[0], spa); Packet* packet = fifo_pop(__nics[0]->output_buffer); assert_memory_equal(packet->buffer + packet->start, arp_announce_packet, 46); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
void init(std::size_t size, bool lock) { pool = (char*) operator new(size); mlock(pool, size); std::memset(pool, 0, size); init_memory_pool(size, pool); }
void main (uint32_t mboot_magic, uint32_t mboot_info) { multiboot_memmap_t *first_free; uint64_t *new_stack; asm volatile ("cli"); cpu_early_init(); x64_gdt_init(); x64_idt_init(); vga_console_init(); printk("\n================================\n"); printk("||==Welcome to CELLOS 64 bit==||"); printk("\n================================\n"); /* Read mboot header */ first_free = mboot_init(mboot_info, mboot_magic); if (first_free == NULL) { panic("No free memory for use! STOP~!\n"); } mb_parse_kernel_image(); paging_init(first_free->base_addr, first_free->base_addr + first_free->length); /* Init page allocator */ page_alloc_init(mem_get_low_addr(),mem_get_high_addr()); /* Initialize the memory pool */ init_memory_pool(CONFIG_KHEAP_SIZE, (void *)page_alloc_contig(CONFIG_KHEAP_SIZE/PAGE_SIZE)); detect_cpu(); acpi_init(); smp_init(); paging_late_init(); sched_core_init(); sched_init(); reschedule(); /* No reached */ }
static void init_ulibc (void) { if (!(_dyn_mem_pool = (char *) ualloc (CONFIG_UDYNAMIC_MEMORY))) _exit (-1); if (!init_memory_pool (CONFIG_UDYNAMIC_MEMORY, _dyn_mem_pool)) _exit (-1); printf("\n Init ulibc memory finished.\n"); // init_lang_supp (); }
static void arp_process_func(void** state) { // Timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); // Arp request Packet* packet = nic_alloc(__nics[0], sizeof(arp_request_packet)); memcpy(packet->buffer + packet->start, arp_request_packet, sizeof(arp_request_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); assert_true(arp_process(packet)); packet = fifo_pop(__nics[0]->output_buffer); assert_memory_equal(packet->buffer + packet->start, arp_reply_packet, 42); nic_free(packet); packet = NULL; // Arp response packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); ether = (Ether*)(packet->buffer + packet->start); arp = (ARP*)ether->payload; addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); uint8_t comp_mac[6] = { 0xcb, 0x66, 0x8f, 0x35, 0xd4, 0x74 }; uint32_t sip = endian32(arp->spa); Map* arp_table = nic_config_get(packet->nic, "net.arp.arptable"); assert_true(arp_process(packet)); ARPEntity* entity = map_get(arp_table, (void*)(uintptr_t)sip); assert_memory_equal((uint8_t*)&entity->mac, comp_mac, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
static void file_init_func() { __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); __fio = fio_create(__malloc_pool); file_init(); /* Can't check that request_ids has been created */ assert_int_equal(0, __fio->event_id); }
void override_heap_init_crt0(char* start, int length) { int res = init_memory_pool(length, start); if(res < 0) { maPanic(1, "init_memory_pool failed!"); } set_malloc_hook((malloc_hook)tlsf_malloc); set_free_hook(tlsf_free); set_realloc_hook((realloc_hook)tlsf_realloc); lprintfln("TLSF initialized!"); }
static void interface_alloc_func(void** state) { void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); IPv4Interface* interface = interface_alloc(malloc_pool); assert_in_range(interface, malloc_pool, malloc_pool + POOL_SIZE); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
void HeapTlsf::TLSF_RESET() { if (mTotalSize > 0) { for (unsigned int n=0;n<mTlsfIndex;n++) { LOGI("Tlsf reset pool: %d\n", n); destroy_memory_pool(mTlsfPools[n]); memset(mTlsfPools[n], 9, DEFAULT_TLSF_SIZE); mTlsfPoolsMem[n] = init_memory_pool( DEFAULT_TLSF_SIZE, mTlsfPools[n] ); } } }
void rpl_init(void* memory, size_t memory_size) { if (memory == NULL) { memory = c_default_pool; memory_size = DEFAULT_POOL_SIZE; } init_memory_pool(memory_size, memory); rpl_error_init(); rpl_error_thread_init(); }
static void file_write_func() { int file_fd = 0; int file_context = 0; int size = 1024; char* buffer; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); buffer = malloc(size); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); for(int i = 0; i < 256; i++) { file_context = file_fd = i; sprintf(buffer, "Write Test %d", file_fd); int status = file_read(file_fd, buffer, size, write_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_WRITE; output_buffer->context = &file_context; output_buffer->fd = file_fd; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->op.file_io.buffer = buffer; output_buffer->op.file_io.size = size; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(status, FIO_OK); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
static void free_func() { size_t pool_size = 0x40000; size_t malloc_size = 253951; void* mem; /* use private pool */ void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); mem = __malloc(malloc_size, malloc_pool); __free(mem, malloc_pool); mem = NULL; mem = __malloc(malloc_size, malloc_pool); assert_non_null(mem); __free(mem, malloc_pool); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; /* use __malloc_pool */ __malloc_pool = malloc(pool_size); init_memory_pool(pool_size, __malloc_pool, 0); mem = __malloc(malloc_size, __malloc_pool); __free(mem, __malloc_pool); mem = NULL; mem = __malloc(malloc_size, __malloc_pool); assert_non_null(mem); __free(mem, __malloc_pool); mem = NULL; destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
int malloc_init() { uint64_t start = (uint64_t)LOCAL_MALLOC_START; uint64_t end = (uint64_t)LOCAL_MALLOC_END; __malloc_pool = (void*)start; init_memory_pool((uint32_t)(end - start), __malloc_pool, 0); #if DEBUG statistics = map_create(512, map_uint64_hash, map_uint64_equals, NULL); tracing = map_create(8192, map_uint64_hash, map_uint64_equals, NULL); is_debug = true; #endif /* DEBUG */ return 0; }
int main(int argc, char **argv) { int ret; size_t pool_size; char *fname; if (argc < 2) { printf("Usage: tlsf_test <file>\n"); printf("\t<file> is swap replay dump file from sr_collect\n"); goto out; } fname = argv[1]; ret = set_sr_file(fname); if (ret < 0) { printf("Error setting replay data file.\n"); goto out; } pool = malloc(MAX_POOL_SIZE); if (pool == NULL) { printf("Error allocating memory pool.\n"); goto out; } pool_size = init_memory_pool(MAX_POOL_SIZE, pool); printf("# total pool size: %u\n", pool_size); page_table = calloc(MAX_TABLE_SIZE, sizeof(*page_table)); if (page_table == NULL) { printf("Error allocating page table structure.\n"); goto out_pgtbl; } set_sr_callbacks(event_read, event_write, event_replay_end); sr_start(); free(page_table); out_pgtbl: destroy_memory_pool(pool); free(pool); out: return 0; }
unsigned long init_sysmem (void) { unsigned long freemem = init_mem (); if (CONFIG_KDYNAMIC_MEMORY > freemem) return -1; if (!(memory_pool = alloc_region ((ulong)ebss, RAMTOP, CONFIG_KDYNAMIC_MEMORY))) return -1; // Setting up TLSF with the largest free area, memory_pool will be // zeroed by this function as well printf ("\nSetting up the dynamic memory manager (%d kbytes at 0x%x)\n", CONFIG_KDYNAMIC_MEMORY/1024, memory_pool); if (init_memory_pool (CONFIG_KDYNAMIC_MEMORY, memory_pool) == 0) return -1; return freemem; }
static void file_opendir_func() { int file_fd = 0; int file_context = 0; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); /* check context & fd unitl get to Max request id */ for(int i = 0; i < 256; i++) { file_context = i; int status = file_opendir("Test", opendir_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; /* FIORequest that put in to output_buffer of __fio */ FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_OPENDIR; output_buffer->context = &file_context; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->fd = file_fd = i; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(FIO_OK, status); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
static void file_close_func() { int file_fd = 0; int file_context = 0; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); for(int i = 0; i < 256; i++) { file_context = file_fd = i; int status = file_close(file_fd, close_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_CLOSE; output_buffer->context = &file_context; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->fd = file_fd; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(status, FIO_OK); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
kogmo_rtdb_objsize_t kogmo_rtdb_obj_mem_init (kogmo_rtdb_handle_t *db_h) { kogmo_rtdb_objsize_t size = db_h->localdata_p->heap_size; kogmo_rtdb_objsize_t free; void *base = db_h->localdata_p->heap; DBGL(DBGL_DB,"mem_init: %i bytes at %p", size, base); #if defined(RTMALLOC_tlsf) free = init_memory_pool (size, base); #elif defined(RTMALLOC_suba) db_h->heapinfo = suba_init(base, size, 1/*rst*/, 1024/*mincell*/); free = size; #else free = size; #endif db_h->localdata_p->heap_free = free; db_h->localdata_p->heap_used = 0; return free; }
static void interface_free_func(void** state) { void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); size_t first_size = get_used_size(malloc_pool); IPv4Interface* interface = interface_alloc(malloc_pool); size_t comp_size = get_used_size(malloc_pool); assert_int_not_equal(comp_size, first_size); interface_free(interface, malloc_pool); comp_size = get_used_size(malloc_pool); assert_int_equal(comp_size, first_size); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
main(){ int *ptr[100]; int i, free_mem; free_mem = init_memory_pool(POOL_SIZE, pool); printf("Total free memory= %d\n", free_mem); for (i=0; i< 100; i++) if (!(ptr[i]=malloc_ex(1024, pool))){ printf("Error\n"); exit(-1); } for (i=0; i< 100; i++) free_ex(ptr[i], pool); destroy_memory_pool(pool); printf("Test OK\n"); exit(0); }
static void arp_get_mac_func(void** state) { //timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); Packet* packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); if(!arp_process(packet)) return; uint32_t spa = 0xc0a80a6f; uint32_t dpa = 0xc0a80a90; uint64_t comp_tha = 0x74d4358f66cb; uint64_t d_mac = arp_get_mac(__nics[0], dpa, spa); assert_memory_equal((uint8_t*)&d_mac, (uint8_t*)&comp_tha, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
memory_pool* get_memory_pool( agent* my_agent, size_t size ) { memory_pool* return_val = NULL; std::map< size_t, memory_pool* >::iterator it = my_agent->dyn_memory_pools->find( size ); if ( it == my_agent->dyn_memory_pools->end() ) { memory_pool* newbie = new memory_pool; init_memory_pool( my_agent, newbie, size, "dynamic" ); my_agent->dyn_memory_pools->insert( std::make_pair< size_t, memory_pool* >( size, newbie ) ); return_val = newbie; } else { return_val = it->second; } return return_val; }
static void realloc_func() { size_t pool_size = 0x40000; size_t malloc_size; for(size_t i = 1; i < pool_size - 0x3e001; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); void* mem = __malloc(malloc_size, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); mem = __realloc(mem, 0x3e000, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } }
int heapobj_pkg_init_private(void) { size_t size; void *mem; /* * We want to know how many bytes from a memory pool TLSF will * use for its own internal use. We get the probe memory from * tlsf_malloc(), so that the main pool will be set up in the * same move. */ mem = tlsf_malloc(__this_node.mem_pool); size = init_memory_pool(__this_node.mem_pool, mem); if (size == (size_t)-1) panic("cannot initialize TLSF memory manager"); destroy_memory_pool(mem); tlsf_pool_overhead = __this_node.mem_pool - size; tlsf_pool_overhead = (tlsf_pool_overhead + 15) & ~15; tlsf_free(mem); return 0; }