1106ba7b6f6e17d29e05d237bbc9f81469320bc9
[dragonfly.git] / share / doc / papers / malloc / intro.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/intro.ms,v 1.6 1999/08/28 00:18:10 peter Exp $
10 .\"
11 .ds RH Introduction
12 .NH
13 Introduction
14 .PP
15 Most programs need to allocate storage dynamically in addition
16 to whatever static storage the compiler reserved at compile-time.
17 To C programmers this fact is rather obvious, but for many years
18 this was not an accepted and recognized fact, and many languages 
19 still used today don't support this notion adequately.
20 .PP
21 The classic UNIX kernel provides two very simple and powerful
22 mechanisms for obtaining dynamic storage, the execution stack 
23 and the heap.
24 The stack is usually put at the far upper end of the address-space,
25 from where it grows down as far as needed, though this may depend on
26 the CPU design.
27 The heap starts at the end of the
28 .B bss
29 segment and grows upwards as needed.
30 .PP
31 There isn't really a kernel-interface to the stack as such.
32 The kernel will allocate some amount of memory for it,
33 not even telling the process the exact size.
34 If the process needs more space than that, it will simply try to access
35 it, hoping that the kernel will detect that access have been 
36 attempted outside the allocated memory, and try to extend it.
37 If the kernel fails to extend the stack, this could be because of lack
38 of resources or permissions or because it may just be impossible
39 to do in the first place, the process will usually be shot down by the 
40 kernel.
41 .PP
42 In the C language, there exists a little used interface to the stack,
43 .B alloca(3) ,
44 which will explicitly allocate space on the stack.
45 This is not a interface to the kernel, but merely an adjustment
46 done to the stack-pointer such that space will be available and
47 unharmed by any subroutine calls yet to be made while the context
48 of the current subroutine is intact.
49 .PP
50 Due to the nature of normal use of the stack, there is no corresponding
51 "free" operator, but instead the space is returned when the current
52 function returns to its caller and the stack frame is dismantled.
53 This is the cause of much grief, and probably the single most important
54 reason that alloca(3) is not, and should not be, used widely.
55 .PP
56 The heap on the other hand has an explicit kernel-interface in the 
57 system call
58 .B brk(2) .
59 The argument to brk(2) is a pointer to where the process wants the
60 heap to end.
61 There is also a interface called
62 .B sbrk(2)
63 taking an increment to the current end of the heap, but this is merely a
64 .B libc
65 front for brk(2).
66 .PP
67 In addition to these two memory resources, modern virtual memory kernels
68 provide the mmap(2)/mmunmap(2) interface which allows almost complete
69 control over any bit of virtual memory in the process address space.
70 .PP
71 Because of the generality of the mmap(2) interface and the way the 
72 data structures representing the regions are laid out, sbrk(2) is actually
73 faster in use than the equivalent mmap(2) call, simply because
74 mmap(2) has to search for information that is implicit in the sbrk(2) call.