core: dynamically size the heap
authorH. Peter Anvin <hpa@zytor.com>
Mon, 25 Apr 2011 23:37:24 +0000 (16:37 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Mon, 25 Apr 2011 23:37:24 +0000 (16:37 -0700)
Dynamically size the heap to 1/16 of available high memory.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
core/com32.inc
core/configinit.inc
core/extern.inc
core/fs/fs.c
core/init.inc
core/mem/init.c
core/ui.inc

index 111590c..ff18fde 100644 (file)
@@ -123,7 +123,7 @@ not_com32r_msg      db ': not a COM32R image', CR, LF, 0
                ;
                ; Danger, Will Robinson: it's not clear the use of
                ; core_xfer_buf is safe here.
-               global __entry_esp, __com32
+               global __entry_esp, __com32, HighMemSize
                alignz 4
 __entry_esp:
                dd 0                            ; Dummy to avoid _exit issues
index 915e77f..6320a0d 100644 (file)
@@ -20,8 +20,6 @@
                section .text16
 
 reset_config:
-               call highmemsize
-
                ; Initialize the .config section
                xor eax,eax
                mov si,__config_lma
@@ -45,7 +43,7 @@ mkkeymap:     stosb
                inc al
                loop mkkeymap
 
-               mov eax,[HighMemSize]
+               mov eax,[MallocStart]
                mov [VKernelEnd],eax
 
                ret
index 816659b..b40d849 100644 (file)
@@ -24,6 +24,9 @@
        ; newconfig.c
        extern pm_is_config_file
 
+       ; mem/init.c
+       extern MallocStart, mem_init
+
 %if IS_PXELINUX
        ; pxe.c
        extern unload_pxe, reset_pxe
index ad2fb37..5409986 100644 (file)
@@ -429,9 +429,6 @@ void fs_init(com32sys_t *regs)
     /* ops is a ptr list for several fs_ops */
     const struct fs_ops **ops = (const struct fs_ops **)regs->eax.l;
 
-    /* Initialize malloc() */
-    mem_init();
-
     /* Default name for the root directory */
     fs.cwd_name[0] = '/';
 
index e06ca96..97889ca 100644 (file)
@@ -29,6 +29,12 @@ common_init:
                jne kaboom
 
 ;
+; Initialize the malloc arena
+;
+               call highmemsize
+               pm_call mem_init
+
+;
 ; Initialize timer
 ;
                call timer_init
index 487bbb3..87e4cdd 100644 (file)
@@ -5,14 +5,16 @@
 
 struct free_arena_header __malloc_head[NHEAP];
 
-static __hugebss char main_heap[128 << 10];
 extern char __lowmem_heap[];
+size_t __bss16 MallocStart;
+extern size_t HighMemSize;
 
 void mem_init(void)
 {
     struct free_arena_header *fp;
     int i;
     uint16_t *bios_free_mem = (uint16_t *)0x413;
+    size_t main_heap_size;
 
     /* Initialize the head nodes */
 
@@ -24,10 +26,14 @@ void mem_init(void)
        fp++;
     }
 
-    /* Initialize the main heap */
-    fp = (struct free_arena_header *)main_heap;
+    /* Initialize the main heap; give it 1/16 of high memory */
+    main_heap_size = (HighMemSize - 0x100000) >> 4;
+    MallocStart    = (HighMemSize - main_heap_size) & ~4095;
+    main_heap_size = (HighMemSize - MallocStart) & ARENA_SIZE_MASK;
+
+    fp = (struct free_arena_header *)MallocStart;
     fp->a.attrs = ARENA_TYPE_USED | (HEAP_MAIN << ARENA_HEAP_POS);
-    ARENA_SIZE_SET(fp->a.attrs, sizeof main_heap);
+    ARENA_SIZE_SET(fp->a.attrs, main_heap_size);
     __inject_free_block(fp);
 
     /* Initialize the lowmem heap */
index 0a4bb56..36b6c6e 100644 (file)
@@ -154,7 +154,7 @@ display_labels:
                mov cx,di
                sub cx,command_line
                call crlf
-               mov esi,[HighMemSize]           ; Start from top of memory
+               mov esi,[MallocStart]           ; Start from top of memory
 .scan:
                cmp esi,[VKernelEnd]
                jbe .not_vk
@@ -360,7 +360,7 @@ clin_opt_ok:
 ; Now check if it is a "virtual kernel"
 ;
 vk_check:
-               mov esi,[HighMemSize]           ; Start from top of memory
+               mov esi,[MallocStart]           ; Start from top of memory
 .scan:
                cmp esi,[VKernelEnd]
                jbe .not_vk