annotate src/ralloc.c @ 181:bfd6434d15b3 r20-3b17

Import from CVS: tag r20-3b17
author cvs
date Mon, 13 Aug 2007 09:53:19 +0200
parents 0132846995bd
children 3d6bfa290dbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Block-relocating memory allocator.
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,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* NOTES:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 rather than all of them. This means allowing for a possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 hole between the first bloc and the end of malloc storage. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "lisp.h" /* Needed for VALBITS. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #undef NULL
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 /* 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
35 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
36 to has a size of 1. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #if 0 /* Arithmetic on void* is a GCC extension. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #ifdef __STDC__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 typedef void *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 typedef unsigned char *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 /* Unconditionally use unsigned char * for this. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 typedef unsigned char *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 typedef unsigned long SIZE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #include "getpagesize.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 #else /* Not emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #include <stddef.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 typedef size_t SIZE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 typedef void *POINTER;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #include <malloc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #endif /* emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #define safe_bcopy(x, y, z) memmove (y, x, z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #define NIL ((POINTER) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 #ifndef HAVE_MMAP
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 /* A flag to indicate whether we have initialized ralloc yet. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 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
76 machines, the dumping procedure makes all static variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 read-only. On these machines, the word static is #defined to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 the empty string, meaning that r_alloc_initialized becomes an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 automatic variable, and loses its value each time Emacs is started up. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 static int r_alloc_initialized = 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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 /* Declarations for working with the malloc, ralloc, and system breaks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 /* Function to set the real break value. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 static POINTER (*real_morecore) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 /* The break value, as seen by malloc (). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 static POINTER virtual_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 /* The break value, viewed by the relocatable blocs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 static POINTER break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 /* The REAL (i.e., page aligned) break value of the process. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 static POINTER page_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 /* 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
98 static int page_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 /* Whenever we get memory from the system, get this many extra bytes. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 must be a multiple of page_size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 static int extra_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 /* Macros for rounding. Note that rounding to any value is possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 by changing the definition of PAGE. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 #define PAGE (getpagesize ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 & ~(page_size - 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 /* Functions to get and return memory from the system. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 /* Obtain SIZE bytes of space. If enough space is not presently available
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 in our process reserve, (i.e., (page_break_value - break_value)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 this means getting more page-aligned space from the system.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 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
119 the memory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 obtain (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 SIZE already_available = page_break_value - break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 if (already_available < size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 SIZE get = ROUNDUP (size - already_available);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 /* Get some extra, so we can come here less often. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 get += extra_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 if ((*real_morecore) (get) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 page_break_value += get;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 break_value += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 /* Obtain SIZE bytes of space and return a pointer to the new area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 If we could not allocate the space, return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 static POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 get_more_space (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 POINTER ptr = break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 if (obtain (size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 return ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 /* Note that SIZE bytes of space have been relinquished by the process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 If SIZE is more than a page, return the space to the system. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 relinquish (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 POINTER new_page_break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 int excess;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 break_value -= size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 new_page_break = (POINTER) ROUNDUP (break_value);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 excess = (char *) page_break_value - (char *) new_page_break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (excess > extra_bytes * 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* Keep extra_bytes worth of empty space.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 And don't free anything unless we can free at least extra_bytes. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 if ((*real_morecore) (extra_bytes - excess) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 page_break_value += extra_bytes - excess;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 /* Zero the space from the end of the "official" break to the actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 break, so that bugs show up faster. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 memset (break_value, 0, ((char *) page_break_value - (char *) break_value));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 /* The meat - allocating, freeing, and relocating blocs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 /* These structures are allocated in the malloc arena.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 The linked list is kept in order of increasing '.data' members.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 The data blocks abut each other; if b->next is non-nil, then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 b->data + b->size == b->next->data. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 typedef struct bp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 struct bp *next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 struct bp *prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 POINTER *variable;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 POINTER data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 SIZE size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 } *bloc_ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 #define NIL_BLOC ((bloc_ptr) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 #define BLOC_PTR_SIZE (sizeof (struct bp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 /* Head and tail of the list of relocatable blocs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 static bloc_ptr first_bloc, last_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 /* Find the bloc referenced by the address in PTR. Returns a pointer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 to that block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 static bloc_ptr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 find_bloc (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 bloc_ptr p = first_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 while (p != NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 if (p->variable == ptr && p->data == *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 return p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 p = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 return p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 /* 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
224 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
225 memory for the new block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 static bloc_ptr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 get_bloc (SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 bloc_ptr new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 || ! (new_bloc->data = get_more_space (size)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 if (new_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 free (new_bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 new_bloc->size = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 new_bloc->next = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 new_bloc->variable = (POINTER *) NIL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 if (first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 new_bloc->prev = last_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 last_bloc->next = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 last_bloc = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 first_bloc = last_bloc = new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 new_bloc->prev = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 return new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 /* Relocate all blocs from BLOC on upward in the list to the zone
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 indicated by ADDRESS. Direction of relocation is determined by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 the position of ADDRESS relative to BLOC->data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 If BLOC is NIL_BLOC, nothing is done.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 Note that ordering of blocs is not affected by this function. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 relocate_some_blocs (bloc_ptr bloc, POINTER address)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 if (bloc != NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 SIZE offset = address - bloc->data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 SIZE data_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 bloc_ptr b;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 for (b = bloc; b != NIL_BLOC; b = b->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 data_size += b->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 b->data += offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 *b->variable = b->data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 memmove (address, address - offset, data_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 /* Free BLOC from the chain of blocs, relocating any blocs above it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 and returning BLOC->size bytes to the free area. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 free_bloc (bloc_ptr bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 if (bloc == first_bloc && bloc == last_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 first_bloc = last_bloc = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 else if (bloc == last_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 last_bloc = bloc->prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 last_bloc->next = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 else if (bloc == first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 first_bloc = bloc->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 first_bloc->prev = NIL_BLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 bloc->next->prev = bloc->prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 bloc->prev->next = bloc->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 relocate_some_blocs (bloc->next, bloc->data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 relinquish (bloc->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 free (bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 /* Interface routines. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 static int use_relocatable_buffers;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 /* Obtain SIZE bytes of storage from the free pool, or the system, as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 necessary. If relocatable blocs are in use, this means relocating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 them. This function gets plugged into the GNU malloc's __morecore
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 hook.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 We provide hysteresis, never relocating by less than extra_bytes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 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
331 __morecore hook values - in particular, __default_morecore in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 GNU malloc package. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 r_alloc_sbrk (long size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 /* This is the first address not currently available for the heap. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 POINTER top;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 /* Amount of empty space below that. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 /* It is not correct to use SIZE here, because that is usually unsigned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 ptrdiff_t would be okay, but is not always available.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 `long' will work in all cases, in practice. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 long already_available;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 POINTER ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 if (! use_relocatable_buffers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 return (*real_morecore) (size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 top = first_bloc ? first_bloc->data : page_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 already_available = (char *) top - (char *) virtual_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 /* Do we not have enough gap already? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 if (size > 0 && already_available < size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 /* Get what we need, plus some extra so we can come here less often. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 SIZE get = size - already_available + extra_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 if (! obtain (get))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 if (first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 relocate_some_blocs (first_bloc, first_bloc->data + get);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 /* Zero out the space we just allocated, to help catch bugs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 quickly. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 memset (virtual_break_value, 0, get);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 /* Can we keep extra_bytes of gap while freeing at least extra_bytes? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 else if (size < 0 && already_available - size > 2 * extra_bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 /* Ok, do so. This is how many to free. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 SIZE give_back = already_available - size - extra_bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 if (first_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 relocate_some_blocs (first_bloc, first_bloc->data - give_back);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 relinquish (give_back);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 ptr = virtual_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 virtual_break_value += size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 return ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 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
387 which will use the data area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 If we can't allocate the necessary memory, set *PTR to zero, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 r_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 bloc_ptr new_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 if (! r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 init_ralloc ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 new_bloc = get_bloc (size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 if (new_bloc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 new_bloc->variable = ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 *ptr = new_bloc->data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 *ptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 Store 0 in *PTR to show there's no block allocated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 r_alloc_free (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 bloc_ptr dead_bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 dead_bloc = find_bloc (ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 if (dead_bloc == NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 free_bloc (dead_bloc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 *ptr = 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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 Do this by shifting all blocks above this one up in memory, unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 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
431 do nothing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 Change *PTR to reflect the new bloc, and return this value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 If more memory cannot be allocated, then leave *PTR unchanged, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 r_re_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 bloc_ptr bloc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 bloc = find_bloc (ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 if (bloc == NIL_BLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 if (size <= bloc->size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 /* Wouldn't it be useful to actually resize the bloc here? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 if (! obtain (size - bloc->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 relocate_some_blocs (bloc->next, bloc->data + size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 /* Zero out the new space in the bloc, to help catch bugs faster. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 memset (bloc->data + bloc->size, 0, size - bloc->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 /* Indicate that this block has a new size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 bloc->size = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 /* The hook `malloc' uses for the function which gets more space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 from the system. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 extern POINTER (*__morecore) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 /* Initialize various things for memory allocation. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 init_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 if (r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 r_alloc_initialized = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 real_morecore = __morecore;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 __morecore = r_alloc_sbrk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 virtual_break_value = break_value = (*real_morecore) (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 if (break_value == NIL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 page_size = PAGE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 extra_bytes = ROUNDUP (50000);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 page_break_value = (POINTER) ROUNDUP (break_value);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 /* From eirik@elf.IThaca.ny.US (Eirik Fuller):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 The extra call to real_morecore guarantees that the end of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 address space is a multiple of page_size, even if page_size is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 not really the page size of the system running the binary in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 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
495 system with one page size and run on a system with a smaller page
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 size. (Such as compiling on a Sun 4/260 4.1.3 and running on a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 Sun 4/65 4.1.3: 8k pages at compile time, 4k pages at run time.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (*real_morecore) (page_break_value - break_value);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 /* 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
502 even though it is after the sbrk value. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 /* Doubly true, with the additional call that explicitly adds the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 rest of that page to the address space. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 memset (break_value, 0, (page_break_value - break_value));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 /* Also from eirik@elf.IThaca.ny.US */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 virtual_break_value = break_value = page_break_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 use_relocatable_buffers = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 #else /* HAVE_MMAP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 A relocating allocator built using the mmap(2) facility available
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 in some OSes. Based on another version written by Paul Flinders,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 from which code (and comments) are snarfed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 The OS should support mmap() with MAP_ANONYMOUS attribute, or have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 /dev/zero. It should support private memory mapping.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 Paul Flinders wrote a version which works well for systems that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 allow callers to specify (virtual) addresses to mmap().
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 Unfortunately, such a scheme doesn't work for certain systems like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 HP-UX that have a system-wide virtual->real address map, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 consequently impose restrictions on the virtual address values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 permitted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 NB: The mapping scheme in HP-UX is motivated by the inverted page
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 table design in some HP processors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 This alternate implementation allows for the addresses to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 optionally chosen by the system. Fortunately, buffer allocation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 doesn't insist upon contiguous memory which Flinders' scheme
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 provides, and this one doesn't.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 We don't really provide for hysteresis here, but add some metering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 to monitor how poorly the allocator actually works. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 documentation for `mmap-hysteresis'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 This implementation actually cycles through the blocks allocated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 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
542 Unfortunately, this is O(n) in the number of mmapped blocks. (Not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 really, as we have a hash table which tries to reduce the cost.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 Also, this dereferences the pointer passed, so it would cause a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 segfault if garbage was passed to it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 #include <sys/mman.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 typedef void *VM_ADDR; /* VM addresses */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 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
553
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 /* Configuration for relocating allocator. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 /* #define MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 /* Define this if you want Emacs to manage the address table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 It is not recommended unless you have major problems with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 default scheme, which allows the OS to pick addresses. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 /* USELESS_LOWER_ADDRESS_BITS defines the number of bits which can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 discarded while computing the hash, as they're always zero. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 default is appropriate for a page size of 4096 bytes. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 #define USELESS_LOWER_ADDRESS_BITS 12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 /* Size of hash table for inverted VM_ADDR->MMAP_HANDLE lookup */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 #define MHASH_PRIME 89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 /* Whether we want to enable metering of some ralloc performance.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 This incurs a constant penalty for each mmap operation. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 #define MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 /* Rename the following to protect against a some smartness elsewhere.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 We need access to the allocator used for non-mmap allocation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 elsewhere, in case we get passed a handle that we didn't allocate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 ourselves. Currently, this default allocator is also used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 maintain local structures for relocatable blocks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 #define UNDERLYING_MALLOC malloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 #define UNDERLYING_FREE free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 #define UNDERLYING_REALLOC realloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 /* MAP_ADDRCHOICE_FLAG is set to MAP_FIXED if MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 is defined, and MAP_VARIABLE otherwise. Some losing systems don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 define the _FIXED/_VARIABLE flags, in which case it is set to 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 #ifdef MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 # ifdef MAP_FIXED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 # define MAP_ADDRCHOICE_FLAG MAP_FIXED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 # endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 #else /* !MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 # ifdef MAP_VARIABLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 # define MAP_ADDRCHOICE_FLAG MAP_VARIABLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 # endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 #endif /* MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 /* Default case. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 #ifndef MAP_ADDRCHOICE_FLAG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 # define MAP_ADDRCHOICE_FLAG 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 #endif /* MAP_ADDRCHOICE_FLAG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 #ifdef MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG | MAP_ANONYMOUS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 #endif /* MAP_ANONYMOUS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 /* (ptf): A flag to indicate whether we have initialized ralloc yet. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 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
617 machines, the dumping procedure makes all static variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 read-only. On these machines, the word static is #defined to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 the empty string, meaning that r_alloc_initialized becomes an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 automatic variable, and loses its value each time Emacs is started up.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 If we're using mmap this flag has three possible values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 0 - initial value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 1 - Normal value when running temacs. In this case buffers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 are allocated using malloc so that any data that they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 contain becomes part of the undumped executable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 2 - Normal value when running emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 static int r_alloc_initialized = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 /* (ptf): Macros for rounding. Note that rounding to any value is possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 by changing the definition of PAGE. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 #define PAGE (getpagesize ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 #define PAGES_FOR(size) (((unsigned long int) (size) + page_size - 1)/page_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 #define ROUNDUP(size) ((unsigned long int)PAGES_FOR(size)*page_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 /* DEV_ZERO_FD is -1 normally, but for systems without MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 points to a file descriptor opened on /dev/zero */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 static int DEV_ZERO_FD = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 /* We actually need a datastructure that can be usefully structured
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 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
645 address, ie a hash-table. Maybe the XEmacs hash table can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 coaxed enough. At the moment, we use lookup on a hash-table to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 decide whether to do an O(n) search on the malloced block list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 Addresses are hashed to a bucket modulo MHASH_PRIME */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 /* We settle for a standard doubly-linked-list. The dynarr type isn't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 very amenable to deletion of items in the middle, so we conjure up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 yet another stupid datastructure. The structure is maintained as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 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
655 right neighbours. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 static void init_MHASH_table (void); /* Forward reference */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 typedef struct alloc_dll
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 size_t size; /* #bytes currently in use */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 size_t space_for; /* #bytes we really have */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 POINTER* aliased_address; /* Address of aliased variable, to tweak if relocating */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 VM_ADDR vm_addr; /* VM address returned by mmap */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 struct alloc_dll *left; /* Left link in circular doubly linked list */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 struct alloc_dll *right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 } *MMAP_HANDLE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 static MMAP_HANDLE mmap_start = 0; /* Head of linked list */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 static size_t page_size = 0; /* Size of VM pages */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 static int mmap_hysteresis; /* Should be size_t, really. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 /* Get a new handle for a fresh block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 new_mmap_handle (size_t nsiz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
677 MMAP_HANDLE h = (MMAP_HANDLE) UNDERLYING_MALLOC( sizeof (struct alloc_dll));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 if ( h == 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 h->size = nsiz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 if (mmap_start == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 init_MHASH_table ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 mmap_start = h; mmap_start->left = h; mmap_start->right = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 MMAP_HANDLE prev = mmap_start->left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 MMAP_HANDLE nex = mmap_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 /* Four pointers need fixing. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 h->right = nex;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 h->left = prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 prev->right = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 nex->left = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 /* Find a handle given the aliased address using linear search. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 find_mmap_handle_lsearch (POINTER *alias)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 MMAP_HANDLE h = mmap_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 if (h == 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 do {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 if (h->aliased_address == alias && *alias == h->vm_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 h = h->right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 } while( h != mmap_start );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 return 0; /* Bogus alias passed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 /* Free a handle. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 free_mmap_handle (MMAP_HANDLE h)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 MMAP_HANDLE prev = h->left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 MMAP_HANDLE nex = h->right;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 if (prev == h || nex == h) /* In fact, this should be && */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 { /* We're the singleton dll */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 UNDERLYING_FREE( h ); /* Free the sole item */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 mmap_start = 0; return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 else if (h == mmap_start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 mmap_start = nex; /* Make sure mmap_start isn't bogus. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 prev->right = nex;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 nex->left = prev;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 UNDERLYING_FREE( h );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 /* A simple hash table to speed up the inverted lookup of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 VM_ADDR->MMAP_HANDLE. We maintain the number of hits for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 particular bucket. We invalidate a hash table entry during block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 deletion if the hash has cached the deleted block's address. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 /* Simple hash check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 int n_hits; /* How many addresses map to this? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 MMAP_HANDLE handle; /* What is the current handle? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 VM_ADDR addr; /* What is it's VM address? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 } MHASH_HITS[ MHASH_PRIME ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 init_MHASH_table (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 int i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 for (; i < MHASH_PRIME; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 MHASH_HITS[i].n_hits = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 MHASH_HITS[i].addr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 MHASH_HITS[i].handle = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 /* Compute the hash value for an address. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 MHASH (VM_ADDR addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 {
116
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
760 #if (LONGBITS == 64)
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
761 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
762 #else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 unsigned int addr_shift = (unsigned int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
116
9f59509498e1 Import from CVS: tag r20-1b10
cvs
parents: 84
diff changeset
764 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 int hval = addr_shift % MHASH_PRIME; /* We could have addresses which are -ve
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 when converted to signed ints */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 return ((hval >= 0) ? hval : MHASH_PRIME + hval);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 /* Add a VM address with it's corresponding handle to the table. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 MHASH_ADD (VM_ADDR addr, MMAP_HANDLE h)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 int kVal = MHASH( addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 if (MHASH_HITS[kVal].n_hits++ == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 { /* Only overwrite the table if there were no hits so far. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 MHASH_HITS[kVal].addr = addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 MHASH_HITS[kVal].handle = h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 }
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 /* Delete a VM address entry from the hash table. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 MHASH_DEL (VM_ADDR addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 int kVal = MHASH( addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 MHASH_HITS[kVal].n_hits--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 if (addr == MHASH_HITS[kVal].addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 MHASH_HITS[kVal].addr = 0; /* Invalidate cache. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 MHASH_HITS[kVal].handle = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 /* End of hash buckets */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 /* Metering malloc performance. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 #ifdef MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 /* If we're metering, we introduce some extra symbols to aid the noble
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 cause of bloating XEmacs core size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 Lisp_Object Qmm_times_mapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 Lisp_Object Qmm_pages_mapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 Lisp_Object Qmm_times_unmapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 Lisp_Object Qmm_times_remapped;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 Lisp_Object Qmm_didnt_copy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 Lisp_Object Qmm_pages_copied;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 Lisp_Object Qmm_average_bumpval;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 Lisp_Object Qmm_wastage;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 Lisp_Object Qmm_live_pages;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 Lisp_Object Qmm_addr_looked_up;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 Lisp_Object Qmm_hash_worked;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 Lisp_Object Qmm_addrlist_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 #define M_Map 0 /* How many times allocated? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 #define M_Pages_Map 1 /* How many pages allocated? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 #define M_Unmap 2 /* How many times freed? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 #define M_Remap 3 /* How many times increased in size? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 #define M_Didnt_Copy 4 /* How many times didn't need to copy? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 #define M_Copy_Pages 5 /* Total # pages copied */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 #define M_Average_Bumpval 6 /* Average bump value */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 #define M_Wastage 7 /* Remaining (unused space) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 #define M_Live_Pages 8 /* #live pages */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 #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
825 #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
826 #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
827
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 #define N_Meterables 12 /* Total number of meterables */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 #define MEMMETER(x) {x;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 #define MVAL(x) (meter[x])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 #define MLVAL(x) (make_int (meter[x]))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 static int meter[N_Meterables];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
834 DEFUN ("mmap-allocator-status", Fmmap_allocator_status, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 Return some information about mmap-based allocator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 mmap-addrlist-size: number of entries in address picking list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 mmap-times-mapped: number of times r_alloc was called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 mmap-pages-mapped: number of pages mapped by r_alloc calls only.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 mmap-times-unmapped: number of times r_free was called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 mmap-times-remapped: number of times r_re_alloc was called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 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
843 mmap-pages-copied: total number of pages copied.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 mmap-average-bumpval: average increase in size demanded to re-alloc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 mmap-wastage: total number of bytes allocated, but not currently in use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 mmap-live-pages: total number of pages live.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
847 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
848 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 Lisp_Object result;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 result = Fcons (Fcons (Qmm_addrlist_size, MLVAL (M_Addrlist_Size)), Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 result = Fcons (Fcons (Qmm_hash_worked, MLVAL (M_Hash_Worked)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 result = Fcons (Fcons (Qmm_addr_looked_up, MLVAL (M_Address_Lookup)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 result = Fcons (Fcons (Qmm_live_pages, MLVAL (M_Live_Pages)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 result = Fcons (Fcons (Qmm_wastage, MLVAL (M_Wastage)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 result = Fcons (Fcons (Qmm_average_bumpval, MLVAL (M_Average_Bumpval)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 result = Fcons (Fcons (Qmm_pages_copied, MLVAL (M_Copy_Pages)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 result = Fcons (Fcons (Qmm_didnt_copy, MLVAL (M_Didnt_Copy)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 result = Fcons (Fcons (Qmm_times_remapped, MLVAL (M_Remap)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 result = Fcons (Fcons (Qmm_times_unmapped, MLVAL (M_Unmap)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 result = Fcons (Fcons (Qmm_pages_mapped, MLVAL (M_Pages_Map)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 result = Fcons (Fcons (Qmm_times_mapped, MLVAL (M_Map)), result);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 return result;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 #else /* !MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 #define MEMMETER(x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 #define MVAL(x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 static MMAP_HANDLE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 find_mmap_handle (POINTER *alias)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 int kval = MHASH( *alias );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 MEMMETER( MVAL(M_Address_Lookup)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 switch( MHASH_HITS[kval].n_hits)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 MEMMETER( MVAL( M_Hash_Worked )++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 if (*alias == MHASH_HITS[kval].addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 MEMMETER( MVAL( M_Hash_Worked) ++ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 return MHASH_HITS[kval].handle;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 /* FALL THROUGH */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 return find_mmap_handle_lsearch( alias );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 } /* switch */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 Some kernels don't like being asked to pick addresses for mapping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 themselves---IRIX is known to become extremely slow if mmap is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 passed a ZERO as the first argument. In such cases, we use an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 address map which is managed local to the XEmacs process. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 address map maintains an ordered linked list of (address, size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 occupancy) triples ordered by the absolute address. Initially, a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 large address area is marked as being empty. The address picking
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 scheme takes bites off the first block which is still empty and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 large enough. If mmap with the specified address fails, it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 marked unavailable and not attempted thereafter. The scheme will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 keep fragmenting the large empty block until it finds an address
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 which can be successfully mmapped, or until there are no free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 blocks of the given size left.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 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
915 fragmentation of the first part of memory earmarked for this
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 purpose. [ACP Vol I]. We can't use the workaround of using a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 randomized first fit because we don't want to presume too much
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 about the memory map. Instead, we try to coalesce empty or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 unavailable blocks at any available opportunity. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
921 /* Initialization procedure for address picking scheme */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
922 static void Addr_Block_initialize(void);
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
923
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
924 /* Get a suitable VM_ADDR via mmap */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
925 static VM_ADDR New_Addr_Block( SIZE sz );
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
926
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
927 /* Free a VM_ADDR allocated via New_Addr_Block */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
928 static void Free_Addr_Block( VM_ADDR addr, SIZE sz );
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 #ifdef MMAP_GENERATE_ADDRESSES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 /* Implementation of the three calls for address picking when XEmacs is incharge */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 /* The enum denotes the status of the following block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 typedef enum { empty = 0, occupied, unavailable } addr_status;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 typedef struct addr_chain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 POINTER addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 SIZE sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 addr_status flag;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 struct addr_chain *next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 } ADDRESS_BLOCK, *ADDRESS_CHAIN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 /* NB: empty and unavailable blocks are concatenated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 static ADDRESS_CHAIN addr_chain = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 /* Start off the address block chain with a humongous address block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 which is empty to start with. Note that addr_chain is invariant
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 WRT the addition/deletion of address blocks because of the assert
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 in Coalesce() and the strict ordering of blocks by their address
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 static void Addr_Block_initialize()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 MEMMETER( MVAL( M_Addrlist_Size )++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 addr_chain = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 addr_chain->next = 0; /* Last block in chain */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 addr_chain->sz = 0x0c000000; /* Size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 addr_chain->addr = (POINTER) (0x04000000 | DATA_SEG_BITS);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 addr_chain->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 /* Coalesce address blocks if they are contiguous. Only empty and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 unavailable slots are coalesced. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 static void Coalesce_Addr_Blocks()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 ADDRESS_CHAIN p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 for (p = addr_chain; p; p = p->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 while (p->next && p->flag == p->next->flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 ADDRESS_CHAIN np;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 np = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 if (p->flag == occupied) break; /* No cigar */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 /* Check if the addresses are contiguous. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 if (p->addr + p->sz != np->addr) break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 MEMMETER( MVAL( M_Addrlist_Size )--)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 /* We can coalesce these two. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 p->sz += np->sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 p->next = np->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 assert( np != addr_chain ); /* We're not freeing the head of the list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 UNDERLYING_FREE( np );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 } /* for all p */
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 /* Get an empty address block of specified size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 static VM_ADDR New_Addr_Block( SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 ADDRESS_CHAIN p = addr_chain;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 VM_ADDR new_addr = VM_FAILURE_ADDR;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 for (; p; p = p->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 if (p->flag == empty && p->sz > sz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 /* Create a new entry following p which is empty. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 ADDRESS_CHAIN remainder = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ) );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 remainder->next = p->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 remainder->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 remainder->addr = p->addr + sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 remainder->sz = p->sz - sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 MEMMETER( MVAL( M_Addrlist_Size )++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 /* Now make p become an occupied block with the appropriate size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 p->next = remainder;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 p->sz = sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 new_addr = mmap( (VM_ADDR) p->addr, p->sz, PROT_READ|PROT_WRITE,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 MAP_FLAGS, DEV_ZERO_FD, 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 if (new_addr == VM_FAILURE_ADDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 p->flag = unavailable;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 p->flag = occupied;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 Coalesce_Addr_Blocks();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 return new_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 /* 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
1025 do any coalescing that may have resulted from this. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 ADDRESS_CHAIN p = addr_chain;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 for (; p; p = p->next )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031 if (p->addr == addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 if (p->sz != sz) abort(); /* ACK! Shouldn't happen at all. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 munmap( (VM_ADDR) p->addr, p->sz );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 p->flag = empty;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 if (!p) abort(); /* Can't happen... we've got a block to free which is not in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 the address list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1041 Coalesce_Addr_Blocks();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 #else /* !MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 /* This is an alternate (simpler) implementation in cases where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 address is picked by the kernel. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046
74
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1047 static void Addr_Block_initialize(void)
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1048 {
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1049 /* Nothing. */
54cc21c15cbb Import from CVS: tag r20-0b32
cvs
parents: 70
diff changeset
1050 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 static VM_ADDR New_Addr_Block( SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054 return mmap( 0, sz, PROT_READ|PROT_WRITE, MAP_FLAGS,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 DEV_ZERO_FD, 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060 munmap( addr, sz );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 #endif /* MMAP_GENERATE_ADDRESSES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066 /* IMPLEMENTATION OF EXPORTED RELOCATOR INTERFACE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069 r_alloc( POINTER, SIZE ): Allocate a relocatable area with the start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 address aliased to the first parameter.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 POINTER r_alloc (POINTER *ptr, SIZE size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 r_alloc (POINTER *ptr, SIZE size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 MMAP_HANDLE mh;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 switch(r_alloc_initialized)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 abort();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083 case 1:
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1084 *ptr = (POINTER) UNDERLYING_MALLOC(size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087 mh = new_mmap_handle( size );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 if (mh)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091 SIZE mmapped_size = ROUNDUP( size + hysteresis );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 MEMMETER( MVAL(M_Map)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 MEMMETER( MVAL(M_Pages_Map) += (mmapped_size/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 MEMMETER( MVAL(M_Wastage) += mmapped_size - size )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1095 MEMMETER( MVAL(M_Live_Pages) += (mmapped_size/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1096 mh->vm_addr = New_Addr_Block( mmapped_size );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1097 if (mh->vm_addr == VM_FAILURE_ADDR) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1098 free_mmap_handle( mh ); /* Free the loser */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1099 *ptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1100 return 0; /* ralloc failed due to mmap() failure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1101 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1102 MHASH_ADD( mh->vm_addr, mh );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1103 mh->space_for = mmapped_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1104 mh->aliased_address = ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105 *ptr = mh->vm_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108 *ptr = 0; /* Malloc of block failed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 Store 0 in *PTR to show there's no block allocated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117 void r_alloc_free (POINTER *ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 r_alloc_free (POINTER *ptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121 switch( r_alloc_initialized) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1122 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1123 abort();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1125 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 UNDERLYING_FREE( *ptr ); /* Certain this is from the heap. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131 MMAP_HANDLE dead_handle = find_mmap_handle( ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1132 /* Check if we've got it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1133 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
1134 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1135 UNDERLYING_FREE( *ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1136 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1137 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1138 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1139 MEMMETER( MVAL( M_Wastage ) -= (dead_handle->space_for - dead_handle->size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1140 MEMMETER( MVAL( M_Live_Pages ) -= (dead_handle->space_for / page_size ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141 MEMMETER(MVAL(M_Unmap)++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1142 MHASH_DEL( dead_handle->vm_addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1143 Free_Addr_Block( dead_handle->vm_addr, dead_handle->space_for );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1144 free_mmap_handle (dead_handle);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148 } /* r_alloc_initialized */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1149 *ptr = 0; /* Zap the pointer's contents. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1150 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 Change *PTR to reflect the new bloc, and return this value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1156 If more memory cannot be allocated, then leave *PTR unchanged, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1157 return zero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 POINTER r_re_alloc (POINTER *ptr, SIZE sz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1160 POINTER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1161 r_re_alloc (POINTER *ptr, SIZE sz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1162 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1163 if (r_alloc_initialized == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1164 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1165 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1166 return 0; /* suppress compiler warning */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1167 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1168 else if (r_alloc_initialized == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1169 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1170 POINTER tmp = (POINTER) realloc(*ptr, sz);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1171 if (tmp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1172 *ptr = tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1173 return tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1174 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1175 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1176 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1177 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1178 SIZE actual_sz = ROUNDUP( sz + hysteresis );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1179 MMAP_HANDLE h = find_mmap_handle( ptr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1180 VM_ADDR new_vm_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1182 if ( h == 0 ) /* Was allocated using malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1183 {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 116
diff changeset
1184 POINTER tmp = (POINTER) UNDERLYING_REALLOC(*ptr, sz);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1185 if (tmp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1186 *ptr = tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1187 return tmp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1188 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1190 MEMMETER(
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1191 MVAL(M_Average_Bumpval) =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1192 (((double) MVAL(M_Remap) * MVAL(M_Average_Bumpval)) + (sz - h->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1193 / (double) (MVAL(M_Remap) + 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1194 MEMMETER(MVAL(M_Remap)++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1195 if (h->space_for > sz) /* We've got some more room */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1196 { /* Also, if a shrinkage was asked for. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1197 MEMMETER( MVAL(M_Didnt_Copy)++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1198 MEMMETER( MVAL(M_Wastage) -= (sz - h->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1199 /* We're pretty dumb at handling shrinkage. We should check for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1200 a larger gap than the standard hysteresis allowable, and if so,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1201 shrink the number of pages. Right now, we simply reset the size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1202 component and return. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1203 h->size = sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1204 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1205 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1206
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1207 new_vm_addr = New_Addr_Block( actual_sz );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1208 if (new_vm_addr == VM_FAILURE_ADDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1209 {/* Failed to realloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1210 /* *ptr = 0; */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1211 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1212 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1214 MHASH_ADD( new_vm_addr, h );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1215 /* 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
1216 new address. We use the old size of this block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1217 memmove(new_vm_addr, h->vm_addr, h->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1218 MHASH_DEL( h->vm_addr );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1219 Free_Addr_Block( h->vm_addr, h->space_for ); /* Unmap old area. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1221 MEMMETER( MVAL( M_Copy_Pages ) += (h->space_for/page_size) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1222 MEMMETER( MVAL( M_Live_Pages ) -= (h->space_for / page_size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1223 MEMMETER( MVAL( M_Live_Pages ) += (actual_sz / page_size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1224 MEMMETER( MVAL( M_Wastage ) -= (h->space_for - h->size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1225 MEMMETER( MVAL( M_Wastage ) += (actual_sz - sz) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1227 /* Update block datastructure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1228 h->space_for = actual_sz; /* New total space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1229 h->size = sz; /* New (requested) size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1230 h->vm_addr = new_vm_addr; /* New VM start address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1231 h->aliased_address = ptr; /* Change alias to reflect block relocation. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1232 *ptr = h->vm_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1233 return *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1234 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1235 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1238 /* Initialize various things for memory allocation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1239 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1240 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1241 init_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1242 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1243 int i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1244 if (r_alloc_initialized > 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1245 return; /* used to return 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1247 if (++r_alloc_initialized == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1248 return; /* used to return 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1250 Addr_Block_initialize(); /* Initialize the address picker, if required. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1251 page_size = PAGE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1252 assert( page_size > 0 ); /* getpagesize() bogosity check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1254 #ifndef MAP_ANONYMOUS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1255 DEV_ZERO_FD = open( "/dev/zero", O_RDWR );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1256 if (DEV_ZERO_FD < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1257 /* Failed. Perhaps we should abort here? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1258 return; /* used to return 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1259 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1261 #ifdef MMAP_METERING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1262 for(i = 0; i < N_Meterables; i++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1263 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1264 meter[i] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1265 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1266 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1267 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1269 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1270 syms_of_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272 #ifdef MMAP_METERING
84
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1273 defsymbol (&Qmm_times_mapped, "mmap-times-mapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1274 defsymbol (&Qmm_pages_mapped, "mmap-pages-mapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1275 defsymbol (&Qmm_times_unmapped, "mmap-times-unmapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1276 defsymbol (&Qmm_times_remapped, "mmap-times-remapped");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1277 defsymbol (&Qmm_didnt_copy, "mmap-didnt-copy");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1278 defsymbol (&Qmm_pages_copied, "mmap-pages-copied");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1279 defsymbol (&Qmm_average_bumpval, "mmap-average-bumpval");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1280 defsymbol (&Qmm_wastage, "mmap-wastage");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1281 defsymbol (&Qmm_live_pages, "mmap-live-pages");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1282 defsymbol (&Qmm_addr_looked_up, "mmap-had-to-look-up-address");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1283 defsymbol (&Qmm_hash_worked, "mmap-hash-table-worked");
ac0620f6398e Import from CVS: tag r20-0b92
cvs
parents: 74
diff changeset
1284 defsymbol (&Qmm_addrlist_size, "mmap-addrlist-size");
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
1285 DEFSUBR (Fmmap_allocator_status);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1286 #endif /* MMAP_METERING */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290 vars_of_ralloc (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 DEFVAR_INT ("mmap-hysteresis", &mmap_hysteresis /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 Extra room left at the end of an allocated arena,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294 so that a re-alloc requesting extra space smaller than this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295 does not actually cause a new arena to be allocated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297 A negative value is considered equal to zero. This is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298 minimum amount of space guaranteed to be left at the end of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299 the arena. Because allocation happens in multiples of the OS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300 page size, it is possible for more space to be left unused.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302 mmap_hysteresis = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 #endif /* HAVE_MMAP */