annotate src/malloc.c @ 388:aabb7f5b1c81 r21-2-9

Import from CVS: tag r21-2-9
author cvs
date Mon, 13 Aug 2007 11:09:42 +0200
parents 8626e4521993
children 7d59cb494b73
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 USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 #define USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 #else /* SIGTSTP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 #ifdef SIGIO
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 #define BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 #endif /* SIGIO */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 #endif /* SIGTSTP */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 #if defined(hpux)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 #define USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 #endif /* not emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
375
a300bb07d72d Import from CVS: tag r21-2b3
cvs
parents: 371
diff changeset
169 #include <stddef.h>
a300bb07d72d Import from CVS: tag r21-2b3
cvs
parents: 371
diff changeset
170
0
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
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
174 #ifdef HAVE_ULIMIT_H
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
175 #include <ulimit.h>
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
176 #endif
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
177
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 #ifndef BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 #ifndef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 #include <sys/vlimit.h> /* warn the user when near the end */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #endif /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 #else /* if BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 #include <sys/time.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 #include <sys/resource.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 #endif /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 375
diff changeset
187 #ifdef __STDC__
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 #ifndef HPUX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 /* not sure where this for NetBSD should really go
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 and it probably applies to other systems */
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 209
diff changeset
191 #if !defined(__NetBSD__) && !defined(__bsdi__) && !defined(__OpenBSD__)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 extern void *sbrk (ptrdiff_t);
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 char *sbrk ();
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 209
diff changeset
195 #endif /* __NetBSD__ or __OpenBSD__ */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 #endif /* HPUX */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 extern void *sbrk ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 #endif /* __STDC__ */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 extern char *start_of_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 #ifndef DATA_SEG_BITS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 #define start_of_data() &etext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 #endif
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 #ifndef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 #define start_of_data() &etext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 #define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 #define ISFREE ((char) 0x54) /* magic byte that implies free block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 /* this is for error checking only */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 #define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 memalign, with the rest of the word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 being the distance to the true
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 beginning of the block. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 extern char etext;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 /* These two are for user programs to look at, when they are interested. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 unsigned int malloc_sbrk_used; /* amount of data space used now */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 unsigned int malloc_sbrk_unused; /* amount more we can have */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 /* start of data space; can be changed by calling init_malloc */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 static char *data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 static int nmalloc[30];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 static int nmal, nfre;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 /* 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
237 whether memory is allocated, an index in nextf[], and a size field; to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 on whether the former can hold the exact size (given the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 '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
241 is allocated, so the 'size' field is never used. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 struct mhead {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 char mh_alloc; /* ISALLOC or ISFREE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 char mh_index; /* index in nextf[] */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 /* Remainder are valid only when block is allocated */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 unsigned short mh_size; /* size, if < 0x10000 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 unsigned mh_nbytes; /* number of bytes allocated */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 int mh_magic4; /* should be == MAGIC4 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 /* Access free-list pointer of a block.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 It is stored at block + 4.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 This is not a field in the mhead structure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 because we want sizeof (struct mhead)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 to describe the overhead for when the block is in use,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 and we do not want the free-list pointer to count in that. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 #define CHAIN(a) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (*(struct mhead **) (sizeof (char *) + (char *) (a)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 /* To implement range checking, we write magic values in at the beginning and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 end of each allocated block, and make sure they are undisturbed whenever a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 free or a realloc occurs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 /* Written in each of the 4 bytes following the block's real space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 #define MAGIC1 0x55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 /* Written in the 4 bytes before the block's real space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 #define MAGIC4 0x55555555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 #define ASSERT(p) if (!(p)) botch("p"); else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 #define EXTRA 4 /* 4 bytes extra for MAGIC1s */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 #define ASSERT(p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 #define EXTRA 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 /* nextf[i] is free list of blocks of size 2**(i + 3) */
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 struct mhead *nextf[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 /* busy[i] is nonzero while allocation of block size i is in progress. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 static char busy[30];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 /* 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
290 extern unsigned int lim_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 /* Level number of warnings already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 0 -- no warnings issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 1 -- 75% warning already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 2 -- 85% warning already issued.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 static int warnlevel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 /* Function to call to issue a warning;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 0 means don't issue them. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 static void (*warnfunction) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 /* nonzero once initial bunch of free blocks made */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 static int gotpool;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 char *_malloc_base;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
308 static void getpool (void);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 /* Cause reinitialization based on job parameters;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 also declare where the end of pure storage is. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 malloc_init (start, warnfun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 char *start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 void (*warnfun) ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 if (start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 data_space_start = start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 lim_data = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 warnlevel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 warnfunction = warnfun;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 /* Return the maximum size to which MEM can be realloc'd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 without actually requiring copying. */
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 malloc_usable_size (mem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 return blocksize - sizeof (struct mhead) - EXTRA;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 static void get_lim_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 morecore (nu) /* ask system for more memory */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 int nu; /* size index to get more of */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 char *cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 int nblks;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 unsigned int siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 int oldmask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 /* ?? There was a suggestion not to block SIGILL, somehow for GDB's sake. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 oldmask = sigsetmask (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 if (!data_space_start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 data_space_start = start_of_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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 if (lim_data == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 get_lim_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 /* On initial startup, get two blocks of each size up to 1k bytes */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 if (!gotpool)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 { getpool (); getpool (); gotpool = 1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 /* Find current end of memory and issue warning if getting near max */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 cp = sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 siz = cp - data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 if (warnfunction)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 switch (warnlevel)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 if (siz > (lim_data / 4) * 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (*warnfunction) ("Warning: past 75% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 if (siz > (lim_data / 20) * 17)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (*warnfunction) ("Warning: past 85% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 case 2:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 if (siz > (lim_data / 20) * 19)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 warnlevel++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (*warnfunction) ("Warning: past 95% of memory limit");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 if ((int) cp & 0x3ff) /* land on 1K boundaries */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 sbrk (1024 - ((int) cp & 0x3ff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 /* 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
401 we're about to get */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 nblks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 if ((siz = nu) < 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 nblks = 1 << ((siz = 8) - nu);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 sigsetmask (oldmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 return; /* no more room! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 malloc_sbrk_used = siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 malloc_sbrk_unused = lim_data - siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 if ((int) cp & 7)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 { /* shouldn't happen, but just in case */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 cp = (char *) (((int) cp + 8) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 nblks--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 /* save new header and link the nblks blocks together */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 nextf[nu] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 siz = 1 << (nu + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 ((struct mhead *) cp) -> mh_index = nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 if (--nblks <= 0) break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 cp += siz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 CHAIN ((struct mhead *) cp) = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 #ifdef BSD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 #ifndef BSD4_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 sigsetmask (oldmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 static void
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 2
diff changeset
445 getpool (void)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 int nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 char *cp = sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 if ((int) cp & 0x3ff) /* land on 1K boundaries */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 sbrk (1024 - ((int) cp & 0x3ff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 /* Record address of start of space allocated by malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 if (_malloc_base == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 _malloc_base = cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 /* Get 2k of storage */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 cp = sbrk (04000);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 if (cp == (char *) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 /* Divide it into an initial 8-word block
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 plus one block of size 2**nu for nu = 3 ... 10. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 CHAIN (cp) = nextf[0];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 nextf[0] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 ((struct mhead *) cp) -> mh_index = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 cp += 8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 for (nu = 0; nu < 7; nu++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 CHAIN (cp) = nextf[nu];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 nextf[nu] = (struct mhead *) cp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 ((struct mhead *) cp) -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 ((struct mhead *) cp) -> mh_index = nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 cp += 8 << nu;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 malloc (n) /* get a block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 unsigned n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 unsigned int nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 int nunits = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 /* Figure out how many bytes are required, rounding up to the nearest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 multiple of 8, then figure out which nestf[] area to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 Both the beginning of the header and the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 block should be on an eight byte boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 unsigned int shiftr = (nbytes - 1) >> 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 while (shiftr >>= 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 nunits++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 /* In case this is reentrant use of malloc from signal handler,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 pick a block size that no other malloc level is currently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 trying to allocate. That's the easiest harmless way not to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 interfere with the other level of execution. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 while (busy[nunits]) nunits++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 busy[nunits] = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 /* If there are no blocks of the appropriate size, go get some */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 if (nextf[nunits] == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 morecore (nunits);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 /* Get one block off the list, and set the new list head */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 if ((p = nextf[nunits]) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 nextf[nunits] = CHAIN (p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 /* Check for free block clobbered */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 /* If not for this check, we would gobble a clobbered free chain ptr */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 /* and bomb out on the NEXT allocate of this size block */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 botch ("block on free list clobbered");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 /* Fill in the info, and if range checking, set up the magic numbers */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 p -> mh_alloc = ISALLOC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 p -> mh_nbytes = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 p -> mh_magic4 = MAGIC4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 /* Get the location n after the beginning of the user's space. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 char *m = (char *) p + ((sizeof *p + 7) & ~7) + n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 p -> mh_size = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 nmalloc[nunits]++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 nmal++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 return (char *) p + ((sizeof *p + 7) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 free (mem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 char *ap = mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 if (ap == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 if (p -> mh_alloc == ISMEMALIGN)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 ap -= p->mh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 #ifndef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 if (p -> mh_alloc != ISALLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 #else /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 if (p -> mh_alloc != ISALLOC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 if (p -> mh_alloc == ISFREE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 botch ("free: Called with already freed block argument\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 botch ("free: Called with bad argument\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 ASSERT (p -> mh_magic4 == MAGIC4);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 ap += p -> mh_nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 #endif /* rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 int nunits = p -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 ASSERT (nunits <= 29);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 p -> mh_alloc = ISFREE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 /* Protect against signal handlers calling malloc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 busy[nunits] = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 /* Put this block on the free list. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 CHAIN (p) = nextf[nunits];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 nextf[nunits] = p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 busy[nunits] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 nmalloc[nunits]--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 nfre++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 realloc (mem, n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 char *mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 unsigned n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 unsigned int tocopy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 unsigned int nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 int nunits;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 if (mem == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 return malloc (n);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 p = (struct mhead *) (mem - ((sizeof *p + 7) & ~7));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 nunits = p -> mh_index;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 ASSERT (p -> mh_alloc == ISALLOC);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 ASSERT (p -> mh_magic4 == MAGIC4);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 char *m = mem + (tocopy = p -> mh_nbytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 if (p -> mh_index >= 13)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 tocopy = (1 << (p -> mh_index + 3)) - ((sizeof *p + 7) & ~7);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 tocopy = p -> mh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 /* See if desired size rounds to same power of 2 as actual size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 /* If ok, use the same block, just marking its size as changed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 #ifdef rcheck
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 char *m = mem + tocopy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 p-> mh_nbytes = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 m = mem + n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 #else /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 p -> mh_size = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 #endif /* not rcheck */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 return mem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 if (n < tocopy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 tocopy = n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 char *new;
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 ((new = malloc (n)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 memcpy (new, mem, tocopy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 free (mem);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 return new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 memalign (alignment, size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 unsigned alignment, size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 char *ptr = malloc (size + alignment);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 char *aligned;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 if (ptr == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 /* If entire block has the desired alignment, just accept it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 if (((int) ptr & (alignment - 1)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 return ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 /* Otherwise, get address of byte in the block that has that alignment. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 /* Store a suitable indication of how to free the block,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 so that free can find the true beginning of it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 p = (struct mhead *) aligned - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 p -> mh_size = aligned - ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 p -> mh_alloc = ISMEMALIGN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 return aligned;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 #ifndef __hpux
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 /* This runs into trouble with getpagesize on HPUX.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 Patching out seems cleaner than the ugly fix needed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 char *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 valloc (size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 unsigned size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 return memalign (getpagesize (), size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 #endif /* not __hpux */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 #ifdef MSTATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 /* Return statistics describing allocation of blocks of size 2**n. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 struct mstats_value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 int blocksize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 int nfree;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 int nused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 };
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 malloc_stats (size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 int size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 struct mstats_value v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 v.nfree = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 if (size < 0 || size >= 30)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 v.blocksize = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 v.nused = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 return v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 v.blocksize = 1 << (size + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 v.nused = nmalloc[size];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 for (p = nextf[size]; p; p = CHAIN (p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 v.nfree++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 return v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 malloc_mem_used ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 int size_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 size_used = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 for (i = 0; i < 30; i++)
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 allocation_size = 1 << (i + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 struct mhead *p;
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 += nmalloc[i] * allocation_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 return size_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 malloc_mem_free ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 int size_unused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 size_unused = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 for (i = 0; i < 30; i++)
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 allocation_size = 1 << (i + 3);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 struct mhead *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 for (p = nextf[i]; p ; p = CHAIN (p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 size_unused += allocation_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 return size_unused;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 #endif /* MSTATS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 * This function returns the total number of bytes that the process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 * will be allowed to allocate via the sbrk(2) system call. On
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 * BSD systems this is the total space allocatable to stack and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 * data. On USG systems this is the data space only.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 get_lim_data ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 #ifdef ULIMIT_BREAK_VALUE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 lim_data = ULIMIT_BREAK_VALUE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 lim_data = ulimit (3, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 lim_data -= (long) data_space_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 #else /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 #ifndef BSD4_2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 get_lim_data ()
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 = vlimit (LIM_DATA, -1);
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 /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 get_lim_data ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 struct rlimit XXrlimit;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 getrlimit (RLIMIT_DATA, &XXrlimit);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 #ifdef RLIM_INFINITY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 lim_data = XXrlimit.rlim_cur; /* soft limit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 #endif /* BSD4_2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 #endif /* not USG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829