void kmalloc_early_init(void) { TRACE(); lmm_init(&kheap_lmm); lmm_add_region(&kheap_lmm, &global_region, (size_t) 0, (size_t) -1, 0, 0); /* * Set up an initial heap starting at kheap_start. */ kheap_early_start = kheap_start; /* * The early heap size is limited by the amount of memory statically * mapped during boot. */ kheap_early_size = BOOT_PAGING_SIZE - (size_t) kheap_early_start; lmm_add_free(&kheap_lmm, kheap_early_start, kheap_early_size); ASSERT_LESSEQ(kmalloc_bytes_free(), kheap_early_size); kheap_used = 0; }
void mb_util_lmm (mbinfo_t *mbi, lmm_t *lmm) { vm_offset_t min; extern char _start[], end[]; /* Memory regions to skip. */ vm_offset_t cmdline_start_pa = mbi->flags & MULTIBOOT_CMDLINE ? mbi->cmdline : 0; vm_offset_t cmdline_end_pa = cmdline_start_pa ? cmdline_start_pa+strlen((char*)phystokv(cmdline_start_pa))+1 : 0; /* Initialize the base memory allocator according to the PC's physical memory regions. */ lmm_init(lmm); /* Do the x86 init dance to build our initial regions */ lmm_add_region(&malloc_lmm, ®1mb, (void*)phystokv(0x00000000), 0x00100000, LMMF_1MB | LMMF_16MB, LMM_PRI_1MB); lmm_add_region(&malloc_lmm, ®16mb, (void*)phystokv(0x00100000), 0x00f00000, LMMF_16MB, LMM_PRI_16MB); lmm_add_region(&malloc_lmm, ®high, (void*)phystokv(0x01000000), 0xfeffffff, 0, LMM_PRI_HIGH); /* Add to the free list all the memory the boot loader told us about, carefully avoiding the areas occupied by boot information. as well as our own executable code, data, and bss. Start at the end of the BIOS data area. */ min = 0x500; do { vm_offset_t max = 0xffffffff; /* Skip the I/O and ROM area. */ skip(mbi->mem_lower * 1024, 0x100000); /* Stop at the end of upper memory. */ skip(0x100000 + mbi->mem_upper * 1024, 0xffffffff); /* Skip our own text, data, and bss. */ skip(kvtophys(_start), kvtophys(end)); /* FIXME: temporary state of affairs */ extern char __kimg_start[]; skip(kvtophys(__kimg_start), kvtophys(end)); /* Skip the important stuff the bootloader passed to us. */ skip(cmdline_start_pa, cmdline_end_pa); if ((mbi->flags & MULTIBOOT_MODS) && (mbi->mods_count > 0)) { struct multiboot_module *m = (struct multiboot_module*) phystokv(mbi->mods_addr); unsigned i; skip(mbi->mods_addr, mbi->mods_addr + mbi->mods_count * sizeof(*m)); for (i = 0; i < mbi->mods_count; i++) { if (m[i].string != 0) { char *s = (char*)phystokv(m[i].string); unsigned len = strlen(s); skip(m[i].string, m[i].string+len+1); } skip(m[i].mod_start, m[i].mod_end); } } /* We actually found a contiguous memory block that doesn't conflict with anything else! Whew! Add it to the free list. */ lmm_add_free(&malloc_lmm, (void *) min, max - min); /* Continue searching just past the end of this region. */ min = max; /* The skip() macro jumps to this label to restart with a different (higher) min address. */ retry:; } while (min < 0xffffffff); }
int main() { char *some_memory[10]; /* an arrray of memory pointers */ int exit_code = EXIT_FAILURE; /* set up exit code for failure */ int i, j; /* Call to mtrace routines in glibc library */ #ifdef MTRACE mtrace(); /* Turn on mtrace function */ #endif lmm_init(); printf("Start of general malloc testing\n\n"); printf("Start allocating 10 1k pieces of memory\n"); for (i=0; i<10; i++) { some_memory[i] = (char *)malloc(ONE_K); printf ("Allocated a piece at 0x%x\n", (int)some_memory[i]); } printf("We have allocated some memory\n"); printf("Now lets free part some memory\n"); for (j=0; j<5; j++) { if (some_memory[j*2] != NULL) { free(some_memory[j*2]); free(some_memory[j*2]); printf ("Freed a piece at 0x%x\n", (int)some_memory[j*2]); exit_code = EXIT_SUCCESS ; } } printf("\n\nNow let us check other functions\n\n"); #ifndef DMALLOC printf("Read after the end of the allocated string\n"); post_read(); #endif printf("\nWrite after the end of the allocated string\n"); post_write(); printf ("\nRead before the allocated string\n"); read_before(); #ifndef MTRACE printf("\nWrite before allocated area\n"); write_before(); #endif lmm_dump_info(); printf("\nMissed memory free of allocated string\n"); miss_free(); #if !defined(MTRACE) && !defined(MEMWATCH) && !defined(DMALLOC) printf("\nUsing uninitialized memory\n"); uninit_mem(); #endif #ifdef MTRACE muntrace(); /* Turn off mtrace function */ #endif while(1); exit(exit_code); }