annotate src/malloc.c @ 288:e11d67e05968 r21-0b42

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