Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/design/usermalloc.txt
734 views
1
User-level malloc
2
-----------------
3
4
The user-level malloc implementation is defined to be simple, not
5
fast or efficient. It uses a very basic first-fit block algorithm.
6
7
There's an 8-byte header which holds the offsets to the previous
8
and next blocks, a used/free bit, and some magic numbers (for
9
consistency checking) in the remaining available header bits. It also
10
allocates in units of 8 bytes to guarantee proper alignment of
11
doubles. (It also assumes its own headers are aligned on 8-byte
12
boundaries.)
13
14
On malloc(), it searches the entire heap starting at the beginning
15
for the first block big enough to hold the allocation. If it doesn't
16
find one, it calls sbrk() to get more memory. If it does find one, it
17
marks the block in use. It splits the remaining portion of the block
18
off as a new free block only if said portion is large enough to hold
19
both a header and some data.
20
21
On free(), it marks the block free and then tries to merge it with
22
the adjacent blocks (both above and below) if they're free.
23
24
That's about all there is to it.
25
26