annotate src/malloc.c @ 169:15872534500d r20-3b11

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