100
|
1 /* Heap management routines for XEmacs on Windows NT.
|
|
2 Copyright (C) 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to the Free
|
|
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
19 02111-1307, USA.
|
|
20
|
|
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
|
|
22
|
|
23 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
|
|
24
|
|
25 #include "config.h"
|
|
26
|
|
27 #include <stdlib.h>
|
|
28 #include <stdio.h>
|
|
29
|
|
30 #include "ntheap.h"
|
|
31 #include "lisp.h" /* for VALMASK */
|
|
32
|
|
33 /* This gives us the page size and the size of the allocation unit on NT. */
|
|
34 SYSTEM_INFO sysinfo_cache;
|
|
35 unsigned long syspage_mask = 0;
|
|
36
|
|
37 /* These are defined to get Emacs to compile, but are not used. */
|
|
38 int edata;
|
|
39 int etext;
|
|
40
|
|
41 /* The major and minor versions of NT. */
|
|
42 int nt_major_version;
|
|
43 int nt_minor_version;
|
|
44
|
|
45 /* Cache information describing the NT system for later use. */
|
|
46 void
|
|
47 cache_system_info (void)
|
|
48 {
|
|
49 union
|
|
50 {
|
|
51 struct info
|
|
52 {
|
|
53 char major;
|
|
54 char minor;
|
|
55 short platform;
|
|
56 } info;
|
|
57 DWORD data;
|
|
58 } version;
|
|
59
|
|
60 /* Cache the version of the operating system. */
|
|
61 version.data = GetVersion ();
|
|
62 nt_major_version = version.info.major;
|
|
63 nt_minor_version = version.info.minor;
|
|
64
|
|
65 /* Cache page size, allocation unit, processor type, etc. */
|
|
66 GetSystemInfo (&sysinfo_cache);
|
|
67 syspage_mask = sysinfo_cache.dwPageSize - 1;
|
|
68 }
|
|
69
|
|
70 /* Round ADDRESS up to be aligned with ALIGN. */
|
|
71 unsigned char *
|
|
72 round_to_next (unsigned char *address, unsigned long align)
|
|
73 {
|
|
74 unsigned long tmp;
|
|
75
|
|
76 tmp = (unsigned long) address;
|
|
77 tmp = (tmp + align - 1) / align;
|
|
78
|
|
79 return (unsigned char *) (tmp * align);
|
|
80 }
|
|
81
|
|
82 /* Info for keeping track of our heap. */
|
169
|
83 unsigned char *data_region_base = UNINIT_PTR;
|
|
84 unsigned char *data_region_end = UNINIT_PTR;
|
|
85 unsigned char *real_data_region_end = UNINIT_PTR;
|
|
86 unsigned long data_region_size = UNINIT_LONG;
|
|
87 unsigned long reserved_heap_size = UNINIT_LONG;
|
100
|
88
|
|
89 /* The start of the data segment. */
|
|
90 unsigned char *
|
|
91 get_data_start (void)
|
|
92 {
|
|
93 return data_region_base;
|
|
94 }
|
|
95
|
|
96 /* The end of the data segment. */
|
|
97 unsigned char *
|
|
98 get_data_end (void)
|
|
99 {
|
|
100 return data_region_end;
|
|
101 }
|
|
102
|
|
103 static char *
|
|
104 allocate_heap (void)
|
|
105 {
|
|
106 /* The base address for our GNU malloc heap is chosen in conjuction
|
|
107 with the link settings for temacs.exe which control the stack size,
|
|
108 the initial default process heap size and the executable image base
|
|
109 address. The link settings and the malloc heap base below must all
|
|
110 correspond; the relationship between these values depends on how NT
|
|
111 and Win95 arrange the virtual address space for a process (and on
|
|
112 the size of the code and data segments in temacs.exe).
|
|
113
|
|
114 The most important thing is to make base address for the executable
|
|
115 image high enough to leave enough room between it and the 4MB floor
|
|
116 of the process address space on Win95 for the primary thread stack,
|
|
117 the process default heap, and other assorted odds and ends
|
|
118 (eg. environment strings, private system dll memory etc) that are
|
|
119 allocated before temacs has a chance to grab its malloc arena. The
|
|
120 malloc heap base can then be set several MB higher than the
|
|
121 executable image base, leaving enough room for the code and data
|
|
122 segments.
|
|
123
|
|
124 Because some parts of Emacs can use rather a lot of stack space
|
|
125 (for instance, the regular expression routines can potentially
|
|
126 allocate several MB of stack space) we allow 8MB for the stack.
|
|
127
|
|
128 Allowing 1MB for the default process heap, and 1MB for odds and
|
|
129 ends, we can base the executable at 16MB and still have a generous
|
|
130 safety margin. At the moment, the executable has about 810KB of
|
|
131 code (for x86) and about 550KB of data - on RISC platforms the code
|
|
132 size could be roughly double, so if we allow 4MB for the executable
|
|
133 we will have plenty of room for expansion.
|
|
134
|
|
135 Thus we would like to set the malloc heap base to 20MB. However,
|
|
136 Win95 refuses to allocate the heap starting at this address, so we
|
|
137 set the base to 27MB to make it happy. Since Emacs now leaves
|
|
138 28 bits available for pointers, this lets us use the remainder of
|
|
139 the region below the 256MB line for our malloc arena - 229MB is
|
|
140 still a pretty decent arena to play in! */
|
|
141
|
|
142 unsigned long base = 0x01B00000; /* 27MB */
|
|
143 unsigned long end = 1 << VALBITS; /* 256MB */
|
|
144 void *ptr = NULL;
|
|
145
|
169
|
146 #define NTHEAP_PROBE_BASE 1
|
100
|
147 #if NTHEAP_PROBE_BASE /* This is never normally defined */
|
|
148 /* Try various addresses looking for one the kernel will let us have. */
|
|
149 while (!ptr && (base < end))
|
|
150 {
|
|
151 reserved_heap_size = end - base;
|
|
152 ptr = VirtualAlloc ((void *) base,
|
|
153 get_reserved_heap_size (),
|
|
154 MEM_RESERVE,
|
|
155 PAGE_NOACCESS);
|
|
156 base += 0x00100000; /* 1MB increment */
|
|
157 }
|
|
158 #else
|
|
159 reserved_heap_size = end - base;
|
|
160 ptr = VirtualAlloc ((void *) base,
|
|
161 get_reserved_heap_size (),
|
|
162 MEM_RESERVE,
|
|
163 PAGE_NOACCESS);
|
|
164 #endif
|
|
165
|
|
166 return ptr;
|
|
167 }
|
|
168
|
|
169
|
|
170 /* Emulate Unix sbrk. */
|
|
171 void *
|
|
172 sbrk (unsigned long increment)
|
|
173 {
|
|
174 void *result;
|
|
175 long size = (long) increment;
|
|
176
|
|
177 /* Allocate our heap if we haven't done so already. */
|
169
|
178 if (data_region_base == UNINIT_PTR)
|
100
|
179 {
|
|
180 data_region_base = allocate_heap ();
|
|
181 if (!data_region_base)
|
|
182 return NULL;
|
|
183
|
|
184 /* Ensure that the addresses don't use the upper tag bits since
|
|
185 the Lisp type goes there. */
|
|
186 if (((unsigned long) data_region_base & ~VALMASK) != 0)
|
|
187 {
|
|
188 printf ("Error: The heap was allocated in upper memory.\n");
|
|
189 exit (1);
|
|
190 }
|
|
191
|
|
192 data_region_end = data_region_base;
|
|
193 real_data_region_end = data_region_end;
|
|
194 data_region_size = get_reserved_heap_size ();
|
|
195 }
|
|
196
|
|
197 result = data_region_end;
|
|
198
|
|
199 /* If size is negative, shrink the heap by decommitting pages. */
|
|
200 if (size < 0)
|
|
201 {
|
|
202 int new_size;
|
|
203 unsigned char *new_data_region_end;
|
|
204
|
|
205 size = -size;
|
|
206
|
|
207 /* Sanity checks. */
|
|
208 if ((data_region_end - size) < data_region_base)
|
|
209 return NULL;
|
|
210
|
|
211 /* We can only decommit full pages, so allow for
|
|
212 partial deallocation [cga]. */
|
|
213 new_data_region_end = (data_region_end - size);
|
|
214 new_data_region_end = (unsigned char *)
|
|
215 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
|
|
216 new_size = real_data_region_end - new_data_region_end;
|
|
217 real_data_region_end = new_data_region_end;
|
|
218 if (new_size > 0)
|
|
219 {
|
|
220 /* Decommit size bytes from the end of the heap. */
|
|
221 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
|
|
222 return NULL;
|
|
223 }
|
|
224
|
|
225 data_region_end -= size;
|
|
226 }
|
|
227 /* If size is positive, grow the heap by committing reserved pages. */
|
|
228 else if (size > 0)
|
|
229 {
|
|
230 /* Sanity checks. */
|
|
231 if ((data_region_end + size) >
|
|
232 (data_region_base + get_reserved_heap_size ()))
|
|
233 return NULL;
|
|
234
|
|
235 /* Commit more of our heap. */
|
|
236 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
|
|
237 PAGE_READWRITE) == NULL)
|
|
238 return NULL;
|
|
239 data_region_end += size;
|
|
240
|
|
241 /* We really only commit full pages, so record where
|
|
242 the real end of committed memory is [cga]. */
|
|
243 real_data_region_end = (unsigned char *)
|
|
244 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
|
|
245 }
|
|
246
|
|
247 return result;
|
|
248 }
|
|
249
|
|
250 #ifndef CANNOT_DUMP
|
|
251
|
|
252 /* Recreate the heap from the data that was dumped to the executable.
|
|
253 EXECUTABLE_PATH tells us where to find the executable. */
|
|
254 void
|
|
255 recreate_heap (char *executable_path)
|
|
256 {
|
|
257 unsigned char *tmp;
|
|
258
|
|
259 /* First reserve the upper part of our heap. (We reserve first
|
|
260 because there have been problems in the past where doing the
|
|
261 mapping first has loaded DLLs into the VA space of our heap.) */
|
|
262 tmp = VirtualAlloc ((void *) get_heap_end (),
|
|
263 get_reserved_heap_size () - get_committed_heap_size (),
|
|
264 MEM_RESERVE,
|
|
265 PAGE_NOACCESS);
|
|
266 if (!tmp)
|
|
267 exit (1);
|
|
268
|
|
269 /* We read in the data for the .bss section from the executable
|
|
270 first and map in the heap from the executable second to prevent
|
|
271 any funny interactions between file I/O and file mapping. */
|
|
272 read_in_bss (executable_path);
|
|
273 map_in_heap (executable_path);
|
|
274 }
|
|
275
|
|
276 #endif /* CANNOT_DUMP */
|
|
277
|
|
278 /* Round the heap up to the given alignment. */
|
|
279 void
|
|
280 round_heap (unsigned long align)
|
|
281 {
|
|
282 unsigned long needs_to_be;
|
|
283 unsigned long need_to_alloc;
|
|
284
|
|
285 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
|
|
286 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
|
|
287
|
|
288 if (need_to_alloc)
|
|
289 sbrk (need_to_alloc);
|
|
290 }
|