1 /* Heap management routines for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1994, 2001-2023 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 <https://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, if unexec is to be used:
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, or always if pdumper is used:
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 <sys/resource.h>
54 #include "w32common.h"
55 #include "w32heap.h"
56 #include "lisp.h"
57 #include "w32.h" /* for FD_SETSIZE */
58
59 /* We chose to leave those declarations here. They are used only in
60 this file. The RtlCreateHeap is available since XP. It is located
61 in ntdll.dll and is available with the DDK. People often
62 complained that HeapCreate doesn't offer the ability to create a
63 heap at a given place, which we need here, and which RtlCreateHeap
64 provides. We reproduce here the definitions available with the
65 DDK. */
66
67 typedef PVOID (WINAPI * RtlCreateHeap_Proc) (
68 /* _In_ */ ULONG Flags,
69 /* _In_opt_ */ PVOID HeapBase,
70 /* _In_opt_ */ SIZE_T ReserveSize,
71 /* _In_opt_ */ SIZE_T CommitSize,
72 /* _In_opt_ */ PVOID Lock,
73 /* _In_opt_ */ PVOID Parameters
74 );
75
76 typedef LONG NTSTATUS;
77
78 typedef NTSTATUS (NTAPI *PRTL_HEAP_COMMIT_ROUTINE) (
79 IN PVOID Base,
80 IN OUT PVOID *CommitAddress,
81 IN OUT PSIZE_T CommitSize
82 );
83
84 typedef struct _RTL_HEAP_PARAMETERS {
85 ULONG Length;
86 SIZE_T SegmentReserve;
87 SIZE_T SegmentCommit;
88 SIZE_T DeCommitFreeBlockThreshold;
89 SIZE_T DeCommitTotalFreeThreshold;
90 SIZE_T MaximumAllocationSize;
91 SIZE_T VirtualMemoryThreshold;
92 SIZE_T InitialCommit;
93 SIZE_T InitialReserve;
94 PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
95 SIZE_T Reserved[ 2 ];
96 } RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;
97
98 /* We reserve space for dumping emacs lisp byte-code inside a static
99 array. By storing it in an array, the generic mechanism in
100 unexecw32.c will be able to dump it without the need to add a
101 special segment to the executable. In order to be able to do this
102 without losing too much space, we need to create a Windows heap at
103 the specific address of the static array. The RtlCreateHeap
104 available inside the NT kernel since XP will do this. It allows the
105 creation of a non-growable heap at a specific address. So before
106 dumping, we create a non-growable heap at the address of the
107 dumped_data[] array. After dumping, we reuse memory allocated
108 there without being able to free it (but most of it is not meant to
109 be freed anyway), and we use a new private heap for all new
110 allocations. */
111
112 /* FIXME: Most of the space reserved for dumped_data[] is only used by
113 the 1st bootstrap-emacs.exe built while bootstrapping. Once the
114 preloaded Lisp files are byte-compiled, the next loadup uses less
115 than half of the size stated below. It would be nice to find a way
116 to build only the first bootstrap-emacs.exe with the large size,
117 and reset that to a lower value afterwards. */
118 #ifndef HAVE_UNEXEC
119 /* We don't use dumped_data[], so define to a small size that won't
120 matter. */
121 # define DUMPED_HEAP_SIZE 10
122 #else
123 # if defined _WIN64 || defined WIDE_EMACS_INT
124 # define DUMPED_HEAP_SIZE (28*1024*1024)
125 # else
126 # define DUMPED_HEAP_SIZE (18*1024*1024)
127 # endif
128 #endif
129
130 static unsigned char dumped_data[DUMPED_HEAP_SIZE];
131
132 /* Info for keeping track of our dynamic heap used after dumping. */
133 unsigned char *data_region_base = NULL;
134 unsigned char *data_region_end = NULL;
135 static DWORD_PTR committed = 0;
136
137 /* The maximum block size that can be handled by a non-growable w32
138 heap is limited by the MaxBlockSize value below.
139
140 This point deserves an explanation.
141
142 The W32 heap allocator can be used for a growable heap or a
143 non-growable one.
144
145 A growable heap is not compatible with a fixed base address for the
146 heap. Only a non-growable one is. One drawback of non-growable
147 heaps is that they can hold only objects smaller than a certain
148 size (the one defined below). Most of the larger blocks are GC'ed
149 before dumping. In any case, and to be safe, we implement a simple
150 first-fit allocation algorithm starting at the end of the
151 dumped_data[] array as depicted below:
152
153 ----------------------------------------------
154 | | | |
155 | Private heap |-> <-| Big chunks |
156 | | | |
157 ----------------------------------------------
158 ^ ^ ^
159 dumped_data dumped_data bc_limit
160 + committed
161
162 */
163
164 /* Info for managing our preload heap, which is essentially a fixed size
165 data area in the executable. */
166 #define PAGE_SIZE 0x1000
167 #define MaxBlockSize (0x80000 - PAGE_SIZE)
168
169 #define MAX_BLOCKS 0x40
170
171 static struct
172 {
173 unsigned char *address;
174 size_t size;
175 DWORD occupied;
176 } blocks[MAX_BLOCKS];
177
178 static DWORD blocks_number = 0;
179 static unsigned char *bc_limit;
180
181 /* Handle for the private heap:
182 - inside the dumped_data[] array before dump with unexec,
183 - outside of it after dump, or always if pdumper is used.
184 */
185 HANDLE heap = NULL;
186
187 /* We redirect the standard allocation functions. */
188 malloc_fn the_malloc_fn;
189 realloc_fn the_realloc_fn;
190 free_fn the_free_fn;
191
192 static void *
193 heap_alloc (size_t size)
194 {
195 void *p = size <= PTRDIFF_MAX ? HeapAlloc (heap, 0, size | !size) : NULL;
196 if (!p)
197 errno = ENOMEM;
198 return p;
199 }
200
201 static void *
202 heap_realloc (void *ptr, size_t size)
203 {
204 void *p = (size <= PTRDIFF_MAX
205 ? HeapReAlloc (heap, 0, ptr, size | !size)
206 : NULL);
207 if (!p)
208 errno = ENOMEM;
209 return p;
210 }
211
212 /* It doesn't seem to be useful to allocate from a file mapping.
213 It would be if the memory was shared.
214 https://stackoverflow.com/questions/307060/what-is-the-purpose-of-allocating-pages-in-the-pagefile-with-createfilemapping */
215
216 /* This is the function to commit memory when the heap allocator
217 claims for new memory. Before dumping with unexec, we allocate
218 space from the fixed size dumped_data[] array.
219 */
220 static NTSTATUS NTAPI
221 dumped_data_commit (PVOID Base, PVOID *CommitAddress, PSIZE_T CommitSize)
222 {
223 /* This is used before dumping.
224
225 The private heap is stored at dumped_data[] address.
226 We commit contiguous areas of the dumped_data array
227 as requests arrive. */
228 *CommitAddress = data_region_base + committed;
229 committed += *CommitSize;
230 /* Check that the private heap area does not overlap the big chunks area. */
231 if (((unsigned char *)(*CommitAddress)) + *CommitSize >= bc_limit)
232 {
233 fprintf (stderr,
234 "dumped_data_commit: memory exhausted.\nEnlarge dumped_data[]!\n");
235 exit (-1);
236 }
237 return 0;
238 }
239
240 /* Heap creation. */
241
242 /* We want to turn on Low Fragmentation Heap for XP and older systems.
243 MinGW32 lacks those definitions. */
244 #ifndef MINGW_W64
245 typedef enum _HEAP_INFORMATION_CLASS {
246 HeapCompatibilityInformation
247 } HEAP_INFORMATION_CLASS;
248
249 typedef WINBASEAPI BOOL (WINAPI * HeapSetInformation_Proc)(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
250 #endif
251
252 void
253 init_heap (bool use_dynamic_heap)
254 {
255 /* FIXME: Remove the condition, the 'else' branch below, and all the
256 related definitions and code, including dumped_data[], when unexec
257 support is removed from Emacs. */
258 if (use_dynamic_heap)
259 {
260 /* After dumping, use a new private heap. We explicitly enable
261 the low fragmentation heap (LFH) here, for the sake of pre
262 Vista versions. Note: this will harmlessly fail on Vista and
263 later, where the low-fragmentation heap is enabled by
264 default. It will also fail on pre-Vista versions when Emacs
265 is run under a debugger; set _NO_DEBUG_HEAP=1 in the
266 environment before starting GDB to get low fragmentation heap
267 on XP and older systems, for the price of losing "certain
268 heap debug options"; for the details see
269 https://msdn.microsoft.com/en-us/library/windows/desktop/aa366705%28v=vs.85%29.aspx. */
270 data_region_end = data_region_base;
271
272 /* Create the private heap. */
273 heap = HeapCreate (0, 0, 0);
274
275 #ifndef MINGW_W64
276 unsigned long enable_lfh = 2;
277 /* Set the low-fragmentation heap for OS before Vista. */
278 HMODULE hm_kernel32dll = LoadLibrary ("kernel32.dll");
279 HeapSetInformation_Proc s_pfn_Heap_Set_Information =
280 (HeapSetInformation_Proc) get_proc_addr (hm_kernel32dll,
281 "HeapSetInformation");
282 if (s_pfn_Heap_Set_Information != NULL)
283 {
284 if (s_pfn_Heap_Set_Information ((PVOID) heap,
285 HeapCompatibilityInformation,
286 &enable_lfh, sizeof(enable_lfh)) == 0)
287 DebPrint (("Enabling Low Fragmentation Heap failed: error %ld\n",
288 GetLastError ()));
289 }
290 #endif
291
292 if (os_subtype == OS_SUBTYPE_9X)
293 {
294 the_malloc_fn = malloc_after_dump_9x;
295 the_realloc_fn = realloc_after_dump_9x;
296 the_free_fn = free_after_dump_9x;
297 }
298 else
299 {
300 the_malloc_fn = malloc_after_dump;
301 the_realloc_fn = realloc_after_dump;
302 the_free_fn = free_after_dump;
303 }
304 }
305 else /* Before dumping with unexec: use static heap. */
306 {
307 /* Find the RtlCreateHeap function. Headers for this function
308 are provided with the w32 DDK, but the function is available
309 in ntdll.dll since XP. */
310 HMODULE hm_ntdll = LoadLibrary ("ntdll.dll");
311 RtlCreateHeap_Proc s_pfn_Rtl_Create_Heap
312 = (RtlCreateHeap_Proc) get_proc_addr (hm_ntdll, "RtlCreateHeap");
313 /* Specific parameters for the private heap. */
314 RTL_HEAP_PARAMETERS params;
315 ZeroMemory (¶ms, sizeof(params));
316 params.Length = sizeof(RTL_HEAP_PARAMETERS);
317
318 data_region_base = (unsigned char *)ROUND_UP (dumped_data, 0x1000);
319 data_region_end = bc_limit = dumped_data + DUMPED_HEAP_SIZE;
320
321 params.InitialCommit = committed = 0x1000;
322 params.InitialReserve = sizeof(dumped_data);
323 /* Use our own routine to commit memory from the dumped_data
324 array. */
325 params.CommitRoutine = &dumped_data_commit;
326
327 /* Create the private heap. */
328 if (s_pfn_Rtl_Create_Heap == NULL)
329 {
330 fprintf (stderr, "Cannot build Emacs without RtlCreateHeap being available; exiting.\n");
331 exit (-1);
332 }
333 heap = s_pfn_Rtl_Create_Heap (0, data_region_base, 0, 0, NULL, ¶ms);
334
335 if (os_subtype == OS_SUBTYPE_9X)
336 {
337 fprintf (stderr, "Cannot dump Emacs on Windows 9X; exiting.\n");
338 exit (-1);
339 }
340 else
341 {
342 the_malloc_fn = malloc_before_dump;
343 the_realloc_fn = realloc_before_dump;
344 the_free_fn = free_before_dump;
345 }
346 }
347
348 /* Update system version information to match current system. */
349 cache_system_info ();
350 }
351
352
353 /* malloc, realloc, free. */
354
355 #undef malloc
356 #undef realloc
357 #undef free
358
359 /* FREEABLE_P checks if the block can be safely freed. */
360 #define FREEABLE_P(addr) \
361 ((DWORD_PTR)(unsigned char *)(addr) > 0 \
362 && ((unsigned char *)(addr) < dumped_data \
363 || (unsigned char *)(addr) >= dumped_data + DUMPED_HEAP_SIZE))
364
365 void *
366 malloc_after_dump (size_t size)
367 {
368 /* Use the new private heap. */
369 void *p = heap_alloc (size);
370
371 /* After dump, keep track of the "brk value" for sbrk(0). */
372 if (p)
373 {
374 unsigned char *new_brk = (unsigned char *)p + size;
375
376 if (new_brk > data_region_end)
377 data_region_end = new_brk;
378 }
379 return p;
380 }
381
382 /* FIXME: The *_before_dump functions should be removed when pdumper
383 becomes the only dumping method. */
384 void *
385 malloc_before_dump (size_t size)
386 {
387 void *p;
388
389 /* Before dumping. The private heap can handle only requests for
390 less than MaxBlockSize. */
391 if (size < MaxBlockSize)
392 {
393 /* Use the private heap if possible. */
394 p = heap_alloc (size);
395 }
396 else
397 {
398 /* Find the first big chunk that can hold the requested size. */
399 int i = 0;
400
401 for (i = 0; i < blocks_number; i++)
402 {
403 if (blocks[i].occupied == 0 && blocks[i].size >= size)
404 break;
405 }
406 if (i < blocks_number)
407 {
408 /* If found, use it. */
409 p = blocks[i].address;
410 blocks[i].occupied = TRUE;
411 }
412 else
413 {
414 /* Allocate a new big chunk from the end of the dumped_data
415 array. */
416 if (blocks_number >= MAX_BLOCKS)
417 {
418 fprintf (stderr,
419 "malloc_before_dump: no more big chunks available.\nEnlarge MAX_BLOCKS!\n");
420 exit (-1);
421 }
422 bc_limit -= size;
423 bc_limit = (unsigned char *)ROUND_DOWN (bc_limit, 0x10);
424 p = bc_limit;
425 blocks[blocks_number].address = p;
426 blocks[blocks_number].size = size;
427 blocks[blocks_number].occupied = TRUE;
428 blocks_number++;
429 /* Check that areas do not overlap. */
430 if (bc_limit < dumped_data + committed)
431 {
432 fprintf (stderr,
433 "malloc_before_dump: memory exhausted.\nEnlarge dumped_data[]!\n");
434 exit (-1);
435 }
436 }
437 }
438 return p;
439 }
440
441 /* Re-allocate the previously allocated block in ptr, making the new
442 block SIZE bytes long. */
443 void *
444 realloc_after_dump (void *ptr, size_t size)
445 {
446 void *p;
447
448 /* After dumping. */
449 if (FREEABLE_P (ptr))
450 {
451 /* Reallocate the block since it lies in the new heap. */
452 p = heap_realloc (ptr, size);
453 }
454 else
455 {
456 /* If the block lies in the dumped data, do not free it. Only
457 allocate a new one. */
458 p = heap_alloc (size);
459 if (p && ptr)
460 CopyMemory (p, ptr, size);
461 }
462 /* After dump, keep track of the "brk value" for sbrk(0). */
463 if (p)
464 {
465 unsigned char *new_brk = (unsigned char *)p + size;
466
467 if (new_brk > data_region_end)
468 data_region_end = new_brk;
469 }
470 return p;
471 }
472
473 void *
474 realloc_before_dump (void *ptr, size_t size)
475 {
476 void *p;
477
478 /* Before dumping. */
479 if (dumped_data < (unsigned char *)ptr
480 && (unsigned char *)ptr < bc_limit && size <= MaxBlockSize)
481 {
482 p = heap_realloc (ptr, size);
483 }
484 else
485 {
486 /* In this case, either the new block is too large for the heap,
487 or the old block was already too large. In both cases,
488 malloc_before_dump() and free_before_dump() will take care of
489 reallocation. */
490 p = malloc_before_dump (size);
491 /* If SIZE is below MaxBlockSize, malloc_before_dump will try to
492 allocate it in the fixed heap. If that fails, we could have
493 kept the block in its original place, above bc_limit, instead
494 of failing the call as below. But this doesn't seem to be
495 worth the added complexity, as loadup allocates only a very
496 small number of large blocks, and never reallocates them. */
497 if (p && ptr)
498 {
499 CopyMemory (p, ptr, size);
500 free_before_dump (ptr);
501 }
502 }
503 return p;
504 }
505
506 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
507 void
508 free_after_dump (void *ptr)
509 {
510 /* After dumping. */
511 if (FREEABLE_P (ptr))
512 {
513 /* Free the block if it is in the new private heap. */
514 HeapFree (heap, 0, ptr);
515 }
516 }
517
518 void
519 free_before_dump (void *ptr)
520 {
521 if (!ptr)
522 return;
523
524 /* Before dumping. */
525 if (dumped_data < (unsigned char *)ptr
526 && (unsigned char *)ptr < bc_limit)
527 {
528 /* Free the block if it is allocated in the private heap. */
529 HeapFree (heap, 0, ptr);
530 }
531 else
532 {
533 /* Look for the big chunk. */
534 int i;
535
536 for (i = 0; i < blocks_number; i++)
537 {
538 if (blocks[i].address == ptr)
539 {
540 /* Reset block occupation if found. */
541 blocks[i].occupied = 0;
542 break;
543 }
544 /* What if the block is not found? We should trigger an
545 error here. */
546 eassert (i < blocks_number);
547 }
548 }
549 }
550
551 /* On Windows 9X, HeapAlloc may return pointers that are not aligned
552 on 8-byte boundary, alignment which is required by the Lisp memory
553 management. To circumvent this problem, manually enforce alignment
554 on Windows 9X. */
555
556 void *
557 malloc_after_dump_9x (size_t size)
558 {
559 void *p = malloc_after_dump (size + 8);
560 void *pa;
561 if (p == NULL)
562 return p;
563 pa = (void*)(((intptr_t)p + 8) & ~7);
564 *((void**)pa-1) = p;
565 return pa;
566 }
567
568 void *
569 realloc_after_dump_9x (void *ptr, size_t size)
570 {
571 if (FREEABLE_P (ptr))
572 {
573 void *po = *((void**)ptr-1);
574 void *p;
575 void *pa;
576 p = realloc_after_dump (po, size + 8);
577 if (p == NULL)
578 return p;
579 pa = (void*)(((intptr_t)p + 8) & ~7);
580 if (ptr != NULL &&
581 (char*)pa - (char*)p != (char*)ptr - (char*)po)
582 {
583 /* Handle the case where alignment in pre-realloc and
584 post-realloc blocks does not match. */
585 MoveMemory (pa, (void*)((char*)p + ((char*)ptr - (char*)po)), size);
586 }
587 *((void**)pa-1) = p;
588 return pa;
589 }
590 else
591 {
592 /* Non-freeable pointers have no alignment-enforcing header
593 (since dumping is not allowed on Windows 9X). */
594 void* p = malloc_after_dump_9x (size);
595 if (p != NULL)
596 CopyMemory (p, ptr, size);
597 return p;
598 }
599 }
600
601 void
602 free_after_dump_9x (void *ptr)
603 {
604 if (FREEABLE_P (ptr))
605 {
606 free_after_dump (*((void**)ptr-1));
607 }
608 }
609
610 void *
611 sys_calloc (size_t number, size_t size)
612 {
613 size_t nbytes = number * size;
614 void *ptr = (*the_malloc_fn) (nbytes);
615 if (ptr)
616 memset (ptr, 0, nbytes);
617 return ptr;
618 }
619
620 #if defined HAVE_UNEXEC && defined ENABLE_CHECKING
621 void
622 report_temacs_memory_usage (void)
623 {
624 DWORD blocks_used = 0;
625 size_t large_mem_used = 0;
626 int i;
627
628 for (i = 0; i < blocks_number; i++)
629 if (blocks[i].occupied)
630 {
631 blocks_used++;
632 large_mem_used += blocks[i].size;
633 }
634
635 /* Emulate 'message', which writes to stderr in non-interactive
636 sessions. */
637 fprintf (stderr,
638 "Dump memory usage: Heap: %" PRIu64 " Large blocks(%lu/%lu): %" PRIu64 "/%" PRIu64 "\n",
639 (unsigned long long)committed, blocks_used, blocks_number,
640 (unsigned long long)large_mem_used,
641 (unsigned long long)(dumped_data + DUMPED_HEAP_SIZE - bc_limit));
642 }
643 #endif
644
645 /* Emulate getpagesize. */
646 int
647 getpagesize (void)
648 {
649 return sysinfo_cache.dwPageSize;
650 }
651
652 void *
653 sbrk (ptrdiff_t increment)
654 {
655 /* data_region_end is the address beyond the last allocated byte.
656 The sbrk() function is not emulated at all, except for a 0 value
657 of its parameter. This is needed by the Emacs Lisp function
658 `memory-limit'. */
659 eassert (increment == 0);
660 return data_region_end;
661 }
662
663
664
665 /* MMAP allocation for buffers. */
666
667 #define MAX_BUFFER_SIZE (512 * 1024 * 1024)
668
669 void *
670 mmap_alloc (void **var, size_t nbytes)
671 {
672 void *p = NULL;
673
674 /* We implement amortized allocation. We start by reserving twice
675 the size requested and commit only the size requested. Then
676 realloc could proceed and use the reserved pages, reallocating
677 only if needed. Buffer shrink would happen only so that we stay
678 in the 2x range. This is a big win when visiting compressed
679 files, where the final size of the buffer is not known in
680 advance, and the buffer is enlarged several times as the data is
681 decompressed on the fly. */
682 if (nbytes < MAX_BUFFER_SIZE)
683 p = VirtualAlloc (NULL, ROUND_UP (nbytes * 2, get_allocation_unit ()),
684 MEM_RESERVE, PAGE_READWRITE);
685
686 /* If it fails, or if the request is above 512MB, try with the
687 requested size. */
688 if (p == NULL)
689 p = VirtualAlloc (NULL, ROUND_UP (nbytes, get_allocation_unit ()),
690 MEM_RESERVE, PAGE_READWRITE);
691
692 if (p != NULL)
693 {
694 /* Now, commit pages for NBYTES. */
695 *var = VirtualAlloc (p, nbytes, MEM_COMMIT, PAGE_READWRITE);
696 if (*var == NULL)
697 p = *var;
698 }
699
700 if (!p)
701 {
702 DWORD e = GetLastError ();
703
704 if (e == ERROR_NOT_ENOUGH_MEMORY)
705 errno = ENOMEM;
706 else
707 {
708 DebPrint (("mmap_alloc: error %ld\n", e));
709 errno = EINVAL;
710 }
711 }
712
713 return *var = p;
714 }
715
716 void
717 mmap_free (void **var)
718 {
719 if (*var)
720 {
721 if (VirtualFree (*var, 0, MEM_RELEASE) == 0)
722 DebPrint (("mmap_free: error %ld\n", GetLastError ()));
723 *var = NULL;
724 }
725 }
726
727 void *
728 mmap_realloc (void **var, size_t nbytes)
729 {
730 MEMORY_BASIC_INFORMATION memInfo, m2;
731 void *old_ptr;
732
733 if (*var == NULL)
734 return mmap_alloc (var, nbytes);
735
736 /* This case happens in init_buffer(). */
737 if (nbytes == 0)
738 {
739 mmap_free (var);
740 return mmap_alloc (var, nbytes);
741 }
742
743 memset (&memInfo, 0, sizeof (memInfo));
744 if (VirtualQuery (*var, &memInfo, sizeof (memInfo)) == 0)
745 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError ()));
746
747 /* We need to enlarge the block. */
748 if (memInfo.RegionSize < nbytes)
749 {
750 memset (&m2, 0, sizeof (m2));
751 if (VirtualQuery ((char *)*var + memInfo.RegionSize, &m2, sizeof(m2)) == 0)
752 DebPrint (("mmap_realloc: VirtualQuery error = %ld\n",
753 GetLastError ()));
754 /* If there is enough room in the current reserved area, then
755 commit more pages as needed. */
756 if (m2.State == MEM_RESERVE
757 && m2.AllocationBase == memInfo.AllocationBase
758 && nbytes <= memInfo.RegionSize + m2.RegionSize)
759 {
760 void *p;
761
762 p = VirtualAlloc (*var, nbytes, MEM_COMMIT, PAGE_READWRITE);
763 if (!p /* && GetLastError() != ERROR_NOT_ENOUGH_MEMORY */)
764 {
765 DebPrint (("realloc enlarge: VirtualAlloc (%p + %I64x, %I64x) error %ld\n",
766 *var, (uint64_t)memInfo.RegionSize,
767 (uint64_t)(nbytes - memInfo.RegionSize),
768 GetLastError ()));
769 DebPrint (("next region: %p %p %I64x %x\n", m2.BaseAddress,
770 m2.AllocationBase, (uint64_t)m2.RegionSize,
771 m2.AllocationProtect));
772 }
773 else
774 return *var;
775 }
776 /* Else we must actually enlarge the block by allocating a new
777 one and copying previous contents from the old to the new one. */
778 old_ptr = *var;
779
780 if (mmap_alloc (var, nbytes))
781 {
782 CopyMemory (*var, old_ptr, memInfo.RegionSize);
783 mmap_free (&old_ptr);
784 return *var;
785 }
786 else
787 {
788 /* We failed to reallocate the buffer. */
789 *var = old_ptr;
790 return NULL;
791 }
792 }
793
794 /* If we are shrinking by more than one page... */
795 if (memInfo.RegionSize > nbytes + getpagesize())
796 {
797 /* If we are shrinking a lot... */
798 if ((memInfo.RegionSize / 2) > nbytes)
799 {
800 /* Let's give some memory back to the system and release
801 some pages. */
802 old_ptr = *var;
803
804 if (mmap_alloc (var, nbytes))
805 {
806 CopyMemory (*var, old_ptr, nbytes);
807 mmap_free (&old_ptr);
808 return *var;
809 }
810 else
811 {
812 /* In case we fail to shrink, try to go on with the old block.
813 But that means there is a lot of memory pressure.
814 We could also decommit pages. */
815 *var = old_ptr;
816 return *var;
817 }
818 }
819
820 /* We still can decommit pages. */
821 if (VirtualFree ((char *)*var + nbytes + get_page_size(),
822 memInfo.RegionSize - nbytes - get_page_size(),
823 MEM_DECOMMIT) == 0)
824 DebPrint (("mmap_realloc: VirtualFree error %ld\n", GetLastError ()));
825 return *var;
826 }
827
828 /* Not enlarging, not shrinking by more than one page. */
829 return *var;
830 }
831
832
833 /* Emulation of getrlimit and setrlimit. */
834
835 int
836 getrlimit (rlimit_resource_t rltype, struct rlimit *rlp)
837 {
838 int retval = -1;
839
840 switch (rltype)
841 {
842 case RLIMIT_STACK:
843 {
844 MEMORY_BASIC_INFORMATION m;
845 /* Implementation note: Posix says that RLIMIT_STACK returns
846 information about the stack size for the main thread. The
847 implementation below returns the stack size for the calling
848 thread, so it's more like pthread_attr_getstacksize. But
849 Emacs clearly wants the latter, given how it uses the
850 results, so the implementation below is more future-proof,
851 if what's now the main thread will become some other thread
852 at some future point. */
853 if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m))
854 errno = EPERM;
855 else
856 {
857 rlp->rlim_cur = (DWORD_PTR) &m - (DWORD_PTR) m.AllocationBase;
858 rlp->rlim_max =
859 (DWORD_PTR) m.BaseAddress + m.RegionSize
860 - (DWORD_PTR) m.AllocationBase;
861
862 /* The last page is the guard page, so subtract that. */
863 rlp->rlim_cur -= getpagesize ();
864 rlp->rlim_max -= getpagesize ();
865 retval = 0;
866 }
867 }
868 break;
869 case RLIMIT_NOFILE:
870 /* Implementation note: The real value is returned by
871 _getmaxstdio. But our FD_SETSIZE is smaller, to cater to
872 Windows 9X, and process.c includes some logic that's based on
873 the assumption that the handle resource is inherited to child
874 processes. We want to avoid that logic, so we tell process.c
875 our current limit is already equal to FD_SETSIZE. */
876 rlp->rlim_cur = FD_SETSIZE;
877 rlp->rlim_max = 2048; /* see _setmaxstdio documentation */
878 retval = 0;
879 break;
880 default:
881 /* Note: we could return meaningful results for other RLIMIT_*
882 requests, but Emacs doesn't currently need that, so we just
883 punt for them. */
884 errno = ENOSYS;
885 break;
886 }
887 return retval;
888 }
889
890 int
891 setrlimit (rlimit_resource_t rltype, const struct rlimit *rlp)
892 {
893 switch (rltype)
894 {
895 case RLIMIT_STACK:
896 case RLIMIT_NOFILE:
897 /* We cannot modify these limits, so we always fail. */
898 errno = EPERM;
899 break;
900 default:
901 errno = ENOSYS;
902 break;
903 }
904 return -1;
905 }