Mercurial > hg > xemacs-beta
annotate src/mc-alloc.c @ 5238:2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
author | Marcus Crestani <crestani@informatik.uni-tuebingen.de> |
---|---|
date | Mon, 05 Jul 2010 18:17:39 +0200 |
parents | 9b8c2168d231 |
children | b249c479f9e1 308d34e9f07d |
rev | line source |
---|---|
2720 | 1 /* New size-based allocator for XEmacs. |
2 Copyright (C) 2005 Marcus Crestani. | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
3 Copyright (C) 2010 Ben Wing. |
2720 | 4 |
5 This file is part of XEmacs. | |
6 | |
7 XEmacs is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with XEmacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 Boston, MA 02111-1307, USA. */ | |
21 | |
22 /* Synched up with: Not in FSF. */ | |
23 | |
5238
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
24 /* |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
25 The New Allocator |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
26 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
27 The ideas and algorithms are based on the allocator of the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
28 Boehm-Demers-Weiser conservative garbage collector. See |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
29 http://www.hpl.hp.com/personal/Hans_ Boehm/gc/index.html. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
30 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
31 The new allocator is enabled when the new garbage collector |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
32 is enabled (with `--with-newgc'). The implementation of |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
33 the new garbage collector is in gc.c. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
34 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
35 The new allocator takes care of: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
36 - allocating objects in a write-barrier-friendly way |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
37 - manage object's mark bits |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
38 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
39 Three-Level Allocation |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
40 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
41 The new allocator efficiently manages the allocation of Lisp |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
42 objects by minimizing the number of times malloc() and free() are |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
43 called. The allocation process has three layers of abstraction: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
44 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
45 1. It allocates memory in very large chunks called heap sections. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
46 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
47 2. The heap sections are subdivided into pages. The page size is |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
48 determined by the constant PAGE_SIZE. It holds the size of a page |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
49 in bytes. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
50 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
51 3. One page consists of one or more cells. Each cell represents |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
52 a memory location for an object. The cells on one page all have |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
53 the same size, thus every page only contains equal-sized |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
54 objects. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
55 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
56 If an object is bigger than page size, it is allocated on a |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
57 multi-page. Then there is only one cell on a multi-page (the cell |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
58 covers the full multi-page). Is an object smaller than 1/2 PAGE_SIZE, |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
59 a page contains several objects and several cells. There |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
60 is only one cell on a page for object sizes from 1/2 PAGE_SIZE to |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
61 PAGE_SIZE (whereas multi-pages always contain 2 only one |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
62 cell). Only in layer one malloc() and free() are called. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
63 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
64 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
65 Size Classes and Page Lists |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
66 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
67 Meta-information about every page and multi-page is kept in a page |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
68 header. The page header contains some bookkeeping information like |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
69 number of used and free cells, and pointers to other page |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
70 headers. The page headers are linked in a page list. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
71 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
72 Every page list builds a size class. A size class contains all |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
73 pages (linked via page headers) for objects of the same size. The |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
74 new allocator does not group objects based on their type, it groups |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
75 objects based on their sizes. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
76 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
77 Here is an example: A cons contains a lrecord_header, a car and cdr |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
78 field. Altogether it uses 12 bytes of memory (on 32 bits |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
79 machines). All conses are allocated on pages with a cell size of 12 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
80 bytes. All theses pages are kept together in a page list, which |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
81 represents the size class for 12 bytes objects. But this size class |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
82 is not exclusively for conses only. Other objects, which are also |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
83 12 bytes big (e.g. weak-boxes), are allocated in the same size |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
84 class and on the same pages. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
85 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
86 The number of size classes is customizable, so is the size step |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
87 between successive size classes. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
88 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
89 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
90 Used and Unused Heap |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
91 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
92 The memory which is managed by the allocator can be divided in two |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
93 logical parts: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
94 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
95 The used heap contains pages, on which objects are allocated. These |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
96 pages are com- pletely or partially occupied. In the used heap, it |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
97 is important to quickly find a free spot for a new |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
98 object. Therefore the size classes of the used heap are defined by |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
99 the size of the cells on the pages. The size classes should match |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
100 common object sizes, to avoid wasting memory. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
101 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
102 The unused heap only contains completely empty pages. They have |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
103 never been used or have been freed completely again. In the unused |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
104 heap, the size of consecutive memory tips the scales. A page is the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
105 smallest entity which is asked for. Therefore, the size classes of |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
106 the unused heap are defined by the number of consecutive pages. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
107 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
108 The parameters for the different size classes can be adjusted |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
109 independently, see `configurable values' below. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
110 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
111 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
112 The Allocator's Data Structures |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
113 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
114 The struct `mc_allocator_globals holds' all the data structures |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
115 that the new allocator uses (lists of used and unused pages, mark |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
116 bits, etc.). |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
117 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
118 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
119 Mapping of Heap Pointers to Page Headers |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
120 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
121 For caching benefits, the page headers and mark bits are stored |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
122 separately from their associated page. During garbage collection |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
123 (i.e. for marking and freeing objects) it is important to identify |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
124 the page header which is responsible for a given Lisp object. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
125 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
126 To do this task quickly, I added a two level search tree: the upper |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
127 10 bits of the heap pointer are the index of the first level. This |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
128 entry of the first level links to the second level, where the next |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
129 10 bits of the heap pointer are used to identify the page |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
130 header. The remaining bits point to the object relative to the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
131 page. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
132 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
133 On architectures with more than 32 bits pointers, a hash value of |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
134 the upper bits is used to index into the first level. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
135 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
136 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
137 Mark Bits |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
138 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
139 For caching purposes, the mark bits are no longer kept within the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
140 objects, they are kept in a separate bit field. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
141 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
142 Every page header has a field for the mark bits of the objects on |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
143 the page. If there are less cells on the page than there fit bits |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
144 in the integral data type EMACS_INT, the mark bits are stored |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
145 directly in this EMACS_INT. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
146 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
147 Otherwise, the mark bits are written in a separate space, with the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
148 page header pointing to this space. This happens to pages with |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
149 rather small objects: many cells fit on a page, thus many mark bits |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
150 are needed. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
151 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
152 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
153 Allocate Memory |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
154 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
155 Use |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
156 void *mc_alloc (size_t size) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
157 to request memory from the allocator. This returns a pointer to a |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
158 newly allocated block of memory of given size. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
159 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
160 This is how the new allocator allocates memory: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
161 1. Determine the size class of the object. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
162 2. Is there already a page in this size class and is there a free |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
163 cell on this page? |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
164 * YES |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
165 3. Unlink free cell from free list, return address of free cell. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
166 DONE. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
167 * NO |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
168 3. Is there a page in the unused heap? |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
169 * YES |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
170 4. Move unused page to used heap. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
171 5. Initialize page header, free list, and mark bits. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
172 6. Unlink first cell from free list, return address of cell. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
173 DONE. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
174 * NO |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
175 4. Expand the heap, add new memory to unused heap |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
176 [go back to 3. and proceed with the YES case]. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
177 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
178 The allocator puts partially filled pages to the front of the page |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
179 list, completely filled ones to the end. That guarantees a fast |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
180 terminating search for free cells. Are there two successive full |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
181 pages at the front of the page list, the complete size class is |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
182 full, a new page has to be added. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
183 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
184 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
185 Expand Heap |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
186 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
187 To expand the heap, a big chunk of contiguous memory is allocated |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
188 using malloc(). These pieces are called heap sections. How big a new |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
189 heap section is (and thus the growth of the heap) is adjustable: See |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
190 MIN_HEAP_INCREASE, MAX_HEAP_INCREASE, and HEAP_GROWTH_DIVISOR below. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
191 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
192 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
193 Free Memory |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
194 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
195 One optimization in XEmacs is that locally used Lisp objects are |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
196 freed manually (the memory is not wasted till the next garbage |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
197 collection). Therefore the new allocator provides this function: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
198 void mc_free (void *ptr) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
199 That frees the object pointed to by ptr. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
200 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
201 This function is also used internally during sweep phase of the |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
202 garbage collection. This is how it works in detail: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
203 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
204 1. Use pointer to identify page header |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
205 (use lookup mechanism described above). |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
206 2. Mark cell as free and hook it into free list. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
207 3. Is the page completely empty? |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
208 * YES |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
209 4. Unlink page from page list. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
210 5. Remove page header, free list, and mark bits. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
211 6. Move page to unused heap. |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
212 * NO |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
213 4. Move page to front of size class (to speed up allocation |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
214 of objects). |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
215 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
216 If the last object of a page is freed, the empty page is returned to |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
217 the unused heap. The allocator tries to coalesce adjacent pages, to |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
218 gain a big piece of contiguous memory. The resulting chunk is hooked |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
219 into the according size class of the unused heap. If this created a |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
220 complete heap section, the heap section is returned to the operating |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
221 system by using free(). |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
222 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
223 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
224 Allocator and Garbage Collector |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
225 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
226 The new allocator simplifies the interface to the Garbage Collector: |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
227 * mark live objects: MARK_[WHITE|GREY|BLACK] (ptr) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
228 * sweep heap: EMACS_INT mc_sweep (void) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
229 * run finalizers: EMACS_INT mc_finalize (void) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
230 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
231 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
232 Allocator and Dumper |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
233 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
234 The new allocator provides special finalization for the portable |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
235 dumper (to save disk space): EMACS_INT mc_finalize_for_disksave (void) |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
236 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
237 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
238 More Information |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
239 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
240 More details can be found in |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
241 http://crestani.de/xemacs/pdf/mc-alloc.pdf . |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
242 |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
243 */ |
2cc24c69446c
Document the new allocator and the new garbage collector in gc.c and mc-alloc.c.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5216
diff
changeset
|
244 |
2720 | 245 #include <config.h> |
3092 | 246 |
2720 | 247 #include "lisp.h" |
248 #include "mc-alloc.h" | |
3092 | 249 #include "getpagesize.h" |
250 | |
251 | |
252 #if 0 | |
253 # define USE_MARK_BITS_FREE_LIST 1 | |
254 #endif | |
255 #if 1 | |
256 # define BLOCKTYPE_ALLOC_PAGE_HEADER 1 | |
257 #endif | |
258 | |
259 /* Memory protection needs the real system-dependent pagesize. */ | |
260 #ifndef WIN32_NATIVE | |
261 #include <unistd.h> /* for getpagesize () */ | |
262 #endif | |
263 #if defined (HAVE_GETPAGESIZE) | |
264 # define SYS_PAGE_SIZE getpagesize () | |
265 #elif defined (_SC_PAGESIZE) | |
266 # define SYS_PAGE_SIZE sysconf (_SC_PAGESIZE) | |
267 #elif defined (_SC_PAGE_SIZE) | |
268 # define SYS_PAGE_SIZE sysconf (_SC_PAGE_SIZE) | |
269 #elif defined(get_page_size) | |
270 # define SYS_PAGE_SIZE get_page_size () | |
271 #elif defined(PAGESIZE) | |
272 # define SYS_PAGE_SIZE PAGESIZE | |
273 #elif defined(PAGE_SIZE) | |
274 # define SYS_PAGE_SIZE PAGE_SIZE | |
275 #else | |
276 /* Valid page sizes are powers of 2. */ | |
277 # define SYS_PAGE_SIZE 4096 | |
278 #endif | |
2720 | 279 |
280 | |
281 /*--- configurable values ----------------------------------------------*/ | |
282 | |
283 /* Definition of size classes */ | |
284 | |
285 /* Heap used list constants: In the used heap, it is important to | |
286 quickly find a free spot for a new object. Therefore the size | |
287 classes of the used heap are defined by the size of the cells on | |
288 the pages. The size classes should match common object sizes, to | |
289 avoid wasting memory. */ | |
290 | |
291 /* Minimum object size in bytes. */ | |
3092 | 292 #if BITS_PER_EMACS_INT > 32 |
293 # define USED_LIST_MIN_OBJECT_SIZE 16 | |
294 #else | |
295 # define USED_LIST_MIN_OBJECT_SIZE 8 | |
296 #endif | |
2720 | 297 |
298 /* The step size by which the size classes increase (up to upper | |
299 threshold). This many bytes are mapped to a single used list: */ | |
3092 | 300 #if BITS_PER_EMACS_INT > 32 |
301 # define USED_LIST_LIN_STEP 8 | |
302 #else | |
303 # define USED_LIST_LIN_STEP 4 | |
304 #endif | |
2720 | 305 |
306 /* The upper threshold should always be set to PAGE_SIZE/2, because if | |
307 a object is larger than PAGE_SIZE/2 there is no room for any other | |
308 object on this page. Objects this big are kept in the page list of | |
309 the multiple pages, since a quick search for free spots is not | |
310 needed for this kind of pages (because there are no free spots). | |
311 PAGE_SIZES_DIV_2 defines maximum size of a used space list. */ | |
3092 | 312 #define USED_LIST_UPPER_THRESHOLD PAGE_SIZE_DIV_2 |
2720 | 313 |
314 | |
315 /* Heap free list constants: In the unused heap, the size of | |
316 consecutive memory tips the scales. A page is smallest entity which | |
317 is asked for. Therefore, the size classes of the unused heap are | |
318 defined by the number of consecutive pages. */ | |
319 /* Sizes up to this many pages each have their own free list. */ | |
320 #define FREE_LIST_LOWER_THRESHOLD 32 | |
321 /* The step size by which the size classes increase (up to upper | |
322 threshold). FREE_LIST_LIN_STEP number of sizes are mapped to a | |
323 single free list for sizes between FREE_LIST_LOWER_THRESHOLD and | |
324 FREE_LIST_UPPER_THRESHOLD. */ | |
325 #define FREE_LIST_LIN_STEP 8 | |
326 /* Sizes of at least this many pages are mapped to a single free | |
327 list. Blocks of memory larger than this number are all kept in a | |
328 single list, which makes searching this list slow. But objects that | |
329 big are really seldom. */ | |
330 #define FREE_LIST_UPPER_THRESHOLD 256 | |
331 | |
332 | |
3092 | 333 /* used heap list count */ |
334 #define N_USED_PAGE_LISTS (((USED_LIST_UPPER_THRESHOLD \ | |
335 - USED_LIST_MIN_OBJECT_SIZE) \ | |
336 / USED_LIST_LIN_STEP) + 1 ) + 1 | |
337 | |
338 /* free heap list count */ | |
339 #define N_FREE_PAGE_LISTS (((FREE_LIST_UPPER_THRESHOLD \ | |
340 - FREE_LIST_LOWER_THRESHOLD) \ | |
341 / FREE_LIST_LIN_STEP) \ | |
342 + FREE_LIST_LOWER_THRESHOLD) | |
343 | |
344 | |
2720 | 345 /* Maximum number of separately added heap sections. */ |
346 #if BITS_PER_EMACS_INT > 32 | |
347 # define MAX_HEAP_SECTS 2048 | |
348 #else | |
349 # define MAX_HEAP_SECTS 768 | |
350 #endif | |
351 | |
352 | |
353 /* Heap growth constants. Heap increases by any number between the | |
354 boundaries (unit is PAGE_SIZE). */ | |
3092 | 355 #define MIN_HEAP_INCREASE 256 |
2720 | 356 #define MAX_HEAP_INCREASE 256 /* not used */ |
357 | |
358 /* Every heap growth is calculated like this: | |
359 needed_pages + ( HEAP_SIZE / ( PAGE_SIZE * HEAP_GROWTH_DIVISOR )). | |
360 So the growth of the heap is influenced by the current size of the | |
361 heap, but kept between MIN_HEAP_INCREASE and MAX_HEAP_INCREASE | |
362 boundaries. | |
363 This reduces the number of heap sectors, the larger the heap grows | |
364 the larger are the newly allocated chunks. */ | |
365 #define HEAP_GROWTH_DIVISOR 3 | |
366 | |
367 | |
368 /* Zero memory before putting on free lists. */ | |
369 #define ZERO_MEM 1 | |
370 | |
371 | |
372 #ifndef CHAR_BIT /* should be included by limits.h */ | |
373 # define CHAR_BIT BITS_PER_CHAR | |
374 #endif | |
375 | |
376 | |
3092 | 377 |
378 /*--- values depending on PAGE_SIZE ------------------------------------*/ | |
2720 | 379 |
3092 | 380 /* initialized in init_mc_allocator () */ |
381 static EMACS_INT log_page_size; | |
382 static EMACS_INT page_size_div_2; | |
2720 | 383 |
3092 | 384 #undef PAGE_SIZE |
385 #define PAGE_SIZE SYS_PAGE_SIZE | |
386 #define LOG_PAGE_SIZE log_page_size | |
387 #define PAGE_SIZE_DIV_2 page_size_div_2 | |
2720 | 388 |
389 | |
390 /* Constants for heap address to page header mapping. */ | |
391 #define LOG_LEVEL2_SIZE 10 | |
392 #define LEVEL2_SIZE (1 << LOG_LEVEL2_SIZE) | |
393 #if BITS_PER_EMACS_INT > 32 | |
394 # define USE_HASH_TABLE 1 | |
395 # define LOG_LEVEL1_SIZE 11 | |
396 #else | |
397 # define LOG_LEVEL1_SIZE \ | |
398 (BITS_PER_EMACS_INT - LOG_LEVEL2_SIZE - LOG_PAGE_SIZE) | |
399 #endif | |
400 #define LEVEL1_SIZE (1 << LOG_LEVEL1_SIZE) | |
401 | |
402 #ifdef USE_HASH_TABLE | |
403 # define HASH(hi) ((hi) & (LEVEL1_SIZE - 1)) | |
4125 | 404 # define L1_INDEX(p) HASH ((EMACS_UINT) p >> (LOG_LEVEL2_SIZE + LOG_PAGE_SIZE)) |
2720 | 405 #else |
4125 | 406 # define L1_INDEX(p) ((EMACS_UINT) p >> (LOG_LEVEL2_SIZE + LOG_PAGE_SIZE)) |
2720 | 407 #endif |
4125 | 408 #define L2_INDEX(p) (((EMACS_UINT) p >> LOG_PAGE_SIZE) & (LEVEL2_SIZE - 1)) |
2720 | 409 |
410 | |
411 | |
412 | |
413 /*--- structs and typedefs ---------------------------------------------*/ | |
414 | |
3092 | 415 /* Links the free lists (mark_bit_free_list and cell free list). */ |
2720 | 416 typedef struct free_link |
417 { | |
418 struct lrecord_header lheader; | |
419 struct free_link *next_free; | |
420 } free_link; | |
421 | |
422 | |
3092 | 423 /* Header for pages. They are held in a doubly linked list. */ |
2720 | 424 typedef struct page_header |
425 { | |
426 struct page_header *next; /* next page_header */ | |
427 struct page_header *prev; /* previous page_header */ | |
428 /* Field plh holds pointer to the according header of the page list.*/ | |
429 struct page_list_header *plh; /* page list header */ | |
430 free_link *free_list; /* links free cells on page */ | |
431 EMACS_INT n_pages; /* number of pages */ | |
432 EMACS_INT cell_size; /* size of cells on page */ | |
433 EMACS_INT cells_on_page; /* total number of cells on page */ | |
434 EMACS_INT cells_used; /* number of used cells on page */ | |
435 /* If the number of objects on page is bigger than BITS_PER_EMACS_INT, | |
436 the mark bits are put in an extra memory area. Then the field | |
437 mark_bits holds the pointer to this area. Is the number of | |
438 objects smaller than BITS_PER_EMACS_INT, the mark bits are held in the | |
439 mark_bit EMACS_INT directly, without an additional indirection. */ | |
3092 | 440 unsigned int black_bit:1; /* objects on page are black */ |
441 unsigned int dirty_bit:1; /* page is dirty */ | |
442 unsigned int protection_bit:1; /* page is write protected */ | |
443 unsigned int array_bit:1; /* page holds arrays */ | |
444 Rawbyte *mark_bits; /* pointer to mark bits */ | |
2720 | 445 void *heap_space; /* pointer to heap, where objects |
446 are stored */ | |
447 } page_header; | |
448 | |
449 | |
450 /* Different list types. */ | |
451 enum list_type_enum { | |
452 USED_LIST, | |
453 FREE_LIST | |
454 }; | |
455 | |
456 | |
457 /* Header for page lists. Field list_type holds the type of the list. */ | |
458 typedef struct page_list_header | |
459 { | |
460 enum list_type_enum list_type; /* the type of the list */ | |
461 /* Size holds the size of one cell (in bytes) in a used heap list, or the | |
462 size of the heap sector (in number of pages). */ | |
463 size_t size; /* size of one cell / heap sector */ | |
464 page_header *first; /* first of page_header list */ | |
465 page_header *last; /* last of page_header list */ | |
466 /* If the number of objects on page is bigger than | |
467 BITS_PER_EMACS_INT, the mark bits are put in an extra memory | |
468 area, which is linked in this free list, if not used. Is the | |
469 number of objects smaller than BITS_PER_EMACS_INT, the mark bits | |
470 are hold in the mark bit EMACS_INT directly, without an | |
471 additional indirection. */ | |
472 free_link *mark_bit_free_list; | |
473 | |
474 #ifdef MEMORY_USAGE_STATS | |
475 EMACS_INT page_count; /* number if pages in list */ | |
476 EMACS_INT used_cells; /* number of objects in list */ | |
477 EMACS_INT used_space; /* used space */ | |
478 EMACS_INT total_cells; /* number of available cells */ | |
479 EMACS_INT total_space; /* available space */ | |
480 #endif | |
481 } page_list_header; | |
482 | |
483 | |
484 /* The heap sectors are stored with their real start pointer and their | |
485 real size. Not aligned to PAGE_SIZE. Needed for freeing heap sectors. */ | |
486 typedef struct heap_sect { | |
487 void *real_start; /* real start pointer (NOT aligned) */ | |
488 size_t real_size; /* NOT multiple of PAGE_SIZE */ | |
489 void *start; /* aligned start pointer */ | |
490 EMACS_INT n_pages; /* multiple of PAGE_SIZE */ | |
491 } heap_sect; | |
492 | |
493 | |
494 /* 2nd tree level for mapping of heap addresses to page headers. */ | |
495 typedef struct level_2_lookup_tree { | |
496 page_header *index[LEVEL2_SIZE]; /* link to page header */ | |
497 EMACS_INT key; /* high order address bits */ | |
498 #ifdef USE_HASH_TABLE | |
499 struct level_2_lookup_tree *hash_link; /* hash chain link */ | |
500 #endif | |
501 } level_2_lookup_tree; | |
502 | |
503 | |
504 | |
505 /*--- global variable definitions --------------------------------------*/ | |
506 | |
507 /* All global allocator variables are kept in this struct. */ | |
508 typedef struct mc_allocator_globals_type { | |
509 | |
510 /* heap size */ | |
511 EMACS_INT heap_size; | |
512 | |
513 /* list of all separatly allocated chunks of heap */ | |
514 heap_sect heap_sections[MAX_HEAP_SECTS]; | |
515 EMACS_INT n_heap_sections; | |
516 | |
517 /* Holds all allocated pages, each object size class in its separate list, | |
518 to guarantee fast allocation on partially filled pages. */ | |
3092 | 519 page_list_header *used_heap_pages; |
2720 | 520 |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
521 /* Holds all allocated pages that contain array elements. */ |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
522 page_list_header array_heap_pages; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
523 |
2720 | 524 /* Holds all free pages in the heap. N multiples of PAGE_SIZE are |
525 kept on the Nth free list. Contiguos pages are coalesced. */ | |
526 page_list_header free_heap_pages[N_FREE_PAGE_LISTS]; | |
527 | |
528 /* ptr lookup table */ | |
3092 | 529 level_2_lookup_tree **ptr_lookup_table; |
2720 | 530 |
3092 | 531 #ifndef BLOCKTYPE_ALLOC_PAGE_HEADER |
2720 | 532 /* page header free list */ |
533 free_link *page_header_free_list; | |
3092 | 534 #endif /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ |
2720 | 535 |
536 #ifdef MEMORY_USAGE_STATS | |
537 EMACS_INT malloced_bytes; | |
538 #endif | |
539 } mc_allocator_globals_type; | |
540 | |
541 mc_allocator_globals_type mc_allocator_globals; | |
542 | |
543 | |
544 | |
545 | |
546 /*--- macro accessors --------------------------------------------------*/ | |
547 | |
548 #define USED_HEAP_PAGES(i) \ | |
549 ((page_list_header*) &mc_allocator_globals.used_heap_pages[i]) | |
550 | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
551 #define ARRAY_HEAP_PAGES \ |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
552 ((page_list_header*) &mc_allocator_globals.array_heap_pages) |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
553 |
2720 | 554 #define FREE_HEAP_PAGES(i) \ |
555 ((page_list_header*) &mc_allocator_globals.free_heap_pages[i]) | |
556 | |
557 #define PLH(plh) plh | |
558 # define PLH_LIST_TYPE(plh) PLH (plh)->list_type | |
559 # define PLH_SIZE(plh) PLH (plh)->size | |
560 # define PLH_FIRST(plh) PLH (plh)->first | |
561 # define PLH_LAST(plh) PLH (plh)->last | |
562 # define PLH_MARK_BIT_FREE_LIST(plh) PLH (plh)->mark_bit_free_list | |
563 #ifdef MEMORY_USAGE_STATS | |
564 # define PLH_PAGE_COUNT(plh) PLH (plh)->page_count | |
565 # define PLH_USED_CELLS(plh) PLH (plh)->used_cells | |
566 # define PLH_USED_SPACE(plh) PLH (plh)->used_space | |
567 # define PLH_TOTAL_CELLS(plh) PLH (plh)->total_cells | |
568 # define PLH_TOTAL_SPACE(plh) PLH (plh)->total_space | |
569 #endif | |
570 | |
571 #define PH(ph) ph | |
572 # define PH_NEXT(ph) PH (ph)->next | |
573 # define PH_PREV(ph) PH (ph)->prev | |
574 # define PH_PLH(ph) PH (ph)->plh | |
575 # define PH_FREE_LIST(ph) PH (ph)->free_list | |
576 # define PH_N_PAGES(ph) PH (ph)->n_pages | |
577 # define PH_CELL_SIZE(ph) PH (ph)->cell_size | |
578 # define PH_CELLS_ON_PAGE(ph) PH (ph)->cells_on_page | |
579 # define PH_CELLS_USED(ph) PH (ph)->cells_used | |
3092 | 580 # define PH_BLACK_BIT(ph) PH (ph)->black_bit |
581 # define PH_DIRTY_BIT(ph) PH (ph)->dirty_bit | |
582 # define PH_PROTECTION_BIT(ph) PH (ph)->protection_bit | |
583 # define PH_ARRAY_BIT(ph) PH (ph)->array_bit | |
2720 | 584 # define PH_MARK_BITS(ph) PH (ph)->mark_bits |
585 # define PH_HEAP_SPACE(ph) PH (ph)->heap_space | |
586 #define PH_LIST_TYPE(ph) PLH_LIST_TYPE (PH_PLH (ph)) | |
587 #define PH_MARK_BIT_FREE_LIST(ph) PLH_MARK_BIT_FREE_LIST (PH_PLH (ph)) | |
588 | |
589 #define HEAP_SIZE mc_allocator_globals.heap_size | |
590 | |
591 #ifdef MEMORY_USAGE_STATS | |
592 # define MC_MALLOCED_BYTES mc_allocator_globals.malloced_bytes | |
593 #endif | |
594 | |
595 #define HEAP_SECTION(index) mc_allocator_globals.heap_sections[index] | |
596 #define N_HEAP_SECTIONS mc_allocator_globals.n_heap_sections | |
597 | |
3092 | 598 #ifndef BLOCKTYPE_ALLOC_PAGE_HEADER |
2720 | 599 #define PAGE_HEADER_FREE_LIST mc_allocator_globals.page_header_free_list |
3092 | 600 #endif /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ |
2720 | 601 |
602 #define NEXT_FREE(free_list) ((free_link*) free_list)->next_free | |
603 #define FREE_LIST(free_list) (free_link*) (free_list) | |
604 | |
605 #define PTR_LOOKUP_TABLE(i) mc_allocator_globals.ptr_lookup_table[i] | |
606 #define LEVEL2(l2, i) l2->index[i] | |
607 # define LEVEL2_KEY(l2) l2->key | |
608 #ifdef USE_HASH_TABLE | |
609 # define LEVEL2_HASH_LINK(l2) l2->hash_link | |
610 #endif | |
611 | |
612 #if ZERO_MEM | |
613 # define ZERO_HEAP_SPACE(ph) \ | |
614 memset (PH_HEAP_SPACE (ph), '\0', PH_N_PAGES (ph) * PAGE_SIZE) | |
615 # define ZERO_PAGE_HEADER(ph) memset (ph, '\0', sizeof (page_header)) | |
616 #endif | |
617 | |
618 #define div_PAGE_SIZE(x) (x >> LOG_PAGE_SIZE) | |
619 #define mult_PAGE_SIZE(x) (x << LOG_PAGE_SIZE) | |
620 | |
621 #define BYTES_TO_PAGES(bytes) (div_PAGE_SIZE ((bytes + (PAGE_SIZE - 1)))) | |
622 | |
623 #define PAGE_SIZE_ALIGNMENT(address) \ | |
4125 | 624 (void *) ((((EMACS_UINT) (address)) + PAGE_SIZE) & ~(PAGE_SIZE - 1)) |
2720 | 625 |
626 #define PH_ON_FREE_LIST_P(ph) \ | |
627 (ph && PH_PLH (ph) && (PLH_LIST_TYPE (PH_PLH (ph)) == FREE_LIST)) | |
628 | |
629 #define PH_ON_USED_LIST_P(ph) \ | |
630 (ph && PH_PLH (ph) && (PLH_LIST_TYPE (PH_PLH (ph)) == USED_LIST)) | |
631 | |
632 | |
3092 | 633 /* Number of mark bits: minimum 1, maximum 8. */ |
634 #define N_MARK_BITS 2 | |
2720 | 635 |
636 | |
637 | |
638 /************************************************************************/ | |
639 /* MC Allocator */ | |
640 /************************************************************************/ | |
641 | |
3305 | 642 /* Set to 1 if memory becomes short. */ |
643 EMACS_INT memory_shortage; | |
2720 | 644 |
645 /*--- misc functions ---------------------------------------------------*/ | |
646 | |
647 /* Visits all pages (page_headers) hooked into the used heap pages | |
648 list and executes f with the current page header as | |
3303 | 649 argument. Needed for sweep. Returns number of processed pages. */ |
650 static EMACS_INT | |
651 visit_all_used_page_headers (EMACS_INT (*f) (page_header *ph)) | |
2720 | 652 { |
3303 | 653 EMACS_INT number_of_pages_processed = 0; |
3092 | 654 EMACS_INT i; |
2720 | 655 for (i = 0; i < N_USED_PAGE_LISTS; i++) |
656 if (PLH_FIRST (USED_HEAP_PAGES (i))) | |
657 { | |
658 page_header *ph = PLH_FIRST (USED_HEAP_PAGES (i)); | |
659 while (PH_NEXT (ph)) | |
660 { | |
661 page_header *next = PH_NEXT (ph); /* in case f removes the page */ | |
3303 | 662 number_of_pages_processed += f (ph); |
2720 | 663 ph = next; |
664 } | |
3303 | 665 number_of_pages_processed += f (ph); |
2720 | 666 } |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
667 |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
668 if (PLH_FIRST (ARRAY_HEAP_PAGES)) |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
669 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
670 page_header *ph = PLH_FIRST (ARRAY_HEAP_PAGES); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
671 while (PH_NEXT (ph)) |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
672 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
673 page_header *next = PH_NEXT (ph); /* in case f removes the page */ |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
674 number_of_pages_processed += f (ph); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
675 ph = next; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
676 } |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
677 number_of_pages_processed += f (ph); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
678 } |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
679 |
3303 | 680 return number_of_pages_processed; |
2720 | 681 } |
682 | |
683 | |
684 | |
685 | |
686 /*--- mapping of heap addresses to page headers and mark bits ----------*/ | |
687 | |
688 /* Sets a heap pointer and page header pair into the lookup table. */ | |
689 static void | |
690 set_lookup_table (void *ptr, page_header *ph) | |
691 { | |
3092 | 692 EMACS_INT l1_index = L1_INDEX (ptr); |
2720 | 693 level_2_lookup_tree *l2 = PTR_LOOKUP_TABLE (l1_index); |
694 #ifdef USE_HASH_TABLE | |
695 while ((l2) && (LEVEL2_KEY (l2) != l1_index)) | |
696 l2 = LEVEL2_HASH_LINK (l2); | |
697 #endif | |
698 if (!l2) | |
699 { | |
700 l2 = (level_2_lookup_tree*) | |
701 xmalloc_and_zero (sizeof (level_2_lookup_tree)); | |
702 #ifdef MEMORY_USAGE_STATS | |
703 MC_MALLOCED_BYTES += | |
704 malloced_storage_size (0, sizeof (level_2_lookup_tree), 0); | |
705 #endif | |
2932 | 706 memset (l2, '\0', sizeof (level_2_lookup_tree)); |
2720 | 707 #ifdef USE_HASH_TABLE |
708 LEVEL2_HASH_LINK (l2) = PTR_LOOKUP_TABLE (l1_index); | |
709 #endif | |
710 PTR_LOOKUP_TABLE (l1_index) = l2; | |
711 LEVEL2_KEY (l2) = l1_index; | |
712 } | |
713 LEVEL2 (l2, L2_INDEX (ptr)) = ph; | |
714 } | |
715 | |
716 | |
717 #ifdef UNSET_LOOKUP_TABLE | |
718 /* Set the lookup table to 0 for given heap address. */ | |
719 static void | |
720 unset_lookup_table (void *ptr) | |
721 { | |
3092 | 722 EMACS_INT l1_index = L1_INDEX (ptr); |
2720 | 723 level_2_lookup_tree *l2 = PTR_LOOKUP_TABLE (l1_index); |
724 #ifdef USE_HASH_TABLE | |
725 while ((l2) && (LEVEL2_KEY (l2) != l1_index)) | |
726 l2 = LEVEL2_HASH_LINK (l2); | |
727 #endif | |
728 if (l2) { | |
729 LEVEL2 (l2, L2_INDEX (ptr)) = 0; | |
730 } | |
731 } | |
732 #endif | |
733 | |
734 /* Returns the page header of a given heap address, or 0 if not in table. | |
735 For internal use, no error checking. */ | |
736 static page_header * | |
737 get_page_header_internal (void *ptr) | |
738 { | |
3092 | 739 EMACS_INT l1_index = L1_INDEX (ptr); |
2720 | 740 level_2_lookup_tree *l2 = PTR_LOOKUP_TABLE (l1_index); |
741 #ifdef USE_HASH_TABLE | |
742 while ((l2) && (LEVEL2_KEY (l2) != l1_index)) | |
743 l2 = LEVEL2_HASH_LINK (l2); | |
744 #endif | |
745 if (!l2) | |
746 return 0; | |
747 return LEVEL2 (l2, L2_INDEX (ptr)); | |
748 } | |
749 | |
750 /* Returns the page header of a given heap address, or 0 if not in table. */ | |
751 static page_header * | |
752 get_page_header (void *ptr) | |
753 { | |
3092 | 754 EMACS_INT l1_index = L1_INDEX (ptr); |
2720 | 755 level_2_lookup_tree *l2 = PTR_LOOKUP_TABLE (l1_index); |
2723 | 756 assert (l2); |
2720 | 757 #ifdef USE_HASH_TABLE |
758 while ((l2) && (LEVEL2_KEY (l2) != l1_index)) | |
759 l2 = LEVEL2_HASH_LINK (l2); | |
760 #endif | |
2723 | 761 assert (LEVEL2 (l2, L2_INDEX (ptr))); |
2720 | 762 return LEVEL2 (l2, L2_INDEX (ptr)); |
763 } | |
764 | |
765 /* Returns the mark bit index of a given heap address. */ | |
766 static EMACS_INT | |
767 get_mark_bit_index (void *ptr, page_header *ph) | |
768 { | |
769 EMACS_INT cell_size = PH_CELL_SIZE (ph); | |
770 if (cell_size) | |
3092 | 771 return (((EMACS_INT) ptr - (EMACS_INT)(PH_HEAP_SPACE (ph))) / cell_size) |
772 * N_MARK_BITS; | |
2720 | 773 else /* only one object on page */ |
774 return 0; | |
775 } | |
776 | |
777 | |
778 /* Adds addresses of pages to lookup table. */ | |
779 static void | |
780 add_pages_to_lookup_table (page_header *ph, EMACS_INT n_pages) | |
781 { | |
3092 | 782 Rawbyte *p = (Rawbyte *) PH_HEAP_SPACE (ph); |
2720 | 783 EMACS_INT end_of_section = (EMACS_INT) p + (PAGE_SIZE * n_pages); |
3092 | 784 for (p = (Rawbyte *) PH_HEAP_SPACE (ph); |
2720 | 785 (EMACS_INT) p < end_of_section; p += PAGE_SIZE) |
786 set_lookup_table (p, ph); | |
787 } | |
788 | |
789 | |
790 /* Initializes lookup table. */ | |
791 static void | |
792 init_lookup_table (void) | |
793 { | |
3092 | 794 EMACS_INT i; |
2720 | 795 for (i = 0; i < LEVEL1_SIZE; i++) |
796 PTR_LOOKUP_TABLE (i) = 0; | |
797 } | |
798 | |
799 | |
800 | |
801 | |
802 /*--- mark bits --------------------------------------------------------*/ | |
803 | |
804 /*--- bit operations --- */ | |
805 | |
806 /* Allocates a bit array of length bits. */ | |
3092 | 807 static Rawbyte * |
2720 | 808 alloc_bit_array(size_t bits) |
809 { | |
3092 | 810 Rawbyte *bit_array; |
811 #ifdef USE_MARK_BITS_FREE_LIST | |
812 size_t size = ((bits + CHAR_BIT - 1) / CHAR_BIT) * sizeof (Rawbyte); | |
813 #else /* not USE_MARK_BITS_FREE_LIST */ | |
814 size_t size = | |
815 ALIGN_FOR_TYPE (((bits + CHAR_BIT - 1) / CHAR_BIT) * sizeof (Rawbyte), | |
816 Rawbyte *); | |
817 #endif /* not USE_MARK_BITS_FREE_LIST */ | |
2720 | 818 if (size < sizeof (free_link)) size = sizeof (free_link); |
819 #ifdef MEMORY_USAGE_STATS | |
820 MC_MALLOCED_BYTES += malloced_storage_size (0, size, 0); | |
821 #endif | |
3092 | 822 bit_array = (Rawbyte *) xmalloc_and_zero (size); |
2720 | 823 return bit_array; |
824 } | |
825 | |
826 | |
827 /* Returns the bit value at pos. */ | |
828 static EMACS_INT | |
3092 | 829 get_bit (Rawbyte *bit_array, EMACS_INT pos) |
2720 | 830 { |
831 #if N_MARK_BITS > 1 | |
832 EMACS_INT result = 0; | |
833 EMACS_INT i; | |
834 #endif | |
835 bit_array += pos / CHAR_BIT; | |
836 #if N_MARK_BITS > 1 | |
837 for (i = 0; i < N_MARK_BITS; i++) | |
3092 | 838 result |= ((*bit_array & (1 << ((pos + i) % CHAR_BIT))) != 0) << i; |
839 return result; | |
2720 | 840 #else |
841 return (*bit_array & (1 << (pos % CHAR_BIT))) != 0; | |
842 #endif | |
843 } | |
844 | |
845 | |
846 /* Bit_Arrays bit at pos to val. */ | |
847 static void | |
4125 | 848 set_bit (Rawbyte *bit_array, EMACS_INT pos, EMACS_UINT val) |
2720 | 849 { |
850 #if N_MARK_BITS > 1 | |
851 EMACS_INT i; | |
852 #endif | |
853 bit_array += pos / CHAR_BIT; | |
854 #if N_MARK_BITS > 1 | |
855 for (i = 0; i < N_MARK_BITS; i++) | |
856 if ((val >> i) & 1) | |
857 *bit_array |= 1 << ((pos + i) % CHAR_BIT); | |
858 else | |
859 *bit_array &= ~(1 << ((pos + i) % CHAR_BIT)); | |
860 #else | |
861 if (val) | |
862 *bit_array |= 1 << (pos % CHAR_BIT); | |
863 else | |
864 *bit_array &= ~(1 << (pos % CHAR_BIT)); | |
865 #endif | |
866 } | |
867 | |
868 | |
869 /*--- mark bit functions ---*/ | |
3092 | 870 #define USE_PNTR_MARK_BITS(ph) \ |
871 ((PH_CELLS_ON_PAGE (ph) * N_MARK_BITS) > BITS_PER_EMACS_INT) | |
872 #define USE_WORD_MARK_BITS(ph) \ | |
873 ((PH_CELLS_ON_PAGE (ph) * N_MARK_BITS) <= BITS_PER_EMACS_INT) | |
2720 | 874 |
3092 | 875 #define GET_BIT_WORD(b, p) get_bit ((Rawbyte *) &b, p) |
2720 | 876 #define GET_BIT_PNTR(b, p) get_bit (b, p) |
877 | |
3092 | 878 #define SET_BIT_WORD(b, p, v) set_bit ((Rawbyte *) &b, p, v) |
2720 | 879 #define SET_BIT_PNTR(b, p, v) set_bit (b, p, v) |
880 | |
881 #define ZERO_MARK_BITS_WORD(ph) PH_MARK_BITS (ph) = 0 | |
3092 | 882 #define ZERO_MARK_BITS_PNTR(ph) \ |
883 do { \ | |
884 memset (PH_MARK_BITS (ph), '\0', \ | |
885 ((PH_CELLS_ON_PAGE (ph) * N_MARK_BITS) \ | |
886 + CHAR_BIT - 1) / CHAR_BIT * sizeof (Rawbyte)); \ | |
2720 | 887 } while (0) |
888 | |
889 #define GET_BIT(bit, ph, p) \ | |
890 do { \ | |
891 if (USE_PNTR_MARK_BITS (ph)) \ | |
892 bit = GET_BIT_PNTR (PH_MARK_BITS (ph), p); \ | |
893 else \ | |
894 bit = GET_BIT_WORD (PH_MARK_BITS (ph), p); \ | |
895 } while (0) | |
896 | |
897 #define SET_BIT(ph, p, v) \ | |
898 do { \ | |
899 if (USE_PNTR_MARK_BITS (ph)) \ | |
900 SET_BIT_PNTR (PH_MARK_BITS (ph), p, v); \ | |
901 else \ | |
902 SET_BIT_WORD (PH_MARK_BITS (ph), p, v); \ | |
903 } while (0) | |
904 | |
905 #define ZERO_MARK_BITS(ph) \ | |
906 do { \ | |
907 if (USE_PNTR_MARK_BITS (ph)) \ | |
908 ZERO_MARK_BITS_PNTR (ph); \ | |
909 else \ | |
910 ZERO_MARK_BITS_WORD (ph); \ | |
911 } while (0) | |
912 | |
913 | |
914 /* Allocates mark-bit space either from a free list or from the OS | |
915 for the given page header. */ | |
3092 | 916 static Rawbyte * |
2720 | 917 alloc_mark_bits (page_header *ph) |
918 { | |
3092 | 919 Rawbyte *result; |
920 #ifdef USE_MARK_BITS_FREE_LIST | |
2720 | 921 if (PH_MARK_BIT_FREE_LIST (ph) == 0) |
3092 | 922 result = (Rawbyte *) alloc_bit_array (PH_CELLS_ON_PAGE (ph) * N_MARK_BITS); |
2720 | 923 else |
924 { | |
3092 | 925 result = (Rawbyte *) PH_MARK_BIT_FREE_LIST (ph); |
2720 | 926 PH_MARK_BIT_FREE_LIST (ph) = NEXT_FREE (result); |
927 } | |
3092 | 928 #else /* not USE_MARK_BITS_FREE_LIST */ |
929 result = (Rawbyte *) alloc_bit_array (PH_CELLS_ON_PAGE (ph) * N_MARK_BITS); | |
930 #endif /* not USE_MARK_BITS_FREE_LIST */ | |
2720 | 931 return result; |
932 } | |
933 | |
934 | |
935 /* Frees by maintaining a free list. */ | |
936 static void | |
937 free_mark_bits (page_header *ph) | |
938 { | |
3092 | 939 #ifdef USE_MARK_BITS_FREE_LIST |
940 NEXT_FREE (PH_MARK_BITS (ph)) = PH_MARK_BIT_FREE_LIST (ph); | |
941 PH_MARK_BIT_FREE_LIST (ph) = FREE_LIST (PH_MARK_BITS (ph)); | |
942 #else /* not USE_MARK_BITS_FREE_LIST */ | |
2720 | 943 if (PH_MARK_BITS (ph)) |
3092 | 944 free (PH_MARK_BITS (ph)); |
945 #endif /* not USE_MARK_BITS_FREE_LIST */ | |
2720 | 946 } |
947 | |
948 | |
949 /* Installs mark bits and zeros bits. */ | |
950 static void | |
951 install_mark_bits (page_header *ph) | |
952 { | |
953 if (USE_PNTR_MARK_BITS (ph)) | |
954 { | |
955 PH_MARK_BITS (ph) = alloc_mark_bits (ph); | |
956 ZERO_MARK_BITS_PNTR (ph); | |
957 } | |
958 else | |
959 ZERO_MARK_BITS_WORD (ph); | |
960 } | |
961 | |
962 | |
963 /* Cleans and frees the mark bits of the given page_header. */ | |
964 static void | |
965 remove_mark_bits (page_header *ph) | |
966 { | |
967 if (USE_PNTR_MARK_BITS (ph)) | |
968 free_mark_bits (ph); | |
969 } | |
970 | |
971 | |
972 /* Zeros all mark bits in given header. */ | |
973 static void | |
974 zero_mark_bits (page_header *ph) | |
975 { | |
976 ZERO_MARK_BITS (ph); | |
977 } | |
978 | |
979 | |
980 /* Returns mark bit for given heap pointer. */ | |
981 EMACS_INT | |
982 get_mark_bit (void *ptr) | |
983 { | |
984 EMACS_INT bit = 0; | |
985 page_header *ph = get_page_header (ptr); | |
986 gc_checking_assert (ph && PH_ON_USED_LIST_P (ph)); | |
987 if (ph) | |
988 { | |
989 GET_BIT (bit, ph, get_mark_bit_index (ptr, ph)); | |
990 } | |
991 return bit; | |
992 } | |
993 | |
994 | |
995 /* Sets mark bit for given heap pointer. */ | |
996 void | |
997 set_mark_bit (void *ptr, EMACS_INT value) | |
998 { | |
999 page_header *ph = get_page_header (ptr); | |
1000 assert (ph && PH_ON_USED_LIST_P (ph)); | |
1001 if (ph) | |
1002 { | |
3092 | 1003 if (value == BLACK) |
1004 if (!PH_BLACK_BIT (ph)) | |
1005 PH_BLACK_BIT (ph) = 1; | |
2720 | 1006 SET_BIT (ph, get_mark_bit_index (ptr, ph), value); |
1007 } | |
1008 } | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 /*--- page header functions --------------------------------------------*/ | |
1014 | |
3092 | 1015 #ifdef BLOCKTYPE_ALLOC_PAGE_HEADER |
1016 #include "blocktype.h" | |
1017 | |
1018 struct page_header_blocktype | |
1019 { | |
1020 Blocktype_declare (page_header); | |
1021 } *the_page_header_blocktype; | |
1022 #endif /* BLOCKTYPE_ALLOC_PAGE_HEADER */ | |
1023 | |
2720 | 1024 /* Allocates a page header either from a free list or from the OS. */ |
1025 static page_header * | |
1026 alloc_page_header (void) | |
1027 { | |
3092 | 1028 #ifdef BLOCKTYPE_ALLOC_PAGE_HEADER |
1029 page_header *result; | |
1030 #ifdef MEMORY_USAGE_STATS | |
1031 MC_MALLOCED_BYTES += malloced_storage_size (0, sizeof (page_header), 0); | |
1032 #endif | |
1033 result = Blocktype_alloc (the_page_header_blocktype); | |
1034 ZERO_PAGE_HEADER (result); | |
1035 return result; | |
1036 #else /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ | |
2720 | 1037 page_header *result; |
1038 if (PAGE_HEADER_FREE_LIST == 0) | |
1039 { | |
1040 result = | |
1041 (page_header *) xmalloc_and_zero ((EMACS_INT) (sizeof (page_header))); | |
1042 #ifdef MEMORY_USAGE_STATS | |
1043 MC_MALLOCED_BYTES += malloced_storage_size (0, sizeof (page_header), 0); | |
1044 #endif | |
1045 } | |
1046 else | |
1047 { | |
1048 result = (page_header*) PAGE_HEADER_FREE_LIST; | |
1049 PAGE_HEADER_FREE_LIST = NEXT_FREE (result); | |
1050 } | |
1051 return result; | |
3092 | 1052 #endif /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ |
2720 | 1053 } |
1054 | |
1055 | |
1056 /* Frees given page header by maintaining a free list. */ | |
1057 static void | |
1058 free_page_header (page_header *ph) | |
1059 { | |
3092 | 1060 #ifdef BLOCKTYPE_ALLOC_PAGE_HEADER |
1061 Blocktype_free (the_page_header_blocktype, ph); | |
1062 #else /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ | |
2720 | 1063 #if ZERO_MEM |
1064 ZERO_PAGE_HEADER (ph); | |
1065 #endif | |
1066 NEXT_FREE (ph) = PAGE_HEADER_FREE_LIST; | |
1067 PAGE_HEADER_FREE_LIST = FREE_LIST (ph); | |
3092 | 1068 #endif /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ |
2720 | 1069 } |
1070 | |
1071 | |
1072 /* Adds given page header to given page list header's list. */ | |
1073 static void | |
1074 add_page_header_to_plh (page_header *ph, page_list_header *plh) | |
1075 { | |
1076 /* insert at the front of the list */ | |
1077 PH_PREV (ph) = 0; | |
1078 PH_NEXT (ph) = PLH_FIRST (plh); | |
1079 PH_PLH (ph) = plh; | |
1080 /* if list is not empty, set prev in the first element */ | |
1081 if (PLH_FIRST (plh)) | |
1082 PH_PREV (PLH_FIRST (plh)) = ph; | |
1083 /* one element in list is first and last at the same time */ | |
1084 PLH_FIRST (plh) = ph; | |
1085 if (!PLH_LAST (plh)) | |
1086 PLH_LAST (plh) = ph; | |
1087 | |
1088 #ifdef MEMORY_USAGE_STATS | |
1089 /* bump page count */ | |
1090 PLH_PAGE_COUNT (plh)++; | |
1091 #endif | |
1092 | |
1093 } | |
1094 | |
1095 | |
1096 /* Removes given page header from given page list header's list. */ | |
1097 static void | |
1098 remove_page_header_from_plh (page_header *ph, page_list_header *plh) | |
1099 { | |
1100 if (PLH_FIRST (plh) == ph) | |
1101 PLH_FIRST (plh) = PH_NEXT (ph); | |
1102 if (PLH_LAST (plh) == ph) | |
1103 PLH_LAST (plh) = PH_PREV (ph); | |
1104 if (PH_NEXT (ph)) | |
1105 PH_PREV (PH_NEXT (ph)) = PH_PREV (ph); | |
1106 if (PH_PREV (ph)) | |
1107 PH_NEXT (PH_PREV (ph)) = PH_NEXT (ph); | |
1108 | |
1109 #ifdef MEMORY_USAGE_STATS | |
1110 /* decrease page count */ | |
1111 PLH_PAGE_COUNT (plh)--; | |
1112 #endif | |
1113 } | |
1114 | |
1115 | |
1116 /* Moves a page header to the front of its the page header list. | |
1117 This is used during sweep: Pages with some alive objects are moved to | |
1118 the front. This makes allocation faster, all pages with free slots | |
1119 can be found at the front of the list. */ | |
1120 static void | |
1121 move_page_header_to_front (page_header *ph) | |
1122 { | |
1123 page_list_header *plh = PH_PLH (ph); | |
1124 /* is page already first? */ | |
1125 if (ph == PLH_FIRST (plh)) return; | |
1126 /* remove from list */ | |
1127 if (PLH_LAST (plh) == ph) | |
1128 PLH_LAST (plh) = PH_PREV (ph); | |
1129 if (PH_NEXT (ph)) | |
1130 PH_PREV (PH_NEXT (ph)) = PH_PREV (ph); | |
1131 if (PH_PREV (ph)) | |
1132 PH_NEXT (PH_PREV (ph)) = PH_NEXT (ph); | |
1133 /* insert at the front */ | |
1134 PH_NEXT (ph) = PLH_FIRST (plh); | |
1135 PH_PREV (ph) = 0; | |
1136 PH_PREV (PH_NEXT (ph)) = ph; | |
1137 PLH_FIRST (plh) = ph; | |
1138 } | |
1139 | |
1140 | |
1141 | |
1142 | |
1143 /*--- page list functions ----------------------------------------------*/ | |
1144 | |
1145 /* Returns the index of the used heap list according to given size. */ | |
1146 static int | |
1147 get_used_list_index (size_t size) | |
1148 { | |
1149 if (size <= USED_LIST_MIN_OBJECT_SIZE) | |
3092 | 1150 { |
1151 // printf ("size %d -> index %d\n", size, 0); | |
1152 return 0; | |
1153 } | |
1154 if (size <= (size_t) USED_LIST_UPPER_THRESHOLD) | |
1155 { | |
1156 // printf ("size %d -> index %d\n", size, | |
1157 // ((size - USED_LIST_MIN_OBJECT_SIZE - 1) | |
1158 // / USED_LIST_LIN_STEP) + 1); | |
1159 return ((size - USED_LIST_MIN_OBJECT_SIZE - 1) | |
1160 / USED_LIST_LIN_STEP) + 1; | |
1161 } | |
1162 // printf ("size %d -> index %d\n", size, N_USED_PAGE_LISTS - 1); | |
2720 | 1163 return N_USED_PAGE_LISTS - 1; |
1164 } | |
1165 | |
1166 /* Returns the size of the used heap list according to given index. */ | |
1167 static size_t | |
1168 get_used_list_size_value (int used_index) | |
1169 { | |
1170 if (used_index < N_USED_PAGE_LISTS - 1) | |
1171 return (used_index * USED_LIST_LIN_STEP) + USED_LIST_MIN_OBJECT_SIZE; | |
1172 return 0; | |
1173 } | |
1174 | |
1175 | |
1176 /* Returns the index of the free heap list according to given size. */ | |
3092 | 1177 static EMACS_INT |
2720 | 1178 get_free_list_index (EMACS_INT n_pages) |
1179 { | |
1180 if (n_pages == 0) | |
1181 return 0; | |
1182 if (n_pages <= FREE_LIST_LOWER_THRESHOLD) | |
1183 return n_pages - 1; | |
1184 if (n_pages >= FREE_LIST_UPPER_THRESHOLD - 1) | |
1185 return N_FREE_PAGE_LISTS - 1; | |
1186 return ((n_pages - FREE_LIST_LOWER_THRESHOLD - 1) | |
1187 / FREE_LIST_LIN_STEP) + FREE_LIST_LOWER_THRESHOLD; | |
1188 | |
1189 } | |
1190 | |
1191 | |
1192 /* Returns the size in number of pages of the given free list at index. */ | |
1193 static size_t | |
3092 | 1194 get_free_list_size_value (EMACS_INT free_index) |
2720 | 1195 { |
1196 if (free_index < FREE_LIST_LOWER_THRESHOLD) | |
1197 return free_index + 1; | |
1198 if (free_index >= N_FREE_PAGE_LISTS) | |
1199 return FREE_LIST_UPPER_THRESHOLD; | |
1200 return ((free_index + 1 - FREE_LIST_LOWER_THRESHOLD) | |
1201 * FREE_LIST_LIN_STEP) + FREE_LIST_LOWER_THRESHOLD; | |
1202 } | |
1203 | |
1204 | |
1205 Bytecount | |
5157
1fae11d56ad2
redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents:
5146
diff
changeset
|
1206 mc_alloced_storage_size (Bytecount claimed_size, struct usage_stats *stats) |
2720 | 1207 { |
1208 size_t used_size = | |
1209 get_used_list_size_value (get_used_list_index (claimed_size)); | |
1210 if (used_size == 0) | |
1211 used_size = mult_PAGE_SIZE (BYTES_TO_PAGES (claimed_size)); | |
1212 | |
1213 if (stats) | |
1214 { | |
1215 stats->was_requested += claimed_size; | |
1216 stats->malloc_overhead += used_size - claimed_size; | |
1217 } | |
1218 | |
1219 return used_size; | |
1220 } | |
1221 | |
1222 | |
1223 | |
1224 /*--- free heap functions ----------------------------------------------*/ | |
1225 | |
1226 /* Frees a heap section, if the heap_section is completly free */ | |
1227 static EMACS_INT | |
1228 free_heap_section (page_header *ph) | |
1229 { | |
3092 | 1230 EMACS_INT i; |
1231 EMACS_INT removed = 0; | |
2720 | 1232 for (i = 0; i < N_HEAP_SECTIONS; i++) |
1233 if (!removed) | |
1234 { | |
1235 if ((PH_HEAP_SPACE (ph) == HEAP_SECTION(i).start) | |
1236 && (PH_N_PAGES (ph) == HEAP_SECTION(i).n_pages)) | |
1237 { | |
1238 xfree_1 (HEAP_SECTION(i).real_start); | |
1239 #ifdef MEMORY_USAGE_STATS | |
1240 MC_MALLOCED_BYTES | |
1241 -= malloced_storage_size (0, HEAP_SECTION(i).real_size, 0); | |
1242 #endif | |
1243 | |
1244 HEAP_SIZE -= PH_N_PAGES (ph) * PAGE_SIZE; | |
1245 | |
1246 removed = 1; | |
1247 } | |
1248 } | |
1249 else | |
1250 { | |
1251 HEAP_SECTION(i-1).real_start = HEAP_SECTION(i).real_start; | |
1252 HEAP_SECTION(i-1).real_size = HEAP_SECTION(i).real_size; | |
1253 HEAP_SECTION(i-1).start = HEAP_SECTION(i).start; | |
1254 HEAP_SECTION(i-1).n_pages = HEAP_SECTION(i).n_pages; | |
1255 } | |
1256 | |
1257 N_HEAP_SECTIONS = N_HEAP_SECTIONS - removed; | |
1258 | |
1259 return removed; | |
1260 } | |
1261 | |
1262 /* Removes page from free list. */ | |
1263 static void | |
1264 remove_page_from_free_list (page_header *ph) | |
1265 { | |
1266 remove_page_header_from_plh (ph, PH_PLH (ph)); | |
1267 PH_PLH (ph) = 0; | |
1268 } | |
1269 | |
1270 | |
1271 /* Adds page to according free list. */ | |
1272 static void | |
1273 add_page_to_free_list (page_header *ph) | |
1274 { | |
1275 PH_PLH (ph) = FREE_HEAP_PAGES (get_free_list_index (PH_N_PAGES (ph))); | |
1276 add_page_header_to_plh (ph, PH_PLH (ph)); | |
1277 } | |
1278 | |
1279 | |
1280 /* Merges two adjacent pages. */ | |
1281 static page_header * | |
1282 merge_pages (page_header *first_ph, page_header *second_ph) | |
1283 { | |
1284 /* merge */ | |
1285 PH_N_PAGES (first_ph) += PH_N_PAGES (second_ph); | |
1286 /* clean up left over page header */ | |
1287 free_page_header (second_ph); | |
1288 /* update lookup table */ | |
1289 add_pages_to_lookup_table (first_ph, PH_N_PAGES (first_ph)); | |
1290 | |
1291 return first_ph; | |
1292 } | |
1293 | |
1294 | |
1295 /* Checks if pages are adjacent, merges them, and adds merged page to | |
1296 free list */ | |
1297 static void | |
1298 merge_into_free_list (page_header *ph) | |
1299 { | |
1300 /* check if you can coalesce adjacent pages */ | |
1301 page_header *prev_ph = | |
1302 get_page_header_internal ((void*) (((EMACS_INT) PH_HEAP_SPACE (ph)) | |
1303 - PAGE_SIZE)); | |
1304 page_header *succ_ph = | |
1305 get_page_header_internal ((void*) (((EMACS_INT) PH_HEAP_SPACE (ph)) | |
1306 + (PH_N_PAGES (ph) * PAGE_SIZE))); | |
1307 if (PH_ON_FREE_LIST_P (prev_ph)) | |
1308 { | |
1309 remove_page_from_free_list (prev_ph); | |
1310 ph = merge_pages (prev_ph, ph); | |
1311 } | |
1312 if (PH_ON_FREE_LIST_P (succ_ph)) | |
1313 { | |
1314 remove_page_from_free_list (succ_ph); | |
1315 ph = merge_pages (ph, succ_ph); | |
1316 } | |
1317 /* try to free heap_section, if the section is complete */ | |
1318 if (!free_heap_section (ph)) | |
1319 /* else add merged page to free list */ | |
1320 add_page_to_free_list (ph); | |
1321 } | |
1322 | |
1323 | |
1324 /* Cuts given page header after n_pages, returns the first (cut) part, and | |
1325 puts the rest on the free list. */ | |
1326 static page_header * | |
1327 split_page (page_header *ph, EMACS_INT n_pages) | |
1328 { | |
1329 page_header *new_ph; | |
1330 EMACS_INT rem_pages = PH_N_PAGES (ph) - n_pages; | |
1331 | |
1332 /* remove the page from the free list if already hooked in */ | |
1333 if (PH_PLH (ph)) | |
1334 remove_page_from_free_list (ph); | |
1335 /* set new number of pages */ | |
1336 PH_N_PAGES (ph) = n_pages; | |
1337 /* add new page to lookup table */ | |
1338 add_pages_to_lookup_table (ph, n_pages); | |
1339 | |
1340 if (rem_pages) | |
1341 { | |
1342 /* build new page with reminder */ | |
1343 new_ph = alloc_page_header (); | |
1344 PH_N_PAGES (new_ph) = rem_pages; | |
1345 PH_HEAP_SPACE (new_ph) = | |
1346 (void*) ((EMACS_INT) (PH_HEAP_SPACE (ph)) + (n_pages * PAGE_SIZE)); | |
1347 /* add new page to lookup table */ | |
1348 add_pages_to_lookup_table (new_ph, rem_pages); | |
1349 /* hook the rest into free list */ | |
1350 add_page_to_free_list (new_ph); | |
1351 } | |
1352 return ph; | |
1353 } | |
1354 | |
1355 | |
1356 /* Expands the heap by given number of pages. */ | |
1357 static page_header * | |
1358 expand_heap (EMACS_INT needed_pages) | |
1359 { | |
1360 page_header *ph; | |
1361 EMACS_INT n_pages; | |
1362 size_t real_size; | |
1363 void *real_start; | |
1364 | |
1365 /* determine number of pages the heap should grow */ | |
3305 | 1366 if (memory_shortage) |
1367 n_pages = needed_pages; | |
1368 else | |
1369 n_pages = max (MIN_HEAP_INCREASE, | |
1370 needed_pages | |
1371 + (HEAP_SIZE / (PAGE_SIZE * HEAP_GROWTH_DIVISOR))); | |
2720 | 1372 |
1373 /* get the real values */ | |
1374 real_size = (n_pages * PAGE_SIZE) + PAGE_SIZE; | |
1375 real_start = xmalloc_and_zero (real_size); | |
1376 #ifdef MEMORY_USAGE_STATS | |
1377 MC_MALLOCED_BYTES += malloced_storage_size (0, real_size, 0); | |
1378 #endif | |
1379 | |
1380 /* maintain heap section count */ | |
1381 if (N_HEAP_SECTIONS >= MAX_HEAP_SECTS) | |
1382 { | |
1383 stderr_out ("Increase number of MAX_HEAP_SECTS"); | |
1384 ABORT (); | |
1385 } | |
1386 HEAP_SECTION(N_HEAP_SECTIONS).real_start = real_start; | |
1387 HEAP_SECTION(N_HEAP_SECTIONS).real_size = real_size; | |
1388 HEAP_SECTION(N_HEAP_SECTIONS).start = PAGE_SIZE_ALIGNMENT (real_start); | |
1389 HEAP_SECTION(N_HEAP_SECTIONS).n_pages = n_pages; | |
1390 N_HEAP_SECTIONS ++; | |
1391 | |
1392 /* get page header */ | |
1393 ph = alloc_page_header (); | |
1394 | |
1395 /* setup page header */ | |
1396 PH_N_PAGES (ph) = n_pages; | |
1397 PH_HEAP_SPACE (ph) = PAGE_SIZE_ALIGNMENT (real_start); | |
1398 assert (((EMACS_INT) (PH_HEAP_SPACE (ph)) % PAGE_SIZE) == 0); | |
1399 HEAP_SIZE += n_pages * PAGE_SIZE; | |
1400 | |
1401 /* this was also done by allocate_lisp_storage */ | |
1402 if (need_to_check_c_alloca) | |
1403 xemacs_c_alloca (0); | |
1404 | |
1405 /* return needed size, put rest on free list */ | |
1406 return split_page (ph, needed_pages); | |
1407 } | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 /*--- used heap functions ----------------------------------------------*/ | |
1413 /* Installs initial free list. */ | |
1414 static void | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1415 install_cell_free_list (page_header *ph) |
2720 | 1416 { |
3092 | 1417 Rawbyte *p; |
1418 EMACS_INT i; | |
2720 | 1419 EMACS_INT cell_size = PH_CELL_SIZE (ph); |
1420 /* write initial free list if cell_size is < PAGE_SIZE */ | |
3092 | 1421 p = (Rawbyte *) PH_HEAP_SPACE (ph); |
2720 | 1422 for (i = 0; i < PH_CELLS_ON_PAGE (ph) - 1; i++) |
1423 { | |
1424 #ifdef ERROR_CHECK_GC | |
1425 assert (!LRECORD_FREE_P (p)); | |
1426 MARK_LRECORD_AS_FREE (p); | |
1427 #endif | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1428 if (!PH_ARRAY_BIT (ph)) |
3092 | 1429 NEXT_FREE (p) = FREE_LIST (p + cell_size); |
2720 | 1430 set_lookup_table (p, ph); |
3092 | 1431 p += cell_size; |
2720 | 1432 } |
1433 #ifdef ERROR_CHECK_GC | |
1434 assert (!LRECORD_FREE_P (p)); | |
1435 MARK_LRECORD_AS_FREE (p); | |
1436 #endif | |
1437 NEXT_FREE (p) = 0; | |
1438 set_lookup_table (p, ph); | |
1439 | |
1440 /* hook free list into header */ | |
1441 PH_FREE_LIST (ph) = FREE_LIST (PH_HEAP_SPACE (ph)); | |
1442 } | |
1443 | |
1444 | |
1445 /* Cleans the object space of the given page_header. */ | |
1446 static void | |
1447 remove_cell_free_list (page_header *ph) | |
1448 { | |
1449 #if ZERO_MEM | |
1450 ZERO_HEAP_SPACE (ph); | |
1451 #endif | |
1452 PH_FREE_LIST (ph) = 0; | |
1453 } | |
1454 | |
1455 | |
1456 /* Installs a new page and hooks it into given page_list_header. */ | |
1457 static page_header * | |
1458 install_page_in_used_list (page_header *ph, page_list_header *plh, | |
3092 | 1459 size_t size, EMACS_INT elemcount) |
2720 | 1460 { |
1461 /* add to list */ | |
1462 add_page_header_to_plh (ph, plh); | |
1463 | |
1464 /* determine cell size */ | |
1465 if (PLH_SIZE (plh)) | |
1466 PH_CELL_SIZE (ph) = PLH_SIZE (plh); | |
1467 else | |
1468 PH_CELL_SIZE (ph) = size; | |
3092 | 1469 if (elemcount == 1) |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1470 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1471 PH_CELLS_ON_PAGE (ph) = (PAGE_SIZE * PH_N_PAGES (ph)) / PH_CELL_SIZE (ph); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1472 PH_ARRAY_BIT (ph) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1473 } |
3092 | 1474 else |
1475 { | |
1476 PH_CELLS_ON_PAGE (ph) = elemcount; | |
1477 PH_ARRAY_BIT (ph) = 1; | |
1478 } | |
2720 | 1479 |
1480 /* init cell count */ | |
1481 PH_CELLS_USED (ph) = 0; | |
1482 | |
1483 /* install mark bits and initialize cell free list */ | |
3092 | 1484 install_mark_bits (ph); |
2720 | 1485 |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1486 install_cell_free_list (ph); |
2720 | 1487 |
1488 #ifdef MEMORY_USAGE_STATS | |
1489 PLH_TOTAL_CELLS (plh) += PH_CELLS_ON_PAGE (ph); | |
1490 PLH_TOTAL_SPACE (plh) += PAGE_SIZE * PH_N_PAGES (ph); | |
1491 #endif | |
1492 | |
1493 return ph; | |
1494 } | |
1495 | |
1496 | |
1497 /* Cleans and frees a page, identified by the given page_header. */ | |
1498 static void | |
1499 remove_page_from_used_list (page_header *ph) | |
1500 { | |
1501 page_list_header *plh = PH_PLH (ph); | |
1502 | |
5050
6f2158fa75ed
Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
1503 assert (!(gc_in_progress && PH_PROTECTION_BIT (ph))); |
3092 | 1504 /* cleanup: remove memory protection, zero page_header bits. */ |
1505 | |
2720 | 1506 #ifdef MEMORY_USAGE_STATS |
1507 PLH_TOTAL_CELLS (plh) -= PH_CELLS_ON_PAGE (ph); | |
1508 PLH_TOTAL_SPACE (plh) -= PAGE_SIZE * PH_N_PAGES (ph); | |
1509 #endif | |
1510 | |
1511 /* clean up mark bits and cell free list */ | |
1512 remove_cell_free_list (ph); | |
1513 if (PH_ON_USED_LIST_P (ph)) | |
1514 remove_mark_bits (ph); | |
1515 | |
1516 /* clean up page header */ | |
1517 PH_CELL_SIZE (ph) = 0; | |
1518 PH_CELLS_ON_PAGE (ph) = 0; | |
1519 PH_CELLS_USED (ph) = 0; | |
1520 | |
1521 /* remove from used list */ | |
1522 remove_page_header_from_plh (ph, plh); | |
1523 | |
1524 /* move to free list */ | |
1525 merge_into_free_list (ph); | |
1526 } | |
1527 | |
1528 | |
1529 | |
1530 | |
1531 /*--- allocation -------------------------------------------------------*/ | |
1532 | |
1533 /* Allocates from cell free list on already allocated pages. */ | |
1534 static page_header * | |
1535 allocate_cell (page_list_header *plh) | |
1536 { | |
1537 page_header *ph = PLH_FIRST (plh); | |
1538 if (ph) | |
1539 { | |
1540 if (PH_FREE_LIST (ph)) | |
1541 /* elements free on first page */ | |
1542 return ph; | |
1543 else if ((PH_NEXT (ph)) | |
1544 && (PH_FREE_LIST (PH_NEXT (ph)))) | |
1545 /* elements free on second page */ | |
1546 { | |
1547 page_header *temp = PH_NEXT (ph); | |
1548 /* move full page (first page) to end of list */ | |
1549 PH_NEXT (PLH_LAST (plh)) = ph; | |
1550 PH_PREV (ph) = PLH_LAST (plh); | |
1551 PLH_LAST (plh) = ph; | |
1552 PH_NEXT (ph) = 0; | |
1553 /* install second page as first page */ | |
1554 ph = temp; | |
1555 PH_PREV (ph) = 0; | |
1556 PLH_FIRST (plh) = ph; | |
1557 return ph; | |
1558 } | |
1559 } | |
1560 return 0; | |
1561 } | |
1562 | |
1563 | |
1564 /* Finds a page which has at least the needed number of pages. | |
1565 Algorithm: FIRST FIT. */ | |
1566 static page_header * | |
1567 find_free_page_first_fit (EMACS_INT needed_pages, page_header *ph) | |
1568 { | |
1569 while (ph) | |
1570 { | |
1571 if (PH_N_PAGES (ph) >= needed_pages) | |
1572 return ph; | |
1573 ph = PH_NEXT (ph); | |
1574 } | |
1575 return 0; | |
1576 } | |
1577 | |
1578 | |
1579 /* Allocates a page from the free list. */ | |
1580 static page_header * | |
1581 allocate_page_from_free_list (EMACS_INT needed_pages) | |
1582 { | |
1583 page_header *ph = 0; | |
3092 | 1584 EMACS_INT i; |
2720 | 1585 for (i = get_free_list_index (needed_pages); i < N_FREE_PAGE_LISTS; i++) |
1586 if ((ph = find_free_page_first_fit (needed_pages, | |
1587 PLH_FIRST (FREE_HEAP_PAGES (i)))) != 0) | |
1588 { | |
1589 if (PH_N_PAGES (ph) > needed_pages) | |
1590 return split_page (ph, needed_pages); | |
1591 else | |
1592 { | |
1593 remove_page_from_free_list (ph); | |
1594 return ph; | |
1595 } | |
1596 } | |
1597 return 0; | |
1598 } | |
1599 | |
1600 | |
1601 /* Allocates a new page, either from free list or by expanding the heap. */ | |
1602 static page_header * | |
3092 | 1603 allocate_new_page (page_list_header *plh, size_t size, EMACS_INT elemcount) |
2720 | 1604 { |
3092 | 1605 EMACS_INT needed_pages = BYTES_TO_PAGES (size * elemcount); |
2720 | 1606 /* first check free list */ |
1607 page_header *result = allocate_page_from_free_list (needed_pages); | |
1608 if (!result) | |
1609 /* expand heap */ | |
1610 result = expand_heap (needed_pages); | |
3092 | 1611 install_page_in_used_list (result, plh, size, elemcount); |
2720 | 1612 return result; |
1613 } | |
1614 | |
1615 | |
1616 /* Selects the correct size class, tries to allocate a cell of this size | |
1617 from the free list, if this fails, a new page is allocated. */ | |
1618 static void * | |
3092 | 1619 mc_alloc_1 (size_t size, EMACS_INT elemcount) |
2720 | 1620 { |
1621 page_list_header *plh = 0; | |
2723 | 1622 page_header *ph = 0; |
1623 void *result = 0; | |
1624 | |
2720 | 1625 if (size == 0) |
1626 return 0; | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1627 |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1628 if (elemcount == 1) |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1629 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1630 plh = USED_HEAP_PAGES (get_used_list_index (size)); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1631 if (size < (size_t) USED_LIST_UPPER_THRESHOLD) |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1632 /* first check any free cells */ |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1633 ph = allocate_cell (plh); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1634 } |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1635 else |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1636 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1637 plh = ARRAY_HEAP_PAGES; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1638 } |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1639 |
2720 | 1640 if (!ph) |
1641 /* allocate a new page */ | |
3092 | 1642 ph = allocate_new_page (plh, size, elemcount); |
2720 | 1643 |
1644 /* return first element of free list and remove it from the list */ | |
1645 result = (void*) PH_FREE_LIST (ph); | |
1646 PH_FREE_LIST (ph) = | |
1647 NEXT_FREE (PH_FREE_LIST (ph)); | |
1648 | |
3092 | 1649 memset (result, '\0', (size * elemcount)); |
1650 MARK_LRECORD_AS_FREE (result); | |
2720 | 1651 |
1652 /* bump used cells counter */ | |
3092 | 1653 PH_CELLS_USED (ph) += elemcount; |
2720 | 1654 |
1655 #ifdef MEMORY_USAGE_STATS | |
3092 | 1656 PLH_USED_CELLS (plh) += elemcount; |
1657 PLH_USED_SPACE (plh) += size * elemcount; | |
2720 | 1658 #endif |
1659 | |
1660 return result; | |
1661 } | |
1662 | |
3092 | 1663 /* Array allocation. */ |
1664 void * | |
1665 mc_alloc_array (size_t size, EMACS_INT elemcount) | |
1666 { | |
1667 return mc_alloc_1 (size, elemcount); | |
1668 } | |
1669 | |
2720 | 1670 void * |
1671 mc_alloc (size_t size) | |
1672 { | |
1673 return mc_alloc_1 (size, 1); | |
1674 } | |
1675 | |
1676 | |
1677 | |
1678 /*--- sweep & free & finalize-------------------------------------------*/ | |
1679 | |
1680 /* Frees a heap pointer. */ | |
1681 static void | |
1682 remove_cell (void *ptr, page_header *ph) | |
1683 { | |
1684 #ifdef MEMORY_USAGE_STATS | |
1685 PLH_USED_CELLS (PH_PLH (ph))--; | |
1686 if (PH_ON_USED_LIST_P (ph)) | |
1687 PLH_USED_SPACE (PH_PLH (ph)) -= | |
1688 detagged_lisp_object_size ((const struct lrecord_header *) ptr); | |
1689 else | |
1690 PLH_USED_SPACE (PH_PLH (ph)) -= PH_CELL_SIZE (ph); | |
1691 #endif | |
2775 | 1692 if (PH_ON_USED_LIST_P (ph)) |
1693 { | |
2994 | 1694 #ifdef ALLOC_TYPE_STATS |
2775 | 1695 dec_lrecord_stats (PH_CELL_SIZE (ph), |
1696 (const struct lrecord_header *) ptr); | |
2994 | 1697 #endif /* ALLOC_TYPE_STATS */ |
2775 | 1698 #ifdef ERROR_CHECK_GC |
1699 assert (!LRECORD_FREE_P (ptr)); | |
1700 deadbeef_memory (ptr, PH_CELL_SIZE (ph)); | |
1701 MARK_LRECORD_AS_FREE (ptr); | |
2720 | 1702 #endif |
2775 | 1703 } |
2720 | 1704 |
1705 /* hooks cell into free list */ | |
1706 NEXT_FREE (ptr) = PH_FREE_LIST (ph); | |
1707 PH_FREE_LIST (ph) = FREE_LIST (ptr); | |
1708 /* decrease cells used */ | |
1709 PH_CELLS_USED (ph)--; | |
1710 } | |
1711 | |
1712 | |
1713 /* Mark free list marks all free list entries. */ | |
1714 static void | |
1715 mark_free_list (page_header *ph) | |
1716 { | |
1717 free_link *fl = PH_FREE_LIST (ph); | |
1718 while (fl) | |
1719 { | |
3092 | 1720 SET_BIT (ph, get_mark_bit_index (fl, ph), BLACK); |
2720 | 1721 fl = NEXT_FREE (fl); |
1722 } | |
1723 } | |
1724 | |
1725 | |
1726 /* Finalize a page for disksave. XEmacs calls this routine before it | |
1727 dumps the heap image. You have to tell mc-alloc how to call your | |
1728 object's finalizer for disksave. Therefore, you have to define the | |
1729 macro MC_ALLOC_CALL_FINALIZER_FOR_DISKSAVE(ptr). This macro should | |
1730 do nothing else then test if there is a finalizer and call it on | |
3303 | 1731 the given argument, which is the heap address of the object. |
1732 Returns number of processed pages. */ | |
1733 static EMACS_INT | |
2720 | 1734 finalize_page_for_disksave (page_header *ph) |
1735 { | |
1736 EMACS_INT heap_space = (EMACS_INT) PH_HEAP_SPACE (ph); | |
1737 EMACS_INT heap_space_step = PH_CELL_SIZE (ph); | |
1738 EMACS_INT mark_bit = 0; | |
1739 EMACS_INT mark_bit_max_index = PH_CELLS_ON_PAGE (ph); | |
1740 | |
1741 for (mark_bit = 0; mark_bit < mark_bit_max_index; mark_bit++) | |
1742 { | |
1743 EMACS_INT ptr = (heap_space + (heap_space_step * mark_bit)); | |
1744 MC_ALLOC_CALL_FINALIZER_FOR_DISKSAVE ((void *) ptr); | |
1745 } | |
3303 | 1746 return 1; |
2720 | 1747 } |
1748 | |
1749 | |
3303 | 1750 /* Finalizes the heap for disksave. Returns number of processed |
1751 pages. */ | |
1752 EMACS_INT | |
2720 | 1753 mc_finalize_for_disksave (void) |
1754 { | |
3303 | 1755 return visit_all_used_page_headers (finalize_page_for_disksave); |
2720 | 1756 } |
1757 | |
1758 | |
3303 | 1759 /* Sweeps a page: all the non-marked cells are freed. If the page is |
1760 empty in the end, it is removed. If some cells are free, it is | |
1761 moved to the front of its page header list. Full pages stay where | |
1762 they are. Returns number of processed pages.*/ | |
1763 static EMACS_INT | |
2720 | 1764 sweep_page (page_header *ph) |
1765 { | |
3092 | 1766 Rawbyte *heap_space = (Rawbyte *) PH_HEAP_SPACE (ph); |
2720 | 1767 EMACS_INT heap_space_step = PH_CELL_SIZE (ph); |
1768 EMACS_INT mark_bit = 0; | |
1769 EMACS_INT mark_bit_max_index = PH_CELLS_ON_PAGE (ph); | |
3092 | 1770 unsigned int bit = 0; |
2720 | 1771 |
1772 mark_free_list (ph); | |
1773 | |
3092 | 1774 /* ARRAY_BIT_HACK */ |
1775 if (PH_ARRAY_BIT (ph)) | |
1776 for (mark_bit = 0; mark_bit < mark_bit_max_index; mark_bit++) | |
1777 { | |
1778 GET_BIT (bit, ph, mark_bit * N_MARK_BITS); | |
1779 if (bit) | |
1780 { | |
1781 zero_mark_bits (ph); | |
1782 PH_BLACK_BIT (ph) = 0; | |
3303 | 1783 return 1; |
3092 | 1784 } |
1785 } | |
1786 | |
2720 | 1787 for (mark_bit = 0; mark_bit < mark_bit_max_index; mark_bit++) |
1788 { | |
3092 | 1789 GET_BIT (bit, ph, mark_bit * N_MARK_BITS); |
1790 if (bit == WHITE) | |
2720 | 1791 { |
3092 | 1792 GC_STAT_FREED; |
2720 | 1793 remove_cell (heap_space + (heap_space_step * mark_bit), ph); |
1794 } | |
1795 } | |
1796 zero_mark_bits (ph); | |
3092 | 1797 PH_BLACK_BIT (ph) = 0; |
2720 | 1798 if (PH_CELLS_USED (ph) == 0) |
1799 remove_page_from_used_list (ph); | |
1800 else if (PH_CELLS_USED (ph) < PH_CELLS_ON_PAGE (ph)) | |
1801 move_page_header_to_front (ph); | |
3303 | 1802 |
1803 return 1; | |
2720 | 1804 } |
1805 | |
1806 | |
3303 | 1807 /* Sweeps the heap. Returns number of processed pages. */ |
1808 EMACS_INT | |
2720 | 1809 mc_sweep (void) |
1810 { | |
3303 | 1811 return visit_all_used_page_headers (sweep_page); |
2720 | 1812 } |
1813 | |
1814 | |
1815 /* Changes the size of the cell pointed to by ptr. | |
1816 Returns the new address of the new cell with new size. */ | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
1817 static void * |
3092 | 1818 mc_realloc_1 (void *ptr, size_t size, int elemcount) |
2720 | 1819 { |
1820 if (ptr) | |
1821 { | |
3092 | 1822 if (size * elemcount) |
2720 | 1823 { |
3092 | 1824 void *result = mc_alloc_1 (size, elemcount); |
2720 | 1825 size_t from_size = PH_CELL_SIZE (get_page_header (ptr)); |
3092 | 1826 size_t cpy_size = size * elemcount; |
1827 if (cpy_size > from_size) | |
2720 | 1828 cpy_size = from_size; |
1829 memcpy (result, ptr, cpy_size); | |
3092 | 1830 #ifdef ALLOC_TYPE_STATS |
1831 inc_lrecord_stats (size, (struct lrecord_header *) result); | |
1832 #endif /* not ALLOC_TYPE_STATS */ | |
2720 | 1833 return result; |
1834 } | |
1835 else | |
1836 { | |
1837 return 0; | |
1838 } | |
1839 } | |
1840 else | |
3092 | 1841 return mc_alloc_1 (size, elemcount); |
2720 | 1842 } |
1843 | |
1844 void * | |
1845 mc_realloc (void *ptr, size_t size) | |
1846 { | |
1847 return mc_realloc_1 (ptr, size, 1); | |
1848 } | |
1849 | |
1850 void * | |
3092 | 1851 mc_realloc_array (void *ptr, size_t size, EMACS_INT elemcount) |
2720 | 1852 { |
3092 | 1853 return mc_realloc_1 (ptr, size, elemcount); |
2720 | 1854 } |
1855 | |
1856 | |
1857 | |
1858 /*--- initialization ---------------------------------------------------*/ | |
1859 | |
1860 /* Call once at the very beginning. */ | |
1861 void | |
1862 init_mc_allocator (void) | |
1863 { | |
3092 | 1864 EMACS_INT i; |
1865 | |
1866 #ifdef MEMORY_USAGE_STATS | |
1867 MC_MALLOCED_BYTES = 0; | |
1868 #endif | |
2720 | 1869 |
3092 | 1870 /* init of pagesize dependent values */ |
1871 switch (SYS_PAGE_SIZE) | |
1872 { | |
1873 case 512: log_page_size = 9; break; | |
1874 case 1024: log_page_size = 10; break; | |
1875 case 2048: log_page_size = 11; break; | |
1876 case 4096: log_page_size = 12; break; | |
1877 case 8192: log_page_size = 13; break; | |
1878 case 16384: log_page_size = 14; break; | |
3212 | 1879 case 32768: log_page_size = 15; break; |
1880 case 65536: log_page_size = 16; break; | |
1881 default: | |
1882 fprintf(stderr, "##### SYS_PAGE_SIZE=%d not supported #####\n", | |
1883 SYS_PAGE_SIZE); | |
1884 ABORT (); | |
3092 | 1885 } |
1886 | |
4125 | 1887 page_size_div_2 = (EMACS_UINT) SYS_PAGE_SIZE >> 1; |
3092 | 1888 |
1889 mc_allocator_globals.used_heap_pages = | |
1890 (page_list_header *) xmalloc_and_zero ((N_USED_PAGE_LISTS + 1) | |
1891 * sizeof (page_list_header)); | |
1892 #ifdef MEMORY_USAGE_STATS | |
1893 MC_MALLOCED_BYTES += (N_USED_PAGE_LISTS + 1) * sizeof (page_list_header); | |
1894 #endif | |
1895 | |
1896 mc_allocator_globals.ptr_lookup_table = | |
1897 (level_2_lookup_tree **) | |
1898 xmalloc_and_zero ((LEVEL1_SIZE + 1) * sizeof (level_2_lookup_tree *)); | |
1899 #ifdef MEMORY_USAGE_STATS | |
1900 MC_MALLOCED_BYTES += (LEVEL1_SIZE + 1) * sizeof (level_2_lookup_tree *); | |
1901 #endif | |
1902 | |
1903 #ifdef BLOCKTYPE_ALLOC_PAGE_HEADER | |
1904 the_page_header_blocktype = Blocktype_new (struct page_header_blocktype); | |
1905 #endif /* BLOCKTYPE_ALLOC_PAGE_HEADER */ | |
2932 | 1906 |
2720 | 1907 for (i = 0; i < N_USED_PAGE_LISTS; i++) |
1908 { | |
1909 page_list_header *plh = USED_HEAP_PAGES (i); | |
1910 PLH_LIST_TYPE (plh) = USED_LIST; | |
1911 PLH_SIZE (plh) = get_used_list_size_value (i); | |
1912 PLH_FIRST (plh) = 0; | |
1913 PLH_LAST (plh) = 0; | |
1914 PLH_MARK_BIT_FREE_LIST (plh) = 0; | |
1915 #ifdef MEMORY_USAGE_STATS | |
1916 PLH_PAGE_COUNT (plh) = 0; | |
1917 PLH_USED_CELLS (plh) = 0; | |
1918 PLH_USED_SPACE (plh) = 0; | |
1919 PLH_TOTAL_CELLS (plh) = 0; | |
1920 PLH_TOTAL_SPACE (plh) = 0; | |
1921 #endif | |
1922 } | |
1923 | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1924 { |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1925 page_list_header *plh = ARRAY_HEAP_PAGES; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1926 PLH_LIST_TYPE (plh) = USED_LIST; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1927 PLH_SIZE (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1928 PLH_FIRST (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1929 PLH_LAST (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1930 PLH_MARK_BIT_FREE_LIST (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1931 #ifdef MEMORY_USAGE_STATS |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1932 PLH_PAGE_COUNT (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1933 PLH_USED_CELLS (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1934 PLH_USED_SPACE (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1935 PLH_TOTAL_CELLS (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1936 PLH_TOTAL_SPACE (plh) = 0; |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1937 #endif |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1938 } |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
1939 |
2720 | 1940 for (i = 0; i < N_FREE_PAGE_LISTS; i++) |
1941 { | |
1942 page_list_header *plh = FREE_HEAP_PAGES (i); | |
1943 PLH_LIST_TYPE (plh) = FREE_LIST; | |
1944 PLH_SIZE (plh) = get_free_list_size_value (i); | |
1945 PLH_FIRST (plh) = 0; | |
1946 PLH_LAST (plh) = 0; | |
1947 PLH_MARK_BIT_FREE_LIST (plh) = 0; | |
1948 #ifdef MEMORY_USAGE_STATS | |
1949 PLH_PAGE_COUNT (plh) = 0; | |
1950 PLH_USED_CELLS (plh) = 0; | |
1951 PLH_USED_SPACE (plh) = 0; | |
1952 PLH_TOTAL_CELLS (plh) = 0; | |
1953 PLH_TOTAL_SPACE (plh) = 0; | |
1954 #endif | |
1955 } | |
1956 | |
3092 | 1957 #ifndef BLOCKTYPE_ALLOC_PAGE_HEADER |
2720 | 1958 PAGE_HEADER_FREE_LIST = 0; |
3092 | 1959 #endif /* not BLOCKTYPE_ALLOC_PAGE_HEADER */ |
2720 | 1960 |
1961 #ifdef MEMORY_USAGE_STATS | |
3092 | 1962 MC_MALLOCED_BYTES += sizeof (mc_allocator_globals); |
2720 | 1963 #endif |
1964 | |
1965 init_lookup_table (); | |
1966 } | |
1967 | |
1968 | |
1969 | |
1970 | |
1971 /*--- lisp function for statistics -------------------------------------*/ | |
1972 | |
1973 #ifdef MEMORY_USAGE_STATS | |
1974 DEFUN ("mc-alloc-memory-usage", Fmc_alloc_memory_usage, 0, 0, 0, /* | |
1975 Returns stats about the mc-alloc memory usage. See diagnose.el. | |
1976 */ | |
1977 ()) | |
1978 { | |
1979 Lisp_Object free_plhs = Qnil; | |
1980 Lisp_Object used_plhs = Qnil; | |
1981 Lisp_Object heap_sects = Qnil; | |
3092 | 1982 EMACS_INT used_size = 0; |
1983 EMACS_INT real_size = 0; | |
2720 | 1984 |
3092 | 1985 EMACS_INT i; |
2720 | 1986 |
1987 for (i = 0; i < N_FREE_PAGE_LISTS; i++) | |
1988 if (PLH_PAGE_COUNT (FREE_HEAP_PAGES(i)) > 0) | |
1989 free_plhs = | |
1990 acons (make_int (PLH_SIZE (FREE_HEAP_PAGES(i))), | |
1991 list1 (make_int (PLH_PAGE_COUNT (FREE_HEAP_PAGES(i)))), | |
1992 free_plhs); | |
1993 | |
1994 for (i = 0; i < N_USED_PAGE_LISTS; i++) | |
1995 if (PLH_PAGE_COUNT (USED_HEAP_PAGES(i)) > 0) | |
1996 used_plhs = | |
1997 acons (make_int (PLH_SIZE (USED_HEAP_PAGES(i))), | |
1998 list5 (make_int (PLH_PAGE_COUNT (USED_HEAP_PAGES(i))), | |
1999 make_int (PLH_USED_CELLS (USED_HEAP_PAGES(i))), | |
2000 make_int (PLH_USED_SPACE (USED_HEAP_PAGES(i))), | |
2001 make_int (PLH_TOTAL_CELLS (USED_HEAP_PAGES(i))), | |
2002 make_int (PLH_TOTAL_SPACE (USED_HEAP_PAGES(i)))), | |
2003 used_plhs); | |
2004 | |
5216
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2005 used_plhs = |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2006 acons (make_int (0), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2007 list5 (make_int (PLH_PAGE_COUNT(ARRAY_HEAP_PAGES)), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2008 make_int (PLH_USED_CELLS (ARRAY_HEAP_PAGES)), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2009 make_int (PLH_USED_SPACE (ARRAY_HEAP_PAGES)), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2010 make_int (PLH_TOTAL_CELLS (ARRAY_HEAP_PAGES)), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2011 make_int (PLH_TOTAL_SPACE (ARRAY_HEAP_PAGES))), |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2012 used_plhs); |
9b8c2168d231
Allocate lrecord arrays in own size class.
Marcus Crestani <crestani@informatik.uni-tuebingen.de>
parents:
5167
diff
changeset
|
2013 |
2720 | 2014 for (i = 0; i < N_HEAP_SECTIONS; i++) { |
2015 used_size += HEAP_SECTION(i).n_pages * PAGE_SIZE; | |
2016 real_size += | |
2017 malloced_storage_size (0, HEAP_SECTION(i).real_size, 0); | |
2018 } | |
2019 | |
2020 heap_sects = | |
2021 list3 (make_int (N_HEAP_SECTIONS), | |
2022 make_int (used_size), | |
2023 make_int (real_size)); | |
2024 | |
2025 return Fcons (make_int (PAGE_SIZE), | |
3092 | 2026 list5 (heap_sects, |
2720 | 2027 Fnreverse (used_plhs), |
2028 Fnreverse (free_plhs), | |
2029 make_int (sizeof (mc_allocator_globals)), | |
2030 make_int (MC_MALLOCED_BYTES))); | |
2031 } | |
2032 #endif /* MEMORY_USAGE_STATS */ | |
2033 | |
2034 void | |
2035 syms_of_mc_alloc (void) | |
2036 { | |
2037 #ifdef MEMORY_USAGE_STATS | |
2038 DEFSUBR (Fmc_alloc_memory_usage); | |
2039 #endif /* MEMORY_USAGE_STATS */ | |
2040 } | |
3092 | 2041 |
2042 | |
2043 /*--- incremental garbage collector ----------------------------------*/ | |
2044 | |
5054 | 2045 #if 0 /* currently unused */ |
2046 | |
3092 | 2047 /* access dirty bit of page header */ |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2048 static void |
3092 | 2049 set_dirty_bit (page_header *ph, unsigned int value) |
2050 { | |
2051 PH_DIRTY_BIT (ph) = value; | |
2052 } | |
2053 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2054 static void |
3092 | 2055 set_dirty_bit_for_address (void *ptr, unsigned int value) |
2056 { | |
2057 set_dirty_bit (get_page_header (ptr), value); | |
2058 } | |
2059 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2060 static unsigned int |
3092 | 2061 get_dirty_bit (page_header *ph) |
2062 { | |
2063 return PH_DIRTY_BIT (ph); | |
2064 } | |
2065 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2066 static unsigned int |
3092 | 2067 get_dirty_bit_for_address (void *ptr) |
2068 { | |
2069 return get_dirty_bit (get_page_header (ptr)); | |
2070 } | |
2071 | |
2072 | |
2073 /* access protection bit of page header */ | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2074 static void |
3092 | 2075 set_protection_bit (page_header *ph, unsigned int value) |
2076 { | |
2077 PH_PROTECTION_BIT (ph) = value; | |
2078 } | |
2079 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2080 static void |
3092 | 2081 set_protection_bit_for_address (void *ptr, unsigned int value) |
2082 { | |
2083 set_protection_bit (get_page_header (ptr), value); | |
2084 } | |
2085 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2086 static unsigned int |
3092 | 2087 get_protection_bit (page_header *ph) |
2088 { | |
2089 return PH_PROTECTION_BIT (ph); | |
2090 } | |
2091 | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2092 static unsigned int |
3092 | 2093 get_protection_bit_for_address (void *ptr) |
2094 { | |
2095 return get_protection_bit (get_page_header (ptr)); | |
2096 } | |
2097 | |
2098 | |
2099 /* Returns the start of the page of the object pointed to by ptr. */ | |
5042
f395ee7ad844
Fix some compile warnings, make vdb test code conditional on DEBUG_XEMACS
Ben Wing <ben@xemacs.org>
parents:
4125
diff
changeset
|
2100 static void * |
3092 | 2101 get_page_start (void *ptr) |
2102 { | |
2103 return PH_HEAP_SPACE (get_page_header (ptr)); | |
2104 } | |
2105 | |
5054 | 2106 #endif /* 0 */ |
2107 | |
3092 | 2108 /* Make PAGE_SIZE globally available. */ |
2109 EMACS_INT | |
2110 mc_get_page_size () | |
2111 { | |
2112 return PAGE_SIZE; | |
2113 } | |
2114 | |
2115 /* Is the fault at ptr on a protected page? */ | |
2116 EMACS_INT | |
2117 fault_on_protected_page (void *ptr) | |
2118 { | |
2119 page_header *ph = get_page_header_internal (ptr); | |
2120 return (ph | |
2121 && PH_HEAP_SPACE (ph) | |
2122 && (PH_HEAP_SPACE (ph) <= ptr) | |
2123 && ((void *) ((EMACS_INT) PH_HEAP_SPACE (ph) | |
2124 + PH_N_PAGES (ph) * PAGE_SIZE) > ptr) | |
2125 && (PH_PROTECTION_BIT (ph) == 1)); | |
2126 } | |
2127 | |
2128 | |
2129 /* Protect the heap page of given page header ph if black objects are | |
3303 | 2130 on the page. Returns number of processed pages. */ |
2131 static EMACS_INT | |
3092 | 2132 protect_heap_page (page_header *ph) |
2133 { | |
2134 if (PH_BLACK_BIT (ph)) | |
2135 { | |
2136 void *heap_space = PH_HEAP_SPACE (ph); | |
2137 EMACS_INT heap_space_size = PH_N_PAGES (ph) * PAGE_SIZE; | |
2138 vdb_protect ((void *) heap_space, heap_space_size); | |
2139 PH_PROTECTION_BIT (ph) = 1; | |
3303 | 2140 return 1; |
3092 | 2141 } |
3303 | 2142 return 0; |
3092 | 2143 } |
2144 | |
3303 | 2145 /* Protect all heap pages with black objects. Returns number of |
2146 processed pages.*/ | |
2147 EMACS_INT | |
3092 | 2148 protect_heap_pages (void) |
2149 { | |
3303 | 2150 return visit_all_used_page_headers (protect_heap_page); |
3092 | 2151 } |
2152 | |
2153 | |
2154 /* Remove protection (if there) of heap page of given page header | |
3303 | 2155 ph. Returns number of processed pages. */ |
2156 static EMACS_INT | |
3092 | 2157 unprotect_heap_page (page_header *ph) |
2158 { | |
2159 if (PH_PROTECTION_BIT (ph)) | |
2160 { | |
2161 void *heap_space = PH_HEAP_SPACE (ph); | |
2162 EMACS_INT heap_space_size = PH_N_PAGES (ph) * PAGE_SIZE; | |
2163 vdb_unprotect (heap_space, heap_space_size); | |
2164 PH_PROTECTION_BIT (ph) = 0; | |
3303 | 2165 return 1; |
3092 | 2166 } |
3303 | 2167 return 0; |
3092 | 2168 } |
2169 | |
3303 | 2170 /* Remove protection for all heap pages which are protected. Returns |
2171 number of processed pages. */ | |
2172 EMACS_INT | |
3092 | 2173 unprotect_heap_pages (void) |
2174 { | |
3303 | 2175 return visit_all_used_page_headers (unprotect_heap_page); |
3092 | 2176 } |
2177 | |
2178 /* Remove protection and mark page dirty. */ | |
2179 void | |
2180 unprotect_page_and_mark_dirty (void *ptr) | |
2181 { | |
2182 page_header *ph = get_page_header (ptr); | |
2183 unprotect_heap_page (ph); | |
2184 PH_DIRTY_BIT (ph) = 1; | |
2185 } | |
2186 | |
2187 /* Repush all objects on dirty pages onto the mark stack. */ | |
2188 int | |
2189 repush_all_objects_on_page (void *ptr) | |
2190 { | |
2191 int repushed_objects = 0; | |
2192 page_header *ph = get_page_header (ptr); | |
2193 Rawbyte *heap_space = (Rawbyte *) PH_HEAP_SPACE (ph); | |
2194 EMACS_INT heap_space_step = PH_CELL_SIZE (ph); | |
2195 EMACS_INT mark_bit = 0; | |
2196 EMACS_INT mark_bit_max_index = PH_CELLS_ON_PAGE (ph); | |
2197 unsigned int bit = 0; | |
2198 for (mark_bit = 0; mark_bit < mark_bit_max_index; mark_bit++) | |
2199 { | |
2200 GET_BIT (bit, ph, mark_bit * N_MARK_BITS); | |
2201 if (bit == BLACK) | |
2202 { | |
2203 repushed_objects++; | |
2204 gc_write_barrier | |
2205 (wrap_pointer_1 ((heap_space + (heap_space_step * mark_bit)))); | |
2206 } | |
2207 } | |
2208 PH_BLACK_BIT (ph) = 0; | |
2209 PH_DIRTY_BIT (ph) = 0; | |
2210 return repushed_objects; | |
2211 } | |
2212 | |
2213 /* Mark black if object is currently grey. This first checks, if the | |
2214 object is really allocated on the mc-heap. If it is, it can be | |
2215 marked black; if it is not, it cannot be marked. */ | |
2216 EMACS_INT | |
2217 maybe_mark_black (void *ptr) | |
2218 { | |
2219 page_header *ph = get_page_header_internal (ptr); | |
2220 unsigned int bit = 0; | |
2221 | |
2222 if (ph && PH_PLH (ph) && PH_ON_USED_LIST_P (ph)) | |
2223 { | |
2224 GET_BIT (bit, ph, get_mark_bit_index (ptr, ph)); | |
2225 if (bit == GREY) | |
2226 { | |
2227 if (!PH_BLACK_BIT (ph)) | |
2228 PH_BLACK_BIT (ph) = 1; | |
2229 SET_BIT (ph, get_mark_bit_index (ptr, ph), BLACK); | |
2230 } | |
2231 return 1; | |
2232 } | |
2233 return 0; | |
2234 } | |
2235 | |
2236 /* Only for debugging --- not used anywhere in the sources. */ | |
2237 EMACS_INT | |
2238 object_on_heap_p (void *ptr) | |
2239 { | |
2240 page_header *ph = get_page_header_internal (ptr); | |
2241 return (ph && PH_ON_USED_LIST_P (ph)); | |
2242 } |