Use -gcoff instead of -g in CFLAGS, for those who
[emacs.git] / src / w32heap.c
blob38f7ffe05346902014fc3d4691324dfaef1bf7dd
1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
29 #include "w32heap.h"
30 #include "lisp.h" /* for VALMASK */
32 #undef RVA_TO_PTR
33 #define RVA_TO_PTR(rva) ((DWORD)(rva) + (DWORD)GetModuleHandle (NULL))
35 /* This gives us the page size and the size of the allocation unit on NT. */
36 SYSTEM_INFO sysinfo_cache;
37 unsigned long syspage_mask = 0;
39 /* These are defined to get Emacs to compile, but are not used. */
40 int edata;
41 int etext;
43 /* The major and minor versions of NT. */
44 int w32_major_version;
45 int w32_minor_version;
47 /* Distinguish between Windows NT and Windows 95. */
48 int os_subtype;
50 /* Cache information describing the NT system for later use. */
51 void
52 cache_system_info (void)
54 union
56 struct info
58 char major;
59 char minor;
60 short platform;
61 } info;
62 DWORD data;
63 } version;
65 /* Cache the version of the operating system. */
66 version.data = GetVersion ();
67 w32_major_version = version.info.major;
68 w32_minor_version = version.info.minor;
70 if (version.info.platform & 0x8000)
71 os_subtype = OS_WIN95;
72 else
73 os_subtype = OS_NT;
75 /* Cache page size, allocation unit, processor type, etc. */
76 GetSystemInfo (&sysinfo_cache);
77 syspage_mask = sysinfo_cache.dwPageSize - 1;
80 /* Emulate getpagesize. */
81 int
82 getpagesize (void)
84 return sysinfo_cache.dwPageSize;
87 /* Info for managing our preload heap, which is essentially a fixed size
88 data area in the executable. */
89 PIMAGE_SECTION_HEADER preload_heap_section;
91 /* Info for keeping track of our heap. */
92 unsigned char *data_region_base = NULL;
93 unsigned char *data_region_end = NULL;
94 unsigned char *real_data_region_end = NULL;
95 unsigned long reserved_heap_size = 0;
97 /* The start of the data segment. */
98 unsigned char *
99 get_data_start (void)
101 return data_region_base;
104 /* The end of the data segment. */
105 unsigned char *
106 get_data_end (void)
108 return data_region_end;
111 static char *
112 allocate_heap (void)
114 /* Try to get as much as possible of the address range from the end of
115 the preload heap section up to the usable address limit. Since GNU
116 malloc can handle gaps in the memory it gets from sbrk, we can
117 simply set the sbrk pointer to the base of the new heap region. */
118 unsigned long base =
119 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
120 + preload_heap_section->Misc.VirtualSize),
121 get_allocation_unit ());
122 unsigned long end = 1 << VALBITS; /* 256MB */
123 void *ptr = NULL;
125 while (!ptr && (base < end))
127 reserved_heap_size = end - base;
128 ptr = VirtualAlloc ((void *) base,
129 get_reserved_heap_size (),
130 MEM_RESERVE,
131 PAGE_NOACCESS);
132 base += 0x00100000; /* 1MB increment */
135 return ptr;
139 /* Emulate Unix sbrk. */
140 void *
141 sbrk (unsigned long increment)
143 void *result;
144 long size = (long) increment;
146 result = data_region_end;
148 /* If size is negative, shrink the heap by decommitting pages. */
149 if (size < 0)
151 int new_size;
152 unsigned char *new_data_region_end;
154 size = -size;
156 /* Sanity checks. */
157 if ((data_region_end - size) < data_region_base)
158 return NULL;
160 /* We can only decommit full pages, so allow for
161 partial deallocation [cga]. */
162 new_data_region_end = (data_region_end - size);
163 new_data_region_end = (unsigned char *)
164 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
165 new_size = real_data_region_end - new_data_region_end;
166 real_data_region_end = new_data_region_end;
167 if (new_size > 0)
169 /* Decommit size bytes from the end of the heap. */
170 if (using_dynamic_heap
171 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
172 return NULL;
175 data_region_end -= size;
177 /* If size is positive, grow the heap by committing reserved pages. */
178 else if (size > 0)
180 /* Sanity checks. */
181 if ((data_region_end + size) >
182 (data_region_base + get_reserved_heap_size ()))
183 return NULL;
185 /* Commit more of our heap. */
186 if (using_dynamic_heap
187 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
188 PAGE_READWRITE) == NULL)
189 return NULL;
190 data_region_end += size;
192 /* We really only commit full pages, so record where
193 the real end of committed memory is [cga]. */
194 real_data_region_end = (unsigned char *)
195 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
198 return result;
201 /* Initialize the internal heap variables used by sbrk. When running in
202 preload phase (ie. in the undumped executable), we rely entirely on a
203 fixed size heap section included in the .exe itself; this is
204 preserved during dumping, and truncated to the size actually used.
206 When running in the dumped executable, we reserve as much as possible
207 of the address range that is addressable by Lisp object pointers, to
208 supplement what is left of the preload heap. Although we cannot rely
209 on the dynamically allocated arena being contiguous with the static
210 heap area, it is not a problem because sbrk can pretend that the gap
211 was allocated by something else; GNU malloc detects when there is a
212 jump in the sbrk values, and starts a new heap block. */
213 void
214 init_heap ()
216 PIMAGE_DOS_HEADER dos_header;
217 PIMAGE_NT_HEADERS nt_header;
219 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
220 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
221 dos_header->e_lfanew);
222 preload_heap_section = find_section ("EMHEAP", nt_header);
224 if (using_dynamic_heap)
226 data_region_base = allocate_heap ();
227 if (!data_region_base)
229 printf ("Error: Could not reserve dynamic heap area.\n");
230 exit (1);
233 /* Ensure that the addresses don't use the upper tag bits since
234 the Lisp type goes there. */
235 if (((unsigned long) data_region_base & ~VALMASK) != 0)
237 printf ("Error: The heap was allocated in upper memory.\n");
238 exit (1);
241 data_region_end = data_region_base;
242 real_data_region_end = data_region_end;
244 else
246 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
247 data_region_end = data_region_base;
248 real_data_region_end = data_region_end;
249 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
252 /* Update system version information to match current system. */
253 cache_system_info ();
256 /* Round the heap up to the given alignment. */
257 void
258 round_heap (unsigned long align)
260 unsigned long needs_to_be;
261 unsigned long need_to_alloc;
263 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
264 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
266 if (need_to_alloc)
267 sbrk (need_to_alloc);
270 #if (_MSC_VER >= 1000)
272 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
273 a heap via HeapCreate. They are normally defined by the runtime,
274 but we override them here so that the unnecessary HeapCreate call
275 is not performed. */
277 int __cdecl
278 _heap_init (void)
280 /* Stepping through the assembly indicates that mainCRTStartup is
281 expecting a nonzero success return value. */
282 return 1;
285 void __cdecl
286 _heap_term (void)
288 return;
291 #endif