annotate src/ntheap.c @ 126:1370575f1259 xemacs-20-1p1

Import from CVS: tag xemacs-20-1p1
author cvs
date Mon, 13 Aug 2007 09:27:39 +0200
parents 4be1180a9e89
children 15872534500d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1 /* Heap management routines for XEmacs on Windows NT.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
3
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
4 This file is part of XEmacs.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
5
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
9 later version.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
10
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
14 for more details.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
15
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to the Free
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
19 02111-1307, USA.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
20
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
22
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
23 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
24
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
25 #include "config.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
26
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
27 #include <stdlib.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
28 #include <stdio.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
29
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
30 #include "ntheap.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
31 #include "lisp.h" /* for VALMASK */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
32
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
33 /* This gives us the page size and the size of the allocation unit on NT. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
34 SYSTEM_INFO sysinfo_cache;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
35 unsigned long syspage_mask = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
36
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
37 /* These are defined to get Emacs to compile, but are not used. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
38 int edata;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
39 int etext;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
40
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
41 /* The major and minor versions of NT. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
42 int nt_major_version;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
43 int nt_minor_version;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
44
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
45 /* Cache information describing the NT system for later use. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
46 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
47 cache_system_info (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
48 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
49 union
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
50 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
51 struct info
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
52 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
53 char major;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
54 char minor;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
55 short platform;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
56 } info;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
57 DWORD data;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
58 } version;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
59
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
60 /* Cache the version of the operating system. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
61 version.data = GetVersion ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
62 nt_major_version = version.info.major;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
63 nt_minor_version = version.info.minor;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
64
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
65 /* Cache page size, allocation unit, processor type, etc. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
66 GetSystemInfo (&sysinfo_cache);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
67 syspage_mask = sysinfo_cache.dwPageSize - 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
68 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
69
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
70 /* Round ADDRESS up to be aligned with ALIGN. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
71 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
72 round_to_next (unsigned char *address, unsigned long align)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
73 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
74 unsigned long tmp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
75
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
76 tmp = (unsigned long) address;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
77 tmp = (tmp + align - 1) / align;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
78
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
79 return (unsigned char *) (tmp * align);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
80 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
81
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
82 /* Info for keeping track of our heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
83 unsigned char *data_region_base = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
84 unsigned char *data_region_end = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
85 unsigned char *real_data_region_end = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
86 unsigned long data_region_size = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
87 unsigned long reserved_heap_size = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
88
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
89 /* The start of the data segment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
90 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
91 get_data_start (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
92 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
93 return data_region_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
94 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
95
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
96 /* The end of the data segment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
97 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
98 get_data_end (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
99 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
100 return data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
101 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
102
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
103 static char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
104 allocate_heap (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
105 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
106 /* The base address for our GNU malloc heap is chosen in conjuction
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
107 with the link settings for temacs.exe which control the stack size,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
108 the initial default process heap size and the executable image base
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
109 address. The link settings and the malloc heap base below must all
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
110 correspond; the relationship between these values depends on how NT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
111 and Win95 arrange the virtual address space for a process (and on
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
112 the size of the code and data segments in temacs.exe).
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
113
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
114 The most important thing is to make base address for the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
115 image high enough to leave enough room between it and the 4MB floor
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
116 of the process address space on Win95 for the primary thread stack,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
117 the process default heap, and other assorted odds and ends
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
118 (eg. environment strings, private system dll memory etc) that are
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
119 allocated before temacs has a chance to grab its malloc arena. The
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
120 malloc heap base can then be set several MB higher than the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
121 executable image base, leaving enough room for the code and data
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
122 segments.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
123
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
124 Because some parts of Emacs can use rather a lot of stack space
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
125 (for instance, the regular expression routines can potentially
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
126 allocate several MB of stack space) we allow 8MB for the stack.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
127
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
128 Allowing 1MB for the default process heap, and 1MB for odds and
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
129 ends, we can base the executable at 16MB and still have a generous
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
130 safety margin. At the moment, the executable has about 810KB of
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
131 code (for x86) and about 550KB of data - on RISC platforms the code
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
132 size could be roughly double, so if we allow 4MB for the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
133 we will have plenty of room for expansion.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
134
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
135 Thus we would like to set the malloc heap base to 20MB. However,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
136 Win95 refuses to allocate the heap starting at this address, so we
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
137 set the base to 27MB to make it happy. Since Emacs now leaves
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
138 28 bits available for pointers, this lets us use the remainder of
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
139 the region below the 256MB line for our malloc arena - 229MB is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
140 still a pretty decent arena to play in! */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
141
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
142 unsigned long base = 0x01B00000; /* 27MB */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
143 unsigned long end = 1 << VALBITS; /* 256MB */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
144 void *ptr = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
145
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
146 #if NTHEAP_PROBE_BASE /* This is never normally defined */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
147 /* Try various addresses looking for one the kernel will let us have. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
148 while (!ptr && (base < end))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
149 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
150 reserved_heap_size = end - base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
151 ptr = VirtualAlloc ((void *) base,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
152 get_reserved_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
153 MEM_RESERVE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
154 PAGE_NOACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
155 base += 0x00100000; /* 1MB increment */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
156 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
157 #else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
158 reserved_heap_size = end - base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
159 ptr = VirtualAlloc ((void *) base,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
160 get_reserved_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
161 MEM_RESERVE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
162 PAGE_NOACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
163 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
164
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
165 return ptr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
166 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
167
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
168
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
169 /* Emulate Unix sbrk. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
170 void *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
171 sbrk (unsigned long increment)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
172 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
173 void *result;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
174 long size = (long) increment;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
175
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
176 /* Allocate our heap if we haven't done so already. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
177 if (!data_region_base)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
178 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
179 data_region_base = allocate_heap ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
180 if (!data_region_base)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
181 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
182
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
183 /* Ensure that the addresses don't use the upper tag bits since
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
184 the Lisp type goes there. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
185 if (((unsigned long) data_region_base & ~VALMASK) != 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
186 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
187 printf ("Error: The heap was allocated in upper memory.\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
188 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
189 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
190
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
191 data_region_end = data_region_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
192 real_data_region_end = data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
193 data_region_size = get_reserved_heap_size ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
194 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
195
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
196 result = data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
197
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
198 /* If size is negative, shrink the heap by decommitting pages. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
199 if (size < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
200 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
201 int new_size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
202 unsigned char *new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
203
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
204 size = -size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
205
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
206 /* Sanity checks. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
207 if ((data_region_end - size) < data_region_base)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
208 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
209
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
210 /* We can only decommit full pages, so allow for
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
211 partial deallocation [cga]. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
212 new_data_region_end = (data_region_end - size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
213 new_data_region_end = (unsigned char *)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
214 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
215 new_size = real_data_region_end - new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
216 real_data_region_end = new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
217 if (new_size > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
218 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
219 /* Decommit size bytes from the end of the heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
220 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
221 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
222 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
223
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
224 data_region_end -= size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
225 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
226 /* If size is positive, grow the heap by committing reserved pages. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
227 else if (size > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
228 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
229 /* Sanity checks. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
230 if ((data_region_end + size) >
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
231 (data_region_base + get_reserved_heap_size ()))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
232 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
233
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
234 /* Commit more of our heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
235 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
236 PAGE_READWRITE) == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
237 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
238 data_region_end += size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
239
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
240 /* We really only commit full pages, so record where
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
241 the real end of committed memory is [cga]. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
242 real_data_region_end = (unsigned char *)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
243 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
244 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
245
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
246 return result;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
247 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
248
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
249 #ifndef CANNOT_DUMP
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
250
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
251 /* Recreate the heap from the data that was dumped to the executable.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
252 EXECUTABLE_PATH tells us where to find the executable. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
253 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
254 recreate_heap (char *executable_path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
255 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
256 unsigned char *tmp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
257
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
258 /* First reserve the upper part of our heap. (We reserve first
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
259 because there have been problems in the past where doing the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
260 mapping first has loaded DLLs into the VA space of our heap.) */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
261 tmp = VirtualAlloc ((void *) get_heap_end (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
262 get_reserved_heap_size () - get_committed_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
263 MEM_RESERVE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
264 PAGE_NOACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
265 if (!tmp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
266 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
267
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
268 /* We read in the data for the .bss section from the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
269 first and map in the heap from the executable second to prevent
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
270 any funny interactions between file I/O and file mapping. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
271 read_in_bss (executable_path);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
272 map_in_heap (executable_path);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
273 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
274
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
275 #endif /* CANNOT_DUMP */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
276
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
277 /* Round the heap up to the given alignment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
278 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
279 round_heap (unsigned long align)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
280 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
281 unsigned long needs_to_be;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
282 unsigned long need_to_alloc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
283
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
284 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
285 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
286
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
287 if (need_to_alloc)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
288 sbrk (need_to_alloc);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
289 }