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> */
|
209
|
24 /* Synced with FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
|
100
|
25
|
272
|
26 #include <config.h>
|
|
27 #include "lisp.h" /* for VALMASK */
|
100
|
28
|
|
29 #include <stdlib.h>
|
|
30
|
|
31 #include "ntheap.h"
|
|
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
|
209
|
45 /* Distinguish between Windows NT and Windows 95. */
|
|
46 int os_subtype;
|
|
47
|
100
|
48 /* Cache information describing the NT system for later use. */
|
|
49 void
|
|
50 cache_system_info (void)
|
|
51 {
|
|
52 union
|
|
53 {
|
|
54 struct info
|
|
55 {
|
|
56 char major;
|
|
57 char minor;
|
|
58 short platform;
|
|
59 } info;
|
|
60 DWORD data;
|
|
61 } version;
|
|
62
|
|
63 /* Cache the version of the operating system. */
|
|
64 version.data = GetVersion ();
|
|
65 nt_major_version = version.info.major;
|
|
66 nt_minor_version = version.info.minor;
|
|
67
|
209
|
68 if (version.info.platform & 0x8000)
|
|
69 os_subtype = OS_WIN95;
|
|
70 else
|
|
71 os_subtype = OS_NT;
|
|
72
|
100
|
73 /* Cache page size, allocation unit, processor type, etc. */
|
|
74 GetSystemInfo (&sysinfo_cache);
|
|
75 syspage_mask = sysinfo_cache.dwPageSize - 1;
|
|
76 }
|
|
77
|
|
78 /* Round ADDRESS up to be aligned with ALIGN. */
|
|
79 unsigned char *
|
|
80 round_to_next (unsigned char *address, unsigned long align)
|
|
81 {
|
|
82 unsigned long tmp;
|
|
83
|
|
84 tmp = (unsigned long) address;
|
|
85 tmp = (tmp + align - 1) / align;
|
|
86
|
|
87 return (unsigned char *) (tmp * align);
|
|
88 }
|
|
89
|
|
90 /* Info for keeping track of our heap. */
|
169
|
91 unsigned char *data_region_base = UNINIT_PTR;
|
|
92 unsigned char *data_region_end = UNINIT_PTR;
|
|
93 unsigned char *real_data_region_end = UNINIT_PTR;
|
|
94 unsigned long data_region_size = UNINIT_LONG;
|
|
95 unsigned long reserved_heap_size = UNINIT_LONG;
|
100
|
96
|
|
97 /* The start of the data segment. */
|
|
98 unsigned char *
|
|
99 get_data_start (void)
|
|
100 {
|
|
101 return data_region_base;
|
|
102 }
|
|
103
|
|
104 /* The end of the data segment. */
|
|
105 unsigned char *
|
|
106 get_data_end (void)
|
|
107 {
|
|
108 return data_region_end;
|
|
109 }
|
|
110
|
|
111 static char *
|
|
112 allocate_heap (void)
|
|
113 {
|
|
114 /* The base address for our GNU malloc heap is chosen in conjuction
|
|
115 with the link settings for temacs.exe which control the stack size,
|
|
116 the initial default process heap size and the executable image base
|
|
117 address. The link settings and the malloc heap base below must all
|
|
118 correspond; the relationship between these values depends on how NT
|
|
119 and Win95 arrange the virtual address space for a process (and on
|
|
120 the size of the code and data segments in temacs.exe).
|
|
121
|
|
122 The most important thing is to make base address for the executable
|
|
123 image high enough to leave enough room between it and the 4MB floor
|
|
124 of the process address space on Win95 for the primary thread stack,
|
|
125 the process default heap, and other assorted odds and ends
|
|
126 (eg. environment strings, private system dll memory etc) that are
|
|
127 allocated before temacs has a chance to grab its malloc arena. The
|
|
128 malloc heap base can then be set several MB higher than the
|
|
129 executable image base, leaving enough room for the code and data
|
|
130 segments.
|
|
131
|
|
132 Because some parts of Emacs can use rather a lot of stack space
|
|
133 (for instance, the regular expression routines can potentially
|
|
134 allocate several MB of stack space) we allow 8MB for the stack.
|
|
135
|
|
136 Allowing 1MB for the default process heap, and 1MB for odds and
|
|
137 ends, we can base the executable at 16MB and still have a generous
|
|
138 safety margin. At the moment, the executable has about 810KB of
|
|
139 code (for x86) and about 550KB of data - on RISC platforms the code
|
|
140 size could be roughly double, so if we allow 4MB for the executable
|
|
141 we will have plenty of room for expansion.
|
|
142
|
|
143 Thus we would like to set the malloc heap base to 20MB. However,
|
|
144 Win95 refuses to allocate the heap starting at this address, so we
|
|
145 set the base to 27MB to make it happy. Since Emacs now leaves
|
|
146 28 bits available for pointers, this lets us use the remainder of
|
|
147 the region below the 256MB line for our malloc arena - 229MB is
|
|
148 still a pretty decent arena to play in! */
|
|
149
|
|
150 unsigned long base = 0x01B00000; /* 27MB */
|
359
|
151 /* Temporary hack for the non-starting problem - use 28 (256Mb) rather than VALBITS (1Gb) */
|
|
152 unsigned long end = 1 << 28; /* 256MB */
|
100
|
153 void *ptr = NULL;
|
|
154
|
169
|
155 #define NTHEAP_PROBE_BASE 1
|
100
|
156 #if NTHEAP_PROBE_BASE /* This is never normally defined */
|
|
157 /* Try various addresses looking for one the kernel will let us have. */
|
|
158 while (!ptr && (base < end))
|
|
159 {
|
|
160 reserved_heap_size = end - base;
|
|
161 ptr = VirtualAlloc ((void *) base,
|
|
162 get_reserved_heap_size (),
|
|
163 MEM_RESERVE,
|
|
164 PAGE_NOACCESS);
|
|
165 base += 0x00100000; /* 1MB increment */
|
|
166 }
|
|
167 #else
|
|
168 reserved_heap_size = end - base;
|
|
169 ptr = VirtualAlloc ((void *) base,
|
|
170 get_reserved_heap_size (),
|
|
171 MEM_RESERVE,
|
|
172 PAGE_NOACCESS);
|
|
173 #endif
|
|
174
|
|
175 return ptr;
|
|
176 }
|
|
177
|
|
178
|
|
179 /* Emulate Unix sbrk. */
|
|
180 void *
|
|
181 sbrk (unsigned long increment)
|
|
182 {
|
|
183 void *result;
|
|
184 long size = (long) increment;
|
|
185
|
|
186 /* Allocate our heap if we haven't done so already. */
|
169
|
187 if (data_region_base == UNINIT_PTR)
|
100
|
188 {
|
|
189 data_region_base = allocate_heap ();
|
|
190 if (!data_region_base)
|
|
191 return NULL;
|
|
192
|
282
|
193 #ifndef USE_MINIMAL_TAGBITS
|
100
|
194 /* Ensure that the addresses don't use the upper tag bits since
|
|
195 the Lisp type goes there. */
|
282
|
196 #ifdef USE_UNION_TYPE
|
|
197 if (((unsigned long) data_region_base & ~((1U << VALBITS) - 1)) != 0)
|
|
198 #else
|
|
199 if (((unsigned long) data_region_base & ~VALMASK) != 0)
|
|
200 #endif
|
100
|
201 {
|
|
202 printf ("Error: The heap was allocated in upper memory.\n");
|
|
203 exit (1);
|
|
204 }
|
282
|
205 #endif
|
100
|
206
|
|
207 data_region_end = data_region_base;
|
|
208 real_data_region_end = data_region_end;
|
|
209 data_region_size = get_reserved_heap_size ();
|
|
210 }
|
|
211
|
|
212 result = data_region_end;
|
|
213
|
|
214 /* If size is negative, shrink the heap by decommitting pages. */
|
|
215 if (size < 0)
|
|
216 {
|
|
217 int new_size;
|
|
218 unsigned char *new_data_region_end;
|
|
219
|
|
220 size = -size;
|
|
221
|
|
222 /* Sanity checks. */
|
|
223 if ((data_region_end - size) < data_region_base)
|
|
224 return NULL;
|
|
225
|
|
226 /* We can only decommit full pages, so allow for
|
|
227 partial deallocation [cga]. */
|
|
228 new_data_region_end = (data_region_end - size);
|
|
229 new_data_region_end = (unsigned char *)
|
|
230 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
|
|
231 new_size = real_data_region_end - new_data_region_end;
|
|
232 real_data_region_end = new_data_region_end;
|
|
233 if (new_size > 0)
|
|
234 {
|
|
235 /* Decommit size bytes from the end of the heap. */
|
|
236 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
|
|
237 return NULL;
|
|
238 }
|
|
239
|
|
240 data_region_end -= size;
|
|
241 }
|
|
242 /* If size is positive, grow the heap by committing reserved pages. */
|
|
243 else if (size > 0)
|
|
244 {
|
|
245 /* Sanity checks. */
|
|
246 if ((data_region_end + size) >
|
|
247 (data_region_base + get_reserved_heap_size ()))
|
|
248 return NULL;
|
|
249
|
|
250 /* Commit more of our heap. */
|
|
251 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
|
|
252 PAGE_READWRITE) == NULL)
|
|
253 return NULL;
|
|
254 data_region_end += size;
|
|
255
|
|
256 /* We really only commit full pages, so record where
|
|
257 the real end of committed memory is [cga]. */
|
|
258 real_data_region_end = (unsigned char *)
|
|
259 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
|
|
260 }
|
|
261
|
|
262 return result;
|
|
263 }
|
|
264
|
|
265 #ifndef CANNOT_DUMP
|
|
266
|
|
267 /* Recreate the heap from the data that was dumped to the executable.
|
|
268 EXECUTABLE_PATH tells us where to find the executable. */
|
|
269 void
|
|
270 recreate_heap (char *executable_path)
|
|
271 {
|
361
|
272 /* First reserve the upper part of our heap. (We reserve first
|
365
|
273 because there have been problems in the past where doing the
|
|
274 mapping first has loaded DLLs into the VA space of our heap.) */
|
100
|
275
|
361
|
276 /* Query the region at the end of the committed heap */
|
|
277 void *tmp;
|
|
278 MEMORY_BASIC_INFORMATION info;
|
367
|
279 DWORD size;
|
361
|
280 unsigned char* base = get_heap_end ();
|
|
281 unsigned char* end = base + get_reserved_heap_size () - get_committed_heap_size ();
|
|
282 VirtualQuery (base, &info, sizeof info);
|
|
283 if (info.State != MEM_FREE)
|
365
|
284 {
|
|
285 /* Oops, something has already reserved or commited it, nothing we can do but exit */
|
|
286 char buf[256];
|
|
287 char modnambuf[80];
|
|
288
|
|
289 /* Find the filename of any DLL mapped at that address. This is a bit
|
|
290 of a hack in that it relies on HMODULEs being pointers to the image
|
|
291 base. However, this will almost certainly be the case for the
|
|
292 forseeable future in MS operating systems.
|
|
293
|
|
294 Note that we don't check for MEM_IMAGE first because it doesn't
|
|
295 exist on Win95 AFAIK - ajh */
|
|
296 if (!GetModuleFileName ((HMODULE) info.AllocationBase,
|
|
297 modnambuf,
|
|
298 sizeof (modnambuf)))
|
|
299 strcpy (modnambuf, "<unknown>");
|
|
300
|
|
301 wsprintf(buf,
|
|
302 "XEmacs cannot start because the memory region required by the heap is not available.\n"
|
|
303 "(BaseAddress = 0x%lx, AllocationBase = 0x%lx, Size = 0x%lx, State = %s, Type = %s, ModuleName = \"%s\")",
|
|
304 info.BaseAddress, info.AllocationBase, info.RegionSize,
|
|
305 info.State == MEM_COMMIT ? "COMMITED" : "RESERVED",
|
|
306 info.Type == MEM_IMAGE ? "IMAGE" : info.Type == MEM_MAPPED ? "MAPPED" : "PRIVATE",
|
|
307 modnambuf);
|
|
308
|
|
309 MessageBox(NULL, buf, "XEmacs", MB_OK | MB_ICONSTOP);
|
|
310 exit(1);
|
|
311 }
|
361
|
312
|
|
313 /* Now try and reserve as much as possible */
|
|
314 size = min (info.RegionSize, end - base);
|
|
315 tmp = VirtualAlloc (base, size, MEM_RESERVE, PAGE_NOACCESS);
|
100
|
316 if (!tmp)
|
365
|
317 {
|
|
318 /* Can't reserve it, nothing we can do but exit */
|
|
319 char buf[256];
|
|
320 wsprintf(buf,
|
|
321 "XEmacs cannot start because it couldn't reserve space required for the heap.\n"
|
|
322 "(VirtualAlloc at 0x%lx of 0x%lx failed (%d))", base, size, GetLastError());
|
|
323 MessageBox(NULL, buf, "XEmacs", MB_OK | MB_ICONSTOP);
|
|
324 exit (1);
|
|
325 }
|
100
|
326
|
|
327 /* We read in the data for the .bss section from the executable
|
|
328 first and map in the heap from the executable second to prevent
|
|
329 any funny interactions between file I/O and file mapping. */
|
|
330 read_in_bss (executable_path);
|
|
331 map_in_heap (executable_path);
|
209
|
332
|
|
333 /* Update system version information to match current system. */
|
|
334 cache_system_info ();
|
100
|
335 }
|
|
336 #endif /* CANNOT_DUMP */
|
|
337
|
|
338 /* Round the heap up to the given alignment. */
|
|
339 void
|
|
340 round_heap (unsigned long align)
|
|
341 {
|
|
342 unsigned long needs_to_be;
|
|
343 unsigned long need_to_alloc;
|
|
344
|
|
345 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
|
|
346 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
|
|
347
|
|
348 if (need_to_alloc)
|
|
349 sbrk (need_to_alloc);
|
|
350 }
|
209
|
351
|
|
352 #if (_MSC_VER >= 1000)
|
|
353
|
|
354 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
|
|
355 a heap via HeapCreate. They are normally defined by the runtime,
|
|
356 but we override them here so that the unnecessary HeapCreate call
|
|
357 is not performed. */
|
|
358
|
|
359 int __cdecl
|
|
360 _heap_init (void)
|
|
361 {
|
|
362 /* Stepping through the assembly indicates that mainCRTStartup is
|
|
363 expecting a nonzero success return value. */
|
|
364 return 1;
|
|
365 }
|
|
366
|
|
367 void __cdecl
|
|
368 _heap_term (void)
|
|
369 {
|
|
370 return;
|
|
371 }
|
|
372
|
|
373 #endif
|