annotate src/w32heap.c @ 14376:eb8910a353fc

(vc-diff): Make NOT-URGENT default to t.
author Richard M. Stallman <rms@gnu.org>
date Fri, 26 Jan 1996 19:36:34 +0000
parents ee40177f6c68
children d7d0413e95e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 /* Heap management routines for GNU Emacs on Windows NT.
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
4 This file is part of GNU Emacs.
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
7 it under the terms of the GNU General Public License as published by
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
9 any later version.
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
14 GNU General Public License for more details.
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
16 You should have received a copy of the GNU General Public License
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 12452
diff changeset
19 Boston, MA 02111-1307, USA.
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
24 #include "config.h"
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
25
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 #include <stdlib.h>
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 #include <stdio.h>
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 #include "ntheap.h"
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 /* This gives us the page size and the size of the allocation unit on NT. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 SYSTEM_INFO sysinfo_cache;
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
33 unsigned long syspage_mask = 0;
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 /* These are defined to get Emacs to compile, but are not used. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 int edata;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 int etext;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 /* The major and minor versions of NT. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 int nt_major_version;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 int nt_minor_version;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 /* Cache information describing the NT system for later use. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 void
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 cache_system_info (void)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 union
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 struct info
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 char major;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 char minor;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 short platform;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 } info;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 DWORD data;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 } version;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 /* Cache the version of the operating system. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 version.data = GetVersion ();
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 nt_major_version = version.info.major;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 nt_minor_version = version.info.minor;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 /* Cache page size, allocation unit, processor type, etc. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 GetSystemInfo (&sysinfo_cache);
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
65 syspage_mask = sysinfo_cache.dwPageSize - 1;
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 /* Round ADDRESS up to be aligned with ALIGN. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 unsigned char *
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 round_to_next (unsigned char *address, unsigned long align)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 unsigned long tmp;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 tmp = (unsigned long) address;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 tmp = (tmp + align - 1) / align;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 return (unsigned char *) (tmp * align);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 /* Info for keeping track of our heap. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 unsigned char *data_region_base = NULL;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 unsigned char *data_region_end = NULL;
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
83 unsigned char *real_data_region_end = NULL;
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 unsigned long data_region_size = 0;
11943
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
85 unsigned long reserved_heap_size = 0;
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 /* The start of the data segment. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 unsigned char *
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 get_data_start (void)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 return data_region_base;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 /* The end of the data segment. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 unsigned char *
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 get_data_end (void)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 return data_region_end;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
101 #ifndef WINDOWS95
11943
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
102 static char *
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
103 allocate_heap (void)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
104 {
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
105 unsigned long base = 0x00030000;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
106 unsigned long end = 0x00D00000;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
107
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
108 reserved_heap_size = end - base;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
109
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
110 return VirtualAlloc ((void *) base,
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
111 get_reserved_heap_size (),
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
112 MEM_RESERVE,
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
113 PAGE_NOACCESS);
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
114 }
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
115 #else
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
116 static char *
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
117 allocate_heap (void)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
118 {
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
119 unsigned long start = 0x400000;
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
120 unsigned long stop = 0xD00000;
11943
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
121 unsigned long increment = 0x100000;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
122 char *ptr, *begin = NULL, *end = NULL;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
123 int i;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
124
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
125 for (i = start; i < stop; i += increment)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
126 {
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
127 ptr = VirtualAlloc ((void *) i, increment, MEM_RESERVE, PAGE_NOACCESS);
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
128 if (ptr)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
129 begin = begin ? begin : ptr;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
130 else if (begin)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
131 {
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
132 end = ptr;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
133 break;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
134 }
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
135 }
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
136
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
137 if (begin && !end)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
138 end = (char *) i;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
139
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
140 if (!begin)
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
141 /* We couldn't allocate any memory for the heap. Exit. */
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
142 exit (-2);
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
143
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
144 reserved_heap_size = end - begin;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
145 return begin;
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
146 }
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
147 #endif
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
148
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
149
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 /* Emulate Unix sbrk. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 void *
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 sbrk (unsigned long increment)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 void *result;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 long size = (long) increment;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 /* Allocate our heap if we haven't done so already. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 if (!data_region_base)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 {
11943
ea533622fb8b (reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11385
diff changeset
160 data_region_base = allocate_heap ();
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 if (!data_region_base)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 return NULL;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 /* Ensure that the addresses don't use the upper 8 bits since
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 the Lisp type goes there (yucko). */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 if (((unsigned long) data_region_base & 0xFF000000) != 0)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 printf ("Error: The heap was allocated in upper memory.\n");
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 exit (1);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 data_region_end = data_region_base;
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
173 real_data_region_end = data_region_end;
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 data_region_size = get_reserved_heap_size ();
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 result = data_region_end;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 /* If size is negative, shrink the heap by decommitting pages. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 if (size < 0)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 {
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
182 int new_size;
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
183 unsigned char *new_data_region_end;
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
184
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 size = -size;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 /* Sanity checks. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 if ((data_region_end - size) < data_region_base)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 return NULL;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
191 /* We can only decommit full pages, so allow for
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
192 partial deallocation [cga]. */
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
193 new_data_region_end = (data_region_end - size);
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
194 new_data_region_end = (unsigned char *)
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
195 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
196 new_size = real_data_region_end - new_data_region_end;
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
197 real_data_region_end = new_data_region_end;
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
198 if (new_size > 0)
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
199 {
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
200 /* Decommit size bytes from the end of the heap. */
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
201 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
202 return NULL;
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
203 }
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 data_region_end -= size;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 /* If size is positive, grow the heap by committing reserved pages. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 else if (size > 0)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 /* Sanity checks. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 if ((data_region_end + size) >
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (data_region_base + get_reserved_heap_size ()))
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 return NULL;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 /* Commit more of our heap. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 PAGE_READWRITE) == NULL)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 return NULL;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 data_region_end += size;
12452
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
220
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
221 /* We really only commit full pages, so record where
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
222 the real end of committed memory is [cga]. */
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
223 real_data_region_end = (unsigned char *)
bd304be0b491 Include config.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11943
diff changeset
224 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
9803
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 return result;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 /* Recreate the heap from the data that was dumped to the executable.
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 EXECUTABLE_PATH tells us where to find the executable. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 void
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 recreate_heap (char *executable_path)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 unsigned char *tmp;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 /* First reserve the upper part of our heap. (We reserve first
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 because there have been problems in the past where doing the
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 mapping first has loaded DLLs into the VA space of our heap.) */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 tmp = VirtualAlloc ((void *) get_heap_end (),
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 get_reserved_heap_size () - get_committed_heap_size (),
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 MEM_RESERVE,
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 PAGE_NOACCESS);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 if (!tmp)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 exit (1);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 /* We read in the data for the .bss section from the executable
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 first and map in the heap from the executable second to prevent
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 any funny interactions between file I/O and file mapping. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 read_in_bss (executable_path);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 map_in_heap (executable_path);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 }
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 /* Round the heap up to the given alignment. */
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 void
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 round_heap (unsigned long align)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 {
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 unsigned long needs_to_be;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 unsigned long need_to_alloc;
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 if (need_to_alloc)
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 sbrk (need_to_alloc);
fa698fd10c05 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 }