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