]> code.delx.au - gnu-emacs/blob - src/w32heap.c
Update years in copyright notice; nfc.
[gnu-emacs] / src / w32heap.c
1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994, 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "w32heap.h"
32 #include "lisp.h" /* for VALMASK */
33
34 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
35
36 /* This gives us the page size and the size of the allocation unit on NT. */
37 SYSTEM_INFO sysinfo_cache;
38
39 /* This gives us version, build, and platform identification. */
40 OSVERSIONINFO osinfo_cache;
41
42 unsigned long syspage_mask = 0;
43
44 /* These are defined to get Emacs to compile, but are not used. */
45 int edata;
46 int etext;
47
48 /* The major and minor versions of NT. */
49 int w32_major_version;
50 int w32_minor_version;
51 int w32_build_number;
52
53 /* Distinguish between Windows NT and Windows 95. */
54 int os_subtype;
55
56 /* Cache information describing the NT system for later use. */
57 void
58 cache_system_info (void)
59 {
60 union
61 {
62 struct info
63 {
64 char major;
65 char minor;
66 short platform;
67 } info;
68 DWORD data;
69 } version;
70
71 /* Cache the version of the operating system. */
72 version.data = GetVersion ();
73 w32_major_version = version.info.major;
74 w32_minor_version = version.info.minor;
75
76 if (version.info.platform & 0x8000)
77 os_subtype = OS_WIN95;
78 else
79 os_subtype = OS_NT;
80
81 /* Cache page size, allocation unit, processor type, etc. */
82 GetSystemInfo (&sysinfo_cache);
83 syspage_mask = sysinfo_cache.dwPageSize - 1;
84
85 /* Cache os info. */
86 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
87 GetVersionEx (&osinfo_cache);
88
89 w32_build_number = osinfo_cache.dwBuildNumber;
90 if (os_subtype == OS_WIN95)
91 w32_build_number &= 0xffff;
92 }
93
94 /* Emulate getpagesize. */
95 int
96 getpagesize (void)
97 {
98 return sysinfo_cache.dwPageSize;
99 }
100
101 /* Info for managing our preload heap, which is essentially a fixed size
102 data area in the executable. */
103 PIMAGE_SECTION_HEADER preload_heap_section;
104
105 /* Info for keeping track of our heap. */
106 unsigned char *data_region_base = NULL;
107 unsigned char *data_region_end = NULL;
108 unsigned char *real_data_region_end = NULL;
109 unsigned long reserved_heap_size = 0;
110
111 /* The start of the data segment. */
112 unsigned char *
113 get_data_start (void)
114 {
115 return data_region_base;
116 }
117
118 /* The end of the data segment. */
119 unsigned char *
120 get_data_end (void)
121 {
122 return data_region_end;
123 }
124
125 static char *
126 allocate_heap (void)
127 {
128 /* Try to get as much as possible of the address range from the end of
129 the preload heap section up to the usable address limit. Since GNU
130 malloc can handle gaps in the memory it gets from sbrk, we can
131 simply set the sbrk pointer to the base of the new heap region. */
132 unsigned long base =
133 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
134 + preload_heap_section->Misc.VirtualSize),
135 get_allocation_unit ());
136 unsigned long end = 1 << VALBITS; /* 256MB */
137 void *ptr = NULL;
138
139 while (!ptr && (base < end))
140 {
141 reserved_heap_size = end - base;
142 ptr = VirtualAlloc ((void *) base,
143 get_reserved_heap_size (),
144 MEM_RESERVE,
145 PAGE_NOACCESS);
146 base += 0x00100000; /* 1MB increment */
147 }
148
149 return ptr;
150 }
151
152
153 /* Emulate Unix sbrk. */
154 void *
155 sbrk (unsigned long increment)
156 {
157 void *result;
158 long size = (long) increment;
159
160 result = data_region_end;
161
162 /* If size is negative, shrink the heap by decommitting pages. */
163 if (size < 0)
164 {
165 int new_size;
166 unsigned char *new_data_region_end;
167
168 size = -size;
169
170 /* Sanity checks. */
171 if ((data_region_end - size) < data_region_base)
172 return NULL;
173
174 /* We can only decommit full pages, so allow for
175 partial deallocation [cga]. */
176 new_data_region_end = (data_region_end - size);
177 new_data_region_end = (unsigned char *)
178 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
179 new_size = real_data_region_end - new_data_region_end;
180 real_data_region_end = new_data_region_end;
181 if (new_size > 0)
182 {
183 /* Decommit size bytes from the end of the heap. */
184 if (using_dynamic_heap
185 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
186 return NULL;
187 }
188
189 data_region_end -= size;
190 }
191 /* If size is positive, grow the heap by committing reserved pages. */
192 else if (size > 0)
193 {
194 /* Sanity checks. */
195 if ((data_region_end + size) >
196 (data_region_base + get_reserved_heap_size ()))
197 return NULL;
198
199 /* Commit more of our heap. */
200 if (using_dynamic_heap
201 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
202 PAGE_READWRITE) == NULL)
203 return NULL;
204 data_region_end += size;
205
206 /* We really only commit full pages, so record where
207 the real end of committed memory is [cga]. */
208 real_data_region_end = (unsigned char *)
209 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
210 }
211
212 return result;
213 }
214
215 /* Initialize the internal heap variables used by sbrk. When running in
216 preload phase (ie. in the undumped executable), we rely entirely on a
217 fixed size heap section included in the .exe itself; this is
218 preserved during dumping, and truncated to the size actually used.
219
220 When running in the dumped executable, we reserve as much as possible
221 of the address range that is addressable by Lisp object pointers, to
222 supplement what is left of the preload heap. Although we cannot rely
223 on the dynamically allocated arena being contiguous with the static
224 heap area, it is not a problem because sbrk can pretend that the gap
225 was allocated by something else; GNU malloc detects when there is a
226 jump in the sbrk values, and starts a new heap block. */
227 void
228 init_heap ()
229 {
230 PIMAGE_DOS_HEADER dos_header;
231 PIMAGE_NT_HEADERS nt_header;
232
233 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
234 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
235 dos_header->e_lfanew);
236 preload_heap_section = find_section ("EMHEAP", nt_header);
237
238 if (using_dynamic_heap)
239 {
240 data_region_base = allocate_heap ();
241 if (!data_region_base)
242 {
243 printf ("Error: Could not reserve dynamic heap area.\n");
244 exit (1);
245 }
246
247 #if defined (NO_UNION_TYPE) && !defined (USE_LSB_TAG)
248 /* Ensure that the addresses don't use the upper tag bits since
249 the Lisp type goes there. */
250 if (((unsigned long) data_region_base & ~VALMASK) != 0)
251 {
252 printf ("Error: The heap was allocated in upper memory.\n");
253 exit (1);
254 }
255 #endif
256 data_region_end = data_region_base;
257 real_data_region_end = data_region_end;
258 }
259 else
260 {
261 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
262 data_region_end = data_region_base;
263 real_data_region_end = data_region_end;
264 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
265 }
266
267 /* Update system version information to match current system. */
268 cache_system_info ();
269 }
270
271 /* Round the heap up to the given alignment. */
272 void
273 round_heap (unsigned long align)
274 {
275 unsigned long needs_to_be;
276 unsigned long need_to_alloc;
277
278 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
279 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
280
281 if (need_to_alloc)
282 sbrk (need_to_alloc);
283 }
284
285 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined(USE_CRT_DLL))
286
287 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
288 a heap via HeapCreate. They are normally defined by the runtime,
289 but we override them here so that the unnecessary HeapCreate call
290 is not performed. */
291
292 int __cdecl
293 _heap_init (void)
294 {
295 /* Stepping through the assembly indicates that mainCRTStartup is
296 expecting a nonzero success return value. */
297 return 1;
298 }
299
300 void __cdecl
301 _heap_term (void)
302 {
303 return;
304 }
305
306 #endif
307
308 /* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
309 (do not change this comment) */