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