]> code.delx.au - gnu-emacs/blob - src/w32heap.c
Use mmap(2) emulation for buffer text on MS-Windows.
[gnu-emacs] / src / w32heap.c
1 /* Heap management routines for GNU Emacs on the Microsoft Windows
2 API. Copyright (C) 1994, 2001-2014 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 /*
24 Heavily modified by Fabrice Popineau (fabrice.popineau@gmail.com) 28-02-2014
25 */
26
27 /*
28 Memory allocation scheme for w32/w64:
29
30 - Buffers are mmap'ed using a very simple emulation of mmap/munmap
31 - During the temacs phase:
32 * we use a private heap declared to be stored into the `dumped_data'
33 * unfortunately, this heap cannot be made growable, so the size of
34 blocks it can allocate is limited to (0x80000 - pagesize)
35 * the blocks that are larger than this are allocated from the end
36 of the `dumped_data' array; there are not so many of them.
37 We use a very simple first-fit scheme to reuse those blocks.
38 * we check that the private heap does not cross the area used
39 by the bigger chunks.
40 - During the emacs phase:
41 * we create a private heap for new memory blocks
42 * we make sure that we never free a block that has been dumped.
43 Freeing a dumped block could work in principle, but may prove
44 unreliable if we distribute binaries of emacs.exe: MS does not
45 guarantee that the heap data structures are the same across all
46 versions of their OS, even though the API is available since XP. */
47
48 #include <config.h>
49 #include <stdio.h>
50
51 #include <sys/mman.h>
52 #include "w32common.h"
53 #include "w32heap.h"
54 #include "lisp.h" /* for VALMASK */
55
56 /* We chose to leave those declarations here. They are used only in
57 this file. The RtlCreateHeap is available since XP. It is located
58 in ntdll.dll and is available with the DDK. People often
59 complained that HeapCreate doesn't offer the ability to create a
60 heap at a given place, which we need here, and which RtlCreateHeap
61 provides. We reproduce here the definitions available with the
62 DDK. */
63
64 typedef PVOID (WINAPI * RtlCreateHeap_Proc) (
65 /* _In_ */ ULONG Flags,
66 /* _In_opt_ */ PVOID HeapBase,
67 /* _In_opt_ */ SIZE_T ReserveSize,
68 /* _In_opt_ */ SIZE_T CommitSize,
69 /* _In_opt_ */ PVOID Lock,
70 /* _In_opt_ */ PVOID Parameters
71 );
72
73 typedef LONG NTSTATUS;
74
75 typedef NTSTATUS
76 (NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
77 IN PVOID Base,
78 IN OUT PVOID *CommitAddress,
79 IN OUT PSIZE_T CommitSize
80 );
81
82 typedef struct _RTL_HEAP_PARAMETERS {
83 ULONG Length;
84 SIZE_T SegmentReserve;
85 SIZE_T SegmentCommit;
86 SIZE_T DeCommitFreeBlockThreshold;
87 SIZE_T DeCommitTotalFreeThreshold;
88 SIZE_T MaximumAllocationSize;
89 SIZE_T VirtualMemoryThreshold;
90 SIZE_T InitialCommit;
91 SIZE_T InitialReserve;
92 PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
93 SIZE_T Reserved[ 2 ];
94 } RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;
95
96 /* We reserve space for dumping emacs lisp byte-code inside a static
97 array. By storing it in an array, the generic mechanism in
98 unexecw32.c will be able to dump it without the need to add a
99 special segment to the executable. In order to be able to do this
100 without losing too much space, we need to create a Windows heap at
101 the specific address of the static array. The RtlCreateHeap
102 available inside the NT kernel since XP will do this. It allows to
103 create a non-growable heap at a specific address. So before
104 dumping, we create a non-growable heap at the address of the
105 dumped_data[] array. After dumping, we reuse memory allocated
106 there without being able to free it (but most of it is not meant to
107 be freed anyway), and we use a new private heap for all new
108 allocations. */
109
110 unsigned char dumped_data[DUMPED_HEAP_SIZE];
111
112 /* Info for managing our preload heap, which is essentially a fixed size
113 data area in the executable. */
114 /* Info for keeping track of our heap. */
115 unsigned char *data_region_base = NULL;
116 unsigned char *data_region_end = NULL;
117 static DWORD_PTR committed = 0;
118
119 /* The maximum block size that can be handled by a non-growable w32
120 heap is limited by the MaxBlockSize value below.
121
122 This point deserves and explanation.
123
124 The W32 heap allocator can be used for a growable
125 heap or a non-growable one.
126
127 A growable heap is not compatible with a fixed base address for the
128 heap. Only a non-growable one is. One drawback of non-growable
129 heaps is that they can hold only objects smaller than a certain
130 size (the one defined below). Most of the largest blocks are GC'ed
131 before dumping. In any case and to be safe, we implement a simple
132 first-fit allocation algorithm starting at the end of the
133 dumped_data[] array like depicted below:
134
135 ----------------------------------------------
136 | | | |
137 | Private heap |-> <-| Big chunks |
138 | | | |
139 ----------------------------------------------
140 ^ ^ ^
141 dumped_data dumped_data bc_limit
142 + committed
143
144 */
145 #define HEAP_ENTRY_SHIFT 3
146 #define PAGE_SIZE 0x1000
147 #define MaxBlockSize (0x80000 - PAGE_SIZE)
148
149 #define MAX_BLOCKS 0x40
150
151 static struct
152 {
153 unsigned char *address;
154 size_t size;
155 DWORD occupied;
156 } blocks[MAX_BLOCKS];
157
158 static DWORD blocks_number = 0;
159 static unsigned char *bc_limit;
160
161 /* Handle for the private heap:
162 - inside the dumped_data[] array before dump,
163 - outside of it after dump.
164 */
165 HANDLE heap = NULL;
166
167 /* We redirect the standard allocation functions. */
168 malloc_fn the_malloc_fn;
169 realloc_fn the_realloc_fn;
170 free_fn the_free_fn;
171
172 /* It doesn't seem to be useful to allocate from a file mapping.
173 It would be if the memory was shared.
174 http://stackoverflow.com/questions/307060/what-is-the-purpose-of-allocating-pages-in-the-pagefile-with-createfilemapping */
175
176 /* This is the function to commit memory when the heap allocator
177 claims for new memory. Before dumping, we allocate space
178 from the fixed size dumped_data[] array.
179 */
180 NTSTATUS NTAPI
181 dumped_data_commit (PVOID Base, PVOID *CommitAddress, PSIZE_T CommitSize)
182 {
183 /* This is used before dumping.
184
185 The private heap is stored at dumped_data[] address.
186 We commit contiguous areas of the dumped_data array
187 as requests arrive. */
188 *CommitAddress = data_region_base + committed;
189 committed += *CommitSize;
190 if (((unsigned char *)(*CommitAddress)) + *CommitSize >= bc_limit)
191 {
192 /* Check that the private heap area does not overlap the big
193 chunks area. */
194 fprintf(stderr,
195 "dumped_data_commit: memory exhausted.\nEnlarge dumped_data[]!\n");
196 exit (-1);
197 }
198 return 0;
199 }
200
201 /* Heap creation. */
202
203 /* Under MinGW32, we want to turn on Low Fragmentation Heap for XP.
204 MinGW32 lacks those definitions. */
205 #ifndef _W64
206 typedef enum _HEAP_INFORMATION_CLASS {
207 HeapCompatibilityInformation
208 } HEAP_INFORMATION_CLASS;
209
210 typedef WINBASEAPI BOOL (WINAPI * HeapSetInformation_Proc)(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
211 #endif
212
213 void
214 init_heap (void)
215 {
216 if (using_dynamic_heap)
217 {
218 unsigned long enable_lfh = 2;
219
220 /* After dumping, use a new private heap. We explicitly enable
221 the low fragmentation heap here, for the sake of pre Vista
222 versions. Note: this will harnlessly fail on Vista and
223 later, whyere the low fragmentation heap is enabled by
224 default. It will also fail on pre-Vista versions when Emacs
225 is run under a debugger; set _NO_DEBUG_HEAP=1 in the
226 environment before starting GDB to get low fragmentation heap
227 on XP and older systems, for the price of losing "certain
228 heap debug options"; for the details see
229 http://msdn.microsoft.com/en-us/library/windows/desktop/aa366705%28v=vs.85%29.aspx. */
230 data_region_end = data_region_base;
231
232 /* Create the private heap. */
233 heap = HeapCreate(0, 0, 0);
234
235 #ifndef _W64
236 /* Set the low-fragmentation heap for OS before XP and Windows
237 Server 2003. */
238 HMODULE hm_kernel32dll = LoadLibrary("kernel32.dll");
239 HeapSetInformation_Proc s_pfn_Heap_Set_Information = (HeapSetInformation_Proc) GetProcAddress(hm_kernel32dll, "HeapSetInformation");
240 if (s_pfn_Heap_Set_Information != NULL)
241 if (s_pfn_Heap_Set_Information ((PVOID) heap,
242 HeapCompatibilityInformation,
243 &enable_lfh, sizeof(enable_lfh)) == 0)
244 DebPrint (("Enabling Low Fragmentation Heap failed\n"));
245 #endif
246
247 the_malloc_fn = malloc_after_dump;
248 the_realloc_fn = realloc_after_dump;
249 the_free_fn = free_after_dump;
250 }
251 else
252 {
253 /* Find the RtlCreateHeap function. Headers for this function
254 are provided with the w32 ddk, but the function is available
255 in ntdll.dll since XP. */
256 HMODULE hm_ntdll = LoadLibrary ("ntdll.dll");
257 RtlCreateHeap_Proc s_pfn_Rtl_Create_Heap
258 = (RtlCreateHeap_Proc) GetProcAddress (hm_ntdll, "RtlCreateHeap");
259 /* Specific parameters for the private heap. */
260 RTL_HEAP_PARAMETERS params;
261 ZeroMemory(&params, sizeof(params));
262 params.Length = sizeof(RTL_HEAP_PARAMETERS);
263
264 data_region_base = (unsigned char *)ROUND_UP (dumped_data, 0x1000);
265 data_region_end = bc_limit = dumped_data + DUMPED_HEAP_SIZE;
266
267 params.InitialCommit = committed = 0x1000;
268 params.InitialReserve = sizeof(dumped_data);
269 /* Use our own routine to commit memory from the dumped_data
270 array. */
271 params.CommitRoutine = &dumped_data_commit;
272
273 /* Create the private heap. */
274 heap = s_pfn_Rtl_Create_Heap (0, data_region_base, 0, 0, NULL, &params);
275 the_malloc_fn = malloc_before_dump;
276 the_realloc_fn = realloc_before_dump;
277 the_free_fn = free_before_dump;
278 }
279
280 /* Update system version information to match current system. */
281 cache_system_info ();
282 }
283
284 #undef malloc
285 #undef realloc
286 #undef calloc
287 #undef free
288
289 /* FREEABLE_P checks if the block can be safely freed. */
290 #define FREEABLE_P(addr) \
291 ((unsigned char *)(addr) < dumped_data \
292 || (unsigned char *)(addr) >= dumped_data + DUMPED_HEAP_SIZE)
293
294 void *
295 malloc_after_dump (size_t size)
296 {
297 /* Use the new private heap. */
298 void *p = HeapAlloc (heap, 0, size);
299
300 /* After dump, keep track of the last allocated byte for sbrk(0). */
301 data_region_end = p + size - 1;
302 return p;
303 }
304
305 void *
306 malloc_before_dump (size_t size)
307 {
308 void *p;
309
310 /* Before dumping. The private heap can handle only requests for
311 less than MaxBlockSize. */
312 if (size < MaxBlockSize)
313 {
314 /* Use the private heap if possible. */
315 p = HeapAlloc (heap, 0, size);
316 }
317 else
318 {
319 /* Find the first big chunk that can hold the requested size. */
320 int i = 0;
321
322 for (i = 0; i < blocks_number; i++)
323 {
324 if (blocks[i].occupied == 0 && blocks[i].size >= size)
325 break;
326 }
327 if (i < blocks_number)
328 {
329 /* If found, use it. */
330 p = blocks[i].address;
331 blocks[i].occupied = TRUE;
332 }
333 else
334 {
335 /* Allocate a new big chunk from the end of the dumped_data
336 array. */
337 if (blocks_number >= MAX_BLOCKS)
338 {
339 fprintf(stderr,
340 "malloc_before_dump: no more big chunks available.\nEnlarge MAX_BLOCKS!\n");
341 exit (-1);
342 }
343 bc_limit -= size;
344 bc_limit = (unsigned char *)ROUND_DOWN (bc_limit, 0x10);
345 p = bc_limit;
346 blocks[blocks_number].address = p;
347 blocks[blocks_number].size = size;
348 blocks[blocks_number].occupied = TRUE;
349 blocks_number++;
350 if (bc_limit < dumped_data + committed)
351 {
352 /* Check that areas do not overlap. */
353 fprintf(stderr,
354 "malloc_before_dump: memory exhausted.\nEnlarge dumped_data[]!\n");
355 exit (-1);
356 }
357 }
358 }
359 return p;
360 }
361
362 /* Re-allocate the previously allocated block in ptr, making the new
363 block SIZE bytes long. */
364 void *
365 realloc_after_dump (void *ptr, size_t size)
366 {
367 void *p;
368
369 /* After dumping. */
370 if (FREEABLE_P (ptr))
371 {
372 /* Reallocate the block since it lies in the new heap. */
373 p = HeapReAlloc (heap, 0, ptr, size);
374 }
375 else
376 {
377 /* If the block lies in the dumped data, do not free it. Only
378 allocate a new one. */
379 p = HeapAlloc (heap, 0, size);
380 CopyMemory (p, ptr, size);
381 }
382 /* After dump, keep track of the last allocated byte for sbrk(0). */
383 data_region_end = p + size - 1;
384 return p;
385 }
386
387 void *
388 realloc_before_dump (void *ptr, size_t size)
389 {
390 void *p;
391
392 /* Before dumping. */
393 if (dumped_data < (unsigned char *)ptr
394 && (unsigned char *)ptr < bc_limit && size <= MaxBlockSize)
395 p = HeapReAlloc (heap, 0, ptr, size);
396 else
397 {
398 /* In this case, either the new block is too large for the heap,
399 or the old block was already too large. In both cases,
400 malloc_before_dump() and free_before_dump() will take care of
401 reallocation. */
402 p = malloc_before_dump (size);
403 CopyMemory (p, ptr, size);
404 free_before_dump (ptr);
405 }
406 return p;
407 }
408
409 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
410 void
411 free_after_dump (void *ptr)
412 {
413 /* After dumping. */
414 if (FREEABLE_P (ptr))
415 {
416 /* Free the block if it is in the new private heap. */
417 HeapFree (heap, 0, ptr);
418 }
419 }
420
421 void
422 free_before_dump (void *ptr)
423 {
424 /* Before dumping. */
425 if (dumped_data < (unsigned char *)ptr
426 && (unsigned char *)ptr < bc_limit)
427 {
428 /* Free the block if it is allocated in the private heap. */
429 HeapFree (heap, 0, ptr);
430 }
431 else
432 {
433 /* Look for the big chunk. */
434 int i;
435
436 for(i = 0; i < blocks_number; i++)
437 {
438 if (blocks[i].address == ptr)
439 {
440 /* Reset block occupation if found. */
441 blocks[i].occupied = 0;
442 break;
443 }
444 /* What if the block is not found? We should trigger an
445 error here. */
446 eassert (i < blocks_number);
447 }
448 }
449 }
450
451 /* Emulate getpagesize. */
452 int
453 getpagesize (void)
454 {
455 return sysinfo_cache.dwPageSize;
456 }
457
458 void *
459 sbrk (ptrdiff_t increment)
460 {
461 /* The data_region_end address is the one of the last byte
462 allocated. The sbrk() function is not emulated at all, except
463 for a 0 value of its parameter. This is needed by the emacs lisp
464 function `memory-limit'. */
465 return data_region_end;
466 }
467
468 #define MAX_BUFFER_SIZE (512 * 1024 * 1024)
469
470 /* MMAP allocation for buffers. */
471 void *
472 mmap_alloc (void **var, size_t nbytes)
473 {
474 void *p = NULL;
475
476 /* We implement amortized allocation. We start by reserving twice
477 the size requested and commit only the size requested. Then
478 realloc could proceed and use the reserved pages, reallocating
479 only if needed. Buffer shrink would happen only so that we stay
480 in the 2x range. This is a big win when visiting compressed
481 files, where the final size of the buffer is not known in
482 advance, and the buffer is enlarged several times as the data is
483 decompressed on the fly. */
484 if (nbytes < MAX_BUFFER_SIZE)
485 p = VirtualAlloc (NULL, (nbytes * 2), MEM_RESERVE, PAGE_READWRITE);
486
487 /* If it fails, or if the request is above 512MB, try with the
488 requested size. */
489 if (p == NULL)
490 p = VirtualAlloc (NULL, nbytes, MEM_RESERVE, PAGE_READWRITE);
491
492 if (p != NULL)
493 {
494 /* Now, commit pages for NBYTES. */
495 *var = VirtualAlloc (p, nbytes, MEM_COMMIT, PAGE_READWRITE);
496 }
497
498 if (!p && GetLastError () != ERROR_NOT_ENOUGH_MEMORY)
499 DebPrint (("mmap_alloc: error %ld\n", GetLastError()));
500
501 return *var = p;
502 }
503
504 void
505 mmap_free (void **var)
506 {
507 if (*var)
508 {
509 if (VirtualFree (*var, 0, MEM_RELEASE) == 0)
510 DebPrint (("mmap_free: error %ld\n", GetLastError()));
511 *var = NULL;
512 }
513 }
514
515 void *
516 mmap_realloc (void **var, size_t nbytes)
517 {
518 MEMORY_BASIC_INFORMATION memInfo, m2;
519
520 if (*var == NULL)
521 return mmap_alloc (var, nbytes);
522
523 /* This case happens in init_buffer(). */
524 if (nbytes == 0)
525 {
526 mmap_free (var);
527 return mmap_alloc (var, nbytes);
528 }
529
530 if (VirtualQuery (*var, &memInfo, sizeof (memInfo)) == 0)
531 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError()));
532
533 /* We need to enlarge the block. */
534 if (memInfo.RegionSize < nbytes)
535 {
536 if (VirtualQuery (*var + memInfo.RegionSize, &m2, sizeof(m2)) == 0)
537 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError()));
538 /* If there is enough room in the current reserved area, then
539 commit more pages as needed. */
540 if (m2.State == MEM_RESERVE
541 && nbytes <= memInfo.RegionSize + m2.RegionSize)
542 {
543 void *p;
544
545 p = VirtualAlloc (*var + memInfo.RegionSize,
546 nbytes - memInfo.RegionSize,
547 MEM_COMMIT, PAGE_READWRITE);
548 if (!p /* && GetLastError() != ERROR_NOT_ENOUGH_MEMORY */)
549 DebPrint (("realloc enlarge: VirtualAlloc error %ld\n",
550 GetLastError()));
551 return *var;
552 }
553 else
554 {
555 /* Else we must actually enlarge the block by allocating a
556 new one and copying previous contents from the old to the
557 new one. */
558 void *old_ptr = *var;
559
560 if (mmap_alloc (var, nbytes))
561 {
562 CopyMemory (*var, old_ptr, memInfo.RegionSize);
563 mmap_free (&old_ptr);
564 return *var;
565 }
566 else
567 {
568 /* We failed to enlarge the buffer. */
569 *var = old_ptr;
570 return NULL;
571 }
572 }
573 }
574
575 /* If we are shrinking by more than one page... */
576 if (memInfo.RegionSize > nbytes + getpagesize())
577 {
578 /* If we are shrinking a lot... */
579 if ((memInfo.RegionSize / 2) > nbytes)
580 {
581 /* Let's give some memory back to the system and release
582 some pages. */
583 void *old_ptr = *var;
584
585 if (mmap_alloc (var, nbytes))
586 {
587 CopyMemory (*var, old_ptr, nbytes);
588 mmap_free (&old_ptr);
589 return *var;
590 }
591 else
592 {
593 /* In case we fail to shrink, try to go on with the old block.
594 But that means there is a lot of memory pressure.
595 We could also decommit pages. */
596 *var = old_ptr;
597 return *var;
598 }
599 }
600
601 /* We still can decommit pages. */
602 if (VirtualFree (*var + nbytes + get_page_size(),
603 memInfo.RegionSize - nbytes - get_page_size(),
604 MEM_DECOMMIT) == 0)
605 DebPrint (("mmap_realloc: VirtualFree error %ld\n", GetLastError()));
606 return *var;
607 }
608
609 /* Not enlarging, not shrinking by more than one page. */
610 return *var;
611 }