Malloc()

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pamplemouse

Malloc()

Post by pamplemouse »

can anybody tell me how to rewrite malloc() free() realloc() so I can use them in a kernel?
ka3r

RE:Malloc()

Post by ka3r »

First of all, you must implement a kernel heap: if you reserve 256 MB for example you needn't allocate them, this is the role of the allocator.
You have several ways to code them:
- the easiest way is allocating pages on the heap for every allocation request: it is very fast, but if you allocate 2 bytes, your allocator will allocate 4096, causing a large waste of RAM.
- you can have buckets for every size of allocation (for example, 8 bytes, 16, 32, 64, 128, 256, 512, 1024, and so on) and pick the best fitting memory chunk

Also check out Doug Lea's dlmalloc, a fast and space-efficient heap allocator.
gaffi

RE:Malloc()

Post by gaffi »

There are some very usefull documents on Bonafide OS development.
http://osdev.neopages.net/tutorials.php        (Memory Management)

Hope this helps,
Daniel Raffler
Post Reply