1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
33 #include "lisp.h" /* for VALMASK */
35 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
37 /* This gives us the page size and the size of the allocation unit on NT. */
38 SYSTEM_INFO sysinfo_cache
;
40 /* This gives us version, build, and platform identification. */
41 OSVERSIONINFO osinfo_cache
;
43 unsigned long syspage_mask
= 0;
45 /* The major and minor versions of NT. */
46 int w32_major_version
;
47 int w32_minor_version
;
50 /* Distinguish between Windows NT and Windows 95. */
53 /* Cache information describing the NT system for later use. */
55 cache_system_info (void)
68 /* Cache the version of the operating system. */
69 version
.data
= GetVersion ();
70 w32_major_version
= version
.info
.major
;
71 w32_minor_version
= version
.info
.minor
;
73 if (version
.info
.platform
& 0x8000)
74 os_subtype
= OS_WIN95
;
78 /* Cache page size, allocation unit, processor type, etc. */
79 GetSystemInfo (&sysinfo_cache
);
80 syspage_mask
= sysinfo_cache
.dwPageSize
- 1;
83 osinfo_cache
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
84 GetVersionEx (&osinfo_cache
);
86 w32_build_number
= osinfo_cache
.dwBuildNumber
;
87 if (os_subtype
== OS_WIN95
)
88 w32_build_number
&= 0xffff;
91 /* Emulate getpagesize. */
95 return sysinfo_cache
.dwPageSize
;
98 /* Info for managing our preload heap, which is essentially a fixed size
99 data area in the executable. */
100 PIMAGE_SECTION_HEADER preload_heap_section
;
102 /* Info for keeping track of our heap. */
103 unsigned char *data_region_base
= NULL
;
104 unsigned char *data_region_end
= NULL
;
105 unsigned char *real_data_region_end
= NULL
;
106 unsigned long reserved_heap_size
= 0;
108 /* The start of the data segment. */
110 get_data_start (void)
112 return data_region_base
;
115 /* The end of the data segment. */
119 return data_region_end
;
122 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
126 /* Try to get as much as possible of the address range from the end of
127 the preload heap section up to the usable address limit. Since GNU
128 malloc can handle gaps in the memory it gets from sbrk, we can
129 simply set the sbrk pointer to the base of the new heap region. */
131 ROUND_UP ((RVA_TO_PTR (preload_heap_section
->VirtualAddress
)
132 + preload_heap_section
->Misc
.VirtualSize
),
133 get_allocation_unit ());
134 unsigned long end
= 1 << VALBITS
; /* 256MB */
137 while (!ptr
&& (base
< end
))
139 reserved_heap_size
= end
- base
;
140 ptr
= VirtualAlloc ((void *) base
,
141 get_reserved_heap_size (),
144 base
+= 0x00100000; /* 1MB increment */
149 #else /* USE_LISP_UNION_TYPE || USE_LSB_TAG */
153 unsigned long size
= 0x80000000; /* start by asking for 2GB */
156 while (!ptr
&& size
> 0x00100000)
158 reserved_heap_size
= size
;
159 ptr
= VirtualAlloc (NULL
,
160 get_reserved_heap_size (),
163 size
-= 0x00800000; /* if failed, decrease request by 8MB */
168 #endif /* USE_LISP_UNION_TYPE || USE_LSB_TAG */
171 /* Emulate Unix sbrk. Note that ralloc.c expects the return value to
172 be the address of the _start_ (not end) of the new block in case of
173 success, and zero (not -1) in case of failure. */
175 sbrk (unsigned long increment
)
178 long size
= (long) increment
;
180 result
= data_region_end
;
182 /* If size is negative, shrink the heap by decommitting pages. */
186 unsigned char *new_data_region_end
;
191 if ((data_region_end
- size
) < data_region_base
)
194 /* We can only decommit full pages, so allow for
195 partial deallocation [cga]. */
196 new_data_region_end
= (data_region_end
- size
);
197 new_data_region_end
= (unsigned char *)
198 ((long) (new_data_region_end
+ syspage_mask
) & ~syspage_mask
);
199 new_size
= real_data_region_end
- new_data_region_end
;
200 real_data_region_end
= new_data_region_end
;
203 /* Decommit size bytes from the end of the heap. */
204 if (using_dynamic_heap
205 && !VirtualFree (real_data_region_end
, new_size
, MEM_DECOMMIT
))
209 data_region_end
-= size
;
211 /* If size is positive, grow the heap by committing reserved pages. */
215 if ((data_region_end
+ size
) >
216 (data_region_base
+ get_reserved_heap_size ()))
219 /* Commit more of our heap. */
220 if (using_dynamic_heap
221 && VirtualAlloc (data_region_end
, size
, MEM_COMMIT
,
222 PAGE_READWRITE
) == NULL
)
224 data_region_end
+= size
;
226 /* We really only commit full pages, so record where
227 the real end of committed memory is [cga]. */
228 real_data_region_end
= (unsigned char *)
229 ((long) (data_region_end
+ syspage_mask
) & ~syspage_mask
);
235 /* Initialize the internal heap variables used by sbrk. When running in
236 preload phase (ie. in the undumped executable), we rely entirely on a
237 fixed size heap section included in the .exe itself; this is
238 preserved during dumping, and truncated to the size actually used.
240 When running in the dumped executable, we reserve as much as possible
241 of the address range that is addressable by Lisp object pointers, to
242 supplement what is left of the preload heap. Although we cannot rely
243 on the dynamically allocated arena being contiguous with the static
244 heap area, it is not a problem because sbrk can pretend that the gap
245 was allocated by something else; GNU malloc detects when there is a
246 jump in the sbrk values, and starts a new heap block. */
250 PIMAGE_DOS_HEADER dos_header
;
251 PIMAGE_NT_HEADERS nt_header
;
253 dos_header
= (PIMAGE_DOS_HEADER
) RVA_TO_PTR (0);
254 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
255 dos_header
->e_lfanew
);
256 preload_heap_section
= find_section ("EMHEAP", nt_header
);
258 if (using_dynamic_heap
)
260 data_region_base
= allocate_heap ();
261 if (!data_region_base
)
263 printf ("Error: Could not reserve dynamic heap area.\n");
267 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
268 /* Ensure that the addresses don't use the upper tag bits since
269 the Lisp type goes there. */
270 if (((unsigned long) data_region_base
& ~VALMASK
) != 0)
272 printf ("Error: The heap was allocated in upper memory.\n");
276 data_region_end
= data_region_base
;
277 real_data_region_end
= data_region_end
;
281 data_region_base
= RVA_TO_PTR (preload_heap_section
->VirtualAddress
);
282 data_region_end
= data_region_base
;
283 real_data_region_end
= data_region_end
;
284 reserved_heap_size
= preload_heap_section
->Misc
.VirtualSize
;
287 /* Update system version information to match current system. */
288 cache_system_info ();
291 /* Round the heap up to the given alignment. */
293 round_heap (unsigned long align
)
295 unsigned long needs_to_be
;
296 unsigned long need_to_alloc
;
298 needs_to_be
= (unsigned long) ROUND_UP (get_heap_end (), align
);
299 need_to_alloc
= needs_to_be
- (unsigned long) get_heap_end ();
302 sbrk (need_to_alloc
);
305 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL))
307 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
308 a heap via HeapCreate. They are normally defined by the runtime,
309 but we override them here so that the unnecessary HeapCreate call
315 /* Stepping through the assembly indicates that mainCRTStartup is
316 expecting a nonzero success return value. */
328 /* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
329 (do not change this comment) */