c - Memory management in OS development -
i'm not sure if question on-topic (and apologize if it's not), wonder how memory management can accomplished when creating operating system. understanding is:
- the os provides memory management.
- any programming language (above assembly, e.g. c) needs already managed memory (for stack frames , heap allocations).
this sounds oxymoron. how can memory manager written if tool write needs memory manager in first place? must done in assembly?
c not require managed memory. thinking of malloc
library function, function (though standardised available user programs).
an easy implemented memory allocation scheme following:
char * free_space; void * kmalloc(size_t s) { char * block = free_space; free_space += s; return block; } // free not possible
the pointer free_space
must set during initialisation start of known free area of memory. might given boot loader through multiboot information.
a more complex example can found in this code kernel wrote long time ago.
generally memory management divided multiple phases:
during initialisation simple scheme 1 above helps setting more complex allocator 1 wrote.
this allocator provides blocks of fixed size (4kb usually, multiples of size).
these blocks requested higher level allocator when it's memory pool gets filled up. allocator 1 call via malloc
. prominent example doug lea's dlmalloc.
concerning stack: compiled code increment , decrement of stack pointer. has set available space before. kernel, during initialisation, set somewhere know free memory, example part of binary. done in assembly:
lea esp, kstack ; ... call startup_kernel ; ... [section .bss] resb 32768 kstack:
startup code in assembly aforementioned kernel
for processes later on allocate frame or more using in-kernel allocator , set stack point it's end (in case of decrementing stack). example another kernel shows done setup stack new process (this highly depended on actual kernel / task switch code). (in case there no dynamically allocated memory used in embedded scenario).
Comments
Post a Comment