Commit | Line | Data |
---|---|---|
b46a6a83 | 1 | /* Heap management routines for GNU Emacs on the Microsoft Windows API. |
acaf905b | 2 | Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc. |
95ed0025 | 3 | |
3b7ad313 | 4 | This file is part of GNU Emacs. |
95ed0025 | 5 | |
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
3b7ad313 | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
95ed0025 | 10 | |
3b7ad313 EN |
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. | |
95ed0025 | 15 | |
3b7ad313 | 16 | You should have received a copy of the GNU General Public License |
9ec0b715 | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
95ed0025 | 18 | |
9ec0b715 | 19 | /* |
95ed0025 RS |
20 | Geoff Voelker (voelker@cs.washington.edu) 7-29-94 |
21 | */ | |
22 | ||
4838e624 | 23 | #include <config.h> |
95ed0025 RS |
24 | #include <stdio.h> |
25 | ||
489f9371 | 26 | #include "w32heap.h" |
8dfdd41f | 27 | #include "lisp.h" /* for VALMASK */ |
95ed0025 | 28 | |
2e4f6477 | 29 | #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL))) |
30d2b1c2 | 30 | |
95ed0025 RS |
31 | /* This gives us the page size and the size of the allocation unit on NT. */ |
32 | SYSTEM_INFO sysinfo_cache; | |
b9cad9c1 GV |
33 | |
34 | /* This gives us version, build, and platform identification. */ | |
35 | OSVERSIONINFO osinfo_cache; | |
36 | ||
3bbabc43 | 37 | unsigned long syspage_mask = 0; |
95ed0025 | 38 | |
95ed0025 | 39 | /* The major and minor versions of NT. */ |
fbd6baed GV |
40 | int w32_major_version; |
41 | int w32_minor_version; | |
39f1f652 | 42 | int w32_build_number; |
95ed0025 | 43 | |
801f68b9 GV |
44 | /* Distinguish between Windows NT and Windows 95. */ |
45 | int os_subtype; | |
46 | ||
95ed0025 RS |
47 | /* Cache information describing the NT system for later use. */ |
48 | void | |
49 | cache_system_info (void) | |
50 | { | |
ce20e03e | 51 | union |
95ed0025 | 52 | { |
ce20e03e | 53 | struct info |
95ed0025 RS |
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 (); | |
fbd6baed GV |
64 | w32_major_version = version.info.major; |
65 | w32_minor_version = version.info.minor; | |
95ed0025 | 66 | |
801f68b9 | 67 | if (version.info.platform & 0x8000) |
417a7a0e | 68 | os_subtype = OS_9X; |
801f68b9 GV |
69 | else |
70 | os_subtype = OS_NT; | |
71 | ||
95ed0025 RS |
72 | /* Cache page size, allocation unit, processor type, etc. */ |
73 | GetSystemInfo (&sysinfo_cache); | |
3bbabc43 | 74 | syspage_mask = sysinfo_cache.dwPageSize - 1; |
b9cad9c1 GV |
75 | |
76 | /* Cache os info. */ | |
77 | osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); | |
78 | GetVersionEx (&osinfo_cache); | |
39f1f652 AI |
79 | |
80 | w32_build_number = osinfo_cache.dwBuildNumber; | |
417a7a0e | 81 | if (os_subtype == OS_9X) |
39f1f652 | 82 | w32_build_number &= 0xffff; |
95ed0025 RS |
83 | } |
84 | ||
e54c8cd1 GV |
85 | /* Emulate getpagesize. */ |
86 | int | |
87 | getpagesize (void) | |
88 | { | |
89 | return sysinfo_cache.dwPageSize; | |
90 | } | |
91 | ||
30d2b1c2 AI |
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; | |
95ed0025 RS |
95 | |
96 | /* Info for keeping track of our heap. */ | |
97 | unsigned char *data_region_base = NULL; | |
98 | unsigned char *data_region_end = NULL; | |
3bbabc43 | 99 | unsigned char *real_data_region_end = NULL; |
011db670 | 100 | unsigned long reserved_heap_size = 0; |
95ed0025 RS |
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 | ||
646b5f55 | 116 | #if !USE_LSB_TAG |
011db670 GV |
117 | static char * |
118 | allocate_heap (void) | |
119 | { | |
30d2b1c2 AI |
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 ()); | |
8dfdd41f | 128 | unsigned long end = 1 << VALBITS; /* 256MB */ |
709fd16b | 129 | void *ptr = NULL; |
011db670 | 130 | |
709fd16b GV |
131 | while (!ptr && (base < end)) |
132 | { | |
709fd16b GV |
133 | reserved_heap_size = end - base; |
134 | ptr = VirtualAlloc ((void *) base, | |
135 | get_reserved_heap_size (), | |
136 | MEM_RESERVE, | |
137 | PAGE_NOACCESS); | |
709fd16b GV |
138 | base += 0x00100000; /* 1MB increment */ |
139 | } | |
0d05360d | 140 | |
709fd16b | 141 | return ptr; |
011db670 | 142 | } |
646b5f55 | 143 | #else /* USE_LSB_TAG */ |
fab624aa EZ |
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 | } | |
646b5f55 | 162 | #endif /* USE_LSB_TAG */ |
011db670 GV |
163 | |
164 | ||
fab624aa EZ |
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. */ | |
95ed0025 RS |
168 | void * |
169 | sbrk (unsigned long increment) | |
170 | { | |
171 | void *result; | |
172 | long size = (long) increment; | |
ce20e03e | 173 | |
95ed0025 | 174 | result = data_region_end; |
ce20e03e | 175 | |
95ed0025 | 176 | /* If size is negative, shrink the heap by decommitting pages. */ |
ce20e03e | 177 | if (size < 0) |
95ed0025 | 178 | { |
3bbabc43 GV |
179 | int new_size; |
180 | unsigned char *new_data_region_end; | |
181 | ||
95ed0025 RS |
182 | size = -size; |
183 | ||
184 | /* Sanity checks. */ | |
95ed0025 RS |
185 | if ((data_region_end - size) < data_region_base) |
186 | return NULL; | |
187 | ||
ce20e03e | 188 | /* We can only decommit full pages, so allow for |
3bbabc43 GV |
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; | |
30d2b1c2 | 195 | if (new_size > 0) |
3bbabc43 GV |
196 | { |
197 | /* Decommit size bytes from the end of the heap. */ | |
30d2b1c2 AI |
198 | if (using_dynamic_heap |
199 | && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT)) | |
3bbabc43 GV |
200 | return NULL; |
201 | } | |
95ed0025 RS |
202 | |
203 | data_region_end -= size; | |
ce20e03e | 204 | } |
95ed0025 | 205 | /* If size is positive, grow the heap by committing reserved pages. */ |
ce20e03e | 206 | else if (size > 0) |
95ed0025 RS |
207 | { |
208 | /* Sanity checks. */ | |
95ed0025 RS |
209 | if ((data_region_end + size) > |
210 | (data_region_base + get_reserved_heap_size ())) | |
211 | return NULL; | |
212 | ||
213 | /* Commit more of our heap. */ | |
30d2b1c2 AI |
214 | if (using_dynamic_heap |
215 | && VirtualAlloc (data_region_end, size, MEM_COMMIT, | |
216 | PAGE_READWRITE) == NULL) | |
95ed0025 RS |
217 | return NULL; |
218 | data_region_end += size; | |
3bbabc43 GV |
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); | |
95ed0025 | 224 | } |
ce20e03e | 225 | |
95ed0025 RS |
226 | return result; |
227 | } | |
228 | ||
30d2b1c2 AI |
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. */ | |
95ed0025 | 241 | void |
b56ceb92 | 242 | init_heap (void) |
95ed0025 | 243 | { |
30d2b1c2 AI |
244 | PIMAGE_DOS_HEADER dos_header; |
245 | PIMAGE_NT_HEADERS nt_header; | |
246 | ||
247 | dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0); | |
ce20e03e | 248 | nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + |
30d2b1c2 AI |
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 | ||
646b5f55 | 261 | #if !USE_LSB_TAG |
30d2b1c2 AI |
262 | /* Ensure that the addresses don't use the upper tag bits since |
263 | the Lisp type goes there. */ | |
ce20e03e | 264 | if (((unsigned long) data_region_base & ~VALMASK) != 0) |
30d2b1c2 AI |
265 | { |
266 | printf ("Error: The heap was allocated in upper memory.\n"); | |
267 | exit (1); | |
268 | } | |
3ae12c8d | 269 | #endif |
30d2b1c2 AI |
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 | } | |
801f68b9 GV |
280 | |
281 | /* Update system version information to match current system. */ | |
282 | cache_system_info (); | |
95ed0025 RS |
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; | |
ce20e03e | 291 | |
30d2b1c2 | 292 | needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align); |
95ed0025 | 293 | need_to_alloc = needs_to_be - (unsigned long) get_heap_end (); |
ce20e03e JB |
294 | |
295 | if (need_to_alloc) | |
95ed0025 RS |
296 | sbrk (need_to_alloc); |
297 | } |