Add 2009 to copyright years.
[bpt/emacs.git] / src / w32heap.c
CommitLineData
e9e23e23 1/* Heap management routines for GNU Emacs on the Microsoft W32 API.
9ec0b715 2 Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
76b6f707 3 2008, 2009 Free Software Foundation, Inc.
95ed0025 4
3b7ad313 5This file is part of GNU Emacs.
95ed0025 6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
3b7ad313 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
95ed0025 11
3b7ad313
EN
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
95ed0025 16
3b7ad313 17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
95ed0025 19
9ec0b715 20/*
95ed0025
RS
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
22*/
23
4838e624
PJ
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
3bbabc43 27
95ed0025
RS
28#include <stdlib.h>
29#include <stdio.h>
30
489f9371 31#include "w32heap.h"
8dfdd41f 32#include "lisp.h" /* for VALMASK */
95ed0025 33
2e4f6477 34#define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
30d2b1c2 35
95ed0025
RS
36/* This gives us the page size and the size of the allocation unit on NT. */
37SYSTEM_INFO sysinfo_cache;
b9cad9c1
GV
38
39/* This gives us version, build, and platform identification. */
40OSVERSIONINFO osinfo_cache;
41
3bbabc43 42unsigned long syspage_mask = 0;
95ed0025 43
95ed0025 44/* The major and minor versions of NT. */
fbd6baed
GV
45int w32_major_version;
46int w32_minor_version;
39f1f652 47int w32_build_number;
95ed0025 48
801f68b9
GV
49/* Distinguish between Windows NT and Windows 95. */
50int os_subtype;
51
95ed0025
RS
52/* Cache information describing the NT system for later use. */
53void
54cache_system_info (void)
55{
ce20e03e 56 union
95ed0025 57 {
ce20e03e 58 struct info
95ed0025
RS
59 {
60 char major;
61 char minor;
62 short platform;
63 } info;
64 DWORD data;
65 } version;
66
67 /* Cache the version of the operating system. */
68 version.data = GetVersion ();
fbd6baed
GV
69 w32_major_version = version.info.major;
70 w32_minor_version = version.info.minor;
95ed0025 71
801f68b9
GV
72 if (version.info.platform & 0x8000)
73 os_subtype = OS_WIN95;
74 else
75 os_subtype = OS_NT;
76
95ed0025
RS
77 /* Cache page size, allocation unit, processor type, etc. */
78 GetSystemInfo (&sysinfo_cache);
3bbabc43 79 syspage_mask = sysinfo_cache.dwPageSize - 1;
b9cad9c1
GV
80
81 /* Cache os info. */
82 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
83 GetVersionEx (&osinfo_cache);
39f1f652
AI
84
85 w32_build_number = osinfo_cache.dwBuildNumber;
86 if (os_subtype == OS_WIN95)
87 w32_build_number &= 0xffff;
95ed0025
RS
88}
89
e54c8cd1
GV
90/* Emulate getpagesize. */
91int
92getpagesize (void)
93{
94 return sysinfo_cache.dwPageSize;
95}
96
30d2b1c2
AI
97/* Info for managing our preload heap, which is essentially a fixed size
98 data area in the executable. */
99PIMAGE_SECTION_HEADER preload_heap_section;
95ed0025
RS
100
101/* Info for keeping track of our heap. */
102unsigned char *data_region_base = NULL;
103unsigned char *data_region_end = NULL;
3bbabc43 104unsigned char *real_data_region_end = NULL;
011db670 105unsigned long reserved_heap_size = 0;
95ed0025
RS
106
107/* The start of the data segment. */
108unsigned char *
109get_data_start (void)
110{
111 return data_region_base;
112}
113
114/* The end of the data segment. */
115unsigned char *
116get_data_end (void)
117{
118 return data_region_end;
119}
120
011db670
GV
121static char *
122allocate_heap (void)
123{
30d2b1c2
AI
124 /* Try to get as much as possible of the address range from the end of
125 the preload heap section up to the usable address limit. Since GNU
126 malloc can handle gaps in the memory it gets from sbrk, we can
127 simply set the sbrk pointer to the base of the new heap region. */
128 unsigned long base =
129 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
130 + preload_heap_section->Misc.VirtualSize),
131 get_allocation_unit ());
8dfdd41f 132 unsigned long end = 1 << VALBITS; /* 256MB */
709fd16b 133 void *ptr = NULL;
011db670 134
709fd16b
GV
135 while (!ptr && (base < end))
136 {
709fd16b
GV
137 reserved_heap_size = end - base;
138 ptr = VirtualAlloc ((void *) base,
139 get_reserved_heap_size (),
140 MEM_RESERVE,
141 PAGE_NOACCESS);
709fd16b
GV
142 base += 0x00100000; /* 1MB increment */
143 }
0d05360d 144
709fd16b 145 return ptr;
011db670 146}
011db670
GV
147
148
95ed0025
RS
149/* Emulate Unix sbrk. */
150void *
151sbrk (unsigned long increment)
152{
153 void *result;
154 long size = (long) increment;
ce20e03e 155
95ed0025 156 result = data_region_end;
ce20e03e 157
95ed0025 158 /* If size is negative, shrink the heap by decommitting pages. */
ce20e03e 159 if (size < 0)
95ed0025 160 {
3bbabc43
GV
161 int new_size;
162 unsigned char *new_data_region_end;
163
95ed0025
RS
164 size = -size;
165
166 /* Sanity checks. */
95ed0025
RS
167 if ((data_region_end - size) < data_region_base)
168 return NULL;
169
ce20e03e 170 /* We can only decommit full pages, so allow for
3bbabc43
GV
171 partial deallocation [cga]. */
172 new_data_region_end = (data_region_end - size);
173 new_data_region_end = (unsigned char *)
174 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
175 new_size = real_data_region_end - new_data_region_end;
176 real_data_region_end = new_data_region_end;
30d2b1c2 177 if (new_size > 0)
3bbabc43
GV
178 {
179 /* Decommit size bytes from the end of the heap. */
30d2b1c2
AI
180 if (using_dynamic_heap
181 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
3bbabc43
GV
182 return NULL;
183 }
95ed0025
RS
184
185 data_region_end -= size;
ce20e03e 186 }
95ed0025 187 /* If size is positive, grow the heap by committing reserved pages. */
ce20e03e 188 else if (size > 0)
95ed0025
RS
189 {
190 /* Sanity checks. */
95ed0025
RS
191 if ((data_region_end + size) >
192 (data_region_base + get_reserved_heap_size ()))
193 return NULL;
194
195 /* Commit more of our heap. */
30d2b1c2
AI
196 if (using_dynamic_heap
197 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
198 PAGE_READWRITE) == NULL)
95ed0025
RS
199 return NULL;
200 data_region_end += size;
3bbabc43
GV
201
202 /* We really only commit full pages, so record where
203 the real end of committed memory is [cga]. */
204 real_data_region_end = (unsigned char *)
205 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
95ed0025 206 }
ce20e03e 207
95ed0025
RS
208 return result;
209}
210
30d2b1c2
AI
211/* Initialize the internal heap variables used by sbrk. When running in
212 preload phase (ie. in the undumped executable), we rely entirely on a
213 fixed size heap section included in the .exe itself; this is
214 preserved during dumping, and truncated to the size actually used.
215
216 When running in the dumped executable, we reserve as much as possible
217 of the address range that is addressable by Lisp object pointers, to
218 supplement what is left of the preload heap. Although we cannot rely
219 on the dynamically allocated arena being contiguous with the static
220 heap area, it is not a problem because sbrk can pretend that the gap
221 was allocated by something else; GNU malloc detects when there is a
222 jump in the sbrk values, and starts a new heap block. */
95ed0025 223void
30d2b1c2 224init_heap ()
95ed0025 225{
30d2b1c2
AI
226 PIMAGE_DOS_HEADER dos_header;
227 PIMAGE_NT_HEADERS nt_header;
228
229 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
ce20e03e 230 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
30d2b1c2
AI
231 dos_header->e_lfanew);
232 preload_heap_section = find_section ("EMHEAP", nt_header);
233
234 if (using_dynamic_heap)
235 {
236 data_region_base = allocate_heap ();
237 if (!data_region_base)
238 {
239 printf ("Error: Could not reserve dynamic heap area.\n");
240 exit (1);
241 }
242
6e5cb96f 243#if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
30d2b1c2
AI
244 /* Ensure that the addresses don't use the upper tag bits since
245 the Lisp type goes there. */
ce20e03e 246 if (((unsigned long) data_region_base & ~VALMASK) != 0)
30d2b1c2
AI
247 {
248 printf ("Error: The heap was allocated in upper memory.\n");
249 exit (1);
250 }
3ae12c8d 251#endif
30d2b1c2
AI
252 data_region_end = data_region_base;
253 real_data_region_end = data_region_end;
254 }
255 else
256 {
257 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
258 data_region_end = data_region_base;
259 real_data_region_end = data_region_end;
260 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
261 }
801f68b9
GV
262
263 /* Update system version information to match current system. */
264 cache_system_info ();
95ed0025
RS
265}
266
267/* Round the heap up to the given alignment. */
268void
269round_heap (unsigned long align)
270{
271 unsigned long needs_to_be;
272 unsigned long need_to_alloc;
ce20e03e 273
30d2b1c2 274 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
95ed0025 275 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
ce20e03e
JB
276
277 if (need_to_alloc)
95ed0025
RS
278 sbrk (need_to_alloc);
279}
801f68b9 280
ce20e03e 281#if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined(USE_CRT_DLL))
801f68b9
GV
282
283/* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
284 a heap via HeapCreate. They are normally defined by the runtime,
285 but we override them here so that the unnecessary HeapCreate call
286 is not performed. */
287
288int __cdecl
289_heap_init (void)
290{
291 /* Stepping through the assembly indicates that mainCRTStartup is
292 expecting a nonzero success return value. */
293 return 1;
294}
295
296void __cdecl
297_heap_term (void)
298{
299 return;
300}
301
302#endif
ab5796a9
MB
303
304/* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
305 (do not change this comment) */