annotate src/ralloc.c @ 266:18d185df8c54

Added tag r20-5b31 for changeset 8efd647ea9ca
author cvs
date Mon, 13 Aug 2007 10:25:39 +0200
parents 084402c475ba
children c5d627a313b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1 /* Block-relocating memory allocator.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
19 Boston, MA 02111-1307, USA.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
20
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
21 Synched Up with: FSF 20.2 (non-mmap portion only)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
22 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /* NOTES:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 rather than all of them. This means allowing for a possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 hole between the first bloc and the end of malloc storage. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include "lisp.h" /* Needed for VALBITS. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #undef NULL
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 /* The important properties of this type are that 1) it's a pointer, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 2) arithmetic on it should work as if the size of the object pointed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 to has a size of 1. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #if 0 /* Arithmetic on void* is a GCC extension. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #ifdef __STDC__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 typedef void *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 typedef unsigned char *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 /* Unconditionally use unsigned char * for this. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 typedef unsigned char *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 typedef unsigned long SIZE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
53 #ifdef DOUG_LEA_MALLOC
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
54 #define M_TOP_PAD -2
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
55 extern int mallopt ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
56 #endif
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
57
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 #include "getpagesize.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #else /* Not emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 #include <stddef.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 typedef size_t SIZE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 typedef void *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #include <malloc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 #endif /* emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 #define safe_bcopy(x, y, z) memmove (y, x, z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 #define NIL ((POINTER) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
80 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 /* A flag to indicate whether we have initialized ralloc yet. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 Emacs's sake, please do not make this local to malloc_init; on some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 machines, the dumping procedure makes all static variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 read-only. On these machines, the word static is #defined to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 the empty string, meaning that r_alloc_initialized becomes an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 automatic variable, and loses its value each time Emacs is started up. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 static int r_alloc_initialized = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 /* Declarations for working with the malloc, ralloc, and system breaks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 /* Function to set the real break value. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 static POINTER (*real_morecore) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 /* The break value, as seen by malloc (). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 static POINTER virtual_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 /* The break value, viewed by the relocatable blocs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 static POINTER break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 /* The REAL (i.e., page aligned) break value of the process. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 static POINTER page_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 /* This is the size of a page. We round memory requests to this boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 static int page_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
108 /* Whenever we get memory from the system, get this many extra bytes. This
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 must be a multiple of page_size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 static int extra_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 /* Macros for rounding. Note that rounding to any value is possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 by changing the definition of PAGE. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 #define PAGE (getpagesize ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 & ~(page_size - 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
119
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
120 #define MEM_ALIGN sizeof(double)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
121 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
122 & ~(MEM_ALIGN - 1))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
124 /* Data structures of heaps and blocs. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
125
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
126 /* The relocatable objects, or blocs, and the malloc data
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
127 both reside within one or more heaps.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
128 Each heap contains malloc data, running from `start' to `bloc_start',
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
129 and relocatable objects, running from `bloc_start' to `free'.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
130
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
131 Relocatable objects may relocate within the same heap
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
132 or may move into another heap; the heaps themselves may grow
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
133 but they never move.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
134
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
135 We try to make just one heap and make it larger as necessary.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
136 But sometimes we can't do that, because we can't get contiguous
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
137 space to add onto the heap. When that happens, we start a new heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
138
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
139 typedef struct heap
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
140 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
141 struct heap *next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
142 struct heap *prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
143 /* Start of memory range of this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
144 POINTER start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
145 /* End of memory range of this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
146 POINTER end;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
147 /* Start of relocatable data in this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
148 POINTER bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
149 /* Start of unused space in this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
150 POINTER free;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
151 /* First bloc in this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
152 struct bp *first_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
153 /* Last bloc in this heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
154 struct bp *last_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
155 } *heap_ptr;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
156
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
157 #define NIL_HEAP ((heap_ptr) 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
158 #define HEAP_PTR_SIZE (sizeof (struct heap))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
159
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
160 /* This is the first heap object.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
161 If we need additional heap objects, each one resides at the beginning of
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
162 the space it covers. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
163 static struct heap heap_base;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
164
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
165 /* Head and tail of the list of heaps. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
166 static heap_ptr first_heap, last_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
167
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
168 /* These structures are allocated in the malloc arena.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
169 The linked list is kept in order of increasing '.data' members.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
170 The data blocks abut each other; if b->next is non-nil, then
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
171 b->data + b->size == b->next->data.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
172
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
173 An element with variable==NIL denotes a freed block, which has not yet
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
174 been collected. They may only appear while r_alloc_freeze > 0, and will be
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
175 freed when the arena is thawed. Currently, these blocs are not reusable,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
176 while the arena is frozen. Very inefficient. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
177
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
178 typedef struct bp
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
179 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
180 struct bp *next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
181 struct bp *prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
182 POINTER *variable;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
183 POINTER data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
184 SIZE size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
185 POINTER new_data; /* temporarily used for relocation */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
186 struct heap *heap; /* Heap this bloc is in. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
187 } *bloc_ptr;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
188
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
189 #define NIL_BLOC ((bloc_ptr) 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
190 #define BLOC_PTR_SIZE (sizeof (struct bp))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
191
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
192 /* Head and tail of the list of relocatable blocs. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
193 static bloc_ptr first_bloc, last_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
194
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
195 static int use_relocatable_buffers;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
196
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
197 /* If >0, no relocation whatsoever takes place. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
198 static int r_alloc_freeze_level;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 /* Obtain SIZE bytes of space. If enough space is not presently available
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 in our process reserve, (i.e., (page_break_value - break_value)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 this means getting more page-aligned space from the system.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 Return non-zero if all went well, or zero if we couldn't allocate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 the memory. */
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
206
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
207 /* Functions to get and return memory from the system. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
208
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
209 /* Find the heap that ADDRESS falls within. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
210
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
211 static heap_ptr
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
212 find_heap (address)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
213 POINTER address;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
215 heap_ptr heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
216
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
217 for (heap = last_heap; heap; heap = heap->prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
218 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
219 if (heap->start <= address && address <= heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
220 return heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
221 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
222
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
223 return NIL_HEAP;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
224 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
225
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
226 /* Find SIZE bytes of space in a heap.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
227 Try to get them at ADDRESS (which must fall within some heap's range)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
228 if we can get that many within one heap.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
229
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
230 If enough space is not presently available in our reserve, this means
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
231 getting more page-aligned space from the system. If the returned space
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
232 is not contiguous to the last heap, allocate a new heap, and append it
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
233
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
234 obtain does not try to keep track of whether space is in use
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
235 or not in use. It just returns the address of SIZE bytes that
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
236 fall within a single heap. If you call obtain twice in a row
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
237 with the same arguments, you typically get the same value.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
238 to the heap list. It's the caller's responsibility to keep
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
239 track of what space is in use.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
240
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
241 Return the address of the space if all went well, or zero if we couldn't
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
242 allocate the memory. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
243
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
244 static POINTER
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
245 obtain (POINTER address, SIZE size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
246 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
247 heap_ptr heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
248 SIZE already_available;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
249
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
250 /* Find the heap that ADDRESS falls within. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
251 for (heap = last_heap; heap; heap = heap->prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
252 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
253 if (heap->start <= address && address <= heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
254 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
255 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
257 if (! heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
258 abort ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
259
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
260 /* If we can't fit SIZE bytes in that heap,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
261 try successive later heaps. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
262 while (heap && address + size > heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
263 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
264 heap = heap->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
265 if (heap == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
266 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
267 address = heap->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
268 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
269
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
270 /* If we can't fit them within any existing heap,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
271 get more space. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
272 if (heap == NIL_HEAP)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
274 POINTER new = (*real_morecore)(0);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
275 SIZE get;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
276
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
277 already_available = (char *)last_heap->end - (char *)address;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
278
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
279 if (new != last_heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
280 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
281 /* Someone else called sbrk. Make a new heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
282
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
283 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
284 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
285
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
286 if ((*real_morecore) (bloc_start - new) != new)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
287 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
289 new_heap->start = new;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
290 new_heap->end = bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
291 new_heap->bloc_start = bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
292 new_heap->free = bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
293 new_heap->next = NIL_HEAP;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
294 new_heap->prev = last_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
295 new_heap->first_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
296 new_heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
297 last_heap->next = new_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
298 last_heap = new_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
299
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
300 address = bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
301 already_available = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
302 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
303
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
304 /* Add space to the last heap (which we may have just created).
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
305 Get some extra, so we can come here less often. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
306
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
307 get = size + extra_bytes - already_available;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
308 get = (char *) ROUNDUP ((char *)last_heap->end + get)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
309 - (char *) last_heap->end;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
310
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
311 if ((*real_morecore) (get) != last_heap->end)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
314 last_heap->end += get;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
317 return address;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
320 #if 0
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 /* Obtain SIZE bytes of space and return a pointer to the new area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 If we could not allocate the space, return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 static POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 get_more_space (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 POINTER ptr = break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 if (obtain (size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 return ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
333 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 /* Note that SIZE bytes of space have been relinquished by the process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 If SIZE is more than a page, return the space to the system. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 static void
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
339 relinquish ()
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
341 register heap_ptr h;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
342 int excess = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
343
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
344 /* Add the amount of space beyond break_value
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
345 in all heaps which have extend beyond break_value at all. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
347 for (h = last_heap; h && break_value < h->end; h = h->prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
348 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
349 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
350 ? h->bloc_start : break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
351 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
352
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
353 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 /* Keep extra_bytes worth of empty space.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 And don't free anything unless we can free at least extra_bytes. */
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
357 excess -= extra_bytes;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
358
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
359 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
360 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
361 /* This heap should have no blocs in it. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
362 if (last_heap->first_bloc != NIL_BLOC
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
363 || last_heap->last_bloc != NIL_BLOC)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
364 abort ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
365
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
366 /* Return the last heap, with its header, to the system. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
367 excess = (char *)last_heap->end - (char *)last_heap->start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
368 last_heap = last_heap->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
369 last_heap->next = NIL_HEAP;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
370 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
371 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
372 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
373 excess = (char *) last_heap->end
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
374 - (char *) ROUNDUP ((char *)last_heap->end - excess);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
375 last_heap->end -= excess;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
376 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
377
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
378 if ((*real_morecore) (- excess) == 0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
381 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
383 /* Return the total size in use by relocating allocator,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
384 above where malloc gets space. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
385
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
386 long
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
387 r_alloc_size_in_use ()
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
388 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
389 return break_value - virtual_break_value;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 /* The meat - allocating, freeing, and relocating blocs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 /* Find the bloc referenced by the address in PTR. Returns a pointer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 to that block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 static bloc_ptr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 find_bloc (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
401 register bloc_ptr p = first_bloc;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 while (p != NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 if (p->variable == ptr && p->data == *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 return p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 p = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 return p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 Returns a pointer to the new bloc, or zero if we couldn't allocate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 memory for the new block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 static bloc_ptr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 get_bloc (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
421 register bloc_ptr new_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
422 register heap_ptr heap;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
425 || ! (new_bloc->data = obtain (break_value, size)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 if (new_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 free (new_bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
433 break_value = new_bloc->data + size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
434
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 new_bloc->size = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 new_bloc->next = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 new_bloc->variable = (POINTER *) NIL;
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
438 new_bloc->new_data = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
440 /* Record in the heap that this space is in use. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
441 heap = find_heap (new_bloc->data);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
442 heap->free = break_value;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
443
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
444 /* Maintain the correspondence between heaps and blocs. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
445 new_bloc->heap = heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
446 heap->last_bloc = new_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
447 if (heap->first_bloc == NIL_BLOC)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
448 heap->first_bloc = new_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
449
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
450 /* Put this bloc on the doubly-linked list of blocs. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 if (first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 new_bloc->prev = last_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 last_bloc->next = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 last_bloc = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 first_bloc = last_bloc = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 new_bloc->prev = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 return new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
466 /* Calculate new locations of blocs in the list beginning with BLOC,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
467 relocating it to start at ADDRESS, in heap HEAP. If enough space is
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
468 not presently available in our reserve, call obtain for
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
469 more space.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
470
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
471 Store the new location of each bloc in its new_data field.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
472 Do not touch the contents of blocs or break_value. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
473
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
474 static int
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
475 relocate_blocs (bloc, heap, address)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
476 bloc_ptr bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
477 heap_ptr heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
478 POINTER address;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
479 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
480 register bloc_ptr b = bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
481
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
482 /* No need to ever call this if arena is frozen, bug somewhere! */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
483 if (r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
484 abort();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
485
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
486 while (b)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
487 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
488 /* If bloc B won't fit within HEAP,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
489 move to the next heap and try again. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
490 while (heap && address + b->size > heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
491 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
492 heap = heap->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
493 if (heap == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
494 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
495 address = heap->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
496 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
497
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
498 /* If BLOC won't fit in any heap,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
499 get enough new space to hold BLOC and all following blocs. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
500 if (heap == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
501 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
502 register bloc_ptr tb = b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
503 register SIZE s = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
504
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
505 /* Add up the size of all the following blocs. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
506 while (tb != NIL_BLOC)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
507 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
508 if (tb->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
509 s += tb->size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
510
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
511 tb = tb->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
512 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
514 /* Get that space. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
515 address = obtain (address, s);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
516 if (address == 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
517 return 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
518
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
519 heap = last_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
520 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
521
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
522 /* Record the new address of this bloc
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
523 and update where the next bloc can start. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
524 b->new_data = address;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
525 if (b->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
526 address += b->size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
527 b = b->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
528 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
529
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
530 return 1;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
531 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
532
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
533 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
534 This is necessary if we put the memory of space of BLOC
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
535 before that of BEFORE. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
537 static void
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
538 reorder_bloc (bloc, before)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
539 bloc_ptr bloc, before;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
540 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
541 bloc_ptr prev, next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
542
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
543 /* Splice BLOC out from where it is. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
544 prev = bloc->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
545 next = bloc->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
546
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
547 if (prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
548 prev->next = next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
549 if (next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
550 next->prev = prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
551
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
552 /* Splice it in before BEFORE. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
553 prev = before->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
554
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
555 if (prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
556 prev->next = bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
557 bloc->prev = prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
558
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
559 before->prev = bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
560 bloc->next = before;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
561 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
562
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
563 /* Update the records of which heaps contain which blocs, starting
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
564 with heap HEAP and bloc BLOC. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 static void
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
567 update_heap_bloc_correspondence (bloc, heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
568 bloc_ptr bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
569 heap_ptr heap;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
571 register bloc_ptr b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
572
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
573 /* Initialize HEAP's status to reflect blocs before BLOC. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
574 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
575 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
576 /* The previous bloc is in HEAP. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
577 heap->last_bloc = bloc->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
578 heap->free = bloc->prev->data + bloc->prev->size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
579 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
580 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
581 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
582 /* HEAP contains no blocs before BLOC. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
583 heap->first_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
584 heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
585 heap->free = heap->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
586 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
587
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
588 /* Advance through blocs one by one. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
589 for (b = bloc; b != NIL_BLOC; b = b->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
590 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
591 /* Advance through heaps, marking them empty,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
592 till we get to the one that B is in. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
593 while (heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
594 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
595 if (heap->bloc_start <= b->data && b->data <= heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
596 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
597 heap = heap->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
598 /* We know HEAP is not null now,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
599 because there has to be space for bloc B. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
600 heap->first_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
601 heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
602 heap->free = heap->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
603 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
604
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
605 /* Update HEAP's status for bloc B. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
606 heap->free = b->data + b->size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
607 heap->last_bloc = b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
608 if (heap->first_bloc == NIL_BLOC)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
609 heap->first_bloc = b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
610
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
611 /* Record that B is in HEAP. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
612 b->heap = heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
613 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
614
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
615 /* If there are any remaining heaps and no blocs left,
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
616 mark those heaps as empty. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
617 heap = heap->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
618 while (heap)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
620 heap->first_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
621 heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
622 heap->free = heap->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
623 heap = heap->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
624 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
625 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
626
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
627 /* Resize BLOC to SIZE bytes. This relocates the blocs
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
628 that come after BLOC in memory. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
629
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
630 static int
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
631 resize_bloc (bloc, size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
632 bloc_ptr bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
633 SIZE size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
634 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
635 register bloc_ptr b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
636 heap_ptr heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
637 POINTER address;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
638 SIZE old_size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
639
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
640 /* No need to ever call this if arena is frozen, bug somewhere! */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
641 if (r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
642 abort();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
643
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
644 if (bloc == NIL_BLOC || size == bloc->size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
645 return 1;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
646
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
647 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
648 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
649 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
650 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
651 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
652
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
653 if (heap == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
654 abort ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
655
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
656 old_size = bloc->size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
657 bloc->size = size;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
658
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
659 /* Note that bloc could be moved into the previous heap. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
660 address = (bloc->prev ? bloc->prev->data + bloc->prev->size
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
661 : first_heap->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
662 while (heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
663 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
664 if (heap->bloc_start <= address && address <= heap->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
665 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
666 heap = heap->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
667 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
668
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
669 if (! relocate_blocs (bloc, heap, address))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
670 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
671 bloc->size = old_size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
672 return 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
673 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
674
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
675 if (size > old_size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
676 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
677 for (b = last_bloc; b != bloc; b = b->prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
678 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
679 if (!b->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
680 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
681 b->size = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
682 b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
683 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
684 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
685 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
686 safe_bcopy (b->data, b->new_data, b->size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
687 *b->variable = b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
688 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
689 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
690 if (!bloc->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
691 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
692 bloc->size = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
693 bloc->data = bloc->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
694 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
695 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
696 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
697 safe_bcopy (bloc->data, bloc->new_data, old_size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
698 memset (bloc->new_data + old_size, 0, size - old_size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
699 *bloc->variable = bloc->data = bloc->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
700 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
701 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
702 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
703 {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 for (b = bloc; b != NIL_BLOC; b = b->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
706 if (!b->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
707 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
708 b->size = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
709 b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
710 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
711 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
712 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
713 safe_bcopy (b->data, b->new_data, b->size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
714 *b->variable = b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
715 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
717 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
719 update_heap_bloc_correspondence (bloc, heap);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
720
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
721 break_value = (last_bloc ? last_bloc->data + last_bloc->size
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
722 : first_heap->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
723 return 1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
725
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 /* Free BLOC from the chain of blocs, relocating any blocs above it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 and returning BLOC->size bytes to the free area. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 free_bloc (bloc_ptr bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
732 heap_ptr heap = bloc->heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
733
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
734 if (r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
735 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
736 bloc->variable = (POINTER *) NIL;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
737 return;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
738 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
739
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
740 resize_bloc (bloc, 0);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
741
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 if (bloc == first_bloc && bloc == last_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 first_bloc = last_bloc = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 else if (bloc == last_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 last_bloc = bloc->prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 last_bloc->next = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 else if (bloc == first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 first_bloc = bloc->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 first_bloc->prev = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 bloc->next->prev = bloc->prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 bloc->prev->next = bloc->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
762 /* Update the records of which blocs are in HEAP. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
763 if (heap->first_bloc == bloc)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
764 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
765 if (bloc->next != 0 && bloc->next->heap == heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
766 heap->first_bloc = bloc->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
767 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
768 heap->first_bloc = heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
769 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
770 if (heap->last_bloc == bloc)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
771 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
772 if (bloc->prev != 0 && bloc->prev->heap == heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
773 heap->last_bloc = bloc->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
774 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
775 heap->first_bloc = heap->last_bloc = NIL_BLOC;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
776 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
777
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
778 relinquish ();
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 free (bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 /* Interface routines. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 /* Obtain SIZE bytes of storage from the free pool, or the system, as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 necessary. If relocatable blocs are in use, this means relocating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 them. This function gets plugged into the GNU malloc's __morecore
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 hook.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 We provide hysteresis, never relocating by less than extra_bytes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 If we're out of memory, we should return zero, to imitate the other
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 __morecore hook values - in particular, __default_morecore in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 GNU malloc package. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
795 POINTER
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 r_alloc_sbrk (long size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
798 register bloc_ptr b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
799 POINTER address;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
800
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
801 if (! r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
802 init_ralloc ();
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 if (! use_relocatable_buffers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 return (*real_morecore) (size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
807 if (size == 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
808 return virtual_break_value;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
810 if (size > 0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
812 /* Allocate a page-aligned space. GNU malloc would reclaim an
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
813 extra space if we passed an unaligned one. But we could
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
814 not always find a space which is contiguous to the previous. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
815 POINTER new_bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
816 heap_ptr h = first_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
817 SIZE get = ROUNDUP (size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
818
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
819 address = (POINTER) ROUNDUP (virtual_break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
820
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
821 /* Search the list upward for a heap which is large enough. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
822 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
823 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
824 h = h->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
825 if (h == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
826 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
827 address = (POINTER) ROUNDUP (h->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
828 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
829
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
830 /* If not found, obtain more space. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
831 if (h == NIL_HEAP)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
832 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
833 get += extra_bytes + page_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
835 if (! obtain (address, get))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
836 return 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
837
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
838 if (first_heap == last_heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
839 address = (POINTER) ROUNDUP (virtual_break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
840 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
841 address = (POINTER) ROUNDUP (last_heap->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
842 h = last_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
843 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
844
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
845 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
846
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
847 if (first_heap->bloc_start < new_bloc_start)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
848 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
849 /* This is no clean solution - no idea how to do it better. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
850 if (r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
851 return NIL;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
852
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
853 /* There is a bug here: if the above obtain call succeeded, but the
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
854 relocate_blocs call below does not succeed, we need to free
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
855 the memory that we got with obtain. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
856
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
857 /* Move all blocs upward. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
858 if (! relocate_blocs (first_bloc, h, new_bloc_start))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
859 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
861 /* Note that (POINTER)(h+1) <= new_bloc_start since
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
862 get >= page_size, so the following does not destroy the heap
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
863 header. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
864 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
865 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
866 safe_bcopy (b->data, b->new_data, b->size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
867 *b->variable = b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
868 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
869
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
870 h->bloc_start = new_bloc_start;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
872 update_heap_bloc_correspondence (first_bloc, h);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
873 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
874 if (h != first_heap)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
875 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
876 /* Give up managing heaps below the one the new
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
877 virtual_break_value points to. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
878 first_heap->prev = NIL_HEAP;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
879 first_heap->next = h->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
880 first_heap->start = h->start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
881 first_heap->end = h->end;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
882 first_heap->free = h->free;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
883 first_heap->first_bloc = h->first_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
884 first_heap->last_bloc = h->last_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
885 first_heap->bloc_start = h->bloc_start;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
886
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
887 if (first_heap->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
888 first_heap->next->prev = first_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
889 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
890 last_heap = first_heap;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
891 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
892
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
893 memset (address, 0, size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
895 else /* size < 0 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
897 SIZE excess = (char *)first_heap->bloc_start
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
898 - ((char *)virtual_break_value + size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
899
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
900 address = virtual_break_value;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
901
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
902 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
903 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
904 excess -= extra_bytes;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
905 first_heap->bloc_start
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
906 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
907
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
908 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
910 for (b = first_bloc; b != NIL_BLOC; b = b->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
911 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
912 safe_bcopy (b->data, b->new_data, b->size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
913 *b->variable = b->data = b->new_data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
914 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
915 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
916
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
917 if ((char *)virtual_break_value + size < (char *)first_heap->start)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
918 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
919 /* We found an additional space below the first heap */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
920 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
921 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
924 virtual_break_value = (POINTER) ((char *)address + size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
925 break_value = (last_bloc
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
926 ? last_bloc->data + last_bloc->size
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
927 : first_heap->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
928 if (size < 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
929 relinquish ();
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
931 return address;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 the data is returned in *PTR. PTR is thus the address of some variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 which will use the data area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
938 The allocation of 0 bytes is valid.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
939 In case r_alloc_freeze is set, a best fit of unused blocs could be done
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
940 before allocating a new area. Not yet done.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
941
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 If we can't allocate the necessary memory, set *PTR to zero, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 r_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 bloc_ptr new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 if (! r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 init_ralloc ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 new_bloc = get_bloc (size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 if (new_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 new_bloc->variable = ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 *ptr = new_bloc->data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 *ptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 Store 0 in *PTR to show there's no block allocated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 r_alloc_free (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
971 register bloc_ptr dead_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
972
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
973 if (! r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
974 init_ralloc ();
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 dead_bloc = find_bloc (ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 if (dead_bloc == NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 free_bloc (dead_bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 *ptr = 0;
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
982
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
983 #ifdef emacs
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
984 refill_memory_reserve ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
985 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 Do this by shifting all blocks above this one up in memory, unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 SIZE is less than or equal to the current bloc size, in which case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 do nothing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
993 In case r_alloc_freeze is set, a new bloc is allocated, and the
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
994 memory copied to it. Not very efficient. We could traverse the
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
995 bloc_list for a best fit of free blocs first.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
996
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 Change *PTR to reflect the new bloc, and return this value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 If more memory cannot be allocated, then leave *PTR unchanged, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 r_re_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 {
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1005 register bloc_ptr bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1006
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1007 if (! r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1008 init_ralloc ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1009
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1010 if (!*ptr)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1011 return r_alloc (ptr, size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1012 if (!size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1013 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1014 r_alloc_free (ptr);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1015 return r_alloc (ptr, 0);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1016 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 bloc = find_bloc (ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 if (bloc == NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1022 if (size < bloc->size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1023 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1024 /* Wouldn't it be useful to actually resize the bloc here? */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1025 /* I think so too, but not if it's too expensive... */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1026 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1027 && r_alloc_freeze_level == 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1028 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1029 resize_bloc (bloc, MEM_ROUNDUP (size));
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1030 /* Never mind if this fails, just do nothing... */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1031 /* It *should* be infallible! */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1032 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1033 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1034 else if (size > bloc->size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1035 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1036 if (r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1037 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1038 bloc_ptr new_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1039 new_bloc = get_bloc (MEM_ROUNDUP (size));
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1040 if (new_bloc)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1041 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1042 new_bloc->variable = ptr;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1043 *ptr = new_bloc->data;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1044 bloc->variable = (POINTER *) NIL;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1045 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1046 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1047 return NIL;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1048 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1049 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1050 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1051 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1052 return NIL;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1053 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1054 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1057
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1058 /* Disable relocations, after making room for at least SIZE bytes
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1059 of non-relocatable heap if possible. The relocatable blocs are
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1060 guaranteed to hold still until thawed, even if this means that
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1061 malloc must return a null pointer. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1062
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1063 void
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1064 r_alloc_freeze (size)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1065 long size;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1066 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1067 if (! r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1068 init_ralloc ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1069
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1070 /* If already frozen, we can't make any more room, so don't try. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1071 if (r_alloc_freeze_level > 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1072 size = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1073 /* If we can't get the amount requested, half is better than nothing. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1074 while (size > 0 && r_alloc_sbrk (size) == 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1075 size /= 2;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1076 ++r_alloc_freeze_level;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1077 if (size > 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1078 r_alloc_sbrk (-size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1079 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1080
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1081 void
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1082 r_alloc_thaw ()
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1083 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1084
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1085 if (! r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1086 init_ralloc ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1087
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1088 if (--r_alloc_freeze_level < 0)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1089 abort ();
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1090
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1091 /* This frees all unused blocs. It is not too inefficient, as the resize
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1092 and bcopy is done only once. Afterwards, all unreferenced blocs are
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1093 already shrunk to zero size. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1094 if (!r_alloc_freeze_level)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1095 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1096 bloc_ptr *b = &first_bloc;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1097 while (*b)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1098 if (!(*b)->variable)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1099 free_bloc (*b);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1100 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1101 b = &(*b)->next;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1102 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1103 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1104
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 /* The hook `malloc' uses for the function which gets more space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 from the system. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108 extern POINTER (*__morecore) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 /* Initialize various things for memory allocation. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113 init_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 if (r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 r_alloc_initialized = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 real_morecore = __morecore;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 __morecore = r_alloc_sbrk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1122 first_heap = last_heap = &heap_base;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1123 first_heap->next = first_heap->prev = NIL_HEAP;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1124 first_heap->start = first_heap->bloc_start
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1125 = virtual_break_value = break_value = (*real_morecore) (0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 if (break_value == NIL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 page_size = PAGE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 extra_bytes = ROUNDUP (50000);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1132 #ifdef DOUG_LEA_MALLOC
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1133 mallopt (M_TOP_PAD, 64 * 4096);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1134 #else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1135 #if 0 /* Hasn't been synched yet */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1136 /* Give GNU malloc's morecore some hysteresis
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1137 so that we move all the relocatable blocks much less often. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1138 __malloc_extra_blocks = 64;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1139 #endif
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1140 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1142 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1143
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1144 /* The extra call to real_morecore guarantees that the end of the
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 address space is a multiple of page_size, even if page_size is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 not really the page size of the system running the binary in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 which page_size is stored. This allows a binary to be built on a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148 system with one page size and run on a system with a smaller page
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1149 size. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1150 (*real_morecore) (first_heap->end - first_heap->start);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152 /* Clear the rest of the last page; this memory is in our address space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153 even though it is after the sbrk value. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 /* Doubly true, with the additional call that explicitly adds the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155 rest of that page to the address space. */
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1156 memset (first_heap->start, 0, first_heap->end - first_heap->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1157 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158 use_relocatable_buffers = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 }
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1160
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1161 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1162
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1163 /* Reinitialize the morecore hook variables after restarting a dumped
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1164 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1165 void
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1166 r_alloc_reinit ()
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1167 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1168 /* Only do this if the hook has been reset, so that we don't get an
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1169 infinite loop, in case Emacs was linked statically. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1170 if (__morecore != r_alloc_sbrk)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1171 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1172 real_morecore = __morecore;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1173 __morecore = r_alloc_sbrk;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1174 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1175 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1176 #if 0
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1177 #ifdef DEBUG
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1178
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1179 void
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1180 r_alloc_check ()
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1181 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1182 int found = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1183 heap_ptr h, ph = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1184 bloc_ptr b, pb = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1185
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1186 if (!r_alloc_initialized)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1187 return;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1188
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1189 assert (first_heap);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1190 assert (last_heap->end <= (POINTER) sbrk (0));
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1191 assert ((POINTER) first_heap < first_heap->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1192 assert (first_heap->start <= virtual_break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1193 assert (virtual_break_value <= first_heap->end);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1194
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1195 for (h = first_heap; h; h = h->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1196 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1197 assert (h->prev == ph);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1198 assert ((POINTER) ROUNDUP (h->end) == h->end);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1199 #if 0 /* ??? The code in ralloc.c does not really try to ensure
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1200 the heap start has any sort of alignment.
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1201 Perhaps it should. */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1202 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1203 #endif
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1204 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1205 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1206
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1207 if (ph)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1208 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1209 assert (ph->end < h->start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1210 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1211 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1212
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1213 if (h->bloc_start <= break_value && break_value <= h->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1214 found = 1;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1215
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1216 ph = h;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1217 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1218
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1219 assert (found);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1220 assert (last_heap == ph);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1221
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1222 for (b = first_bloc; b; b = b->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1223 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1224 assert (b->prev == pb);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1225 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1226 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1227
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1228 ph = 0;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1229 for (h = first_heap; h; h = h->next)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1230 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1231 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1232 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1233 ph = h;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1234 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1235
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1236 assert (h);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1237
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1238 if (pb && pb->data + pb->size != b->data)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1239 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1240 assert (ph && b->data == h->bloc_start);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1241 while (ph)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1242 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1243 if (ph->bloc_start <= pb->data
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1244 && pb->data + pb->size <= ph->end)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1245 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1246 assert (pb->data + pb->size + b->size > ph->end);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1247 break;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1248 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1249 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1250 {
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1251 assert (ph->bloc_start + b->size > ph->end);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1252 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1253 ph = ph->prev;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1254 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1255 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1256 pb = b;
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1257 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1258
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1259 assert (last_bloc == pb);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1260
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1261 if (last_bloc)
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1262 assert (last_bloc->data + last_bloc->size == break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1263 else
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1264 assert (first_heap->bloc_start == break_value);
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1265 }
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1266 #endif /* DEBUG */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1267 #endif /* 0 */
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1268
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1269 #endif
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 185
diff changeset
1270
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 #else /* HAVE_MMAP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1273 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1274 A relocating allocator built using the mmap(2) facility available
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1275 in some OSes. Based on another version written by Paul Flinders,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1276 from which code (and comments) are snarfed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1278 The OS should support mmap() with MAP_ANONYMOUS attribute, or have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1279 /dev/zero. It should support private memory mapping.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1281 Paul Flinders wrote a version which works well for systems that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1282 allow callers to specify (virtual) addresses to mmap().
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1283 Unfortunately, such a scheme doesn't work for certain systems like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1284 HP-UX that have a system-wide virtual->real address map, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1285 consequently impose restrictions on the virtual address values
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1286 permitted.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288 NB: The mapping scheme in HP-UX is motivated by the inverted page
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 table design in some HP processors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 This alternate implementation allows for the addresses to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 optionally chosen by the system. Fortunately, buffer allocation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 doesn't insist upon contiguous memory which Flinders' scheme
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294 provides, and this one doesn't.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296 We don't really provide for hysteresis here, but add some metering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297 to monitor how poorly the allocator actually works. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298 documentation for `mmap-hysteresis'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300 This implementation actually cycles through the blocks allocated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 via mmap() and only sends it to free() if it wasn't one of them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302 Unfortunately, this is O(n) in the number of mmapped blocks. (Not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 really, as we have a hash table which tries to reduce the cost.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304 Also, this dereferences the pointer passed, so it would cause a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 segfault if garbage was passed to it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1307 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1308 #include <sys/mman.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1309 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1311 typedef void *VM_ADDR; /* VM addresses */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1312 static CONST VM_ADDR VM_FAILURE_ADDR = (VM_ADDR) -1; /* mmap returns this when it fails. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1314 /* Configuration for relocating allocator. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1315
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1316 /* #define MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1317 /* Define this if you want Emacs to manage the address table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1318 It is not recommended unless you have major problems with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1319 default scheme, which allows the OS to pick addresses. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1320
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1321 /* USELESS_LOWER_ADDRESS_BITS defines the number of bits which can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1322 discarded while computing the hash, as they're always zero. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1323 default is appropriate for a page size of 4096 bytes. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1324
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1325 #define USELESS_LOWER_ADDRESS_BITS 12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1328 /* Size of hash table for inverted VM_ADDR->MMAP_HANDLE lookup */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1330 #define MHASH_PRIME 89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1333 /* Whether we want to enable metering of some ralloc performance.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1334 This incurs a constant penalty for each mmap operation. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1336 #define MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1339 /* Rename the following to protect against a some smartness elsewhere.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1340 We need access to the allocator used for non-mmap allocation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1341 elsewhere, in case we get passed a handle that we didn't allocate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1342 ourselves. Currently, this default allocator is also used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1343 maintain local structures for relocatable blocks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1344
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1345 #define UNDERLYING_MALLOC malloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1346 #define UNDERLYING_FREE free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1347 #define UNDERLYING_REALLOC realloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1349 /* MAP_ADDRCHOICE_FLAG is set to MAP_FIXED if MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1350 is defined, and MAP_VARIABLE otherwise. Some losing systems don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1351 define the _FIXED/_VARIABLE flags, in which case it is set to 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1353 #ifdef MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1354 # ifdef MAP_FIXED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1355 # define MAP_ADDRCHOICE_FLAG MAP_FIXED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1356 # endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1357 #else /* !MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1358 # ifdef MAP_VARIABLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1359 # define MAP_ADDRCHOICE_FLAG MAP_VARIABLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1360 # endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1361 #endif /* MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1363 /* Default case. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1364 #ifndef MAP_ADDRCHOICE_FLAG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1365 # define MAP_ADDRCHOICE_FLAG 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1366 #endif /* MAP_ADDRCHOICE_FLAG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1368 #ifdef MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1369 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG | MAP_ANONYMOUS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1370 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1371 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1372 #endif /* MAP_ANONYMOUS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1374
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1375 /* (ptf): A flag to indicate whether we have initialized ralloc yet. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1376 Emacs's sake, please do not make this local to malloc_init; on some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1377 machines, the dumping procedure makes all static variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1378 read-only. On these machines, the word static is #defined to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1379 the empty string, meaning that r_alloc_initialized becomes an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1380 automatic variable, and loses its value each time Emacs is started up.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1382 If we're using mmap this flag has three possible values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1383 0 - initial value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1384 1 - Normal value when running temacs. In this case buffers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1385 are allocated using malloc so that any data that they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1386 contain becomes part of the undumped executable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1387 2 - Normal value when running emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1388 static int r_alloc_initialized = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1390 /* (ptf): Macros for rounding. Note that rounding to any value is possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1391 by changing the definition of PAGE. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1392 #define PAGE (getpagesize ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1393 #define PAGES_FOR(size) (((unsigned long int) (size) + page_size - 1)/page_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1394 #define ROUNDUP(size) ((unsigned long int)PAGES_FOR(size)*page_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1395
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1397 /* DEV_ZERO_FD is -1 normally, but for systems without MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1398 points to a file descriptor opened on /dev/zero */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1400 static int DEV_ZERO_FD = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1401
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1403 /* We actually need a datastructure that can be usefully structured
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1404 based on the VM address, and allows an ~O(1) lookup on an arbitrary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1405 address, ie a hash-table. Maybe the XEmacs hash table can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1406 coaxed enough. At the moment, we use lookup on a hash-table to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1407 decide whether to do an O(n) search on the malloced block list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1408 Addresses are hashed to a bucket modulo MHASH_PRIME */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1410
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1411 /* We settle for a standard doubly-linked-list. The dynarr type isn't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1412 very amenable to deletion of items in the middle, so we conjure up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1413 yet another stupid datastructure. The structure is maintained as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1414 ring, and the singleton ring has the sole element as it's left and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1415 right neighbours. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1416
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1417 static void init_MHASH_table (void); /* Forward reference */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1418
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1419 typedef struct alloc_dll
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1420 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1421 size_t size; /* #bytes currently in use */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1422 size_t space_for; /* #bytes we really have */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1423 POINTER* aliased_address; /* Address of aliased variable, to tweak if relocating */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1424 VM_ADDR vm_addr; /* VM address returned by mmap */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1425 struct alloc_dll *left; /* Left link in circular doubly linked list */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1426 struct alloc_dll *right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1427 } *MMAP_HANDLE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1428
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1429 static MMAP_HANDLE mmap_start = 0; /* Head of linked list */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1430 static size_t page_size = 0; /* Size of VM pages */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1431 static int mmap_hysteresis; /* Should be size_t, really. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1432
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1433 /* Get a new handle for a fresh block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1434 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1435 new_mmap_handle (size_t nsiz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1436 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1437 MMAP_HANDLE h = (MMAP_HANDLE) UNDERLYING_MALLOC( sizeof (struct alloc_dll));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1438 if ( h == 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1439 h->size = nsiz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1440 if (mmap_start == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1441 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1442 init_MHASH_table ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1443 mmap_start = h; mmap_start->left = h; mmap_start->right = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1444 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1445 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1446 MMAP_HANDLE prev = mmap_start->left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1447 MMAP_HANDLE nex = mmap_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1449 /* Four pointers need fixing. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1450 h->right = nex;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1451 h->left = prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1452 prev->right = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1453 nex->left = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1454 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1455 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1456 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1458 /* Find a handle given the aliased address using linear search. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1459 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1460 find_mmap_handle_lsearch (POINTER *alias)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1461 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1462 MMAP_HANDLE h = mmap_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1463 if (h == 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1464 do {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1465 if (h->aliased_address == alias && *alias == h->vm_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1466 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1467 h = h->right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1468 } while( h != mmap_start );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1469 return 0; /* Bogus alias passed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1470 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1471
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1472 /* Free a handle. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1473 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1474 free_mmap_handle (MMAP_HANDLE h)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1475 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1476 MMAP_HANDLE prev = h->left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1477 MMAP_HANDLE nex = h->right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1478 if (prev == h || nex == h) /* In fact, this should be && */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1479 { /* We're the singleton dll */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1480 UNDERLYING_FREE( h ); /* Free the sole item */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1481 mmap_start = 0; return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1482 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1483 else if (h == mmap_start)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1484 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1485 mmap_start = nex; /* Make sure mmap_start isn't bogus. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1486 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1487 prev->right = nex;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1488 nex->left = prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1489 UNDERLYING_FREE( h );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1490 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1492 /* A simple hash table to speed up the inverted lookup of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1493 VM_ADDR->MMAP_HANDLE. We maintain the number of hits for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1494 particular bucket. We invalidate a hash table entry during block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1495 deletion if the hash has cached the deleted block's address. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1496
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1497 /* Simple hash check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1498 struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1499 int n_hits; /* How many addresses map to this? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1500 MMAP_HANDLE handle; /* What is the current handle? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1501 VM_ADDR addr; /* What is it's VM address? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1502 } MHASH_HITS[ MHASH_PRIME ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1503
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1504 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1505 init_MHASH_table (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1506 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1507 int i = 0;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1508 for (; i < MHASH_PRIME; i++)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1509 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1510 MHASH_HITS[i].n_hits = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1511 MHASH_HITS[i].addr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1512 MHASH_HITS[i].handle = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1513 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1514 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1515
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1516 /* Compute the hash value for an address. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1517 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1518 MHASH (VM_ADDR addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1519 {
116
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
1520 #if (LONGBITS == 64)
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
1521 unsigned long int addr_shift = (unsigned long int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
1522 #else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1523 unsigned int addr_shift = (unsigned int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
116
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
1524 #endif
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1525 int hval = addr_shift % MHASH_PRIME; /* We could have addresses which are -ve
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1526 when converted to signed ints */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1527 return ((hval >= 0) ? hval : MHASH_PRIME + hval);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1528 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1529
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1530 /* Add a VM address with it's corresponding handle to the table. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1531 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1532 MHASH_ADD (VM_ADDR addr, MMAP_HANDLE h)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1533 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1534 int kVal = MHASH( addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1535 if (MHASH_HITS[kVal].n_hits++ == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1536 { /* Only overwrite the table if there were no hits so far. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1537 MHASH_HITS[kVal].addr = addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1538 MHASH_HITS[kVal].handle = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1539 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1540 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1542 /* Delete a VM address entry from the hash table. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1543 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1544 MHASH_DEL (VM_ADDR addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1545 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1546 int kVal = MHASH( addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1547 MHASH_HITS[kVal].n_hits--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1548 if (addr == MHASH_HITS[kVal].addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1549 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1550 MHASH_HITS[kVal].addr = 0; /* Invalidate cache. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1551 MHASH_HITS[kVal].handle = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1552 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1553 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1554
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1555 /* End of hash buckets */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1556
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1557 /* Metering malloc performance. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1558 #ifdef MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1559 /* If we're metering, we introduce some extra symbols to aid the noble
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1560 cause of bloating XEmacs core size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1561
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1562 Lisp_Object Qmm_times_mapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1563 Lisp_Object Qmm_pages_mapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1564 Lisp_Object Qmm_times_unmapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1565 Lisp_Object Qmm_times_remapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1566 Lisp_Object Qmm_didnt_copy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1567 Lisp_Object Qmm_pages_copied;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1568 Lisp_Object Qmm_average_bumpval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1569 Lisp_Object Qmm_wastage;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1570 Lisp_Object Qmm_live_pages;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1571 Lisp_Object Qmm_addr_looked_up;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1572 Lisp_Object Qmm_hash_worked;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1573 Lisp_Object Qmm_addrlist_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1574
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1575 #define M_Map 0 /* How many times allocated? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1576 #define M_Pages_Map 1 /* How many pages allocated? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1577 #define M_Unmap 2 /* How many times freed? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1578 #define M_Remap 3 /* How many times increased in size? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1579 #define M_Didnt_Copy 4 /* How many times didn't need to copy? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1580 #define M_Copy_Pages 5 /* Total # pages copied */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1581 #define M_Average_Bumpval 6 /* Average bump value */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1582 #define M_Wastage 7 /* Remaining (unused space) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1583 #define M_Live_Pages 8 /* #live pages */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1584 #define M_Address_Lookup 9 /* How many times did we need to check if an addr is in the block? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1585 #define M_Hash_Worked 10 /* How many times did the simple hash check work? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1586 #define M_Addrlist_Size 11 /* What is the size of the XEmacs memory map? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1587
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1588 #define N_Meterables 12 /* Total number of meterables */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1589 #define MEMMETER(x) {x;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1590 #define MVAL(x) (meter[x])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1591 #define MLVAL(x) (make_int (meter[x]))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1592 static int meter[N_Meterables];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1593
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
1594 DEFUN ("mmap-allocator-status", Fmmap_allocator_status, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1595 Return some information about mmap-based allocator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1596
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1597 mmap-addrlist-size: number of entries in address picking list.
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1598 mmap-times-mapped: number of times r_alloc was called.
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1599 mmap-pages-mapped: number of pages mapped by r_alloc calls only.
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1600 mmap-times-unmapped: number of times r_free was called.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1601 mmap-times-remapped: number of times r_re_alloc was called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1602 mmap-didnt-copy: number of times re-alloc didn\'t have to move the block.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1603 mmap-pages-copied: total number of pages copied.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1604 mmap-average-bumpval: average increase in size demanded to re-alloc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1605 mmap-wastage: total number of bytes allocated, but not currently in use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1606 mmap-live-pages: total number of pages live.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
1607 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
1608 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1609 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1610 Lisp_Object result;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1611
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1612 result = Fcons (Fcons (Qmm_addrlist_size, MLVAL (M_Addrlist_Size)), Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1613 result = Fcons (Fcons (Qmm_hash_worked, MLVAL (M_Hash_Worked)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1614 result = Fcons (Fcons (Qmm_addr_looked_up, MLVAL (M_Address_Lookup)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1615 result = Fcons (Fcons (Qmm_live_pages, MLVAL (M_Live_Pages)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1616 result = Fcons (Fcons (Qmm_wastage, MLVAL (M_Wastage)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1617 result = Fcons (Fcons (Qmm_average_bumpval, MLVAL (M_Average_Bumpval)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1618 result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1619 result = Fcons (Fcons (Qmm_pages_copied, MLVAL (M_Copy_Pages)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1620 result = Fcons (Fcons (Qmm_didnt_copy, MLVAL (M_Didnt_Copy)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1621 result = Fcons (Fcons (Qmm_times_remapped, MLVAL (M_Remap)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1622 result = Fcons (Fcons (Qmm_times_unmapped, MLVAL (M_Unmap)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1623 result = Fcons (Fcons (Qmm_pages_mapped, MLVAL (M_Pages_Map)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1624 result = Fcons (Fcons (Qmm_times_mapped, MLVAL (M_Map)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1625
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1626 return result;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1627 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1628
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1629 #else /* !MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1630
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1631 #define MEMMETER(x)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1632 #define MVAL(x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1633
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1634 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1635
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1636 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1637 find_mmap_handle (POINTER *alias)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1638 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1639 int kval = MHASH( *alias );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1640 MEMMETER( MVAL(M_Address_Lookup)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1641 switch( MHASH_HITS[kval].n_hits)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1642 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1643 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1644 MEMMETER( MVAL( M_Hash_Worked )++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1645 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1646
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1647 case 1:
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1648 if (*alias == MHASH_HITS[kval].addr)
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1649 {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1650 MEMMETER( MVAL( M_Hash_Worked) ++ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1651 return MHASH_HITS[kval].handle;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1652 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1653 /* FALL THROUGH */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1654 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1655 return find_mmap_handle_lsearch( alias );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1656 } /* switch */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1657 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1658
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1659 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1660 Some kernels don't like being asked to pick addresses for mapping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1661 themselves---IRIX is known to become extremely slow if mmap is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1662 passed a ZERO as the first argument. In such cases, we use an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1663 address map which is managed local to the XEmacs process. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1664 address map maintains an ordered linked list of (address, size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1665 occupancy) triples ordered by the absolute address. Initially, a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1666 large address area is marked as being empty. The address picking
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1667 scheme takes bites off the first block which is still empty and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1668 large enough. If mmap with the specified address fails, it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1669 marked unavailable and not attempted thereafter. The scheme will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1670 keep fragmenting the large empty block until it finds an address
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1671 which can be successfully mmapped, or until there are no free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1672 blocks of the given size left.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1673
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1674 Note that this scheme, given it's first-fit strategy, is prone to
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1675 fragmentation of the first part of memory earmarked for this
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1676 purpose. [ACP Vol I]. We can't use the workaround of using a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1677 randomized first fit because we don't want to presume too much
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1678 about the memory map. Instead, we try to coalesce empty or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1679 unavailable blocks at any available opportunity. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1680
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1681 /* Initialization procedure for address picking scheme */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1682 static void Addr_Block_initialize(void);
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1683
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1684 /* Get a suitable VM_ADDR via mmap */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1685 static VM_ADDR New_Addr_Block( SIZE sz );
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1686
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1687 /* Free a VM_ADDR allocated via New_Addr_Block */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1688 static void Free_Addr_Block( VM_ADDR addr, SIZE sz );
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1689
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1690 #ifdef MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1691 /* Implementation of the three calls for address picking when XEmacs is incharge */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1692
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1693 /* The enum denotes the status of the following block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1694 typedef enum { empty = 0, occupied, unavailable } addr_status;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1695
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1696 typedef struct addr_chain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1697 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1698 POINTER addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1699 SIZE sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1700 addr_status flag;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1701 struct addr_chain *next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1702 } ADDRESS_BLOCK, *ADDRESS_CHAIN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1703 /* NB: empty and unavailable blocks are concatenated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1704
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1705 static ADDRESS_CHAIN addr_chain = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1706 /* Start off the address block chain with a humongous address block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1707 which is empty to start with. Note that addr_chain is invariant
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1708 WRT the addition/deletion of address blocks because of the assert
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1709 in Coalesce() and the strict ordering of blocks by their address
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1710 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1711 static void Addr_Block_initialize()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1712 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1713 MEMMETER( MVAL( M_Addrlist_Size )++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1714 addr_chain = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1715 addr_chain->next = 0; /* Last block in chain */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1716 addr_chain->sz = 0x0c000000; /* Size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1717 addr_chain->addr = (POINTER) (0x04000000 | DATA_SEG_BITS);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1718 addr_chain->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1719 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1720
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1721 /* Coalesce address blocks if they are contiguous. Only empty and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1722 unavailable slots are coalesced. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1723 static void Coalesce_Addr_Blocks()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1724 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1725 ADDRESS_CHAIN p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1726 for (p = addr_chain; p; p = p->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1727 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1728 while (p->next && p->flag == p->next->flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1729 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1730 ADDRESS_CHAIN np;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1731 np = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1732
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1733 if (p->flag == occupied) break; /* No cigar */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1734
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1735 /* Check if the addresses are contiguous. */
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1736 if (p->addr + p->sz != np->addr) break;
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1737
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1738 MEMMETER( MVAL( M_Addrlist_Size )--)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1739 /* We can coalesce these two. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1740 p->sz += np->sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1741 p->next = np->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1742 assert( np != addr_chain ); /* We're not freeing the head of the list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1743 UNDERLYING_FREE( np );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1744 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1745 } /* for all p */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1746 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1748 /* Get an empty address block of specified size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1749 static VM_ADDR New_Addr_Block( SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1750 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1751 ADDRESS_CHAIN p = addr_chain;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1752 VM_ADDR new_addr = VM_FAILURE_ADDR;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1753 for (; p; p = p->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1754 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1755 if (p->flag == empty && p->sz > sz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1756 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1757 /* Create a new entry following p which is empty. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1758 ADDRESS_CHAIN remainder = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ) );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1759 remainder->next = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1760 remainder->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1761 remainder->addr = p->addr + sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1762 remainder->sz = p->sz - sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1763
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1764 MEMMETER( MVAL( M_Addrlist_Size )++)
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1765
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1766 /* Now make p become an occupied block with the appropriate size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1767 p->next = remainder;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1768 p->sz = sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1769 new_addr = mmap( (VM_ADDR) p->addr, p->sz, PROT_READ|PROT_WRITE,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1770 MAP_FLAGS, DEV_ZERO_FD, 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1771 if (new_addr == VM_FAILURE_ADDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1772 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1773 p->flag = unavailable;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1774 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1775 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1776 p->flag = occupied;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1777 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1778 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1779 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1780 Coalesce_Addr_Blocks();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1781 return new_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1782 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1783
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1784 /* Free an address block. We mark the block as being empty, and attempt to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1785 do any coalescing that may have resulted from this. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1786 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1787 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1788 ADDRESS_CHAIN p = addr_chain;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1789 for (; p; p = p->next )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1790 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1791 if (p->addr == addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1792 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1793 if (p->sz != sz) abort(); /* ACK! Shouldn't happen at all. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1794 munmap( (VM_ADDR) p->addr, p->sz );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1795 p->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1796 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1797 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1798 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1799 if (!p) abort(); /* Can't happen... we've got a block to free which is not in
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1800 the address list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1801 Coalesce_Addr_Blocks();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1802 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1803 #else /* !MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1804 /* This is an alternate (simpler) implementation in cases where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1805 address is picked by the kernel. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1806
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1807 static void Addr_Block_initialize(void)
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1808 {
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1809 /* Nothing. */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1810 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1811
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1812 static VM_ADDR New_Addr_Block( SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1813 {
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1814 return mmap (0, sz, PROT_READ|PROT_WRITE, MAP_FLAGS,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1815 DEV_ZERO_FD, 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1816 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1817
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1818 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1819 {
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1820 munmap ((caddr_t) addr, sz );
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1821 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1822
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1823 #endif /* MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1824
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1825
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1826 /* IMPLEMENTATION OF EXPORTED RELOCATOR INTERFACE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1827
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1828 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1829 r_alloc( POINTER, SIZE ): Allocate a relocatable area with the start
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1830 address aliased to the first parameter.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1831 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1832
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1833 POINTER r_alloc (POINTER *ptr, SIZE size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1834 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1835 r_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1836 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1837 MMAP_HANDLE mh;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1838
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1839 switch(r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1840 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1841 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1842 abort();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1843 case 1:
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1844 *ptr = (POINTER) UNDERLYING_MALLOC(size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1845 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1846 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1847 mh = new_mmap_handle( size );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1848 if (mh)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1849 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1850 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1851 SIZE mmapped_size = ROUNDUP( size + hysteresis );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1852 MEMMETER( MVAL(M_Map)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1853 MEMMETER( MVAL(M_Pages_Map) += (mmapped_size/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1854 MEMMETER( MVAL(M_Wastage) += mmapped_size - size )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1855 MEMMETER( MVAL(M_Live_Pages) += (mmapped_size/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1856 mh->vm_addr = New_Addr_Block( mmapped_size );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1857 if (mh->vm_addr == VM_FAILURE_ADDR) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1858 free_mmap_handle( mh ); /* Free the loser */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1859 *ptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1860 return 0; /* ralloc failed due to mmap() failure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1861 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1862 MHASH_ADD( mh->vm_addr, mh );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1863 mh->space_for = mmapped_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1864 mh->aliased_address = ptr;
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1865 *ptr = (POINTER) mh->vm_addr;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1866 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1867 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1868 *ptr = 0; /* Malloc of block failed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1869 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1870 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1871 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1872 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1873
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1874 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1875 Store 0 in *PTR to show there's no block allocated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1876
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1877 void r_alloc_free (POINTER *ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1878 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1879 r_alloc_free (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1880 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1881 switch( r_alloc_initialized) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1882 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1883 abort();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1884
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1885 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1886 UNDERLYING_FREE( *ptr ); /* Certain this is from the heap. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1887 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1888
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1889 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1890 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1891 MMAP_HANDLE dead_handle = find_mmap_handle( ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1892 /* Check if we've got it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1893 if (dead_handle == 0) /* Didn't find it in the list of mmap handles */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1894 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1895 UNDERLYING_FREE( *ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1896 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1897 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1898 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1899 MEMMETER( MVAL( M_Wastage ) -= (dead_handle->space_for - dead_handle->size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1900 MEMMETER( MVAL( M_Live_Pages ) -= (dead_handle->space_for / page_size ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1901 MEMMETER(MVAL(M_Unmap)++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1902 MHASH_DEL( dead_handle->vm_addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1903 Free_Addr_Block( dead_handle->vm_addr, dead_handle->space_for );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1904 free_mmap_handle (dead_handle);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1905 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1906 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1907 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1908 } /* r_alloc_initialized */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1909 *ptr = 0; /* Zap the pointer's contents. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1910 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1911
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1912 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1913
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1914 Change *PTR to reflect the new bloc, and return this value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1915
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1916 If more memory cannot be allocated, then leave *PTR unchanged, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1917 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1918
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1919 POINTER r_re_alloc (POINTER *ptr, SIZE sz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1920 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1921 r_re_alloc (POINTER *ptr, SIZE sz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1922 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1923 if (r_alloc_initialized == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1924 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1925 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1926 return 0; /* suppress compiler warning */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1927 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1928 else if (r_alloc_initialized == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1929 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1930 POINTER tmp = (POINTER) realloc(*ptr, sz);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1931 if (tmp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1932 *ptr = tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1933 return tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1934 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1935 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1936 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1937 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1938 SIZE actual_sz = ROUNDUP( sz + hysteresis );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1939 MMAP_HANDLE h = find_mmap_handle( ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1940 VM_ADDR new_vm_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1941
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1942 if ( h == 0 ) /* Was allocated using malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1943 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1944 POINTER tmp = (POINTER) UNDERLYING_REALLOC(*ptr, sz);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1945 if (tmp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1946 *ptr = tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1947 return tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1948 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1949
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1950 MEMMETER(
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1951 MVAL(M_Average_Bumpval) =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1952 (((double) MVAL(M_Remap) * MVAL(M_Average_Bumpval)) + (sz - h->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1953 / (double) (MVAL(M_Remap) + 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1954 MEMMETER(MVAL(M_Remap)++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1955 if (h->space_for > sz) /* We've got some more room */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1956 { /* Also, if a shrinkage was asked for. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1957 MEMMETER( MVAL(M_Didnt_Copy)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1958 MEMMETER( MVAL(M_Wastage) -= (sz - h->size))
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1959 /* We're pretty dumb at handling shrinkage. We should check for
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1960 a larger gap than the standard hysteresis allowable, and if so,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1961 shrink the number of pages. Right now, we simply reset the size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1962 component and return. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1963 h->size = sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1964 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1965 }
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1966
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1967 new_vm_addr = New_Addr_Block( actual_sz );
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1968 if (new_vm_addr == VM_FAILURE_ADDR)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1969 {/* Failed to realloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1970 /* *ptr = 0; */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1971 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1972 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1973
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1974 MHASH_ADD( new_vm_addr, h );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1975 /* We got a block OK: now we should move the old contents to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1976 new address. We use the old size of this block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1977 memmove(new_vm_addr, h->vm_addr, h->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1978 MHASH_DEL( h->vm_addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1979 Free_Addr_Block( h->vm_addr, h->space_for ); /* Unmap old area. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1980
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1981 MEMMETER( MVAL( M_Copy_Pages ) += (h->space_for/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1982 MEMMETER( MVAL( M_Live_Pages ) -= (h->space_for / page_size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1983 MEMMETER( MVAL( M_Live_Pages ) += (actual_sz / page_size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1984 MEMMETER( MVAL( M_Wastage ) -= (h->space_for - h->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1985 MEMMETER( MVAL( M_Wastage ) += (actual_sz - sz) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1986
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1987 /* Update block datastructure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1988 h->space_for = actual_sz; /* New total space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1989 h->size = sz; /* New (requested) size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1990 h->vm_addr = new_vm_addr; /* New VM start address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1991 h->aliased_address = ptr; /* Change alias to reflect block relocation. */
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
1992 *ptr = (POINTER) h->vm_addr;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1993 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1994 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1995 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1996
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1997
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1998 /* Initialize various things for memory allocation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1999 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2000 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2001 init_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2002 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2003 int i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2004 if (r_alloc_initialized > 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2005 return; /* used to return 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2006
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2007 if (++r_alloc_initialized == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2008 return; /* used to return 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2009
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2010 Addr_Block_initialize(); /* Initialize the address picker, if required. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2011 page_size = PAGE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2012 assert( page_size > 0 ); /* getpagesize() bogosity check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2013
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2014 #ifndef MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2015 DEV_ZERO_FD = open( "/dev/zero", O_RDWR );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2016 if (DEV_ZERO_FD < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2017 /* Failed. Perhaps we should abort here? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2018 return; /* used to return 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2019 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2020
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2021 #ifdef MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2022 for(i = 0; i < N_Meterables; i++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2023 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2024 meter[i] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2025 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2026 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2027 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2028
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2029 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2030 syms_of_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2031 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2032 #ifdef MMAP_METERING
84
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2033 defsymbol (&Qmm_times_mapped, "mmap-times-mapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2034 defsymbol (&Qmm_pages_mapped, "mmap-pages-mapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2035 defsymbol (&Qmm_times_unmapped, "mmap-times-unmapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2036 defsymbol (&Qmm_times_remapped, "mmap-times-remapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2037 defsymbol (&Qmm_didnt_copy, "mmap-didnt-copy");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2038 defsymbol (&Qmm_pages_copied, "mmap-pages-copied");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2039 defsymbol (&Qmm_average_bumpval, "mmap-average-bumpval");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2040 defsymbol (&Qmm_wastage, "mmap-wastage");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2041 defsymbol (&Qmm_live_pages, "mmap-live-pages");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2042 defsymbol (&Qmm_addr_looked_up, "mmap-had-to-look-up-address");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2043 defsymbol (&Qmm_hash_worked, "mmap-hash-table-worked");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
2044 defsymbol (&Qmm_addrlist_size, "mmap-addrlist-size");
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
2045 DEFSUBR (Fmmap_allocator_status);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2046 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2047 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2048
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2049 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2050 vars_of_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2051 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2052 DEFVAR_INT ("mmap-hysteresis", &mmap_hysteresis /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2053 Extra room left at the end of an allocated arena,
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
2054 so that a re-alloc requesting extra space smaller than this
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2055 does not actually cause a new arena to be allocated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2056
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
2057 A negative value is considered equal to zero. This is the
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 163
diff changeset
2058 minimum amount of space guaranteed to be left at the end of
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2059 the arena. Because allocation happens in multiples of the OS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2060 page size, it is possible for more space to be left unused.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2061 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2062 mmap_hysteresis = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2063 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2064
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2065 #endif /* HAVE_MMAP */