Merge from emacs-24; up to 2014-06-06T02:22:40Z!monnier@iro.umontreal.ca
[emacs.git] / src / w32heap.c
blob94ba34287946c6b392fc34eccabaf75be35e1942
1 /* Heap management routines for GNU Emacs on the Microsoft Windows
2 API. Copyright (C) 1994, 2001-2014 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
20 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
24 Heavily modified by Fabrice Popineau (fabrice.popineau@gmail.com) 28-02-2014
28 Memory allocation scheme for w32/w64:
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. */
48 #include <config.h>
49 #include <stdio.h>
50 #include <errno.h>
52 #include <sys/mman.h>
53 #include "w32common.h"
54 #include "w32heap.h"
55 #include "lisp.h" /* for VALMASK */
57 /* We chose to leave those declarations here. They are used only in
58 this file. The RtlCreateHeap is available since XP. It is located
59 in ntdll.dll and is available with the DDK. People often
60 complained that HeapCreate doesn't offer the ability to create a
61 heap at a given place, which we need here, and which RtlCreateHeap
62 provides. We reproduce here the definitions available with the
63 DDK. */
65 typedef PVOID (WINAPI * RtlCreateHeap_Proc) (
66 /* _In_ */ ULONG Flags,
67 /* _In_opt_ */ PVOID HeapBase,
68 /* _In_opt_ */ SIZE_T ReserveSize,
69 /* _In_opt_ */ SIZE_T CommitSize,
70 /* _In_opt_ */ PVOID Lock,
71 /* _In_opt_ */ PVOID Parameters
74 typedef LONG NTSTATUS;
76 typedef NTSTATUS
77 (NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
78 IN PVOID Base,
79 IN OUT PVOID *CommitAddress,
80 IN OUT PSIZE_T CommitSize
83 typedef struct _RTL_HEAP_PARAMETERS {
84 ULONG Length;
85 SIZE_T SegmentReserve;
86 SIZE_T SegmentCommit;
87 SIZE_T DeCommitFreeBlockThreshold;
88 SIZE_T DeCommitTotalFreeThreshold;
89 SIZE_T MaximumAllocationSize;
90 SIZE_T VirtualMemoryThreshold;
91 SIZE_T InitialCommit;
92 SIZE_T InitialReserve;
93 PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
94 SIZE_T Reserved[ 2 ];
95 } RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;
97 /* We reserve space for dumping emacs lisp byte-code inside a static
98 array. By storing it in an array, the generic mechanism in
99 unexecw32.c will be able to dump it without the need to add a
100 special segment to the executable. In order to be able to do this
101 without losing too much space, we need to create a Windows heap at
102 the specific address of the static array. The RtlCreateHeap
103 available inside the NT kernel since XP will do this. It allows to
104 create a non-growable heap at a specific address. So before
105 dumping, we create a non-growable heap at the address of the
106 dumped_data[] array. After dumping, we reuse memory allocated
107 there without being able to free it (but most of it is not meant to
108 be freed anyway), and we use a new private heap for all new
109 allocations. */
111 /* FIXME: Most of the space reserved for dumped_data[] is only used by
112 the 1st bootstrap-emacs.exe built while bootstrapping. Once the
113 preloaded Lisp files are byte-compiled, the next loadup uses less
114 than half of the size stated below. It would be nice to find a way
115 to build only the first bootstrap-emacs.exe with the large size,
116 and reset that to a lower value afterwards. */
117 #ifdef _WIN64
118 # define DUMPED_HEAP_SIZE (18*1024*1024)
119 #else
120 # define DUMPED_HEAP_SIZE (11*1024*1024)
121 #endif
123 static unsigned char dumped_data[DUMPED_HEAP_SIZE];
125 /* Info for keeping track of our dynamic heap used after dumping. */
126 unsigned char *data_region_base = NULL;
127 unsigned char *data_region_end = NULL;
128 static DWORD_PTR committed = 0;
130 /* The maximum block size that can be handled by a non-growable w32
131 heap is limited by the MaxBlockSize value below.
133 This point deserves and explanation.
135 The W32 heap allocator can be used for a growable
136 heap or a non-growable one.
138 A growable heap is not compatible with a fixed base address for the
139 heap. Only a non-growable one is. One drawback of non-growable
140 heaps is that they can hold only objects smaller than a certain
141 size (the one defined below). Most of the largest blocks are GC'ed
142 before dumping. In any case and to be safe, we implement a simple
143 first-fit allocation algorithm starting at the end of the
144 dumped_data[] array like depicted below:
146 ----------------------------------------------
147 | | | |
148 | Private heap |-> <-| Big chunks |
149 | | | |
150 ----------------------------------------------
151 ^ ^ ^
152 dumped_data dumped_data bc_limit
153 + committed
157 /* Info for managing our preload heap, which is essentially a fixed size
158 data area in the executable. */
159 #define PAGE_SIZE 0x1000
160 #define MaxBlockSize (0x80000 - PAGE_SIZE)
162 #define MAX_BLOCKS 0x40
164 static struct
166 unsigned char *address;
167 size_t size;
168 DWORD occupied;
169 } blocks[MAX_BLOCKS];
171 static DWORD blocks_number = 0;
172 static unsigned char *bc_limit;
174 /* Handle for the private heap:
175 - inside the dumped_data[] array before dump,
176 - outside of it after dump.
178 HANDLE heap = NULL;
180 /* We redirect the standard allocation functions. */
181 malloc_fn the_malloc_fn;
182 realloc_fn the_realloc_fn;
183 free_fn the_free_fn;
185 /* It doesn't seem to be useful to allocate from a file mapping.
186 It would be if the memory was shared.
187 http://stackoverflow.com/questions/307060/what-is-the-purpose-of-allocating-pages-in-the-pagefile-with-createfilemapping */
189 /* This is the function to commit memory when the heap allocator
190 claims for new memory. Before dumping, we allocate space
191 from the fixed size dumped_data[] array.
193 NTSTATUS NTAPI
194 dumped_data_commit (PVOID Base, PVOID *CommitAddress, PSIZE_T CommitSize)
196 /* This is used before dumping.
198 The private heap is stored at dumped_data[] address.
199 We commit contiguous areas of the dumped_data array
200 as requests arrive. */
201 *CommitAddress = data_region_base + committed;
202 committed += *CommitSize;
203 if (((unsigned char *)(*CommitAddress)) + *CommitSize >= bc_limit)
205 /* Check that the private heap area does not overlap the big
206 chunks area. */
207 fprintf(stderr,
208 "dumped_data_commit: memory exhausted.\nEnlarge dumped_data[]!\n");
209 exit (-1);
211 return 0;
214 /* Heap creation. */
216 /* We want to turn on Low Fragmentation Heap for XP and older systems.
217 MinGW32 lacks those definitions. */
218 #ifndef _W64
219 typedef enum _HEAP_INFORMATION_CLASS {
220 HeapCompatibilityInformation
221 } HEAP_INFORMATION_CLASS;
223 typedef WINBASEAPI BOOL (WINAPI * HeapSetInformation_Proc)(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
224 #endif
226 void
227 init_heap (void)
229 if (using_dynamic_heap)
231 unsigned long enable_lfh = 2;
233 /* After dumping, use a new private heap. We explicitly enable
234 the low fragmentation heap (LFH) here, for the sake of pre
235 Vista versions. Note: this will harmlessly fail on Vista and
236 later, where the low-fragmentation heap is enabled by
237 default. It will also fail on pre-Vista versions when Emacs
238 is run under a debugger; set _NO_DEBUG_HEAP=1 in the
239 environment before starting GDB to get low fragmentation heap
240 on XP and older systems, for the price of losing "certain
241 heap debug options"; for the details see
242 http://msdn.microsoft.com/en-us/library/windows/desktop/aa366705%28v=vs.85%29.aspx. */
243 data_region_end = data_region_base;
245 /* Create the private heap. */
246 heap = HeapCreate(0, 0, 0);
248 #ifndef _W64
249 /* Set the low-fragmentation heap for OS before Vista. */
250 HMODULE hm_kernel32dll = LoadLibrary("kernel32.dll");
251 HeapSetInformation_Proc s_pfn_Heap_Set_Information = (HeapSetInformation_Proc) GetProcAddress(hm_kernel32dll, "HeapSetInformation");
252 if (s_pfn_Heap_Set_Information != NULL)
253 if (s_pfn_Heap_Set_Information ((PVOID) heap,
254 HeapCompatibilityInformation,
255 &enable_lfh, sizeof(enable_lfh)) == 0)
256 DebPrint (("Enabling Low Fragmentation Heap failed: error %ld\n",
257 GetLastError ()));
258 #endif
260 the_malloc_fn = malloc_after_dump;
261 the_realloc_fn = realloc_after_dump;
262 the_free_fn = free_after_dump;
264 else
266 /* Find the RtlCreateHeap function. Headers for this function
267 are provided with the w32 ddk, but the function is available
268 in ntdll.dll since XP. */
269 HMODULE hm_ntdll = LoadLibrary ("ntdll.dll");
270 RtlCreateHeap_Proc s_pfn_Rtl_Create_Heap
271 = (RtlCreateHeap_Proc) GetProcAddress (hm_ntdll, "RtlCreateHeap");
272 /* Specific parameters for the private heap. */
273 RTL_HEAP_PARAMETERS params;
274 ZeroMemory(&params, sizeof(params));
275 params.Length = sizeof(RTL_HEAP_PARAMETERS);
277 data_region_base = (unsigned char *)ROUND_UP (dumped_data, 0x1000);
278 data_region_end = bc_limit = dumped_data + DUMPED_HEAP_SIZE;
280 params.InitialCommit = committed = 0x1000;
281 params.InitialReserve = sizeof(dumped_data);
282 /* Use our own routine to commit memory from the dumped_data
283 array. */
284 params.CommitRoutine = &dumped_data_commit;
286 /* Create the private heap. */
287 heap = s_pfn_Rtl_Create_Heap (0, data_region_base, 0, 0, NULL, &params);
288 the_malloc_fn = malloc_before_dump;
289 the_realloc_fn = realloc_before_dump;
290 the_free_fn = free_before_dump;
293 /* Update system version information to match current system. */
294 cache_system_info ();
297 #undef malloc
298 #undef realloc
299 #undef free
301 /* FREEABLE_P checks if the block can be safely freed. */
302 #define FREEABLE_P(addr) \
303 ((unsigned char *)(addr) < dumped_data \
304 || (unsigned char *)(addr) >= dumped_data + DUMPED_HEAP_SIZE)
306 void *
307 malloc_after_dump (size_t size)
309 /* Use the new private heap. */
310 void *p = HeapAlloc (heap, 0, size);
312 /* After dump, keep track of the "brk value" for sbrk(0). */
313 if (p)
315 unsigned char *new_brk = (unsigned char *)p + size;
317 if (new_brk > data_region_end)
318 data_region_end = new_brk;
320 else
321 errno = ENOMEM;
322 return p;
325 void *
326 malloc_before_dump (size_t size)
328 void *p;
330 /* Before dumping. The private heap can handle only requests for
331 less than MaxBlockSize. */
332 if (size < MaxBlockSize)
334 /* Use the private heap if possible. */
335 p = HeapAlloc (heap, 0, size);
336 if (!p)
337 errno = ENOMEM;
339 else
341 /* Find the first big chunk that can hold the requested size. */
342 int i = 0;
344 for (i = 0; i < blocks_number; i++)
346 if (blocks[i].occupied == 0 && blocks[i].size >= size)
347 break;
349 if (i < blocks_number)
351 /* If found, use it. */
352 p = blocks[i].address;
353 blocks[i].occupied = TRUE;
355 else
357 /* Allocate a new big chunk from the end of the dumped_data
358 array. */
359 if (blocks_number >= MAX_BLOCKS)
361 fprintf(stderr,
362 "malloc_before_dump: no more big chunks available.\nEnlarge MAX_BLOCKS!\n");
363 exit (-1);
365 bc_limit -= size;
366 bc_limit = (unsigned char *)ROUND_DOWN (bc_limit, 0x10);
367 p = bc_limit;
368 blocks[blocks_number].address = p;
369 blocks[blocks_number].size = size;
370 blocks[blocks_number].occupied = TRUE;
371 blocks_number++;
372 if (bc_limit < dumped_data + committed)
374 /* Check that areas do not overlap. */
375 fprintf(stderr,
376 "malloc_before_dump: memory exhausted.\nEnlarge dumped_data[]!\n");
377 exit (-1);
381 return p;
384 /* Re-allocate the previously allocated block in ptr, making the new
385 block SIZE bytes long. */
386 void *
387 realloc_after_dump (void *ptr, size_t size)
389 void *p;
391 /* After dumping. */
392 if (FREEABLE_P (ptr))
394 /* Reallocate the block since it lies in the new heap. */
395 p = HeapReAlloc (heap, 0, ptr, size);
396 if (!p)
397 errno = ENOMEM;
399 else
401 /* If the block lies in the dumped data, do not free it. Only
402 allocate a new one. */
403 p = HeapAlloc (heap, 0, size);
404 if (p)
405 CopyMemory (p, ptr, size);
406 else
407 errno = ENOMEM;
409 /* After dump, keep track of the "brk value" for sbrk(0). */
410 if (p)
412 unsigned char *new_brk = (unsigned char *)p + size;
414 if (new_brk > data_region_end)
415 data_region_end = new_brk;
417 return p;
420 void *
421 realloc_before_dump (void *ptr, size_t size)
423 void *p;
425 /* Before dumping. */
426 if (dumped_data < (unsigned char *)ptr
427 && (unsigned char *)ptr < bc_limit && size <= MaxBlockSize)
429 p = HeapReAlloc (heap, 0, ptr, size);
430 if (!p)
431 errno = ENOMEM;
433 else
435 /* In this case, either the new block is too large for the heap,
436 or the old block was already too large. In both cases,
437 malloc_before_dump() and free_before_dump() will take care of
438 reallocation. */
439 p = malloc_before_dump (size);
440 /* If SIZE is below MaxBlockSize, malloc_before_dump will try to
441 allocate it in the fixed heap. If that fails, we could have
442 kept the block in its original place, above bc_limit, instead
443 of failing the call as below. But this doesn't seem to be
444 worth the added complexity, as loadup allocates only a very
445 small number of large blocks, and never reallocates them. */
446 if (p)
448 CopyMemory (p, ptr, size);
449 free_before_dump (ptr);
452 return p;
455 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
456 void
457 free_after_dump (void *ptr)
459 /* After dumping. */
460 if (FREEABLE_P (ptr))
462 /* Free the block if it is in the new private heap. */
463 HeapFree (heap, 0, ptr);
467 void
468 free_before_dump (void *ptr)
470 /* Before dumping. */
471 if (dumped_data < (unsigned char *)ptr
472 && (unsigned char *)ptr < bc_limit)
474 /* Free the block if it is allocated in the private heap. */
475 HeapFree (heap, 0, ptr);
477 else
479 /* Look for the big chunk. */
480 int i;
482 for (i = 0; i < blocks_number; i++)
484 if (blocks[i].address == ptr)
486 /* Reset block occupation if found. */
487 blocks[i].occupied = 0;
488 break;
490 /* What if the block is not found? We should trigger an
491 error here. */
492 eassert (i < blocks_number);
497 #ifdef ENABLE_CHECKING
498 void
499 report_temacs_memory_usage (void)
501 DWORD blocks_used = 0;
502 size_t large_mem_used = 0;
503 int i;
505 for (i = 0; i < blocks_number; i++)
506 if (blocks[i].occupied)
508 blocks_used++;
509 large_mem_used += blocks[i].size;
512 /* Emulate 'message', which writes to stderr in non-interactive
513 sessions. */
514 fprintf (stderr,
515 "Dump memory usage: Heap: %" PRIu64 " Large blocks(%lu/%lu): %" PRIu64 "/%" PRIu64 "\n",
516 (unsigned long long)committed, blocks_used, blocks_number,
517 (unsigned long long)large_mem_used,
518 (unsigned long long)(dumped_data + DUMPED_HEAP_SIZE - bc_limit));
520 #endif
522 /* Emulate getpagesize. */
524 getpagesize (void)
526 return sysinfo_cache.dwPageSize;
529 void *
530 sbrk (ptrdiff_t increment)
532 /* data_region_end is the address beyond the last allocated byte.
533 The sbrk() function is not emulated at all, except for a 0 value
534 of its parameter. This is needed by the Emacs Lisp function
535 `memory-limit'. */
536 eassert (increment == 0);
537 return data_region_end;
540 #define MAX_BUFFER_SIZE (512 * 1024 * 1024)
542 /* MMAP allocation for buffers. */
543 void *
544 mmap_alloc (void **var, size_t nbytes)
546 void *p = NULL;
548 /* We implement amortized allocation. We start by reserving twice
549 the size requested and commit only the size requested. Then
550 realloc could proceed and use the reserved pages, reallocating
551 only if needed. Buffer shrink would happen only so that we stay
552 in the 2x range. This is a big win when visiting compressed
553 files, where the final size of the buffer is not known in
554 advance, and the buffer is enlarged several times as the data is
555 decompressed on the fly. */
556 if (nbytes < MAX_BUFFER_SIZE)
557 p = VirtualAlloc (NULL, (nbytes * 2), MEM_RESERVE, PAGE_READWRITE);
559 /* If it fails, or if the request is above 512MB, try with the
560 requested size. */
561 if (p == NULL)
562 p = VirtualAlloc (NULL, nbytes, MEM_RESERVE, PAGE_READWRITE);
564 if (p != NULL)
566 /* Now, commit pages for NBYTES. */
567 *var = VirtualAlloc (p, nbytes, MEM_COMMIT, PAGE_READWRITE);
570 if (!p)
572 if (GetLastError () == ERROR_NOT_ENOUGH_MEMORY)
573 errno = ENOMEM;
574 else
576 DebPrint (("mmap_alloc: error %ld\n", GetLastError ()));
577 errno = EINVAL;
581 return *var = p;
584 void
585 mmap_free (void **var)
587 if (*var)
589 if (VirtualFree (*var, 0, MEM_RELEASE) == 0)
590 DebPrint (("mmap_free: error %ld\n", GetLastError ()));
591 *var = NULL;
595 void *
596 mmap_realloc (void **var, size_t nbytes)
598 MEMORY_BASIC_INFORMATION memInfo, m2;
600 if (*var == NULL)
601 return mmap_alloc (var, nbytes);
603 /* This case happens in init_buffer(). */
604 if (nbytes == 0)
606 mmap_free (var);
607 return mmap_alloc (var, nbytes);
610 if (VirtualQuery (*var, &memInfo, sizeof (memInfo)) == 0)
611 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError ()));
613 /* We need to enlarge the block. */
614 if (memInfo.RegionSize < nbytes)
616 if (VirtualQuery (*var + memInfo.RegionSize, &m2, sizeof(m2)) == 0)
617 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n",
618 GetLastError ()));
619 /* If there is enough room in the current reserved area, then
620 commit more pages as needed. */
621 if (m2.State == MEM_RESERVE
622 && nbytes <= memInfo.RegionSize + m2.RegionSize)
624 void *p;
626 p = VirtualAlloc (*var + memInfo.RegionSize,
627 nbytes - memInfo.RegionSize,
628 MEM_COMMIT, PAGE_READWRITE);
629 if (!p /* && GetLastError() != ERROR_NOT_ENOUGH_MEMORY */)
631 DebPrint (("realloc enlarge: VirtualAlloc error %ld\n",
632 GetLastError ()));
633 errno = ENOMEM;
635 return *var;
637 else
639 /* Else we must actually enlarge the block by allocating a
640 new one and copying previous contents from the old to the
641 new one. */
642 void *old_ptr = *var;
644 if (mmap_alloc (var, nbytes))
646 CopyMemory (*var, old_ptr, memInfo.RegionSize);
647 mmap_free (&old_ptr);
648 return *var;
650 else
652 /* We failed to enlarge the buffer. */
653 *var = old_ptr;
654 return NULL;
659 /* If we are shrinking by more than one page... */
660 if (memInfo.RegionSize > nbytes + getpagesize())
662 /* If we are shrinking a lot... */
663 if ((memInfo.RegionSize / 2) > nbytes)
665 /* Let's give some memory back to the system and release
666 some pages. */
667 void *old_ptr = *var;
669 if (mmap_alloc (var, nbytes))
671 CopyMemory (*var, old_ptr, nbytes);
672 mmap_free (&old_ptr);
673 return *var;
675 else
677 /* In case we fail to shrink, try to go on with the old block.
678 But that means there is a lot of memory pressure.
679 We could also decommit pages. */
680 *var = old_ptr;
681 return *var;
685 /* We still can decommit pages. */
686 if (VirtualFree (*var + nbytes + get_page_size(),
687 memInfo.RegionSize - nbytes - get_page_size(),
688 MEM_DECOMMIT) == 0)
689 DebPrint (("mmap_realloc: VirtualFree error %ld\n", GetLastError ()));
690 return *var;
693 /* Not enlarging, not shrinking by more than one page. */
694 return *var;