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)
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
30 #include "lisp.h" /* for VALMASK */
32 /* This gives us the page size and the size of the allocation unit on NT. */
33 SYSTEM_INFO sysinfo_cache
;
34 unsigned long syspage_mask
= 0;
36 /* These are defined to get Emacs to compile, but are not used. */
40 /* The major and minor versions of NT. */
41 int w32_major_version
;
42 int w32_minor_version
;
44 /* Cache information describing the NT system for later use. */
46 cache_system_info (void)
59 /* Cache the version of the operating system. */
60 version
.data
= GetVersion ();
61 w32_major_version
= version
.info
.major
;
62 w32_minor_version
= version
.info
.minor
;
64 /* Cache page size, allocation unit, processor type, etc. */
65 GetSystemInfo (&sysinfo_cache
);
66 syspage_mask
= sysinfo_cache
.dwPageSize
- 1;
69 /* Emulate getpagesize. */
73 return sysinfo_cache
.dwPageSize
;
76 /* Round ADDRESS up to be aligned with ALIGN. */
78 round_to_next (unsigned char *address
, unsigned long align
)
82 tmp
= (unsigned long) address
;
83 tmp
= (tmp
+ align
- 1) / align
;
85 return (unsigned char *) (tmp
* align
);
88 /* Force zero initialized variables to be placed in the .data segment;
89 MSVC 5.0 otherwise places them in .bss, which breaks the dumping code. */
90 #pragma data_seg(".data")
92 /* Info for keeping track of our heap. */
93 unsigned char *data_region_base
= NULL
;
94 unsigned char *data_region_end
= NULL
;
95 unsigned char *real_data_region_end
= NULL
;
96 unsigned long data_region_size
= 0;
97 unsigned long reserved_heap_size
= 0;
99 /* The start of the data segment. */
101 get_data_start (void)
103 return data_region_base
;
106 /* The end of the data segment. */
110 return data_region_end
;
116 /* The base address for our GNU malloc heap is chosen in conjuction
117 with the link settings for temacs.exe which control the stack size,
118 the initial default process heap size and the executable image base
119 address. The link settings and the malloc heap base below must all
120 correspond; the relationship between these values depends on how NT
121 and Windows 95 arrange the virtual address space for a process (and on
122 the size of the code and data segments in temacs.exe).
124 The most important thing is to make base address for the executable
125 image high enough to leave enough room between it and the 4MB floor
126 of the process address space on Windows 95 for the primary thread stack,
127 the process default heap, and other assorted odds and ends
128 (eg. environment strings, private system dll memory etc) that are
129 allocated before temacs has a chance to grab its malloc arena. The
130 malloc heap base can then be set several MB higher than the
131 executable image base, leaving enough room for the code and data
134 Because some parts of Emacs can use rather a lot of stack space
135 (for instance, the regular expression routines can potentially
136 allocate several MB of stack space) we allow 8MB for the stack.
138 Allowing 1MB for the default process heap, and 1MB for odds and
139 ends, we can base the executable at 16MB and still have a generous
140 safety margin. At the moment, the executable has about 810KB of
141 code (for x86) and about 550KB of data - on RISC platforms the code
142 size could be roughly double, so if we allow 4MB for the executable
143 we will have plenty of room for expansion.
145 Thus we would like to set the malloc heap base to 20MB. However,
146 Windows 95 refuses to allocate the heap starting at this address, so we
147 set the base to 27MB to make it happy. Since Emacs now leaves
148 28 bits available for pointers, this lets us use the remainder of
149 the region below the 256MB line for our malloc arena - 229MB is
150 still a pretty decent arena to play in! */
152 unsigned long base
= 0x01B00000; /* 27MB */
153 unsigned long end
= 1 << VALBITS
; /* 256MB */
156 #if NTHEAP_PROBE_BASE /* This is never normally defined */
157 /* Try various addresses looking for one the kernel will let us have. */
158 while (!ptr
&& (base
< end
))
160 reserved_heap_size
= end
- base
;
161 ptr
= VirtualAlloc ((void *) base
,
162 get_reserved_heap_size (),
165 base
+= 0x00100000; /* 1MB increment */
168 reserved_heap_size
= end
- base
;
169 ptr
= VirtualAlloc ((void *) base
,
170 get_reserved_heap_size (),
179 /* Emulate Unix sbrk. */
181 sbrk (unsigned long increment
)
184 long size
= (long) increment
;
186 /* Allocate our heap if we haven't done so already. */
187 if (!data_region_base
)
189 data_region_base
= allocate_heap ();
190 if (!data_region_base
)
193 /* Ensure that the addresses don't use the upper tag bits since
194 the Lisp type goes there. */
195 if (((unsigned long) data_region_base
& ~VALMASK
) != 0)
197 printf ("Error: The heap was allocated in upper memory.\n");
201 data_region_end
= data_region_base
;
202 real_data_region_end
= data_region_end
;
203 data_region_size
= get_reserved_heap_size ();
206 result
= data_region_end
;
208 /* If size is negative, shrink the heap by decommitting pages. */
212 unsigned char *new_data_region_end
;
217 if ((data_region_end
- size
) < data_region_base
)
220 /* We can only decommit full pages, so allow for
221 partial deallocation [cga]. */
222 new_data_region_end
= (data_region_end
- size
);
223 new_data_region_end
= (unsigned char *)
224 ((long) (new_data_region_end
+ syspage_mask
) & ~syspage_mask
);
225 new_size
= real_data_region_end
- new_data_region_end
;
226 real_data_region_end
= new_data_region_end
;
229 /* Decommit size bytes from the end of the heap. */
230 if (!VirtualFree (real_data_region_end
, new_size
, MEM_DECOMMIT
))
234 data_region_end
-= size
;
236 /* If size is positive, grow the heap by committing reserved pages. */
240 if ((data_region_end
+ size
) >
241 (data_region_base
+ get_reserved_heap_size ()))
244 /* Commit more of our heap. */
245 if (VirtualAlloc (data_region_end
, size
, MEM_COMMIT
,
246 PAGE_READWRITE
) == NULL
)
248 data_region_end
+= size
;
250 /* We really only commit full pages, so record where
251 the real end of committed memory is [cga]. */
252 real_data_region_end
= (unsigned char *)
253 ((long) (data_region_end
+ syspage_mask
) & ~syspage_mask
);
259 /* Recreate the heap from the data that was dumped to the executable.
260 EXECUTABLE_PATH tells us where to find the executable. */
262 recreate_heap (char *executable_path
)
266 /* First reserve the upper part of our heap. (We reserve first
267 because there have been problems in the past where doing the
268 mapping first has loaded DLLs into the VA space of our heap.) */
269 tmp
= VirtualAlloc ((void *) get_heap_end (),
270 get_reserved_heap_size () - get_committed_heap_size (),
276 /* We read in the data for the .bss section from the executable
277 first and map in the heap from the executable second to prevent
278 any funny interactions between file I/O and file mapping. */
279 read_in_bss (executable_path
);
280 map_in_heap (executable_path
);
283 /* Round the heap up to the given alignment. */
285 round_heap (unsigned long align
)
287 unsigned long needs_to_be
;
288 unsigned long need_to_alloc
;
290 needs_to_be
= (unsigned long) round_to_next (get_heap_end (), align
);
291 need_to_alloc
= needs_to_be
- (unsigned long) get_heap_end ();
294 sbrk (need_to_alloc
);