]> code.delx.au - gnu-emacs/blob - src/w32heap.c
c0a17551d27ba30b5945ce4bf3999731c98ec25b
[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 #include <errno.h>
51
52 #include <sys/mman.h>
53 #include "w32common.h"
54 #include "w32heap.h"
55 #include "lisp.h" /* for VALMASK */
56
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. */
64
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
72 );
73
74 typedef LONG NTSTATUS;
75
76 typedef NTSTATUS
77 (NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
78 IN PVOID Base,
79 IN OUT PVOID *CommitAddress,
80 IN OUT PSIZE_T CommitSize
81 );
82
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;
96
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. */
110
111 unsigned char dumped_data[DUMPED_HEAP_SIZE];
112
113 /* Info for managing our preload heap, which is essentially a fixed size
114 data area in the executable. */
115 /* Info for keeping track of our heap. */
116 unsigned char *data_region_base = NULL;
117 unsigned char *data_region_end = NULL;
118 static DWORD_PTR committed = 0;
119
120 /* The maximum block size that can be handled by a non-growable w32
121 heap is limited by the MaxBlockSize value below.
122
123 This point deserves and explanation.
124
125 The W32 heap allocator can be used for a growable
126 heap or a non-growable one.
127
128 A growable heap is not compatible with a fixed base address for the
129 heap. Only a non-growable one is. One drawback of non-growable
130 heaps is that they can hold only objects smaller than a certain
131 size (the one defined below). Most of the largest blocks are GC'ed
132 before dumping. In any case and to be safe, we implement a simple
133 first-fit allocation algorithm starting at the end of the
134 dumped_data[] array like depicted below:
135
136 ----------------------------------------------
137 | | | |
138 | Private heap |-> <-| Big chunks |
139 | | | |
140 ----------------------------------------------
141 ^ ^ ^
142 dumped_data dumped_data bc_limit
143 + committed
144
145 */
146 #define HEAP_ENTRY_SHIFT 3
147 #define PAGE_SIZE 0x1000
148 #define MaxBlockSize (0x80000 - PAGE_SIZE)
149
150 #define MAX_BLOCKS 0x40
151
152 static struct
153 {
154 unsigned char *address;
155 size_t size;
156 DWORD occupied;
157 } blocks[MAX_BLOCKS];
158
159 static DWORD blocks_number = 0;
160 static unsigned char *bc_limit;
161
162 /* Handle for the private heap:
163 - inside the dumped_data[] array before dump,
164 - outside of it after dump.
165 */
166 HANDLE heap = NULL;
167
168 /* We redirect the standard allocation functions. */
169 malloc_fn the_malloc_fn;
170 realloc_fn the_realloc_fn;
171 free_fn the_free_fn;
172
173 /* It doesn't seem to be useful to allocate from a file mapping.
174 It would be if the memory was shared.
175 http://stackoverflow.com/questions/307060/what-is-the-purpose-of-allocating-pages-in-the-pagefile-with-createfilemapping */
176
177 /* This is the function to commit memory when the heap allocator
178 claims for new memory. Before dumping, we allocate space
179 from the fixed size dumped_data[] array.
180 */
181 NTSTATUS NTAPI
182 dumped_data_commit (PVOID Base, PVOID *CommitAddress, PSIZE_T CommitSize)
183 {
184 /* This is used before dumping.
185
186 The private heap is stored at dumped_data[] address.
187 We commit contiguous areas of the dumped_data array
188 as requests arrive. */
189 *CommitAddress = data_region_base + committed;
190 committed += *CommitSize;
191 if (((unsigned char *)(*CommitAddress)) + *CommitSize >= bc_limit)
192 {
193 /* Check that the private heap area does not overlap the big
194 chunks area. */
195 fprintf(stderr,
196 "dumped_data_commit: memory exhausted.\nEnlarge dumped_data[]!\n");
197 exit (-1);
198 }
199 return 0;
200 }
201
202 /* Heap creation. */
203
204 /* Under MinGW32, we want to turn on Low Fragmentation Heap for XP.
205 MinGW32 lacks those definitions. */
206 #ifndef _W64
207 typedef enum _HEAP_INFORMATION_CLASS {
208 HeapCompatibilityInformation
209 } HEAP_INFORMATION_CLASS;
210
211 typedef WINBASEAPI BOOL (WINAPI * HeapSetInformation_Proc)(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
212 #endif
213
214 void
215 init_heap (void)
216 {
217 if (using_dynamic_heap)
218 {
219 unsigned long enable_lfh = 2;
220
221 /* After dumping, use a new private heap. We explicitly enable
222 the low fragmentation heap here, for the sake of pre Vista
223 versions. Note: this will harnlessly fail on Vista and
224 later, whyere the low fragmentation heap is enabled by
225 default. It will also fail on pre-Vista versions when Emacs
226 is run under a debugger; set _NO_DEBUG_HEAP=1 in the
227 environment before starting GDB to get low fragmentation heap
228 on XP and older systems, for the price of losing "certain
229 heap debug options"; for the details see
230 http://msdn.microsoft.com/en-us/library/windows/desktop/aa366705%28v=vs.85%29.aspx. */
231 data_region_end = data_region_base;
232
233 /* Create the private heap. */
234 heap = HeapCreate(0, 0, 0);
235
236 #ifndef _W64
237 /* Set the low-fragmentation heap for OS before XP and Windows
238 Server 2003. */
239 HMODULE hm_kernel32dll = LoadLibrary("kernel32.dll");
240 HeapSetInformation_Proc s_pfn_Heap_Set_Information = (HeapSetInformation_Proc) GetProcAddress(hm_kernel32dll, "HeapSetInformation");
241 if (s_pfn_Heap_Set_Information != NULL)
242 if (s_pfn_Heap_Set_Information ((PVOID) heap,
243 HeapCompatibilityInformation,
244 &enable_lfh, sizeof(enable_lfh)) == 0)
245 DebPrint (("Enabling Low Fragmentation Heap failed: error %ld\n",
246 GetLastError ()));
247 #endif
248
249 the_malloc_fn = malloc_after_dump;
250 the_realloc_fn = realloc_after_dump;
251 the_free_fn = free_after_dump;
252 }
253 else
254 {
255 /* Find the RtlCreateHeap function. Headers for this function
256 are provided with the w32 ddk, but the function is available
257 in ntdll.dll since XP. */
258 HMODULE hm_ntdll = LoadLibrary ("ntdll.dll");
259 RtlCreateHeap_Proc s_pfn_Rtl_Create_Heap
260 = (RtlCreateHeap_Proc) GetProcAddress (hm_ntdll, "RtlCreateHeap");
261 /* Specific parameters for the private heap. */
262 RTL_HEAP_PARAMETERS params;
263 ZeroMemory(&params, sizeof(params));
264 params.Length = sizeof(RTL_HEAP_PARAMETERS);
265
266 data_region_base = (unsigned char *)ROUND_UP (dumped_data, 0x1000);
267 data_region_end = bc_limit = dumped_data + DUMPED_HEAP_SIZE;
268
269 params.InitialCommit = committed = 0x1000;
270 params.InitialReserve = sizeof(dumped_data);
271 /* Use our own routine to commit memory from the dumped_data
272 array. */
273 params.CommitRoutine = &dumped_data_commit;
274
275 /* Create the private heap. */
276 heap = s_pfn_Rtl_Create_Heap (0, data_region_base, 0, 0, NULL, &params);
277 the_malloc_fn = malloc_before_dump;
278 the_realloc_fn = realloc_before_dump;
279 the_free_fn = free_before_dump;
280 }
281
282 /* Update system version information to match current system. */
283 cache_system_info ();
284 }
285
286 #undef malloc
287 #undef realloc
288 #undef calloc
289 #undef free
290
291 /* FREEABLE_P checks if the block can be safely freed. */
292 #define FREEABLE_P(addr) \
293 ((unsigned char *)(addr) < dumped_data \
294 || (unsigned char *)(addr) >= dumped_data + DUMPED_HEAP_SIZE)
295
296 void *
297 malloc_after_dump (size_t size)
298 {
299 /* Use the new private heap. */
300 void *p = HeapAlloc (heap, 0, size);
301
302 /* After dump, keep track of the "brk value" for sbrk(0). */
303 if (p)
304 {
305 unsigned char *new_brk = (unsigned char *)p + size;
306
307 if (new_brk > data_region_end)
308 data_region_end = new_brk;
309 }
310 else
311 errno = ENOMEM;
312 return p;
313 }
314
315 void *
316 malloc_before_dump (size_t size)
317 {
318 void *p;
319
320 /* Before dumping. The private heap can handle only requests for
321 less than MaxBlockSize. */
322 if (size < MaxBlockSize)
323 {
324 /* Use the private heap if possible. */
325 p = HeapAlloc (heap, 0, size);
326 if (!p)
327 errno = ENOMEM;
328 }
329 else
330 {
331 /* Find the first big chunk that can hold the requested size. */
332 int i = 0;
333
334 for (i = 0; i < blocks_number; i++)
335 {
336 if (blocks[i].occupied == 0 && blocks[i].size >= size)
337 break;
338 }
339 if (i < blocks_number)
340 {
341 /* If found, use it. */
342 p = blocks[i].address;
343 blocks[i].occupied = TRUE;
344 }
345 else
346 {
347 /* Allocate a new big chunk from the end of the dumped_data
348 array. */
349 if (blocks_number >= MAX_BLOCKS)
350 {
351 fprintf(stderr,
352 "malloc_before_dump: no more big chunks available.\nEnlarge MAX_BLOCKS!\n");
353 exit (-1);
354 }
355 bc_limit -= size;
356 bc_limit = (unsigned char *)ROUND_DOWN (bc_limit, 0x10);
357 p = bc_limit;
358 blocks[blocks_number].address = p;
359 blocks[blocks_number].size = size;
360 blocks[blocks_number].occupied = TRUE;
361 blocks_number++;
362 if (bc_limit < dumped_data + committed)
363 {
364 /* Check that areas do not overlap. */
365 fprintf(stderr,
366 "malloc_before_dump: memory exhausted.\nEnlarge dumped_data[]!\n");
367 exit (-1);
368 }
369 }
370 }
371 return p;
372 }
373
374 /* Re-allocate the previously allocated block in ptr, making the new
375 block SIZE bytes long. */
376 void *
377 realloc_after_dump (void *ptr, size_t size)
378 {
379 void *p;
380
381 /* After dumping. */
382 if (FREEABLE_P (ptr))
383 {
384 /* Reallocate the block since it lies in the new heap. */
385 p = HeapReAlloc (heap, 0, ptr, size);
386 if (!p)
387 errno = ENOMEM;
388 }
389 else
390 {
391 /* If the block lies in the dumped data, do not free it. Only
392 allocate a new one. */
393 p = HeapAlloc (heap, 0, size);
394 if (p)
395 CopyMemory (p, ptr, size);
396 else
397 errno = ENOMEM;
398 }
399 /* After dump, keep track of the "brk value" for sbrk(0). */
400 if (p)
401 {
402 unsigned char *new_brk = (unsigned char *)p + size;
403
404 if (new_brk > data_region_end)
405 data_region_end = new_brk;
406 }
407 return p;
408 }
409
410 void *
411 realloc_before_dump (void *ptr, size_t size)
412 {
413 void *p;
414
415 /* Before dumping. */
416 if (dumped_data < (unsigned char *)ptr
417 && (unsigned char *)ptr < bc_limit && size <= MaxBlockSize)
418 {
419 p = HeapReAlloc (heap, 0, ptr, size);
420 if (!p)
421 errno = ENOMEM;
422 }
423 else
424 {
425 /* In this case, either the new block is too large for the heap,
426 or the old block was already too large. In both cases,
427 malloc_before_dump() and free_before_dump() will take care of
428 reallocation. */
429 p = malloc_before_dump (size);
430 /* If SIZE is below MaxBlockSize, malloc_before_dump will try to
431 allocate it in the fixed heap. If that fails, we could have
432 kept the block in its original place, above bc_limit, instead
433 of failing the call as below. But this doesn't seem to be
434 worth the added complexity, as loadup allocates only a very
435 small number of large blocks, and never reallocates them. */
436 if (p)
437 {
438 CopyMemory (p, ptr, size);
439 free_before_dump (ptr);
440 }
441 }
442 return p;
443 }
444
445 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
446 void
447 free_after_dump (void *ptr)
448 {
449 /* After dumping. */
450 if (FREEABLE_P (ptr))
451 {
452 /* Free the block if it is in the new private heap. */
453 HeapFree (heap, 0, ptr);
454 }
455 }
456
457 void
458 free_before_dump (void *ptr)
459 {
460 /* Before dumping. */
461 if (dumped_data < (unsigned char *)ptr
462 && (unsigned char *)ptr < bc_limit)
463 {
464 /* Free the block if it is allocated in the private heap. */
465 HeapFree (heap, 0, ptr);
466 }
467 else
468 {
469 /* Look for the big chunk. */
470 int i;
471
472 for(i = 0; i < blocks_number; i++)
473 {
474 if (blocks[i].address == ptr)
475 {
476 /* Reset block occupation if found. */
477 blocks[i].occupied = 0;
478 break;
479 }
480 /* What if the block is not found? We should trigger an
481 error here. */
482 eassert (i < blocks_number);
483 }
484 }
485 }
486
487 #ifdef ENABLE_CHECKING
488 void
489 report_temacs_memory_usage (void)
490 {
491 /* Emulate 'message', which writes to stderr in non-interactive
492 sessions. */
493 fprintf (stderr,
494 "Dump memory usage: Heap: %" PRIu64 " Large blocks(%lu): %" PRIu64 "\n",
495 (unsigned long long)committed, blocks_number,
496 (unsigned long long)(dumped_data + DUMPED_HEAP_SIZE - bc_limit));
497 }
498 #endif
499
500 /* Emulate getpagesize. */
501 int
502 getpagesize (void)
503 {
504 return sysinfo_cache.dwPageSize;
505 }
506
507 void *
508 sbrk (ptrdiff_t increment)
509 {
510 /* data_region_end is the address beyond the last allocated byte.
511 The sbrk() function is not emulated at all, except for a 0 value
512 of its parameter. This is needed by the Emacs Lisp function
513 `memory-limit'. */
514 eassert (increment == 0);
515 return data_region_end;
516 }
517
518 #define MAX_BUFFER_SIZE (512 * 1024 * 1024)
519
520 /* MMAP allocation for buffers. */
521 void *
522 mmap_alloc (void **var, size_t nbytes)
523 {
524 void *p = NULL;
525
526 /* We implement amortized allocation. We start by reserving twice
527 the size requested and commit only the size requested. Then
528 realloc could proceed and use the reserved pages, reallocating
529 only if needed. Buffer shrink would happen only so that we stay
530 in the 2x range. This is a big win when visiting compressed
531 files, where the final size of the buffer is not known in
532 advance, and the buffer is enlarged several times as the data is
533 decompressed on the fly. */
534 if (nbytes < MAX_BUFFER_SIZE)
535 p = VirtualAlloc (NULL, (nbytes * 2), MEM_RESERVE, PAGE_READWRITE);
536
537 /* If it fails, or if the request is above 512MB, try with the
538 requested size. */
539 if (p == NULL)
540 p = VirtualAlloc (NULL, nbytes, MEM_RESERVE, PAGE_READWRITE);
541
542 if (p != NULL)
543 {
544 /* Now, commit pages for NBYTES. */
545 *var = VirtualAlloc (p, nbytes, MEM_COMMIT, PAGE_READWRITE);
546 }
547
548 if (!p)
549 {
550 if (GetLastError () == ERROR_NOT_ENOUGH_MEMORY)
551 errno = ENOMEM;
552 else
553 {
554 DebPrint (("mmap_alloc: error %ld\n", GetLastError ()));
555 errno = EINVAL;
556 }
557 }
558
559 return *var = p;
560 }
561
562 void
563 mmap_free (void **var)
564 {
565 if (*var)
566 {
567 if (VirtualFree (*var, 0, MEM_RELEASE) == 0)
568 DebPrint (("mmap_free: error %ld\n", GetLastError ()));
569 *var = NULL;
570 }
571 }
572
573 void *
574 mmap_realloc (void **var, size_t nbytes)
575 {
576 MEMORY_BASIC_INFORMATION memInfo, m2;
577
578 if (*var == NULL)
579 return mmap_alloc (var, nbytes);
580
581 /* This case happens in init_buffer(). */
582 if (nbytes == 0)
583 {
584 mmap_free (var);
585 return mmap_alloc (var, nbytes);
586 }
587
588 if (VirtualQuery (*var, &memInfo, sizeof (memInfo)) == 0)
589 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError ()));
590
591 /* We need to enlarge the block. */
592 if (memInfo.RegionSize < nbytes)
593 {
594 if (VirtualQuery (*var + memInfo.RegionSize, &m2, sizeof(m2)) == 0)
595 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n",
596 GetLastError ()));
597 /* If there is enough room in the current reserved area, then
598 commit more pages as needed. */
599 if (m2.State == MEM_RESERVE
600 && nbytes <= memInfo.RegionSize + m2.RegionSize)
601 {
602 void *p;
603
604 p = VirtualAlloc (*var + memInfo.RegionSize,
605 nbytes - memInfo.RegionSize,
606 MEM_COMMIT, PAGE_READWRITE);
607 if (!p /* && GetLastError() != ERROR_NOT_ENOUGH_MEMORY */)
608 {
609 DebPrint (("realloc enlarge: VirtualAlloc error %ld\n",
610 GetLastError ()));
611 errno = ENOMEM;
612 }
613 return *var;
614 }
615 else
616 {
617 /* Else we must actually enlarge the block by allocating a
618 new one and copying previous contents from the old to the
619 new one. */
620 void *old_ptr = *var;
621
622 if (mmap_alloc (var, nbytes))
623 {
624 CopyMemory (*var, old_ptr, memInfo.RegionSize);
625 mmap_free (&old_ptr);
626 return *var;
627 }
628 else
629 {
630 /* We failed to enlarge the buffer. */
631 *var = old_ptr;
632 return NULL;
633 }
634 }
635 }
636
637 /* If we are shrinking by more than one page... */
638 if (memInfo.RegionSize > nbytes + getpagesize())
639 {
640 /* If we are shrinking a lot... */
641 if ((memInfo.RegionSize / 2) > nbytes)
642 {
643 /* Let's give some memory back to the system and release
644 some pages. */
645 void *old_ptr = *var;
646
647 if (mmap_alloc (var, nbytes))
648 {
649 CopyMemory (*var, old_ptr, nbytes);
650 mmap_free (&old_ptr);
651 return *var;
652 }
653 else
654 {
655 /* In case we fail to shrink, try to go on with the old block.
656 But that means there is a lot of memory pressure.
657 We could also decommit pages. */
658 *var = old_ptr;
659 return *var;
660 }
661 }
662
663 /* We still can decommit pages. */
664 if (VirtualFree (*var + nbytes + get_page_size(),
665 memInfo.RegionSize - nbytes - get_page_size(),
666 MEM_DECOMMIT) == 0)
667 DebPrint (("mmap_realloc: VirtualFree error %ld\n", GetLastError ()));
668 return *var;
669 }
670
671 /* Not enlarging, not shrinking by more than one page. */
672 return *var;
673 }