Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / doc / papers / malloc / malloc.ms
1 .\"
2 .\" ----------------------------------------------------------------------------
3 .\" "THE BEER-WARE LICENSE" (Revision 42):
4 .\" <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5 .\" can do whatever you want with this stuff. If we meet some day, and you think
6 .\" this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 .\" ----------------------------------------------------------------------------
8 .\"
9 .\" $FreeBSD: src/share/doc/papers/malloc/malloc.ms,v 1.7 1999/08/28 00:18:10 peter Exp $
10 .\"
11 .ds RH Malloc and free
12 .NH
13 Malloc and free
14 .PP
15 The job of malloc(3) is to turn the rather simple
16 brk(2) facility into a service programs can
17 actually use without getting hurt.
18 .PP
19 The archetypical malloc(3) implementation keeps track of the memory between
20 the end of the bss section, as defined by the 
21 .B _end 
22 symbol, and the current brk(2) point using a linked list of chunks of memory.
23 Each item on the list has a status as either free or used, a pointer
24 to the next entry and in most cases to the previous as well, to speed
25 up inserts and deletes in the list.
26 .PP
27 When a malloc(3) request comes in, the list is traversed from the
28 front and if a free chunk big enough to hold the request is found,
29 it is returned, if the free chunk is bigger than the size requested,
30 a new free chunk is made from the excess and put back on the list.
31 .PP
32 When a chunk is 
33 .B free(3) 'ed,
34 the chunk is found in the list, its status
35 is changed to free and if one or both of the surrounding chunks
36 are free, they are collapsed to one.
37 .PP
38 A third kind of request, 
39 .B realloc(3) ,
40 will resize
41 a chunk, trying to avoid copying the contents if possible.
42 It is seldom used, and has only had a significant impact on performance
43 in a few special situations.
44 The typical pattern of use is to malloc(3) a chunk of the maximum size 
45 needed, read in the data and adjust the size of the chunk to match the
46 size of the data read using realloc(3).
47 .PP
48 For reasons of efficiency, the original implementation of malloc(3)
49 put the small structure used to contain the next and previous pointers
50 plus the state of the chunk right before the chunk itself.
51 .PP
52 As a matter of fact, the canonical malloc(3) implementation can be 
53 studied in the ``Old testament'', chapter 8 verse 7 [Kernighan & Ritchie]
54 .PP
55 Various optimisations can be applied to the above basic algorithm:
56 .IP
57 If in freeing a chunk, we end up with the last chunk on the list being
58 free, we can return that to the kernel by calling brk(2) with the first
59 address of that chunk and then make the previous chunk the last on the
60 chain by terminating its ``next'' pointer.
61 .IP
62 A best-fit algorithm can be used instead of first-fit at an expense 
63 of memory, because statistically fewer chances to brk(2) backwards will 
64 present themselves.
65 .IP
66 Splitting the list in two, one for used and one for free chunks, to
67 speed the searching.
68 .IP
69 Putting free chunks on one of several free lists, depending on their size,
70 to speed allocation.
71 .IP
72 \&...