]> code.delx.au - gnu-emacs/blob - src/w32heap.c
fix NT build
[gnu-emacs] / 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_PTR)(rva) + (DWORD_PTR)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 extern unsigned long syspage_mask;
36 OSVERSIONINFO osinfo_cache;
37
38 /* The major and minor versions of NT. */
39 int w32_major_version;
40 int w32_minor_version;
41 int w32_build_number;
42
43 /* Distinguish between Windows NT and Windows 95. */
44 int os_subtype;
45
46 /* Emulate getpagesize. */
47 int
48 getpagesize (void)
49 {
50 return sysinfo_cache.dwPageSize;
51 }
52
53 /* Info for managing our preload heap, which is essentially a fixed size
54 data area in the executable. */
55 PIMAGE_SECTION_HEADER preload_heap_section;
56
57 /* Info for keeping track of our heap. */
58 unsigned char *data_region_base = NULL;
59 unsigned char *data_region_end = NULL;
60 unsigned char *real_data_region_end = NULL;
61 size_t reserved_heap_size = 0;
62
63 /* The start of the data segment. */
64 unsigned char *
65 get_data_start (void)
66 {
67 return data_region_base;
68 }
69
70 /* The end of the data segment. */
71 unsigned char *
72 get_data_end (void)
73 {
74 return data_region_end;
75 }
76
77 #if !USE_LSB_TAG
78 static char *
79 allocate_heap (void)
80 {
81 /* Try to get as much as possible of the address range from the end of
82 the preload heap section up to the usable address limit. Since GNU
83 malloc can handle gaps in the memory it gets from sbrk, we can
84 simply set the sbrk pointer to the base of the new heap region. */
85 DWORD_PTR base =
86 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
87 + preload_heap_section->Misc.VirtualSize),
88 get_allocation_unit ());
89 DWORD_PTR end = ((unsigned __int64)1) << VALBITS; /* 256MB */
90 void *ptr = NULL;
91
92 while (!ptr && (base < end))
93 {
94 #ifdef _WIN64
95 reserved_heap_size = min(end - base, 0x4000000000i64); /* Limit to 256Gb */
96 #else
97 reserved_heap_size = end - base;
98 #endif
99 ptr = VirtualAlloc ((void *) base,
100 get_reserved_heap_size (),
101 MEM_RESERVE,
102 PAGE_NOACCESS);
103 base += 0x00100000; /* 1MB increment */
104 }
105
106 return ptr;
107 }
108 #else /* USE_LSB_TAG */
109 static char *
110 allocate_heap (void)
111 {
112 #ifdef _WIN64
113 size_t size = 0x4000000000i64; /* start by asking for 32GB */
114 #else
115 size_t size = 0x80000000; /* start by asking for 2GB */
116 #endif
117 void *ptr = NULL;
118
119 while (!ptr && size > 0x00100000)
120 {
121 reserved_heap_size = size;
122 ptr = VirtualAlloc (NULL,
123 get_reserved_heap_size (),
124 MEM_RESERVE,
125 PAGE_NOACCESS);
126 size -= 0x00800000; /* if failed, decrease request by 8MB */
127 }
128
129 return ptr;
130 }
131 #endif /* USE_LSB_TAG */
132
133
134 /* Emulate Unix sbrk. Note that ralloc.c expects the return value to
135 be the address of the _start_ (not end) of the new block in case of
136 success, and zero (not -1) in case of failure. */
137 void *
138 sbrk (ptrdiff_t increment)
139 {
140 void *result;
141 ptrdiff_t size = increment;
142
143 result = data_region_end;
144
145 /* If size is negative, shrink the heap by decommitting pages. */
146 if (size < 0)
147 {
148 ptrdiff_t new_size;
149 unsigned char *new_data_region_end;
150
151 size = -size;
152
153 /* Sanity checks. */
154 if ((data_region_end - size) < data_region_base)
155 return NULL;
156
157 /* We can only decommit full pages, so allow for
158 partial deallocation [cga]. */
159 new_data_region_end = (data_region_end - size);
160 new_data_region_end = (unsigned char *)
161 ((DWORD_PTR) (new_data_region_end + syspage_mask) & ~syspage_mask);
162 new_size = real_data_region_end - new_data_region_end;
163 real_data_region_end = new_data_region_end;
164 if (new_size > 0)
165 {
166 /* Decommit size bytes from the end of the heap. */
167 if (using_dynamic_heap
168 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
169 return NULL;
170 }
171
172 data_region_end -= size;
173 }
174 /* If size is positive, grow the heap by committing reserved pages. */
175 else if (size > 0)
176 {
177 /* Sanity checks. */
178 if ((data_region_end + size) >
179 (data_region_base + get_reserved_heap_size ()))
180 return NULL;
181
182 /* Commit more of our heap. */
183 if (using_dynamic_heap
184 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
185 PAGE_READWRITE) == NULL)
186 return NULL;
187 data_region_end += size;
188
189 /* We really only commit full pages, so record where
190 the real end of committed memory is [cga]. */
191 real_data_region_end = (unsigned char *)
192 ((DWORD_PTR) (data_region_end + syspage_mask) & ~syspage_mask);
193 }
194
195 return result;
196 }
197
198 /* Initialize the internal heap variables used by sbrk. When running in
199 preload phase (ie. in the undumped executable), we rely entirely on a
200 fixed size heap section included in the .exe itself; this is
201 preserved during dumping, and truncated to the size actually used.
202
203 When running in the dumped executable, we reserve as much as possible
204 of the address range that is addressable by Lisp object pointers, to
205 supplement what is left of the preload heap. Although we cannot rely
206 on the dynamically allocated arena being contiguous with the static
207 heap area, it is not a problem because sbrk can pretend that the gap
208 was allocated by something else; GNU malloc detects when there is a
209 jump in the sbrk values, and starts a new heap block. */
210 void
211 init_heap (void)
212 {
213 PIMAGE_DOS_HEADER dos_header;
214 PIMAGE_NT_HEADERS nt_header;
215
216 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
217 nt_header = (PIMAGE_NT_HEADERS) (((DWORD_PTR) dos_header) +
218 dos_header->e_lfanew);
219 preload_heap_section = find_section ("EMHEAP", nt_header);
220
221 if (using_dynamic_heap)
222 {
223 data_region_base = allocate_heap ();
224 if (!data_region_base)
225 {
226 printf ("Error: Could not reserve dynamic heap area.\n");
227 exit (1);
228 }
229
230 #if !USE_LSB_TAG
231 /* Ensure that the addresses don't use the upper tag bits since
232 the Lisp type goes there. */
233 if (((DWORD_PTR) data_region_base & ~VALMASK) != 0)
234 {
235 printf ("Error: The heap was allocated in upper memory.\n");
236 exit (1);
237 }
238 #endif
239 data_region_end = data_region_base;
240 real_data_region_end = data_region_end;
241 }
242 else
243 {
244 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
245 data_region_end = data_region_base;
246 real_data_region_end = data_region_end;
247 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
248 }
249
250 /* Update system version information to match current system. */
251 cache_system_info ();
252 }
253
254 /* Round the heap up to the given alignment. */
255 void
256 round_heap (size_t align)
257 {
258 DWORD_PTR needs_to_be;
259 DWORD_PTR need_to_alloc;
260
261 needs_to_be = (DWORD_PTR) ROUND_UP (get_heap_end (), align);
262 need_to_alloc = needs_to_be - (DWORD_PTR) get_heap_end ();
263
264 if (need_to_alloc)
265 sbrk (need_to_alloc);
266 }