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