Merge from vendor branch LIBSTDC++:
[dragonfly.git] / share / doc / papers / malloc / kernel.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/kernel.ms,v 1.7 1999/08/28 00:18:10 peter Exp $
10 .\" $DragonFly: src/share/doc/papers/malloc/kernel.ms,v 1.2 2003/06/17 04:36:56 dillon Exp $
11 .\"
12 .ds RH The kernel and memory
13 .NH
14 The kernel and memory
15 .PP
16 Brk(2) isn't a particularly convenient interface,
17 it was probably made more to fit the memory model of the 
18 hardware being used, than to fill the needs of the programmers.
19 .PP
20 Before paged and/or virtual memory systems became
21 common, the most popular memory management facility used for
22 UNIX was segments.
23 This was also very often the only vehicle for imposing protection on 
24 various parts of memory.
25 Depending on the hardware, segments can be anything, and consequently 
26 how the kernels exploited them varied a lot from UNIX to UNIX and from
27 machine to machine.
28 .PP
29 Typically a process would have one segment for the text section, one
30 for the data and bss section combined and one for the stack.
31 On some systems the text shared a segment with the data and bss, and was
32 consequently just as writable as them.
33 .PP
34 In this setup all the brk(2) system call has to do is to find the
35 right amount of free storage, possibly moving things around in physical
36 memory, maybe even swapping out a segment or two to make space,
37 and change the upper limit on the data segment according to the address given.
38 .PP
39 In a more modern page based virtual memory implementation this is still
40 pretty much the situation, except that the granularity is now pages:
41 The kernel finds the right number of free pages, possibly paging some
42 pages out to free them up, and then plugs them into the page-table of 
43 the process.
44 .PP
45 As such the difference is very small, the real difference is that in
46 the old world of swapping, either the entire process was in primary
47 storage or it wouldn't be selected to be run.  In a modern VM kernel,
48 a process might only have a subset of its pages in primary memory,
49 the rest will be paged in, if and when the process tries to access them.
50 .PP
51 Only very few programs deal with the brk(2) interface directly.
52 The few that do usually have their own memory management facilities.
53 LISP or FORTH interpreters are good examples.
54 Most other programs use the
55 .B malloc(3) 
56 interface instead, and leave it to the malloc implementation to 
57 use brk(2) to get storage allocated from the kernel.