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