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
|
|
26 #include "config.h"
|
|
27
|
|
28 #include <stdlib.h>
|
|
29 #include <stdio.h>
|
|
30
|
|
31 #include "ntheap.h"
|
|
32 #include "lisp.h" /* for VALMASK */
|
|
33
|
|
34 /* This gives us the page size and the size of the allocation unit on NT. */
|
|
35 SYSTEM_INFO sysinfo_cache;
|
|
36 unsigned long syspage_mask = 0;
|
|
37
|
|
38 /* These are defined to get Emacs to compile, but are not used. */
|
|
39 int edata;
|
|
40 int etext;
|
|
41
|
|
42 /* The major and minor versions of NT. */
|
|
43 int nt_major_version;
|
|
44 int nt_minor_version;
|
|
45
|
209
|
46 /* Distinguish between Windows NT and Windows 95. */
|
|
47 int os_subtype;
|
|
48
|
100
|
49 /* Cache information describing the NT system for later use. */
|
|
50 void
|
|
51 cache_system_info (void)
|
|
52 {
|
|
53 union
|
|
54 {
|
|
55 struct info
|
|
56 {
|
|
57 char major;
|
|
58 char minor;
|
|
59 short platform;
|
|
60 } info;
|
|
61 DWORD data;
|
|
62 } version;
|
|
63
|
|
64 /* Cache the version of the operating system. */
|
|
65 version.data = GetVersion ();
|
|
66 nt_major_version = version.info.major;
|
|
67 nt_minor_version = version.info.minor;
|
|
68
|
209
|
69 if (version.info.platform & 0x8000)
|
|
70 os_subtype = OS_WIN95;
|
|
71 else
|
|
72 os_subtype = OS_NT;
|
|
73
|
100
|
74 /* Cache page size, allocation unit, processor type, etc. */
|
|
75 GetSystemInfo (&sysinfo_cache);
|
|
76 syspage_mask = sysinfo_cache.dwPageSize - 1;
|
|
77 }
|
|
78
|
|
79 /* Round ADDRESS up to be aligned with ALIGN. */
|
|
80 unsigned char *
|
|
81 round_to_next (unsigned char *address, unsigned long align)
|
|
82 {
|
|
83 unsigned long tmp;
|
|
84
|
|
85 tmp = (unsigned long) address;
|
|
86 tmp = (tmp + align - 1) / align;
|
|
87
|
|
88 return (unsigned char *) (tmp * align);
|
|
89 }
|
|
90
|
|
91 /* Info for keeping track of our heap. */
|
169
|
92 unsigned char *data_region_base = UNINIT_PTR;
|
|
93 unsigned char *data_region_end = UNINIT_PTR;
|
|
94 unsigned char *real_data_region_end = UNINIT_PTR;
|
|
95 unsigned long data_region_size = UNINIT_LONG;
|
|
96 unsigned long reserved_heap_size = UNINIT_LONG;
|
100
|
97
|
|
98 /* The start of the data segment. */
|
|
99 unsigned char *
|
|
100 get_data_start (void)
|
|
101 {
|
|
102 return data_region_base;
|
|
103 }
|
|
104
|
|
105 /* The end of the data segment. */
|
|
106 unsigned char *
|
|
107 get_data_end (void)
|
|
108 {
|
|
109 return data_region_end;
|
|
110 }
|
|
111
|
|
112 static char *
|
|
113 allocate_heap (void)
|
|
114 {
|
|
115 /* The base address for our GNU malloc heap is chosen in conjuction
|
|
116 with the link settings for temacs.exe which control the stack size,
|
|
117 the initial default process heap size and the executable image base
|
|
118 address. The link settings and the malloc heap base below must all
|
|
119 correspond; the relationship between these values depends on how NT
|
|
120 and Win95 arrange the virtual address space for a process (and on
|
|
121 the size of the code and data segments in temacs.exe).
|
|
122
|
|
123 The most important thing is to make base address for the executable
|
|
124 image high enough to leave enough room between it and the 4MB floor
|
|
125 of the process address space on Win95 for the primary thread stack,
|
|
126 the process default heap, and other assorted odds and ends
|
|
127 (eg. environment strings, private system dll memory etc) that are
|
|
128 allocated before temacs has a chance to grab its malloc arena. The
|
|
129 malloc heap base can then be set several MB higher than the
|
|
130 executable image base, leaving enough room for the code and data
|
|
131 segments.
|
|
132
|
|
133 Because some parts of Emacs can use rather a lot of stack space
|
|
134 (for instance, the regular expression routines can potentially
|
|
135 allocate several MB of stack space) we allow 8MB for the stack.
|
|
136
|
|
137 Allowing 1MB for the default process heap, and 1MB for odds and
|
|
138 ends, we can base the executable at 16MB and still have a generous
|
|
139 safety margin. At the moment, the executable has about 810KB of
|
|
140 code (for x86) and about 550KB of data - on RISC platforms the code
|
|
141 size could be roughly double, so if we allow 4MB for the executable
|
|
142 we will have plenty of room for expansion.
|
|
143
|
|
144 Thus we would like to set the malloc heap base to 20MB. However,
|
|
145 Win95 refuses to allocate the heap starting at this address, so we
|
|
146 set the base to 27MB to make it happy. Since Emacs now leaves
|
|
147 28 bits available for pointers, this lets us use the remainder of
|
|
148 the region below the 256MB line for our malloc arena - 229MB is
|
|
149 still a pretty decent arena to play in! */
|
|
150
|
|
151 unsigned long base = 0x01B00000; /* 27MB */
|
|
152 unsigned long end = 1 << VALBITS; /* 256MB */
|
|
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
|
|
193 /* Ensure that the addresses don't use the upper tag bits since
|
|
194 the Lisp type goes there. */
|
|
195 if (((unsigned long) data_region_base & ~VALMASK) != 0)
|
|
196 {
|
|
197 printf ("Error: The heap was allocated in upper memory.\n");
|
|
198 exit (1);
|
|
199 }
|
|
200
|
|
201 data_region_end = data_region_base;
|
|
202 real_data_region_end = data_region_end;
|
|
203 data_region_size = get_reserved_heap_size ();
|
|
204 }
|
|
205
|
|
206 result = data_region_end;
|
|
207
|
|
208 /* If size is negative, shrink the heap by decommitting pages. */
|
|
209 if (size < 0)
|
|
210 {
|
|
211 int new_size;
|
|
212 unsigned char *new_data_region_end;
|
|
213
|
|
214 size = -size;
|
|
215
|
|
216 /* Sanity checks. */
|
|
217 if ((data_region_end - size) < data_region_base)
|
|
218 return NULL;
|
|
219
|
|
220 /* We can only decommit full pages, so allow for
|
|
221 partial deallocation [cga]. */
|
|
222 new_data_region_end = (data_region_end - size);
|
|
223 new_data_region_end = (unsigned char *)
|
|
224 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
|
|
225 new_size = real_data_region_end - new_data_region_end;
|
|
226 real_data_region_end = new_data_region_end;
|
|
227 if (new_size > 0)
|
|
228 {
|
|
229 /* Decommit size bytes from the end of the heap. */
|
|
230 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
|
|
231 return NULL;
|
|
232 }
|
|
233
|
|
234 data_region_end -= size;
|
|
235 }
|
|
236 /* If size is positive, grow the heap by committing reserved pages. */
|
|
237 else if (size > 0)
|
|
238 {
|
|
239 /* Sanity checks. */
|
|
240 if ((data_region_end + size) >
|
|
241 (data_region_base + get_reserved_heap_size ()))
|
|
242 return NULL;
|
|
243
|
|
244 /* Commit more of our heap. */
|
|
245 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
|
|
246 PAGE_READWRITE) == NULL)
|
|
247 return NULL;
|
|
248 data_region_end += size;
|
|
249
|
|
250 /* We really only commit full pages, so record where
|
|
251 the real end of committed memory is [cga]. */
|
|
252 real_data_region_end = (unsigned char *)
|
|
253 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
|
|
254 }
|
|
255
|
|
256 return result;
|
|
257 }
|
|
258
|
|
259 #ifndef CANNOT_DUMP
|
|
260
|
|
261 /* Recreate the heap from the data that was dumped to the executable.
|
|
262 EXECUTABLE_PATH tells us where to find the executable. */
|
|
263 void
|
|
264 recreate_heap (char *executable_path)
|
|
265 {
|
|
266 unsigned char *tmp;
|
|
267
|
|
268 /* First reserve the upper part of our heap. (We reserve first
|
|
269 because there have been problems in the past where doing the
|
|
270 mapping first has loaded DLLs into the VA space of our heap.) */
|
|
271 tmp = VirtualAlloc ((void *) get_heap_end (),
|
|
272 get_reserved_heap_size () - get_committed_heap_size (),
|
|
273 MEM_RESERVE,
|
|
274 PAGE_NOACCESS);
|
|
275 if (!tmp)
|
|
276 exit (1);
|
|
277
|
|
278 /* We read in the data for the .bss section from the executable
|
|
279 first and map in the heap from the executable second to prevent
|
|
280 any funny interactions between file I/O and file mapping. */
|
|
281 read_in_bss (executable_path);
|
|
282 map_in_heap (executable_path);
|
209
|
283
|
|
284 /* Update system version information to match current system. */
|
|
285 cache_system_info ();
|
100
|
286 }
|
|
287 #endif /* CANNOT_DUMP */
|
|
288
|
|
289 /* Round the heap up to the given alignment. */
|
|
290 void
|
|
291 round_heap (unsigned long align)
|
|
292 {
|
|
293 unsigned long needs_to_be;
|
|
294 unsigned long need_to_alloc;
|
|
295
|
|
296 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
|
|
297 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
|
|
298
|
|
299 if (need_to_alloc)
|
|
300 sbrk (need_to_alloc);
|
|
301 }
|
209
|
302
|
|
303 #if (_MSC_VER >= 1000)
|
|
304
|
|
305 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
|
|
306 a heap via HeapCreate. They are normally defined by the runtime,
|
|
307 but we override them here so that the unnecessary HeapCreate call
|
|
308 is not performed. */
|
|
309
|
|
310 int __cdecl
|
|
311 _heap_init (void)
|
|
312 {
|
|
313 /* Stepping through the assembly indicates that mainCRTStartup is
|
|
314 expecting a nonzero success return value. */
|
|
315 return 1;
|
|
316 }
|
|
317
|
|
318 void __cdecl
|
|
319 _heap_term (void)
|
|
320 {
|
|
321 return;
|
|
322 }
|
|
323
|
|
324 #endif
|