1 /* Heap management routines for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1994, 2001-2012 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
27 #include "lisp.h" /* for VALMASK */
29 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
31 /* This gives us the page size and the size of the allocation unit on NT. */
32 SYSTEM_INFO sysinfo_cache
;
34 /* This gives us version, build, and platform identification. */
35 OSVERSIONINFO osinfo_cache
;
37 unsigned long syspage_mask
= 0;
39 /* The major and minor versions of NT. */
40 int w32_major_version
;
41 int w32_minor_version
;
44 /* Distinguish between Windows NT and Windows 95. */
47 /* Cache information describing the NT system for later use. */
49 cache_system_info (void)
62 /* Cache the version of the operating system. */
63 version
.data
= GetVersion ();
64 w32_major_version
= version
.info
.major
;
65 w32_minor_version
= version
.info
.minor
;
67 if (version
.info
.platform
& 0x8000)
72 /* Cache page size, allocation unit, processor type, etc. */
73 GetSystemInfo (&sysinfo_cache
);
74 syspage_mask
= sysinfo_cache
.dwPageSize
- 1;
77 osinfo_cache
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
78 GetVersionEx (&osinfo_cache
);
80 w32_build_number
= osinfo_cache
.dwBuildNumber
;
81 if (os_subtype
== OS_9X
)
82 w32_build_number
&= 0xffff;
85 /* Emulate getpagesize. */
89 return sysinfo_cache
.dwPageSize
;
92 /* Info for managing our preload heap, which is essentially a fixed size
93 data area in the executable. */
94 PIMAGE_SECTION_HEADER preload_heap_section
;
96 /* Info for keeping track of our heap. */
97 unsigned char *data_region_base
= NULL
;
98 unsigned char *data_region_end
= NULL
;
99 unsigned char *real_data_region_end
= NULL
;
100 unsigned long reserved_heap_size
= 0;
102 /* The start of the data segment. */
104 get_data_start (void)
106 return data_region_base
;
109 /* The end of the data segment. */
113 return data_region_end
;
120 /* Try to get as much as possible of the address range from the end of
121 the preload heap section up to the usable address limit. Since GNU
122 malloc can handle gaps in the memory it gets from sbrk, we can
123 simply set the sbrk pointer to the base of the new heap region. */
125 ROUND_UP ((RVA_TO_PTR (preload_heap_section
->VirtualAddress
)
126 + preload_heap_section
->Misc
.VirtualSize
),
127 get_allocation_unit ());
128 unsigned long end
= 1 << VALBITS
; /* 256MB */
131 while (!ptr
&& (base
< end
))
133 reserved_heap_size
= end
- base
;
134 ptr
= VirtualAlloc ((void *) base
,
135 get_reserved_heap_size (),
138 base
+= 0x00100000; /* 1MB increment */
143 #else /* USE_LSB_TAG */
147 unsigned long size
= 0x80000000; /* start by asking for 2GB */
150 while (!ptr
&& size
> 0x00100000)
152 reserved_heap_size
= size
;
153 ptr
= VirtualAlloc (NULL
,
154 get_reserved_heap_size (),
157 size
-= 0x00800000; /* if failed, decrease request by 8MB */
162 #endif /* USE_LSB_TAG */
165 /* Emulate Unix sbrk. Note that ralloc.c expects the return value to
166 be the address of the _start_ (not end) of the new block in case of
167 success, and zero (not -1) in case of failure. */
169 sbrk (unsigned long increment
)
172 long size
= (long) increment
;
174 result
= data_region_end
;
176 /* If size is negative, shrink the heap by decommitting pages. */
180 unsigned char *new_data_region_end
;
185 if ((data_region_end
- size
) < data_region_base
)
188 /* We can only decommit full pages, so allow for
189 partial deallocation [cga]. */
190 new_data_region_end
= (data_region_end
- size
);
191 new_data_region_end
= (unsigned char *)
192 ((long) (new_data_region_end
+ syspage_mask
) & ~syspage_mask
);
193 new_size
= real_data_region_end
- new_data_region_end
;
194 real_data_region_end
= new_data_region_end
;
197 /* Decommit size bytes from the end of the heap. */
198 if (using_dynamic_heap
199 && !VirtualFree (real_data_region_end
, new_size
, MEM_DECOMMIT
))
203 data_region_end
-= size
;
205 /* If size is positive, grow the heap by committing reserved pages. */
209 if ((data_region_end
+ size
) >
210 (data_region_base
+ get_reserved_heap_size ()))
213 /* Commit more of our heap. */
214 if (using_dynamic_heap
215 && VirtualAlloc (data_region_end
, size
, MEM_COMMIT
,
216 PAGE_READWRITE
) == NULL
)
218 data_region_end
+= size
;
220 /* We really only commit full pages, so record where
221 the real end of committed memory is [cga]. */
222 real_data_region_end
= (unsigned char *)
223 ((long) (data_region_end
+ syspage_mask
) & ~syspage_mask
);
229 /* Initialize the internal heap variables used by sbrk. When running in
230 preload phase (ie. in the undumped executable), we rely entirely on a
231 fixed size heap section included in the .exe itself; this is
232 preserved during dumping, and truncated to the size actually used.
234 When running in the dumped executable, we reserve as much as possible
235 of the address range that is addressable by Lisp object pointers, to
236 supplement what is left of the preload heap. Although we cannot rely
237 on the dynamically allocated arena being contiguous with the static
238 heap area, it is not a problem because sbrk can pretend that the gap
239 was allocated by something else; GNU malloc detects when there is a
240 jump in the sbrk values, and starts a new heap block. */
244 PIMAGE_DOS_HEADER dos_header
;
245 PIMAGE_NT_HEADERS nt_header
;
247 dos_header
= (PIMAGE_DOS_HEADER
) RVA_TO_PTR (0);
248 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
249 dos_header
->e_lfanew
);
250 preload_heap_section
= find_section ("EMHEAP", nt_header
);
252 if (using_dynamic_heap
)
254 data_region_base
= allocate_heap ();
255 if (!data_region_base
)
257 printf ("Error: Could not reserve dynamic heap area.\n");
262 /* Ensure that the addresses don't use the upper tag bits since
263 the Lisp type goes there. */
264 if (((unsigned long) data_region_base
& ~VALMASK
) != 0)
266 printf ("Error: The heap was allocated in upper memory.\n");
270 data_region_end
= data_region_base
;
271 real_data_region_end
= data_region_end
;
275 data_region_base
= RVA_TO_PTR (preload_heap_section
->VirtualAddress
);
276 data_region_end
= data_region_base
;
277 real_data_region_end
= data_region_end
;
278 reserved_heap_size
= preload_heap_section
->Misc
.VirtualSize
;
281 /* Update system version information to match current system. */
282 cache_system_info ();
285 /* Round the heap up to the given alignment. */
287 round_heap (unsigned long align
)
289 unsigned long needs_to_be
;
290 unsigned long need_to_alloc
;
292 needs_to_be
= (unsigned long) ROUND_UP (get_heap_end (), align
);
293 need_to_alloc
= needs_to_be
- (unsigned long) get_heap_end ();
296 sbrk (need_to_alloc
);