Use add/delete_read_fd in xsmfns to simplify. Also restart with initial_argv.
[emacs.git] / src / w32heap.c
blobbbdabd2350261f4ac255dab4dda8351f4262f555
1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994, 2001-2011 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
23 #include <config.h>
24 #include <stdio.h>
25 #include <setjmp.h>
27 #include "w32heap.h"
28 #include "lisp.h" /* for VALMASK */
30 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
32 /* This gives us the page size and the size of the allocation unit on NT. */
33 SYSTEM_INFO sysinfo_cache;
35 /* This gives us version, build, and platform identification. */
36 OSVERSIONINFO osinfo_cache;
38 unsigned long syspage_mask = 0;
40 /* The major and minor versions of NT. */
41 int w32_major_version;
42 int w32_minor_version;
43 int w32_build_number;
45 /* Distinguish between Windows NT and Windows 95. */
46 int os_subtype;
48 /* Cache information describing the NT system for later use. */
49 void
50 cache_system_info (void)
52 union
54 struct info
56 char major;
57 char minor;
58 short platform;
59 } info;
60 DWORD data;
61 } version;
63 /* Cache the version of the operating system. */
64 version.data = GetVersion ();
65 w32_major_version = version.info.major;
66 w32_minor_version = version.info.minor;
68 if (version.info.platform & 0x8000)
69 os_subtype = OS_WIN95;
70 else
71 os_subtype = OS_NT;
73 /* Cache page size, allocation unit, processor type, etc. */
74 GetSystemInfo (&sysinfo_cache);
75 syspage_mask = sysinfo_cache.dwPageSize - 1;
77 /* Cache os info. */
78 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
79 GetVersionEx (&osinfo_cache);
81 w32_build_number = osinfo_cache.dwBuildNumber;
82 if (os_subtype == OS_WIN95)
83 w32_build_number &= 0xffff;
86 /* Emulate getpagesize. */
87 int
88 getpagesize (void)
90 return sysinfo_cache.dwPageSize;
93 /* Info for managing our preload heap, which is essentially a fixed size
94 data area in the executable. */
95 PIMAGE_SECTION_HEADER preload_heap_section;
97 /* Info for keeping track of our heap. */
98 unsigned char *data_region_base = NULL;
99 unsigned char *data_region_end = NULL;
100 unsigned char *real_data_region_end = NULL;
101 unsigned long reserved_heap_size = 0;
103 /* The start of the data segment. */
104 unsigned char *
105 get_data_start (void)
107 return data_region_base;
110 /* The end of the data segment. */
111 unsigned char *
112 get_data_end (void)
114 return data_region_end;
117 static char *
118 allocate_heap (void)
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. */
124 unsigned long base =
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 */
129 void *ptr = NULL;
131 while (!ptr && (base < end))
133 reserved_heap_size = end - base;
134 ptr = VirtualAlloc ((void *) base,
135 get_reserved_heap_size (),
136 MEM_RESERVE,
137 PAGE_NOACCESS);
138 base += 0x00100000; /* 1MB increment */
141 return ptr;
145 /* Emulate Unix sbrk. */
146 void *
147 sbrk (unsigned long increment)
149 void *result;
150 long size = (long) increment;
152 result = data_region_end;
154 /* If size is negative, shrink the heap by decommitting pages. */
155 if (size < 0)
157 int new_size;
158 unsigned char *new_data_region_end;
160 size = -size;
162 /* Sanity checks. */
163 if ((data_region_end - size) < data_region_base)
164 return NULL;
166 /* We can only decommit full pages, so allow for
167 partial deallocation [cga]. */
168 new_data_region_end = (data_region_end - size);
169 new_data_region_end = (unsigned char *)
170 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
171 new_size = real_data_region_end - new_data_region_end;
172 real_data_region_end = new_data_region_end;
173 if (new_size > 0)
175 /* Decommit size bytes from the end of the heap. */
176 if (using_dynamic_heap
177 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
178 return NULL;
181 data_region_end -= size;
183 /* If size is positive, grow the heap by committing reserved pages. */
184 else if (size > 0)
186 /* Sanity checks. */
187 if ((data_region_end + size) >
188 (data_region_base + get_reserved_heap_size ()))
189 return NULL;
191 /* Commit more of our heap. */
192 if (using_dynamic_heap
193 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
194 PAGE_READWRITE) == NULL)
195 return NULL;
196 data_region_end += size;
198 /* We really only commit full pages, so record where
199 the real end of committed memory is [cga]. */
200 real_data_region_end = (unsigned char *)
201 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
204 return result;
207 /* Initialize the internal heap variables used by sbrk. When running in
208 preload phase (ie. in the undumped executable), we rely entirely on a
209 fixed size heap section included in the .exe itself; this is
210 preserved during dumping, and truncated to the size actually used.
212 When running in the dumped executable, we reserve as much as possible
213 of the address range that is addressable by Lisp object pointers, to
214 supplement what is left of the preload heap. Although we cannot rely
215 on the dynamically allocated arena being contiguous with the static
216 heap area, it is not a problem because sbrk can pretend that the gap
217 was allocated by something else; GNU malloc detects when there is a
218 jump in the sbrk values, and starts a new heap block. */
219 void
220 init_heap (void)
222 PIMAGE_DOS_HEADER dos_header;
223 PIMAGE_NT_HEADERS nt_header;
225 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
226 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
227 dos_header->e_lfanew);
228 preload_heap_section = find_section ("EMHEAP", nt_header);
230 if (using_dynamic_heap)
232 data_region_base = allocate_heap ();
233 if (!data_region_base)
235 printf ("Error: Could not reserve dynamic heap area.\n");
236 exit (1);
239 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
240 /* Ensure that the addresses don't use the upper tag bits since
241 the Lisp type goes there. */
242 if (((unsigned long) data_region_base & ~VALMASK) != 0)
244 printf ("Error: The heap was allocated in upper memory.\n");
245 exit (1);
247 #endif
248 data_region_end = data_region_base;
249 real_data_region_end = data_region_end;
251 else
253 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
254 data_region_end = data_region_base;
255 real_data_region_end = data_region_end;
256 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
259 /* Update system version information to match current system. */
260 cache_system_info ();
263 /* Round the heap up to the given alignment. */
264 void
265 round_heap (unsigned long align)
267 unsigned long needs_to_be;
268 unsigned long need_to_alloc;
270 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
271 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
273 if (need_to_alloc)
274 sbrk (need_to_alloc);
277 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL))
279 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
280 a heap via HeapCreate. They are normally defined by the runtime,
281 but we override them here so that the unnecessary HeapCreate call
282 is not performed. */
284 int __cdecl
285 _heap_init (void)
287 /* Stepping through the assembly indicates that mainCRTStartup is
288 expecting a nonzero success return value. */
289 return 1;
292 void __cdecl
293 _heap_term (void)
295 return;
298 #endif