/* * Allocate and initialize an mm_struct. */ struct mm_struct *mm_alloc(void) { struct mm_struct *mm; mm = allocate_mm(); if (!mm) return NULL; memset(mm, 0, sizeof(*mm)); mm_init_cpumask(mm); return mm_init(mm, current); }
/** * mm_free - Free a block * @param bp Block to be freed */ void mm_free(void *bp) { if(bp == 0) return; size_t size = GET_SIZE(HDRP(bp)); if (heap_listp == 0){ mm_init(); } PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); bp = add_free_list_lifo(bp); }
struct mm_struct *dup_mm(struct task_struct *tsk) { struct mm_struct *mm, *oldmm = current->mm; int err; if (!oldmm) return NULL; mm = allocate_mm(); if (!mm) goto fail_nomem; memcpy(mm, oldmm, sizeof(*mm)); mm_init_cpumask(mm); #ifdef CONFIG_TRANSPARENT_HUGEPAGE mm->pmd_huge_pte = NULL; #endif if (!mm_init(mm, tsk)) goto fail_nomem; if (init_new_context(tsk, mm)) goto fail_nocontext; dup_mm_exe_file(oldmm, mm); err = dup_mmap(mm, oldmm); if (err) goto free_pt; mm->hiwater_rss = get_mm_rss(mm); mm->hiwater_vm = mm->total_vm; if (mm->binfmt && !try_module_get(mm->binfmt->module)) goto free_pt; return mm; free_pt: mm->binfmt = NULL; mmput(mm); fail_nomem: return NULL; fail_nocontext: mm_free_pgd(mm); free_mm(mm); return NULL; }
/* * Allocate a new mm structure and copy contents from the * mm structure of the passed in task structure. */ static struct mm_struct *dup_mm(struct task_struct *tsk) { struct mm_struct *mm, *oldmm = current->mm; int err; mm = allocate_mm(); if (!mm) goto fail_nomem; memcpy(mm, oldmm, sizeof(*mm)); mm_init_cpumask(mm); #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS mm->pmd_huge_pte = NULL; #endif if (!mm_init(mm, tsk)) goto fail_nomem; if (init_new_context(tsk, mm)) goto fail_nocontext; dup_mm_exe_file(oldmm, mm); err = dup_mmap(mm, oldmm); if (err) goto free_pt; mm->hiwater_rss = get_mm_rss(mm); mm->hiwater_vm = mm->total_vm; if (mm->binfmt && !try_module_get(mm->binfmt->module)) goto free_pt; return mm; free_pt: /* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt = NULL; mmput(mm); fail_nomem: return NULL; fail_nocontext: /* * If init_new_context() failed, we cannot use mmput() to free the mm * because it calls destroy_context() */ mm_free_pgd(mm); free_mm(mm); return NULL; }
int main(int argc, char *argv[]) { e_int32 ret,i; hd_connect_t connect; msg_monitor_t monitor = { 0 }; ethread_t* threads[MAX_CLIENT_SIZE]; e_uint8 name[128]={'t','e','s','t',0}; ret = sc_open_socket(&connect, "127.0.0.1", 6666); e_assert(ret>0, ret); ret = sc_connect(&connect); e_assert(ret>0, ret); ret = mm_init(&monitor, &connect); e_assert(ret>0, ret); mm_start(&monitor); #if 0 fsocket_t* fd = mm_create_socket(&monitor, name); if (!fd) { DMSG((STDOUT,"ERROR CREATE FAKE SOCKET\r\n")); } else{ request_thread(fd); } #else while (count--) { sprintf(name,"Thread[%d]",count); fsocket_t* fd = mm_create_socket(&monitor,name); DMSG((STDOUT,"create request thread %d\r\n",count)); ret = createthread("request", (thread_func) &request_thread, (void*) fd, NULL, &threads[count]); if (ret <= 0) { DMSG((STDOUT,"createhread failed!\r\n")); return E_ERROR; } ret = resumethread(threads[count]); if (ret <= 0) { DMSG((STDOUT,"resumethread failed!\r\n")); return E_ERROR; } } #endif while(count<MAX_CLIENT_SIZE-1) { //DMSG((STDOUT,"COUNT = %d",count)); sleep(1); } for (i=0;i<MAX_CLIENT_SIZE;i++) { killthread( threads[i]); } mm_stop(&monitor); sc_close(&connect); return 0; }
/* Example main function that invokes mymalloc and myfree. */ int main(int argc, char *argv[]) { pthread_t threads[MAX_THREADS]; long tid; int err = 0; struct timeval start, end; double diff; FILE *fp; if(argc != 2) { printf("Usage: %s trace_file\n", argv[0]); exit(1); } if((fp = fopen(argv[1], "r")) == NULL) { perror("Trace file open:"); exit(1); } int num_threads = load_trace(fp); if (pthread_mutex_init(&mywait, NULL)) { fprintf(stderr, "Error: mutex initialization failed.\n"); return 1; } // start_heap = sbrk(0); mm_init(); gettimeofday(&start, NULL); for (tid = 0; tid < num_threads; tid++) { err = pthread_create(&threads[tid], NULL, dowork, (void *)tid); if (err) { fprintf(stderr, "Error: pthread_create failed on dowork thread %li.\n", tid); return 1; } } for (tid = 0; tid < num_threads; tid++) { err = pthread_join(threads[tid], NULL); if(err) { fprintf(stderr, "Error: pthread_join failed on thread %li.\n", tid); } } gettimeofday(&end, NULL); diff = 1000000 *(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec); fprintf(stdout, "Time: %dus\n", (int)diff); fprintf(stdout, "Max heap extent: %ld\n", (long int)(max_heap - start_heap)); return 0; }
int main (int argc, char * argv[]) { int nthreads; int iterations; int objSize; int repetitions; if (argc > 4) { nthreads = atoi(argv[1]); iterations = atoi(argv[2]); objSize = atoi(argv[3]); repetitions = atoi(argv[4]); } else { fprintf (stderr, "Usage: %s nthreads iterations objSize repetitions\n", argv[0]); exit(1); } pthread_t threads[nthreads]; int numCPU = getNumProcessors(); pthread_setconcurrency(numCPU); int i; /* Call allocator-specific initialization function */ mm_init(); pthread_attr_t attr; initialize_pthread_attr(PTHREAD_CREATE_JOINABLE, SCHED_RR, -10, PTHREAD_EXPLICIT_SCHED, PTHREAD_SCOPE_SYSTEM, &attr); timer_start(); for (i = 0; i < nthreads; i++) { struct workerArg * w = (struct workerArg *)mm_malloc(sizeof(struct workerArg)); w->_objSize = objSize; w->_repetitions = repetitions / nthreads; w->_iterations = iterations; w->_cpu = (i+1)%numCPU; pthread_create(&threads[i], &attr, &worker, (void *)w); } for (i = 0; i < nthreads; i++) { pthread_join(threads[i], NULL); } double t = timer_stop(); printf ("Time elapsed = %f seconds\n", t); printf ("Memory used = %d bytes\n",mem_usage()); return 0; }
/* * free - Free a block */ void free(void *bp) { if (bp == 0) return; size_t size = GET_SIZE(HDRP(bp)); if (heap_listp == 0){ mm_init(); } PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); coalesce(bp); }
/* * free */ void free(void *bp) { if(bp == 0) return; if (first_block == 0) mm_init(); UNSET_ALLOC(HDRP(bp)); PUT(FTRP(bp), GET(HDRP(bp))); coalesce(bp); #ifdef DEBUG mm_checkheap(1); #endif }
/* * Allocate a new mm structure and copy contents from the * mm structure of the passed in task structure. */ struct mm_struct *dup_mm(struct task_struct *tsk) { struct mm_struct *mm, *oldmm = current->mm; int err; if (!oldmm) return NULL; mm = allocate_mm(); if (!mm) goto fail_nomem; memcpy(mm, oldmm, sizeof(*mm)); /* Initializing for Swap token stuff */ mm->token_priority = 0; mm->last_interval = 0; if (!mm_init(mm, tsk)) goto fail_nomem; if (init_new_context(tsk, mm)) goto fail_nocontext; dup_mm_exe_file(oldmm, mm); err = dup_mmap(mm, oldmm); if (err) goto free_pt; mm->hiwater_rss = get_mm_rss(mm); mm->hiwater_vm = mm->total_vm; return mm; free_pt: mmput(mm); fail_nomem: return NULL; fail_nocontext: /* * If init_new_context() failed, we cannot use mmput() to free the mm * because it calls destroy_context() */ mm_free_pgd(mm); free_mm(mm); return NULL; }
int main () { int* vm_ptr; int PAGE_SIZE = sysconf(_SC_PAGE_SIZE); //printf("%d\n", PAGE_SIZE); int vm_size = 16*PAGE_SIZE;NUM_VM_PAGES = vm_size/PAGE_SIZE; int temp; FILE* f1 = fopen("test4.txt", "w"); vm_ptr=(int*)memalign(PAGE_SIZE, vm_size); if(vm_ptr==NULL) { printf("FAILURE in virtual memory allocation\n"); return 0; } mm_init((void*)vm_ptr, vm_size, 4, PAGE_SIZE, 2); mm_log(f1); /* virtual memory access starts */ temp = vm_ptr[8]; // Read virtual page 1 mm_log(f1); temp = vm_ptr[8 + ((int)((PAGE_SIZE)/sizeof(int)))]; // Read virtual page 2 mm_log(f1); vm_ptr[16] = 12; // Write virtual page 1 mm_log(f1); temp = vm_ptr[8 + ((int)((2*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 3 mm_log(f1); temp = vm_ptr[8 + ((int)((3*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 4 mm_log(f1); temp = vm_ptr[7]; // Read virtual page 1 mm_log(f1); temp = vm_ptr[8 + ((int)((4*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 5 mm_log(f1); vm_ptr[32] = 64; // Write virtual page 1 mm_log(f1); vm_ptr[16 + ((int)((PAGE_SIZE)/sizeof(int)))] = 49; // Write virtual page 2 mm_log(f1); vm_ptr[16 + ((int)((2*PAGE_SIZE)/sizeof(int)))] = 49; // Write virtual page 3 mm_log(f1); vm_ptr[16 + ((int)((3*PAGE_SIZE)/sizeof(int)))] = 49; // Write virtual page 4 mm_log(f1); vm_ptr[16 + ((int)((4*PAGE_SIZE)/sizeof(int)))] = 49; // Write virtual page 5 mm_log(f1); /* virtual memory access ends */ free(vm_ptr); return 0; }
static void test_spatial() { monkeymind mind; n_int i; printf("test_spatial..."); mm_init(&mind, MM_SEX_FEMALE,3,6,NULL); for (i = 0; i < MM_SIZE_SPATIAL*MM_SIZE_SPATIAL; i++) { assert(mm_id_get(&mind.spatial[i].id,0) == i); } printf("Ok\n"); }
void kern_main(multiboot_info_t *boot_info) { term_clear(); term_color(TERM_GREEN, TERM_BLACK); term_print("Booting\n"); term_print("Initializing memory manager\n"); if (mm_init(boot_info)) { term_print("Failed to initialize memory manager\n"); return; } com_init(); com_print(COM1, "Hello COM1!\n"); log_info("This is a logging test!"); return; }
/* * free */ void free (void *ptr) { if (ptr == NULL) { return; } size_t size = GET_SIZE(HDRP(ptr)); /* $end mmfree */ if (heap_listp == 0){ mm_init(); } /* $begin mmfree */ PUT(HDRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0)); coalesce(ptr); }
/* This is the C kernel entry point */ void kmain(struct multiboot *mboot_header, addr_t initial_stack) { /* Store passed values, and initiate some early things * We want serial log output as early as possible */ kernel_state_flags=0; mtboot = mboot_header; initial_boot_stack = initial_stack; loader_parse_kernel_elf(mboot_header, &kernel_sections); #if CONFIG_MODULES loader_init_kernel_symbols(); #endif serial_init(); cpu_early_init(); #if CONFIG_MODULES loader_init_modules(); #endif syscall_init(); fs_initrd_load(mtboot); cpu_timer_install(1000); cpu_processor_init_1(); /* Now get the management stuff going */ printk(1, "[kernel]: Starting system management\n"); mm_init(mtboot); syslog_init(); parse_kernel_command_line((char *)(addr_t)mtboot->cmdline); tm_init_multitasking(); dm_init(); fs_init(); net_init(); trace_init(); /* Load the rest... */ printk(KERN_MILE, "[kernel]: Kernel is setup (kv=%d, bpl=%d: ok)\n", CONFIG_VERSION_NUMBER, BITS_PER_LONG); printk(KERN_DEBUG, "[kernel]: structure sizes: process=%d bytes, thread=%d bytes, inode=%d bytes\n", sizeof(struct process), sizeof(struct thread), sizeof(struct inode)); cpu_interrupt_set(1); sys_setup(); cpu_processor_init_2(); timer_calibrate(); #if CONFIG_SMP if(boot_cpus) cpu_boot_all_aps(); #endif tm_clone(0, __init_entry, 0); sys_setsid(); kt_kernel_idle_task(); }
void kmain() { irq_disable(); /* * A primeira coisa a se fazer é iniciar todo o gerenciador * de memória. */ mm_init(); arch_early_init(); ioremap_init(); irq_init(); sched_init(); timer_init(); /* * Neste momento temos o gerenciador de memória e escalonador prontos, * já podemos habilitar as interrupções, que podem ser utilizadas * pelos drivers. */ irq_enable(); /* Inicia os drivers da plataforma */ arch_setup(); /* Requisita um modo se existir um framebuffer*/ fb_set_mode(); /* Inicia o console sobre o framebuffer */ fb_console_init(); kernel_info(); #if 1 irq_disable(); semaphore_init(&sem, 1); create_task("a", 4); create_task("b", 5); create_task("c", 6); create_task("d", 7); create_task("b", 8); create_task("b", 9); irq_enable(); /* Fica de boas esperando as trocas de contexto */ #endif /* Como queremos imprimir para depuração do driver, inicializamos ele agora */ //bcm2835_emmc_init(); for (;;) { led_blink(); //printk("-"); } }
/* * malloc - Allocate a block by incrementing the brk pointer. * Always allocate a block whose size is a * multiple of the alignment * * @return - generic pointer to first byte of allocated memory * - NULL if error occured during mem allocation */ void *malloc(size_t sizeToAlloc) { /*int newsize = ALIGN(sizeToAlloc + SIZE_T_SIZE); unsigned char *p = mem_sbrk(newsize); if( (long)p < 0 ) if p is -1 or NULL, allocation error occured return NULL; else { p += SIZE_T_SIZE; *SIZE_PTR(p) = newsize; return p; }*/ size_t asize; /* Adjusted block size */ size_t extendsize; /* Amount to extend heap by, if you run out of space */ char *blockPtr; if( heap_ptr == 0 ) mm_init(); if( sizeToAlloc == 0 ) return NULL; if( sizeToAlloc <= DSIZE ) asize = 2*DSIZE; else asize = DSIZE * ( (sizeToAlloc + DSIZE + (DSIZE - 1)) / DSIZE ); blockPtr = find_fit(asize); // Search the free list for a fit if( blockPtr != NULL ) { place( blockPtr, asize ); return blockPtr; } // No fit found. Get more memory and place the block extendsize = MAX(asize, CHUNK_SIZE); blockPtr = extend_heap(extendsize/WSIZE); if( blockPtr == NULL ) return NULL; place( blockPtr, asize ); return blockPtr; }
/* * free- Free the occupied block and coalesces the block */ void free(void *ptr) { //printf("\nFree Count: %d\n",++free_count); if (ptr == 0) return; size_t size = GET_SIZE(HDRP(ptr)); if (heap_list_head == 0) mm_init(); PUT(HDRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0)); coalesce(ptr); //mm_checkheap(1); }
void main(void) { printf("\n\rZINIX v%d.%d for the N8VEM\n\r\n", MAJOR_VERSION, MINOR_VERSION); mm_init(); ptable_init(); intr = 1; enable_intr(); swapbank(KMOD_FS); bankcpy(KMOD_FS, 0x100, ROM_2, 0, 0x4000); kmod_init(); panic("end of main!"); }
/* * malloc */ block_ptr malloc(size_t size) { size_t asize; /* Adjusted block size */ size_t extendsize; /* Amount to extend heap if no fit */ char *bp; if (heap_listp == 0) { mm_init(); } /* Ignore spurious requests */ if (size == 0) return NULL; /* Adjust block size to include overhead and alignment reqs. */ if (size <= DSIZE) asize = 2 * DSIZE; else asize = DSIZE * ((size + (WSIZE) + (DSIZE - 1)) / DSIZE); #ifdef DEBUG printf("\nMalloc request: size = %zu, rounded to %zu \033[41;37m[ID:%d]\033[0m\n", size, asize, operid++); #endif /* Search the free list for a fit */ if ((bp = find_fit(asize)) != NULL) { #ifdef DEBUG { puts("Found fit!"); checkblock(bp); printblock(bp); } #endif place(bp, asize); return bp; } /* No fit found. Get more memory and place the block */ extendsize = MAX(asize, BLOCKSIZE); if ((bp = extend_heap(extendsize / WSIZE)) == NULL) return NULL; place(bp, asize); return bp; }
//uint64_t exception_stack; void start(uint32_t* modulep, void* physbase, void* physfree) { uint64_t address = (uint64_t)(&idt); uint16_t size_idt = (uint16_t)(sizeof(idt)); int my_variable = 0; // int i = 922222895; //int i =0; struct smap_t { uint64_t base, length; uint32_t type; }__attribute__((packed)) *smap; struct process *newproc1,*newproc2,*newproc3; cls(); // while(--i>0); while(modulep[0] != 0x9001) modulep += modulep[1]+2; for(smap = (struct smap_t*)(modulep+2); smap < (struct smap_t*)((char*)modulep+modulep[1]+2*4); ++smap) { if (smap->type == 1 /* memory */ && smap->length != 0) { kprintf("Available Physical Memory [%x-%x]\n", smap->base, smap->base + smap->length); } } kprintf("tarfs in [%p:%p]\n", &_binary_tarfs_start, &_binary_tarfs_end); set_all_handlers(); setup_idtr(address,size_idt); npages_determine(134205440); set_consts((uint64_t)physbase,(uint64_t)physfree); mm_init(); loadcr3(katopa((uint64_t)pml4e),(uint64_t)physbase,(uint64_t)physfree); my_variable++; newproc1 = (struct process*)process_setup((uint64_t)physbase,(uint64_t)physfree,"bin/newhello"); curproc_count++; newproc2 = process_setup((uint64_t)physbase,(uint64_t)physfree,"bin/sacrifice"); curproc_count++; newproc3 = process_setup((uint64_t)physbase,(uint64_t)physfree,"bin/pragathi"); proc_status(newproc1); proc_status(newproc2); proc_status(newproc3); cur_proc = newproc1; /*while(i<10000) { kmalloc(30); i++; }*/ exception_stack = page_alloc(); first_sched(); while(1){} }
int mman_release_page(spdid_t spd, vaddr_t addr, int flags) { struct mapping *m; int ret = 0; LOCK(); mm_init(); m = mapping_lookup(spd, addr); if (!m) { ret = -1; /* -EINVAL */ goto done; } mapping_del(m); done: UNLOCK(); return ret; }
void start_kernel(void) { seg_t base, end; /* We set the idle task as #0, and init_task() will be task #1 */ sched_init(); /* This block of functions don't need console */ setup_arch(&base, &end); mm_init(base, end); buffer_init(); inode_init(); init_IRQ(); tty_init(); init_console(); #if (CONFIG_BOGOMIPS == 0) calibrate_delay(); #endif device_setup(); #ifdef CONFIG_SOCKET sock_init(); #endif fs_init(); mm_stat(base, end); printk("ELKS version %s\n", system_utsname.release); kfork_proc(init_task); wake_up_process(&task[1]); /* * We are now the idle task. We won't run unless no other process can run. */ while (1) { schedule(); #ifdef CONFIG_IDLE_HALT idle_halt (); #endif } }
int main () { int* vm_ptr; int PAGE_SIZE = sysconf(_SC_PAGE_SIZE); //printf("%d\n", PAGE_SIZE); int vm_size = 16*PAGE_SIZE; int temp; vm_ptr=memalign(PAGE_SIZE, vm_size); if(vm_ptr==NULL) { printf("FAILURE in virtual memory allocation\n"); return 0; } FILE* f1 = fopen("results.txt", "w"); mm_init((void*)vm_ptr, vm_size, 4, PAGE_SIZE, 2); mm_log(f1); /* virtual memory access starts */ temp = vm_ptr[8]; // Read virtual page 1 mm_log(f1); vm_ptr[8 + ((int)((1*PAGE_SIZE)/sizeof(int)))] = 72; // Write virtual page 2 mm_log(f1); temp = vm_ptr[8 + ((int)((2*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 3 mm_log(f1); temp = vm_ptr[8 + ((int)((3*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 4 mm_log(f1); temp = vm_ptr[8 + ((int)((4*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 5 mm_log(f1); temp = vm_ptr[8 + ((int)((1*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 2 mm_log(f1); temp = vm_ptr[9]; // Read virtual page 1 mm_log(f1); temp = vm_ptr[2 + ((int)((1*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 3 mm_log(f1); temp = vm_ptr[8 + ((int)((4*PAGE_SIZE)/sizeof(int)))]; // Read virtual page 5 mm_log(f1); /* virtual memory access ends */ free(vm_ptr); fclose(f1); return 0; }
MM *mm_create(size_t size) { MM *p; if (size == 0) size = 1 << 25; p = mm_create_shm(size); if (p == (MM *) MAP_FAILED) return NULL; mm_init(p); if (!mm_init_lock(p->lock)) { mm_destroy_shm(p); return NULL; } return p; }
/* $begin mmfree */ void mm_free(void *bp) { /* $end mmfree */ if(bp == 0) return; /* $begin mmfree */ size_t size = GET_SIZE(HDRP(bp)); /* $end mmfree */ if (heap_listp == 0){ mm_init(); } /* $begin mmfree */ PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); coalesce(bp); }
int __init main() { /* keep the calling order below because of dependencies */ sys_init(); mm_init(); fs_init(); device_init(); systick_init(); scheduler_init(); console_init(); make_init_task(); load_user_task(); /* that are registered statically */ softirq_init(); #ifdef CONFIG_TIMER timer_init(); #endif #ifndef DEFSTR #define DEFMKSTR(x) #x #define DEFSTR(x) DEFMKSTR(x) #endif /* a banner */ printk("yaos %s %s\n", DEFSTR(VERSION), DEFSTR(MACHINE)); /* switch from boot stack memory to new one */ set_user_sp(init.mm.sp); set_kernel_sp(init.mm.kernel.sp); /* everything ready now */ #ifndef ARMv7A sei(); #endif resched(); /* it doesn't really reach up to this point. init task becomes idle as * its context is already set to idle */ __context_restore(current); __ret_from_exc(0); freeze(); return 0; }
/* * Allocate a new mm structure and copy contents from the * mm structure of the passed in task structure. */ static struct mm_struct *dup_mm(struct task_struct *tsk) { struct mm_struct *mm, *oldmm = current->mm; int err; if (!oldmm) return NULL; mm = allocate_mm(); if (!mm) goto fail_nomem; memcpy(mm, oldmm, sizeof(*mm)); if (!mm_init(mm)) goto fail_nomem; if (init_new_context(tsk, mm)) goto fail_nocontext; err = dup_mmap(mm, oldmm); if (err) goto free_pt; mm->hiwater_rss = get_mm_rss(mm); mm->hiwater_vm = mm->total_vm; return mm; free_pt: mmput(mm); fail_nomem: return NULL; fail_nocontext: /* * If init_new_context() failed, we cannot use mmput() to free the mm * because it calls destroy_context() */ mm_free_pgd(mm); free_mm(mm); return NULL; }
/* * free - free a allocated block */ void free(void *bp) { if (bp == NULL) { return; } dbg_printf("want to free %d size block in address 0x%lx\n", (int)block_size(bp), (long)bp); print_heap(); if (block_alloc(bp) == 0) { return; } if (heap_head == NULL) { mm_init(); } mark(bp, block_size(bp), block_prev_alloc(bp), 0); mark(next_block(bp), block_size(next_block(bp)), 0, block_alloc(next_block(bp))); insert_to_list(bp); bp = coalesce(bp); dbg_printf("want return from free %d size block in address 0x%lx\n", (int)block_size(bp), (long)bp); print_heap(); }
void mm_setup(multiboot_info_t * mbi) { /* 1 MB Safe distance */ #define SAFE_DISTANCE (1024*1024) #define MINIMUM_LEN (1024*1024) unsigned long int largest_len = 0; unsigned long int largest_addr; unsigned long int safe_addr = (unsigned long int)kmain + SAFE_DISTANCE + 1; multiboot_memory_map_t *mmap = (multiboot_memory_map_t *)mbi->mmap_addr; while(mmap < (multiboot_memory_map_t *)(mbi->mmap_addr + mbi->mmap_length)) { if (mmap->len > largest_len) { /* Heuristics to determine a ensure a safe distance from kernel code. */ if (mmap->addr > safe_addr) { largest_len = mmap->len; largest_addr = mmap->addr; } else if ((safe_addr - mmap->addr) > mmap->len) { largest_len = mmap->len - ((unsigned long int)safe_addr - mmap->addr); largest_addr = safe_addr; } } mmap = (multiboot_memory_map_t*) ( (unsigned int)mmap + mmap->size + sizeof(unsigned int) ); } if (largest_len < MINIMUM_LEN) kpanic("mm_setup - Could not find any suitable memory map"); mm_init((void *)largest_addr, largest_len); }