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