annotate src/malloc.c @ 108:360340f9fd5f r20-1b6

Import from CVS: tag r20-1b6
author cvs
date Mon, 13 Aug 2007 09:18:39 +0200
parents ac2d302a0011
children 15872534500d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* dynamic memory allocation for GNU.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1985, 1987 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 NO WARRANTY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 CORRECTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 GENERAL PUBLIC LICENSE TO COPY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 1. You may copy and distribute verbatim copies of this source file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 as you receive it, in any medium, provided that you conspicuously and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 appropriately publish on each copy a valid copyright notice "Copyright
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (C) 1985 Free Software Foundation, Inc."; and include following the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 copyright notice a verbatim copy of the above disclaimer of warranty
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 and of this License. You may charge a distribution fee for the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 physical act of transferring a copy.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 2. You may modify your copy or copies of this source file or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 any portion of it, and copy and distribute such modifications under
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 the terms of Paragraph 1 above, provided that you also do the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 a) cause the modified files to carry prominent notices stating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 that you changed the files and the date of any change; and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 b) cause the whole of any work that you distribute or publish,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 that in whole or in part contains or is a derivative of this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 program or any part thereof, to be licensed at no charge to all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 third parties on terms identical to those contained in this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 License Agreement (except that you may choose to grant more extensive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 warranty protection to some or all third parties, at your option).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 c) You may charge a distribution fee for the physical act of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 transferring a copy, and you may at your option offer warranty
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 protection in exchange for a fee.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 Mere aggregation of another unrelated program with this program (or its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 derivative) on a volume of a storage or distribution medium does not bring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 the other program under the scope of these terms.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 3. You may copy and distribute this program (or a portion or derivative
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 of it, under Paragraph 2) in object code or executable form under the terms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 of Paragraphs 1 and 2 above provided that you also do one of the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 a) accompany it with the complete corresponding machine-readable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 source code, which must be distributed under the terms of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 Paragraphs 1 and 2 above; or,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 b) accompany it with a written offer, valid for at least three
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 years, to give any third party free (except for a nominal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 shipping charge) a complete machine-readable copy of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 corresponding source code, to be distributed under the terms of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 Paragraphs 1 and 2 above; or,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 c) accompany it with the information you received as to where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 corresponding source code may be obtained. (This alternative is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 allowed only for noncommercial distribution and only if you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 received the program in object code or executable form alone.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 For an executable file, complete source code means all the source code for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 all modules it contains; but, as a special exception, it need not include
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 source code for modules which are standard libraries that accompany the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 operating system on which the executable file runs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 4. You may not copy, sublicense, distribute or transfer this program
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 except as expressly provided under this License Agreement. Any attempt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 otherwise to copy, sublicense, distribute or transfer this program is void and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 your rights to use the program under this License agreement shall be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 automatically terminated. However, parties who have received computer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 software programs from you with this License Agreement will not have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 their licenses terminated so long as such parties remain in full compliance.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 5. If you wish to incorporate parts of this program into other free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 programs whose distribution conditions are different, write to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 MA 02111-1307, USA.. We have not yet worked out a simple rule that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 can be stated here, but we will often permit this. We will be guided
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 by the two goals of preserving the free status of all derivatives of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 our free software and of promoting the sharing and reuse of software.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 In other words, you are welcome to use, share and improve this program.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 You are forbidden to forbid anyone else to use, share and improve
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 what you give them. Help stamp out software-hoarding! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 /* Synched up with: Not synched with FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 * @(#)nmalloc.c 1 (Caltech) 2/21/82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 * U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 * Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 * This is a very fast storage allocator. It allocates blocks of a small
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 * number of different sizes, and keeps free lists of each size. Blocks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 * that don't exactly fit are passed up to the next larger size. In this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 * This is designed for use in a program that uses vast quantities of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 * memory, but bombs when it runs out. To make it a little better, it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 * warns the user when he starts to get near the end.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 * June 84, ACT: modified rcheck code to check the range given to malloc,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 * rather than the range determined by the 2-power used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 * You should call malloc_init to reinitialize after loading dumped Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 * Call malloc_stats to get info on memory stats if MSTATS turned on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 * realloc knows how to return same block given, just changing its size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 * if the power of 2 is correct.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 * smallest allocatable block is 8 bytes. The overhead information will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 * go in the first int of the block, and the returned pointer will point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 * to the second.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 * nmalloc[i] is the difference between the number of mallocs and frees
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 * for a given block size.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 #endif MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 /* config.h specifies which kind of system this is. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 /* Determine which kind of system this is. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 #include <signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 #ifndef SIGTSTP
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 #ifndef VMS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 #ifndef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 #define USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 #endif /* not VMS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 #else /* SIGTSTP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 #ifdef SIGIO
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 #define BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 #endif /* SIGIO */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 #endif /* SIGTSTP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 #if defined(hpux)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 #define USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 #endif /* not emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 /* Define getpagesize () if the system does not. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 #include "getpagesize.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 #ifndef BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 #ifndef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 #include <sys/vlimit.h> /* warn the user when near the end */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 #endif /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 #else /* if BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 #include <sys/time.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 #include <sys/resource.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #endif /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 #ifdef __STDC_
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 #ifndef HPUX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 /* not sure where this for NetBSD should really go
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 and it probably applies to other systems */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 #if !defined(__NetBSD__) && !defined(__bsdi__)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 extern void *sbrk (ptrdiff_t);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 extern char *sbrk ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 #endif /* __NetBSD__ */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 #endif /* HPUX */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 extern void *sbrk ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 #endif /* __STDC__ */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 extern char *start_of_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 #ifndef DATA_SEG_BITS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 #define start_of_data() &etext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 #ifndef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 #define start_of_data() &etext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 #define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 #define ISFREE ((char) 0x54) /* magic byte that implies free block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 /* this is for error checking only */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 #define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 memalign, with the rest of the word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 being the distance to the true
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 beginning of the block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 extern char etext;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 /* These two are for user programs to look at, when they are interested. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 unsigned int malloc_sbrk_used; /* amount of data space used now */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 unsigned int malloc_sbrk_unused; /* amount more we can have */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 /* start of data space; can be changed by calling init_malloc */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 static char *data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 static int nmalloc[30];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 static int nmal, nfre;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 /* If range checking is not turned on, all we have is a flag indicating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 whether memory is allocated, an index in nextf[], and a size field; to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 on whether the former can hold the exact size (given the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 'index'). If range checking is on, we always need to know how much space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 is allocated, so the 'size' field is never used. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 struct mhead {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 char mh_alloc; /* ISALLOC or ISFREE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 char mh_index; /* index in nextf[] */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 /* Remainder are valid only when block is allocated */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 unsigned short mh_size; /* size, if < 0x10000 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 unsigned mh_nbytes; /* number of bytes allocated */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 int mh_magic4; /* should be == MAGIC4 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 /* Access free-list pointer of a block.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 It is stored at block + 4.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 This is not a field in the mhead structure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 because we want sizeof (struct mhead)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 to describe the overhead for when the block is in use,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 and we do not want the free-list pointer to count in that. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 #define CHAIN(a) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (*(struct mhead **) (sizeof (char *) + (char *) (a)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 /* To implement range checking, we write magic values in at the beginning and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 end of each allocated block, and make sure they are undisturbed whenever a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 free or a realloc occurs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 /* Written in each of the 4 bytes following the block's real space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 #define MAGIC1 0x55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 /* Written in the 4 bytes before the block's real space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 #define MAGIC4 0x55555555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 #define ASSERT(p) if (!(p)) botch("p"); else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 #define EXTRA 4 /* 4 bytes extra for MAGIC1s */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 #define ASSERT(p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 #define EXTRA 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 /* nextf[i] is free list of blocks of size 2**(i + 3) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 static struct mhead *nextf[30];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 /* busy[i] is nonzero while allocation of block size i is in progress. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 static char busy[30];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 /* Number of bytes of writable memory we can expect to be able to get */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 extern unsigned int lim_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 /* Level number of warnings already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 0 -- no warnings issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 1 -- 75% warning already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 2 -- 85% warning already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 static int warnlevel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 /* Function to call to issue a warning;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 0 means don't issue them. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 static void (*warnfunction) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 /* nonzero once initial bunch of free blocks made */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 static int gotpool;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 char *_malloc_base;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 static void getpool ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 /* Cause reinitialization based on job parameters;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 also declare where the end of pure storage is. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 malloc_init (start, warnfun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 char *start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 void (*warnfun) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 if (start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 data_space_start = start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 lim_data = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 warnlevel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 warnfunction = warnfun;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 /* Return the maximum size to which MEM can be realloc'd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 without actually requiring copying. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 malloc_usable_size (mem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 return blocksize - sizeof (struct mhead) - EXTRA;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 static void get_lim_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 morecore (nu) /* ask system for more memory */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 int nu; /* size index to get more of */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 char *cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 int nblks;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 unsigned int siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 int oldmask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 /* ?? There was a suggestion not to block SIGILL, somehow for GDB's sake. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 oldmask = sigsetmask (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 if (!data_space_start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 data_space_start = start_of_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 if (lim_data == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 get_lim_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 /* On initial startup, get two blocks of each size up to 1k bytes */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 if (!gotpool)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 { getpool (); getpool (); gotpool = 1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 /* Find current end of memory and issue warning if getting near max */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 #ifndef VMS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 /* Maximum virtual memory on VMS is difficult to calculate since it
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
366 * depends on several dynamically changing things. Also, alignment
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 * isn't that important. That is why much of the code here is ifdef'ed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 * out for VMS systems.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 cp = sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 siz = cp - data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 if (warnfunction)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 switch (warnlevel)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 if (siz > (lim_data / 4) * 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 (*warnfunction) ("Warning: past 75% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 if (siz > (lim_data / 20) * 17)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (*warnfunction) ("Warning: past 85% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 case 2:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 if (siz > (lim_data / 20) * 19)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (*warnfunction) ("Warning: past 95% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 if ((int) cp & 0x3ff) /* land on 1K boundaries */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 sbrk (1024 - ((int) cp & 0x3ff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 #endif /* not VMS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 /* Take at least 2k, and figure out how many blocks of the desired size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 we're about to get */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 nblks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 if ((siz = nu) < 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 nblks = 1 << ((siz = 8) - nu);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 sigsetmask (oldmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 return; /* no more room! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 malloc_sbrk_used = siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 malloc_sbrk_unused = lim_data - siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 #ifndef VMS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 if ((int) cp & 7)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 { /* shouldn't happen, but just in case */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 cp = (char *) (((int) cp + 8) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 nblks--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 #endif /* not VMS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 /* save new header and link the nblks blocks together */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 nextf[nu] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 siz = 1 << (nu + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 ((struct mhead *) cp) -> mh_index = nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 if (--nblks <= 0) break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 cp += siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 CHAIN ((struct mhead *) cp) = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 sigsetmask (oldmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 getpool ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 int nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 char *cp = sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 if ((int) cp & 0x3ff) /* land on 1K boundaries */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 sbrk (1024 - ((int) cp & 0x3ff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 /* Record address of start of space allocated by malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 if (_malloc_base == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 _malloc_base = cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 /* Get 2k of storage */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 cp = sbrk (04000);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 if (cp == (char *) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 /* Divide it into an initial 8-word block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 plus one block of size 2**nu for nu = 3 ... 10. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 CHAIN (cp) = nextf[0];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 nextf[0] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ((struct mhead *) cp) -> mh_index = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 cp += 8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 for (nu = 0; nu < 7; nu++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 CHAIN (cp) = nextf[nu];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 nextf[nu] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 ((struct mhead *) cp) -> mh_index = nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 cp += 8 << nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 malloc (n) /* get a block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 unsigned n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 unsigned int nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 int nunits = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 /* Figure out how many bytes are required, rounding up to the nearest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 multiple of 8, then figure out which nestf[] area to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 Both the beginning of the header and the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 block should be on an eight byte boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 unsigned int shiftr = (nbytes - 1) >> 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 while (shiftr >>= 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 nunits++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 /* In case this is reentrant use of malloc from signal handler,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 pick a block size that no other malloc level is currently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 trying to allocate. That's the easiest harmless way not to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 interfere with the other level of execution. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 while (busy[nunits]) nunits++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 busy[nunits] = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 /* If there are no blocks of the appropriate size, go get some */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 if (nextf[nunits] == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 morecore (nunits);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 /* Get one block off the list, and set the new list head */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 if ((p = nextf[nunits]) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 nextf[nunits] = CHAIN (p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 /* Check for free block clobbered */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 /* If not for this check, we would gobble a clobbered free chain ptr */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 /* and bomb out on the NEXT allocate of this size block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 botch ("block on free list clobbered");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 /* Fill in the info, and if range checking, set up the magic numbers */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 p -> mh_alloc = ISALLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 p -> mh_nbytes = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 p -> mh_magic4 = MAGIC4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 /* Get the location n after the beginning of the user's space. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 char *m = (char *) p + ((sizeof *p + 7) & ~7) + n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 p -> mh_size = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 nmalloc[nunits]++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 nmal++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 return (char *) p + ((sizeof *p + 7) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 free (mem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 char *ap = mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 if (ap == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 if (p -> mh_alloc == ISMEMALIGN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 ap -= p->mh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 #ifndef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 if (p -> mh_alloc != ISALLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 #else /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 if (p -> mh_alloc != ISALLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 if (p -> mh_alloc == ISFREE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 botch ("free: Called with already freed block argument\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 botch ("free: Called with bad argument\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 ASSERT (p -> mh_magic4 == MAGIC4);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 ap += p -> mh_nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 int nunits = p -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 ASSERT (nunits <= 29);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 p -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 /* Protect against signal handlers calling malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 busy[nunits] = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 /* Put this block on the free list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 CHAIN (p) = nextf[nunits];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 nextf[nunits] = p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 nmalloc[nunits]--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 nfre++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 realloc (mem, n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 unsigned n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 unsigned int tocopy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 unsigned int nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 int nunits;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 if (mem == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 return malloc (n);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 p = (struct mhead *) (mem - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 nunits = p -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 ASSERT (p -> mh_alloc == ISALLOC);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 ASSERT (p -> mh_magic4 == MAGIC4);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 char *m = mem + (tocopy = p -> mh_nbytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 if (p -> mh_index >= 13)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 tocopy = (1 << (p -> mh_index + 3)) - ((sizeof *p + 7) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 tocopy = p -> mh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 /* See if desired size rounds to same power of 2 as actual size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 /* If ok, use the same block, just marking its size as changed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 char *m = mem + tocopy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 p-> mh_nbytes = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 m = mem + n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 p -> mh_size = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 return mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 if (n < tocopy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 tocopy = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 char *new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 if ((new = malloc (n)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 memcpy (new, mem, tocopy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 free (mem);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 return new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 #ifndef VMS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 memalign (alignment, size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 unsigned alignment, size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 char *ptr = malloc (size + alignment);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 char *aligned;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 if (ptr == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 /* If entire block has the desired alignment, just accept it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 if (((int) ptr & (alignment - 1)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 return ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 /* Otherwise, get address of byte in the block that has that alignment. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 /* Store a suitable indication of how to free the block,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 so that free can find the true beginning of it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 p = (struct mhead *) aligned - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 p -> mh_size = aligned - ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 p -> mh_alloc = ISMEMALIGN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 return aligned;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 #ifndef __hpux
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 /* This runs into trouble with getpagesize on HPUX.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 Patching out seems cleaner than the ugly fix needed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 valloc (size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 unsigned size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 return memalign (getpagesize (), size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 #endif /* not __hpux */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 #endif /* not VMS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 /* Return statistics describing allocation of blocks of size 2**n. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 struct mstats_value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 int blocksize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 int nfree;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 int nused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 struct mstats_value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 malloc_stats (size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 int size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 struct mstats_value v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 v.nfree = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 if (size < 0 || size >= 30)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 v.blocksize = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 v.nused = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 return v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 v.blocksize = 1 << (size + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 v.nused = nmalloc[size];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 for (p = nextf[size]; p; p = CHAIN (p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 v.nfree++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 return v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 malloc_mem_used ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 int size_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 size_used = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 for (i = 0; i < 30; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 int allocation_size = 1 << (i + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 size_used += nmalloc[i] * allocation_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 return size_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 malloc_mem_free ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 int size_unused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 size_unused = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 for (i = 0; i < 30; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 int allocation_size = 1 << (i + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 for (p = nextf[i]; p ; p = CHAIN (p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 size_unused += allocation_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 return size_unused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 * This function returns the total number of bytes that the process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 * will be allowed to allocate via the sbrk(2) system call. On
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 * BSD systems this is the total space allocatable to stack and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 * data. On USG systems this is the data space only.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 get_lim_data ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 extern long ulimit ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 #ifdef ULIMIT_BREAK_VALUE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 lim_data = ULIMIT_BREAK_VALUE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 lim_data = ulimit (3, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 lim_data -= (long) data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 #else /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 #ifndef BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 get_lim_data ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 lim_data = vlimit (LIM_DATA, -1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 #else /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 get_lim_data ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826 struct rlimit XXrlimit;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 getrlimit (RLIMIT_DATA, &XXrlimit);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 #ifdef RLIM_INFINITY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 lim_data = XXrlimit.rlim_cur; /* soft limit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 #endif /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 #endif /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 #ifdef VMS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 /* There is a problem when dumping and restoring things on VMS. Calls
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 * to SBRK don't necessarily result in contiguous allocation. Dumping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 * doesn't work when it isn't. Therefore, we make the initial
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 * allocation contiguous by allocating a big chunk, and do SBRKs from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 * there. Once Emacs has dumped there is no reason to continue
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 * contiguous allocation, malloc doesn't depend on it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 * There is a further problem of using brk and sbrk while using VMS C
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 * run time library routines malloc, calloc, etc. The documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 * says that this is a no-no, although I'm not sure why this would be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 * a problem. In any case, we remove the necessity to call brk and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 * sbrk, by calling calloc (to assure zero filled data) rather than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 * sbrk.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 * VMS_ALLOCATION_SIZE is the size of the allocation array. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 * should be larger than the malloc size before dumping. Making this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 * too large will result in the startup procedure slowing down since
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 * it will require more space and time to map it in.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 * The value for VMS_ALLOCATION_SIZE in the following define was determined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 * by running emacs linked (and a large allocation) with the debugger and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 * looking to see how much storage was used. The allocation was 201 pages,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 * so I rounded it up to a power of two.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 #ifndef VMS_ALLOCATION_SIZE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 #define VMS_ALLOCATION_SIZE (512*256)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 /* Use VMS RTL definitions */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 #undef sbrk
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 #undef brk
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 #undef malloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 int vms_out_initial = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 char vms_initial_buffer[VMS_ALLOCATION_SIZE];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 static char *vms_current_brk = &vms_initial_buffer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 static char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 sys_sbrk (incr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 int incr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 char *sbrk(), *temp, *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 if (vms_out_initial)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 /* out of initial allocation... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 if (!(temp = malloc (incr)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 temp = (char *) -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 /* otherwise, go out of our area */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 ptr = vms_current_brk + incr; /* new current_brk */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 if (ptr <= vms_end_brk)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 temp = vms_current_brk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 vms_current_brk = ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 vms_out_initial = 1; /* mark as out of initial allocation */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 if (!(temp = malloc (incr)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 temp = (char *) -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 return temp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 #endif /* VMS */