annotate src/ralloc.c @ 1169:a40b54fcb2ff

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Sat, 19 Sep 1992 19:21:09 +0000
parents 4b61400a5195
children 761b9b4fd3ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 /* Block-relocating memory allocator.
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
2 Copyright (C) 1992 Free Software Foundation, Inc.
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4 This file is part of GNU Emacs.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 the Free Software Foundation; either version 1, or (at your option)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 any later version.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 GNU General Public License for more details.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 /* NOTES:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 rather than all of them. This means allowing for a possible
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 hole between the first bloc and the end of malloc storage. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 #include "config.h"
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
27 #include "lisp.h" /* Needed for VALBITS. */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 #undef NULL
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 #include "mem_limits.h"
621
eca8812e61cd *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 577
diff changeset
30 #include "getpagesize.h"
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 #define NIL ((POINTER) 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
35 /* Declarations for working with the malloc, ralloc, and system breaks. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
36
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 /* System call to set the break value. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 extern POINTER sbrk ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 /* The break value, as seen by malloc (). */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 static POINTER virtual_break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 /* The break value, viewed by the relocatable blocs. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 static POINTER break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 /* The REAL (i.e., page aligned) break value of the process. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 static POINTER page_break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 /* Macros for rounding. Note that rounding to any value is possible
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 by changing the definition of PAGE. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 #define PAGE (getpagesize ())
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 #define ROUNDUP(size) (((unsigned int) (size) + PAGE) & ~(PAGE - 1))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
56 /* Managing "almost out of memory" warnings. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
57
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 /* Level of warnings issued. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 static int warnlevel;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 /* Function to call to issue a warning;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 0 means don't issue them. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 static void (*warnfunction) ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 static void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 check_memory_limits (address)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 POINTER address;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 SIZE data_size = address - data_space_start;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 switch (warnlevel)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 case 0:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 if (data_size > (lim_data / 4) * 3)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 warnlevel++;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (*warnfunction) ("Warning: past 75% of memory limit");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 break;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 case 1:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 if (data_size > (lim_data / 20) * 17)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 warnlevel++;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 (*warnfunction) ("Warning: past 85% of memory limit");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 break;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 case 2:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 if (data_size > (lim_data / 20) * 19)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 warnlevel++;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 (*warnfunction) ("Warning: past 95% of memory limit");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 break;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 default:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 (*warnfunction) ("Warning: past acceptable memory limits");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 break;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 if (EXCEEDS_ELISP_PTR (address))
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
103 memory_full ();
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
106 /* Functions to get and return memory from the system. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
107
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 /* Obtain SIZE bytes of space. If enough space is not presently available
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 in our process reserve, (i.e., (page_break_value - break_value)),
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 this means getting more page-aligned space from the system. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 static void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 obtain (size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 SIZE already_available = page_break_value - break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 if (already_available < size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 {
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
120 SIZE get = ROUNDUP (size - already_available);
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 if (warnfunction)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 check_memory_limits (page_break_value);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 if (((int) sbrk (get)) < 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 abort ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 page_break_value += get;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 break_value += size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 /* Obtain SIZE bytes of space and return a pointer to the new area. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 static POINTER
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 get_more_space (size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 POINTER ptr = break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 obtain (size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 return ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 /* Note that SIZE bytes of space have been relinquished by the process.
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
146 If SIZE is more than a page, return the space to the system. */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 static void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 relinquish (size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 {
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
152 POINTER new_page_break;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
154 break_value -= size;
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
155 new_page_break = (POINTER) ROUNDUP (break_value);
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
156
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
157 if (new_page_break != page_break_value)
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 {
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
159 if (((int) (sbrk ((char *) new_page_break
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
160 - (char *) page_break_value))) < 0)
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 abort ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
163 page_break_value = new_page_break;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
166 /* Zero the space from the end of the "official" break to the actual
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
167 break, so that bugs show up faster. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
168 bzero (break_value, ((char *) page_break_value - (char *) break_value));
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
171 /* The meat - allocating, freeing, and relocating blocs. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
172
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
173 /* These structures are allocated in the malloc arena.
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
174 The linked list is kept in order of increasing '.data' members.
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
175 The data blocks abut each other; if b->next is non-nil, then
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
176 b->data + b->size == b->next->data. */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 typedef struct bp
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 struct bp *next;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 struct bp *prev;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 POINTER *variable;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 POINTER data;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 } *bloc_ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 #define NIL_BLOC ((bloc_ptr) 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 #define BLOC_PTR_SIZE (sizeof (struct bp))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 /* Head and tail of the list of relocatable blocs. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 static bloc_ptr first_bloc, last_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
192 /* Declared in dispnew.c, this version doesn't screw up if regions
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
193 overlap. */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 extern void safe_bcopy ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
196 /* Find the bloc referenced by the address in PTR. Returns a pointer
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 to that block. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 static bloc_ptr
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 find_bloc (ptr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 POINTER *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 register bloc_ptr p = first_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 while (p != NIL_BLOC)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 if (p->variable == ptr && p->data == *ptr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 return p;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 p = p->next;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 return p;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 Returns a pointer to the new bloc. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 static bloc_ptr
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 get_bloc (size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 register bloc_ptr new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 new_bloc->data = get_more_space (size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 new_bloc->size = size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 new_bloc->next = NIL_BLOC;
1013
6bf2c4766d4c * ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents: 734
diff changeset
228 new_bloc->variable = (POINTER *) NIL;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 if (first_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 new_bloc->prev = last_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 last_bloc->next = new_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 last_bloc = new_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 else
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 first_bloc = last_bloc = new_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 new_bloc->prev = NIL_BLOC;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 return new_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 /* Relocate all blocs from BLOC on upward in the list to the zone
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 indicated by ADDRESS. Direction of relocation is determined by
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 the position of ADDRESS relative to BLOC->data.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 Note that ordering of blocs is not affected by this function. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 static void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 relocate_some_blocs (bloc, address)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 bloc_ptr bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 POINTER address;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 register bloc_ptr b;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 POINTER data_zone = bloc->data;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 register SIZE data_zone_size = 0;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 register SIZE offset = bloc->data - address;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 POINTER new_data_zone = data_zone - offset;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 for (b = bloc; b != NIL_BLOC; b = b->next)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 data_zone_size += b->size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 b->data -= offset;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 *b->variable = b->data;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 safe_bcopy (data_zone, new_data_zone, data_zone_size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 /* Free BLOC from the chain of blocs, relocating any blocs above it
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273 and returning BLOC->size bytes to the free area. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 static void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 free_bloc (bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 bloc_ptr bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 if (bloc == first_bloc && bloc == last_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 first_bloc = last_bloc = NIL_BLOC;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 else if (bloc == last_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 last_bloc = bloc->prev;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286 last_bloc->next = NIL_BLOC;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 else if (bloc == first_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290 first_bloc = bloc->next;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 first_bloc->prev = NIL_BLOC;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 relocate_some_blocs (bloc->next, bloc->data);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294 else
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296 bloc->next->prev = bloc->prev;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297 bloc->prev->next = bloc->next;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
298 relocate_some_blocs (bloc->next, bloc->data);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
299 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
300
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
301 relinquish (bloc->size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
302 free (bloc);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
303 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
304
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
305 /* Interface routines. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
306
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
307 static int use_relocatable_buffers;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
308
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
309 /* Obtain SIZE bytes of storage from the free pool, or the system,
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
310 as neccessary. If relocatable blocs are in use, this means
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
311 relocating them. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
312
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
313 POINTER
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
314 r_alloc_sbrk (size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
315 long size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
316 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
317 POINTER ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
318
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
319 if (! use_relocatable_buffers)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
320 return sbrk (size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
321
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
322 if (size > 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
323 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
324 obtain (size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
325 if (first_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
326 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
327 relocate_some_blocs (first_bloc, first_bloc->data + size);
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
328
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
329 /* Zero out the space we just allocated, to help catch bugs
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
330 quickly. */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
331 bzero (virtual_break_value, size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
332 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
333 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
334 else if (size < 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
335 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
336 if (first_bloc)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
337 relocate_some_blocs (first_bloc, first_bloc->data + size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
338 relinquish (- size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
339 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
340
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
341 ptr = virtual_break_value;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
342 virtual_break_value += size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
343 return ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
344 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
345
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
346 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
347 the data is returned in *PTR. PTR is thus the address of some variable
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
348 which will use the data area. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
349
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
350 POINTER
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
351 r_alloc (ptr, size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
352 POINTER *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
353 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
354 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
355 register bloc_ptr new_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
356
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
357 new_bloc = get_bloc (size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
358 new_bloc->variable = ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
359 *ptr = new_bloc->data;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
360
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
361 return *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
362 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
363
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
364 /* Free a bloc of relocatable storage whose data is pointed to by PTR. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
365
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
366 void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
367 r_alloc_free (ptr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
368 register POINTER *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
369 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
370 register bloc_ptr dead_bloc;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
371
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
372 dead_bloc = find_bloc (ptr);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
373 if (dead_bloc == NIL_BLOC)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
374 abort ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
375
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
376 free_bloc (dead_bloc);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
377 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
378
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
379 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
380 This is done by shifting all blocks above this one up in memory,
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
381 unless SIZE is less than or equal to the current bloc size, in
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
382 which case nothing happens and the current value is returned.
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
383
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
384 The contents of PTR is changed to reflect the new bloc, and this
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
385 value is returned. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
386
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
387 POINTER
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
388 r_re_alloc (ptr, size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
389 POINTER *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
390 SIZE size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
391 {
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
392 register bloc_ptr bloc;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
393
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
394 bloc = find_bloc (ptr);
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
395 if (bloc == NIL_BLOC)
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
396 abort ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
397
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
398 if (size <= bloc->size)
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
399 /* Wouldn't it be useful to actually resize the bloc here? */
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
400 return *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
401
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
402 obtain (size - bloc->size);
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
403 relocate_some_blocs (bloc->next, bloc->data + size);
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
404
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
405 /* Zero out the new space in the bloc, to help catch bugs faster. */
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
406 bzero (bloc->data + bloc->size, size - bloc->size);
1121
4b61400a5195 Fix typo.
Jim Blandy <jimb@redhat.com>
parents: 1087
diff changeset
407
1087
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
408 /* Indicate that this block has a new size. */
6c410cc87574 * ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents: 1013
diff changeset
409 bloc->size = size;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
410
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
411 return *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
412 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
413
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
414 /* The hook `malloc' uses for the function which gets more space
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
415 from the system. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
416 extern POINTER (*__morecore) ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
417
577
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
418 /* A flag to indicate whether we have initialized ralloc yet. For
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
419 Emacs's sake, please do not make this local to malloc_init; on some
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
420 machines, the dumping procedure makes all static variables
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
421 read-only. On these machines, the word static is #defined to be
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
422 the empty string, meaning that malloc_initialized becomes an
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
423 automatic variable, and loses its value each time Emacs is started
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
424 up. */
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
425 static int malloc_initialized = 0;
53f29271d1b0 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
426
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
427 /* Intialize various things for memory allocation. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
428
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
429 void
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
430 malloc_init (start, warn_func)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
431 POINTER start;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
432 void (*warn_func) ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
433 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
434 if (start)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
435 data_space_start = start;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
436
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
437 if (malloc_initialized)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
438 return;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
439
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
440 malloc_initialized = 1;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
441 __morecore = r_alloc_sbrk;
1013
6bf2c4766d4c * ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents: 734
diff changeset
442
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
443 virtual_break_value = break_value = sbrk (0);
1013
6bf2c4766d4c * ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents: 734
diff changeset
444 if (break_value == (POINTER)NULL)
6bf2c4766d4c * ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents: 734
diff changeset
445 (*warn_func)("Malloc initialization returned 0 from sbrk(0).");
6bf2c4766d4c * ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents: 734
diff changeset
446
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
447 page_break_value = (POINTER) ROUNDUP (break_value);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
448 bzero (break_value, (page_break_value - break_value));
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
449 use_relocatable_buffers = 1;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
450
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
451 lim_data = 0;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
452 warnlevel = 0;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
453 warnfunction = warn_func;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
454
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
455 get_lim_data ();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
456 }