Merge from emacs-24; up to 2012-05-07T14:57:18Z!michael.albinus@gmx.de
[bpt/emacs.git] / src / w32heap.c
1 /* Heap management routines for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1994, 2001-2012 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 3 of the License, or
9 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
21 */
22
23 #include <config.h>
24 #include <stdio.h>
25
26 #include "w32heap.h"
27 #include "lisp.h" /* for VALMASK */
28
29 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
30
31 /* This gives us the page size and the size of the allocation unit on NT. */
32 SYSTEM_INFO sysinfo_cache;
33
34 /* This gives us version, build, and platform identification. */
35 OSVERSIONINFO osinfo_cache;
36
37 unsigned long syspage_mask = 0;
38
39 /* The major and minor versions of NT. */
40 int w32_major_version;
41 int w32_minor_version;
42 int w32_build_number;
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_9X;
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 /* Cache os info. */
77 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
78 GetVersionEx (&osinfo_cache);
79
80 w32_build_number = osinfo_cache.dwBuildNumber;
81 if (os_subtype == OS_9X)
82 w32_build_number &= 0xffff;
83 }
84
85 /* Emulate getpagesize. */
86 int
87 getpagesize (void)
88 {
89 return sysinfo_cache.dwPageSize;
90 }
91
92 /* Info for managing our preload heap, which is essentially a fixed size
93 data area in the executable. */
94 PIMAGE_SECTION_HEADER preload_heap_section;
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 reserved_heap_size = 0;
101
102 /* The start of the data segment. */
103 unsigned char *
104 get_data_start (void)
105 {
106 return data_region_base;
107 }
108
109 /* The end of the data segment. */
110 unsigned char *
111 get_data_end (void)
112 {
113 return data_region_end;
114 }
115
116 #if !USE_LSB_TAG
117 static char *
118 allocate_heap (void)
119 {
120 /* Try to get as much as possible of the address range from the end of
121 the preload heap section up to the usable address limit. Since GNU
122 malloc can handle gaps in the memory it gets from sbrk, we can
123 simply set the sbrk pointer to the base of the new heap region. */
124 unsigned long base =
125 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
126 + preload_heap_section->Misc.VirtualSize),
127 get_allocation_unit ());
128 unsigned long end = 1 << VALBITS; /* 256MB */
129 void *ptr = NULL;
130
131 while (!ptr && (base < end))
132 {
133 reserved_heap_size = end - base;
134 ptr = VirtualAlloc ((void *) base,
135 get_reserved_heap_size (),
136 MEM_RESERVE,
137 PAGE_NOACCESS);
138 base += 0x00100000; /* 1MB increment */
139 }
140
141 return ptr;
142 }
143 #else /* USE_LSB_TAG */
144 static char *
145 allocate_heap (void)
146 {
147 unsigned long size = 0x80000000; /* start by asking for 2GB */
148 void *ptr = NULL;
149
150 while (!ptr && size > 0x00100000)
151 {
152 reserved_heap_size = size;
153 ptr = VirtualAlloc (NULL,
154 get_reserved_heap_size (),
155 MEM_RESERVE,
156 PAGE_NOACCESS);
157 size -= 0x00800000; /* if failed, decrease request by 8MB */
158 }
159
160 return ptr;
161 }
162 #endif /* USE_LSB_TAG */
163
164
165 /* Emulate Unix sbrk. Note that ralloc.c expects the return value to
166 be the address of the _start_ (not end) of the new block in case of
167 success, and zero (not -1) in case of failure. */
168 void *
169 sbrk (unsigned long increment)
170 {
171 void *result;
172 long size = (long) increment;
173
174 result = data_region_end;
175
176 /* If size is negative, shrink the heap by decommitting pages. */
177 if (size < 0)
178 {
179 int new_size;
180 unsigned char *new_data_region_end;
181
182 size = -size;
183
184 /* Sanity checks. */
185 if ((data_region_end - size) < data_region_base)
186 return NULL;
187
188 /* We can only decommit full pages, so allow for
189 partial deallocation [cga]. */
190 new_data_region_end = (data_region_end - size);
191 new_data_region_end = (unsigned char *)
192 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
193 new_size = real_data_region_end - new_data_region_end;
194 real_data_region_end = new_data_region_end;
195 if (new_size > 0)
196 {
197 /* Decommit size bytes from the end of the heap. */
198 if (using_dynamic_heap
199 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
200 return NULL;
201 }
202
203 data_region_end -= size;
204 }
205 /* If size is positive, grow the heap by committing reserved pages. */
206 else if (size > 0)
207 {
208 /* Sanity checks. */
209 if ((data_region_end + size) >
210 (data_region_base + get_reserved_heap_size ()))
211 return NULL;
212
213 /* Commit more of our heap. */
214 if (using_dynamic_heap
215 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
216 PAGE_READWRITE) == NULL)
217 return NULL;
218 data_region_end += size;
219
220 /* We really only commit full pages, so record where
221 the real end of committed memory is [cga]. */
222 real_data_region_end = (unsigned char *)
223 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
224 }
225
226 return result;
227 }
228
229 /* Initialize the internal heap variables used by sbrk. When running in
230 preload phase (ie. in the undumped executable), we rely entirely on a
231 fixed size heap section included in the .exe itself; this is
232 preserved during dumping, and truncated to the size actually used.
233
234 When running in the dumped executable, we reserve as much as possible
235 of the address range that is addressable by Lisp object pointers, to
236 supplement what is left of the preload heap. Although we cannot rely
237 on the dynamically allocated arena being contiguous with the static
238 heap area, it is not a problem because sbrk can pretend that the gap
239 was allocated by something else; GNU malloc detects when there is a
240 jump in the sbrk values, and starts a new heap block. */
241 void
242 init_heap (void)
243 {
244 PIMAGE_DOS_HEADER dos_header;
245 PIMAGE_NT_HEADERS nt_header;
246
247 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
248 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
249 dos_header->e_lfanew);
250 preload_heap_section = find_section ("EMHEAP", nt_header);
251
252 if (using_dynamic_heap)
253 {
254 data_region_base = allocate_heap ();
255 if (!data_region_base)
256 {
257 printf ("Error: Could not reserve dynamic heap area.\n");
258 exit (1);
259 }
260
261 #if !USE_LSB_TAG
262 /* Ensure that the addresses don't use the upper tag bits since
263 the Lisp type goes there. */
264 if (((unsigned long) data_region_base & ~VALMASK) != 0)
265 {
266 printf ("Error: The heap was allocated in upper memory.\n");
267 exit (1);
268 }
269 #endif
270 data_region_end = data_region_base;
271 real_data_region_end = data_region_end;
272 }
273 else
274 {
275 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
276 data_region_end = data_region_base;
277 real_data_region_end = data_region_end;
278 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
279 }
280
281 /* Update system version information to match current system. */
282 cache_system_info ();
283 }
284
285 /* Round the heap up to the given alignment. */
286 void
287 round_heap (unsigned long align)
288 {
289 unsigned long needs_to_be;
290 unsigned long need_to_alloc;
291
292 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
293 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
294
295 if (need_to_alloc)
296 sbrk (need_to_alloc);
297 }