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