Commit | Line | Data |
---|---|---|
e9e23e23 | 1 | /* Heap management routines for GNU Emacs on the Microsoft W32 API. |
9ec0b715 | 2 | Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
114f9c96 | 3 | 2008, 2009, 2010 Free Software Foundation, Inc. |
95ed0025 | 4 | |
3b7ad313 | 5 | This file is part of GNU Emacs. |
95ed0025 | 6 | |
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
3b7ad313 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
95ed0025 | 11 | |
3b7ad313 EN |
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. | |
95ed0025 | 16 | |
3b7ad313 | 17 | You should have received a copy of the GNU General Public License |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
95ed0025 | 19 | |
9ec0b715 | 20 | /* |
95ed0025 RS |
21 | Geoff Voelker (voelker@cs.washington.edu) 7-29-94 |
22 | */ | |
23 | ||
4838e624 | 24 | #include <config.h> |
95ed0025 | 25 | #include <stdio.h> |
d7306fe6 | 26 | #include <setjmp.h> |
95ed0025 | 27 | |
489f9371 | 28 | #include "w32heap.h" |
8dfdd41f | 29 | #include "lisp.h" /* for VALMASK */ |
95ed0025 | 30 | |
2e4f6477 | 31 | #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL))) |
30d2b1c2 | 32 | |
95ed0025 RS |
33 | /* This gives us the page size and the size of the allocation unit on NT. */ |
34 | SYSTEM_INFO sysinfo_cache; | |
b9cad9c1 GV |
35 | |
36 | /* This gives us version, build, and platform identification. */ | |
37 | OSVERSIONINFO osinfo_cache; | |
38 | ||
3bbabc43 | 39 | unsigned long syspage_mask = 0; |
95ed0025 | 40 | |
95ed0025 | 41 | /* The major and minor versions of NT. */ |
fbd6baed GV |
42 | int w32_major_version; |
43 | int w32_minor_version; | |
39f1f652 | 44 | int w32_build_number; |
95ed0025 | 45 | |
801f68b9 GV |
46 | /* Distinguish between Windows NT and Windows 95. */ |
47 | int os_subtype; | |
48 | ||
95ed0025 RS |
49 | /* Cache information describing the NT system for later use. */ |
50 | void | |
51 | cache_system_info (void) | |
52 | { | |
ce20e03e | 53 | union |
95ed0025 | 54 | { |
ce20e03e | 55 | struct info |
95ed0025 RS |
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 (); | |
fbd6baed GV |
66 | w32_major_version = version.info.major; |
67 | w32_minor_version = version.info.minor; | |
95ed0025 | 68 | |
801f68b9 GV |
69 | if (version.info.platform & 0x8000) |
70 | os_subtype = OS_WIN95; | |
71 | else | |
72 | os_subtype = OS_NT; | |
73 | ||
95ed0025 RS |
74 | /* Cache page size, allocation unit, processor type, etc. */ |
75 | GetSystemInfo (&sysinfo_cache); | |
3bbabc43 | 76 | syspage_mask = sysinfo_cache.dwPageSize - 1; |
b9cad9c1 GV |
77 | |
78 | /* Cache os info. */ | |
79 | osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); | |
80 | GetVersionEx (&osinfo_cache); | |
39f1f652 AI |
81 | |
82 | w32_build_number = osinfo_cache.dwBuildNumber; | |
83 | if (os_subtype == OS_WIN95) | |
84 | w32_build_number &= 0xffff; | |
95ed0025 RS |
85 | } |
86 | ||
e54c8cd1 GV |
87 | /* Emulate getpagesize. */ |
88 | int | |
89 | getpagesize (void) | |
90 | { | |
91 | return sysinfo_cache.dwPageSize; | |
92 | } | |
93 | ||
30d2b1c2 AI |
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; | |
95ed0025 RS |
97 | |
98 | /* Info for keeping track of our heap. */ | |
99 | unsigned char *data_region_base = NULL; | |
100 | unsigned char *data_region_end = NULL; | |
3bbabc43 | 101 | unsigned char *real_data_region_end = NULL; |
011db670 | 102 | unsigned long reserved_heap_size = 0; |
95ed0025 RS |
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 | ||
011db670 GV |
118 | static char * |
119 | allocate_heap (void) | |
120 | { | |
30d2b1c2 AI |
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 ()); | |
8dfdd41f | 129 | unsigned long end = 1 << VALBITS; /* 256MB */ |
709fd16b | 130 | void *ptr = NULL; |
011db670 | 131 | |
709fd16b GV |
132 | while (!ptr && (base < end)) |
133 | { | |
709fd16b GV |
134 | reserved_heap_size = end - base; |
135 | ptr = VirtualAlloc ((void *) base, | |
136 | get_reserved_heap_size (), | |
137 | MEM_RESERVE, | |
138 | PAGE_NOACCESS); | |
709fd16b GV |
139 | base += 0x00100000; /* 1MB increment */ |
140 | } | |
0d05360d | 141 | |
709fd16b | 142 | return ptr; |
011db670 | 143 | } |
011db670 GV |
144 | |
145 | ||
95ed0025 RS |
146 | /* Emulate Unix sbrk. */ |
147 | void * | |
148 | sbrk (unsigned long increment) | |
149 | { | |
150 | void *result; | |
151 | long size = (long) increment; | |
ce20e03e | 152 | |
95ed0025 | 153 | result = data_region_end; |
ce20e03e | 154 | |
95ed0025 | 155 | /* If size is negative, shrink the heap by decommitting pages. */ |
ce20e03e | 156 | if (size < 0) |
95ed0025 | 157 | { |
3bbabc43 GV |
158 | int new_size; |
159 | unsigned char *new_data_region_end; | |
160 | ||
95ed0025 RS |
161 | size = -size; |
162 | ||
163 | /* Sanity checks. */ | |
95ed0025 RS |
164 | if ((data_region_end - size) < data_region_base) |
165 | return NULL; | |
166 | ||
ce20e03e | 167 | /* We can only decommit full pages, so allow for |
3bbabc43 GV |
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; | |
30d2b1c2 | 174 | if (new_size > 0) |
3bbabc43 GV |
175 | { |
176 | /* Decommit size bytes from the end of the heap. */ | |
30d2b1c2 AI |
177 | if (using_dynamic_heap |
178 | && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT)) | |
3bbabc43 GV |
179 | return NULL; |
180 | } | |
95ed0025 RS |
181 | |
182 | data_region_end -= size; | |
ce20e03e | 183 | } |
95ed0025 | 184 | /* If size is positive, grow the heap by committing reserved pages. */ |
ce20e03e | 185 | else if (size > 0) |
95ed0025 RS |
186 | { |
187 | /* Sanity checks. */ | |
95ed0025 RS |
188 | if ((data_region_end + size) > |
189 | (data_region_base + get_reserved_heap_size ())) | |
190 | return NULL; | |
191 | ||
192 | /* Commit more of our heap. */ | |
30d2b1c2 AI |
193 | if (using_dynamic_heap |
194 | && VirtualAlloc (data_region_end, size, MEM_COMMIT, | |
195 | PAGE_READWRITE) == NULL) | |
95ed0025 RS |
196 | return NULL; |
197 | data_region_end += size; | |
3bbabc43 GV |
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); | |
95ed0025 | 203 | } |
ce20e03e | 204 | |
95ed0025 RS |
205 | return result; |
206 | } | |
207 | ||
30d2b1c2 AI |
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. */ | |
95ed0025 | 220 | void |
b56ceb92 | 221 | init_heap (void) |
95ed0025 | 222 | { |
30d2b1c2 AI |
223 | PIMAGE_DOS_HEADER dos_header; |
224 | PIMAGE_NT_HEADERS nt_header; | |
225 | ||
226 | dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0); | |
ce20e03e | 227 | nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + |
30d2b1c2 AI |
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 | ||
6e5cb96f | 240 | #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG) |
30d2b1c2 AI |
241 | /* Ensure that the addresses don't use the upper tag bits since |
242 | the Lisp type goes there. */ | |
ce20e03e | 243 | if (((unsigned long) data_region_base & ~VALMASK) != 0) |
30d2b1c2 AI |
244 | { |
245 | printf ("Error: The heap was allocated in upper memory.\n"); | |
246 | exit (1); | |
247 | } | |
3ae12c8d | 248 | #endif |
30d2b1c2 AI |
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 | } | |
801f68b9 GV |
259 | |
260 | /* Update system version information to match current system. */ | |
261 | cache_system_info (); | |
95ed0025 RS |
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; | |
ce20e03e | 270 | |
30d2b1c2 | 271 | needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align); |
95ed0025 | 272 | need_to_alloc = needs_to_be - (unsigned long) get_heap_end (); |
ce20e03e JB |
273 | |
274 | if (need_to_alloc) | |
95ed0025 RS |
275 | sbrk (need_to_alloc); |
276 | } | |
801f68b9 | 277 | |
9d4f32e8 | 278 | #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL)) |
801f68b9 GV |
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 | |
ab5796a9 | 300 |