annotate src/gmalloc.c @ 5927:b58b74274fa2 cygwin

changes from long ago, never committed...
author Henry Thompson <ht@markup.co.uk>
date Wed, 15 Dec 2021 19:02:33 +0000
parents 2aa9cd456ae7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Synched up with: Not synched up with FSF 19.28, even though I
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 thought I did so. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 /* Get the configuration files if we're being compiled for Emacs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 #ifdef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 # include <config.h>
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
7 # include "lisp.h"
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
8
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
9 # include "sysdep.h"
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 # include "getpagesize.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 # ifndef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 # define HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 #if defined (__STDC__) && !defined (STDC_HEADERS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 /* The ANSI standard says that defining __STDC__ to a non-zero value means
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 that the compiler conforms to that standard. The standard requires
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 certain header files and library functions to be present. Therefore,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 if your compiler defines __STDC__ to non-0 but does not have ANSI headers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 and the ANSI library routines, then your compiler is buggy. Conversely,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 an ANSI-conforming environment (which has both the ANSI headers and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 library routines, i.e., stdlib.h and `memmove') does not necessarily
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 define the STDC_HEADERS flag. Lucid Emacs requires an ANSI compiler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 Therefore, there is no need to consult the abominable STDC_HEADERS flag.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 -- jwz
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 # define STDC_HEADERS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 /* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 /* Bwaa-haa-haa! Not a chance that this is actually true! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 /* The malloc headers and source files from the C library follow here. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 /* Declarations for `malloc' and friends.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 Written May 1989 by Mike Haertel.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
43 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
44 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
45 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
46 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
48 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
49 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
50 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
51 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
54 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 #ifndef _MALLOC_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #define _MALLOC_H 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 #ifdef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 #include <limits.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 #ifdef HAVE_UNISTD_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 #endif /* _MALLOC_INTERNAL. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 771
diff changeset
79 #if defined (__cplusplus) && !defined (emacs)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 extern "C"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 #undef __P
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 #define __P(args) args
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 #undef __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 #define __ptr_t void *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 #include <stddef.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 #define __malloc_size_t size_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 #ifndef NULL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 #define NULL 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 /* XEmacs: I thought this should be int under SunOS, but that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 apparently fails. Curses on all this shit. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 #define __free_ret_t void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99
551
e9a3f8b4de53 [xemacs-hg @ 2001-05-21 05:26:06 by martinb]
martinb
parents: 452
diff changeset
100 #undef malloc
e9a3f8b4de53 [xemacs-hg @ 2001-05-21 05:26:06 by martinb]
martinb
parents: 452
diff changeset
101 #undef realloc
e9a3f8b4de53 [xemacs-hg @ 2001-05-21 05:26:06 by martinb]
martinb
parents: 452
diff changeset
102 #undef calloc
e9a3f8b4de53 [xemacs-hg @ 2001-05-21 05:26:06 by martinb]
martinb
parents: 452
diff changeset
103 #undef free
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 /* XEmacs: I tried commenting these out and including stdlib.h,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 but that fails badly. Urk! This sucks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 /* Allocate SIZE bytes of memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 extern __ptr_t malloc __P ((size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 /* Re-allocate the previously allocated block
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 in __ptr_t, making the new block SIZE bytes long. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 extern __free_ret_t free __P ((__ptr_t __ptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 /* Allocate SIZE bytes on a page boundary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 extern __ptr_t valloc __P ((size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 #ifdef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 /* The allocator divides the heap into blocks of fixed size; large
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 requests receive one or more whole blocks, and small requests
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 receive a fragment of a block. Fragment sizes are powers of two,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 and all fragments of a block are the same size. When all the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 fragments in a block have been freed, the block itself is freed. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 #define INT_BIT (CHAR_BIT * sizeof(int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 #define BLOCKSIZE (1 << BLOCKLOG)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 /* Determine the amount of memory spanned by the initial heap table
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 (not an absolute limit). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 /* Number of contiguous free blocks allowed to build up at the end of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 memory before they will be returned to the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 #define FINAL_FREE_BLOCKS 8
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 /* Data structure giving per-block information. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 typedef union
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 /* Heap information for a busy block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 struct
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 /* Zero for a large block, or positive giving the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 logarithm to the base two of the fragment size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 int type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 union
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 struct
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 __malloc_size_t nfree; /* Free frags in a fragmented block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 __malloc_size_t first; /* First free fragment of the block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 } frag;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 /* Size (in blocks) of a large cluster. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 __malloc_size_t size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 } info;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 } busy;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 /* Heap information for a free block
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 (that may be the first of a free cluster). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 struct
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 __malloc_size_t size; /* Size (in blocks) of a free cluster. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 __malloc_size_t next; /* Index of next free cluster. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 __malloc_size_t prev; /* Index of previous free cluster. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 } free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 } malloc_info;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 /* Pointer to first block of the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 extern char *_heapbase;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 /* Table indexed by block number giving per-block information. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 extern malloc_info *_heapinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 /* Address to block number and vice versa. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 /* Current search index for the heap table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 extern __malloc_size_t _heapindex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 /* Limit of valid info table indices. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 extern __malloc_size_t _heaplimit;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 /* Doubly linked lists of free fragments. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 struct list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 struct list *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 struct list *prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 /* Free list headers for each fragment size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 extern struct list _fraghead[];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 /* List of blocks allocated with `memalign' (or `valloc'). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 struct alignlist
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 struct alignlist *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 __ptr_t aligned; /* The address that memaligned returned. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 __ptr_t exact; /* The address that malloc returned. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 extern struct alignlist *_aligned_blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 /* Instrumentation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 extern __malloc_size_t _chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 extern __malloc_size_t _bytes_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 extern __malloc_size_t _chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 extern __malloc_size_t _bytes_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 /* Internal version of `free' used in `morecore' (malloc.c). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 extern void _free_internal __P ((__ptr_t __ptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 #endif /* _MALLOC_INTERNAL. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 /* Underlying allocation function; successive calls should
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 return contiguous pieces of memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 /* Default value of `__morecore'. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 /* If not NULL, this function is called after each time
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 `__morecore' is called to increase the data size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 extern void (*__after_morecore_hook) __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 /* Nonzero if `malloc' has been called and done its initialization. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 /* extern int __malloc_initialized; */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 /* Hooks for debugging versions. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 extern void (*__free_hook) __P ((__ptr_t __ptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 extern __ptr_t (*__malloc_hook) __P ((size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 /* Return values for `mprobe': these are the kinds of inconsistencies that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 `mcheck' enables detection of. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 enum mcheck_status
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 MCHECK_OK, /* Block is fine. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 MCHECK_FREE, /* Block freed twice. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 MCHECK_HEAD, /* Memory before the block was clobbered. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 MCHECK_TAIL /* Memory after the block was clobbered. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 /* Activate a standard collection of debugging hooks. This must be called
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 before `malloc' is ever called. ABORTFUNC is called with an error code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 (see enum above) when an inconsistency is detected. If ABORTFUNC is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 null, the standard function prints on stderr and then calls `abort'. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 /* Check for aberrations in a particular malloc'd block. You must have
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 called `mcheck' already. These are the same checks that `mcheck' does
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 when you free or reallocate a block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 /* Activate a standard collection of tracing hooks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 extern void mtrace __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 extern void muntrace __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 /* Statistics available to the user. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 struct mstats
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 __malloc_size_t bytes_total; /* Total size of the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 __malloc_size_t chunks_used; /* Chunks allocated by the user. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 __malloc_size_t chunks_free; /* Chunks in the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 /* Pick up the current statistics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 extern struct mstats mstats __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 771
diff changeset
277 #if defined (__cplusplus) && !defined (emacs)
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
278 }
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
279 #endif
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
280
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 /* Call WARNFUN with a warning message when memory usage is high. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 extern void memory_warnings __P ((__ptr_t __start,
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
283 void (*__warnfun) __P ((const char *))));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 #if 0 /* unused in this file, and conflicting prototypes anyway */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 /* Relocating allocator. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 /* Free the storage allocated in HANDLEPTR. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 extern void r_alloc_free __P ((__ptr_t *__handleptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 #endif /* 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 #endif /* malloc.h */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 /* Allocate memory on a page boundary.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
303 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
304 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
305 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
306 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
308 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
309 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
310 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
311 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
314 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 #if defined (__GNU_LIBRARY__) || defined (_LIBC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 #include <stddef.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 #include <sys/cdefs.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 #if ! (defined (__GLIBC__) && (__GLIBC__ >= 2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 extern size_t __getpagesize __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 #include "getpagesize.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 #define __getpagesize() getpagesize()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 static __malloc_size_t pagesize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 valloc (__malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 if (pagesize == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 pagesize = __getpagesize ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 return memalign (pagesize, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 /* Memory allocator `malloc'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 Written May 1989 by Mike Haertel.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
349 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
350 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
351 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
352 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
354 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
355 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
356 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
357 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
360 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 /* How to really get more memory. */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
371 #if defined (HEAP_IN_DATA) && !defined(PDUMP)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 /* once dumped, free() & realloc() on static heap space will fail */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 #define PURE_DATA(x) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 ((static_heap_dumped && (char*)x >= static_heap_base \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 && (char*)x <= (static_heap_base + static_heap_size) ) ? 1 : 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 extern int initialized;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 extern int purify_flag;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 extern char* static_heap_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 extern char* static_heap_ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 extern char* static_heap_dumped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 extern unsigned long static_heap_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 extern __ptr_t more_static_core __P ((ptrdiff_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = more_static_core;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 #define PURE_DATA(x) 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 /* Debugging hook for `malloc'. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 /* Pointer to the base of the first block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 char *_heapbase;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 /* Block information table. Allocated with align/__free (not malloc/free). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 malloc_info *_heapinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 /* Number of info entries. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 static __malloc_size_t heapsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 /* Search index in the info table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 __malloc_size_t _heapindex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 /* Limit of valid info table indices. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 __malloc_size_t _heaplimit;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 /* Free lists for each fragment size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 struct list _fraghead[BLOCKLOG];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 /* Instrumentation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 __malloc_size_t _chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 __malloc_size_t _bytes_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 __malloc_size_t _chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 __malloc_size_t _bytes_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 /* Are you experienced? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 int __malloc_initialized;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 void (*__after_morecore_hook) __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 /* Aligned allocation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 static __ptr_t align __P ((__malloc_size_t));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 static __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 align (__malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 __ptr_t result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 unsigned long int adj;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 result = (*__morecore) (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 adj = (unsigned long int) ((unsigned long int) ((char *) result -
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 (char *) NULL)) % BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 if (adj != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 adj = BLOCKSIZE - adj;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 (void) (*__morecore) (adj);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 result = (char *) result + adj;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 if (__after_morecore_hook)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 (*__after_morecore_hook) ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 /* Set everything up and remember that we have. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 static int initialize __P ((void));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 initialize ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 {
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
450 #if defined (HEAP_IN_DATA) && !defined(PDUMP)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 if (static_heap_dumped && __morecore == more_static_core)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 __morecore = __default_morecore;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 heapsize = HEAP / BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 if (_heapinfo == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 memset (_fraghead, 0, BLOCKLOG * sizeof (struct list));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 _heapinfo[0].free.size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 _heapindex = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 _heaplimit = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 _heapbase = (char *) _heapinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 /* Account for the _heapinfo block itself in the statistics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 _bytes_used = heapsize * sizeof (malloc_info);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 _chunks_used = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 _chunks_free=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 _bytes_free=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 _aligned_blocks=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 __malloc_initialized = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 /* Get neatly aligned memory, initializing or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 growing the heap info table as necessary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 static __ptr_t morecore __P ((__malloc_size_t));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 static __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 morecore (__malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 __ptr_t result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 malloc_info *newinfo, *oldinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 __malloc_size_t newsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 result = align (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 /* Check if we need to grow the info table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 newsize = heapsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 newsize *= 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 if (newinfo == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 (*__morecore) (-(int)size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 memset (&newinfo[heapsize], 0,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (newsize - heapsize) * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 oldinfo = _heapinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 newinfo[BLOCK (oldinfo)].busy.type = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 newinfo[BLOCK (oldinfo)].busy.info.size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 = BLOCKIFY (heapsize * sizeof (malloc_info));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 _heapinfo = newinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 /* Account for the _heapinfo block itself in the statistics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 _bytes_used += newsize * sizeof (malloc_info);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 ++_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 _free_internal (oldinfo);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 heapsize = newsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 _heaplimit = BLOCK ((char *) result + size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 /* Allocate memory from the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 malloc (__malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 __ptr_t result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 __malloc_size_t block, blocks, lastblocks, start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 __malloc_size_t i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 struct list *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 /* ANSI C allows `malloc (0)' to either return NULL, or to return a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 valid address you can realloc and free (though not dereference).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 It turns out that some extant code (sunrpc, at least Ultrix's version)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 expects `malloc (0)' to return non-NULL and breaks otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 Be compatible. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 #ifdef HAVE_X_WINDOWS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 /* there is at least one Xt bug where calloc(n,x) is blindly called
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 where n can be 0, and yet if 0 is returned, Xt barfs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 if (size == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 size = sizeof (struct list);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 if (size == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 if (__malloc_hook != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 return (*__malloc_hook) (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 if (!__malloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 if (!initialize ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 #ifdef SUNOS_LOCALTIME_BUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 /* Workaround for localtime() allocating 8 bytes and writing 9 bug... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 if (size < 16)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 size = 16;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 if (size < sizeof (struct list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 size = sizeof (struct list);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 /* Determine the allocation policy based on the request size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 if (size <= BLOCKSIZE / 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 /* Small allocation to receive a fragment of a block.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 Determine the logarithm to base two of the fragment size. */
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
571 __malloc_size_t log2 = 1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 --size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 while ((size /= 2) != 0)
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
574 ++log2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 /* Look in the fragment lists for a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 free fragment of the desired size. */
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
578 next = _fraghead[log2].next;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 if (next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 /* There are free fragments of this size.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 Pop a fragment out of the fragment list and return it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 Update the block's nfree and first counters. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 result = (__ptr_t) next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 next->prev->next = next->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 if (next->next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 next->next->prev = next->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 block = BLOCK (result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 if (--_heapinfo[block].busy.info.frag.nfree != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 _heapinfo[block].busy.info.frag.first = (unsigned long int)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 ((unsigned long int) ((char *) next->next - (char *) NULL)
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
592 % BLOCKSIZE) >> log2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 /* Update the statistics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 ++_chunks_used;
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
596 _bytes_used += 1 << log2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 --_chunks_free;
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
598 _bytes_free -= 1 << log2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 /* No free fragments of the desired size, so get a new block
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 and break it into fragments, returning the first. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 result = malloc (BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 /* Link all fragments but the first into the free list. */
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
609 for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> log2); ++i)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 {
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
611 next = (struct list *) ((char *) result + (i << log2));
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
612 next->next = _fraghead[log2].next;
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
613 next->prev = &_fraghead[log2];
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 next->prev->next = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 if (next->next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 next->next->prev = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 /* Initialize the nfree and first counters for this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 block = BLOCK (result);
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
621 _heapinfo[block].busy.type = log2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 _heapinfo[block].busy.info.frag.nfree = i - 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 _heapinfo[block].busy.info.frag.first = i - 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624
2312
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
625 _chunks_free += (BLOCKSIZE >> log2) - 1;
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
626 _bytes_free += BLOCKSIZE - (1 << log2);
ed6535aefb7e [xemacs-hg @ 2004-09-27 18:51:20 by james]
james
parents: 1204
diff changeset
627 _bytes_used -= BLOCKSIZE - (1 << log2);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 /* Large allocation to receive one or more blocks.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 Search the free list in a circle starting at the last place visited.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 If we loop completely around without finding a large enough
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 space we will have to get more memory from the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 blocks = BLOCKIFY (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 start = block = _heapindex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 while (_heapinfo[block].free.size < blocks)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 block = _heapinfo[block].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 if (block == start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 /* Need to get more from the system. Check to see if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 the new core will be contiguous with the final free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 block; if so we don't need to get as much. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 block = _heapinfo[0].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 lastblocks = _heapinfo[block].free.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 (*__morecore) (0) == ADDRESS (block + lastblocks) &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 /* Which block we are extending (the `final free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 block' referred to above) might have changed, if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 it got combined with a freed info table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 block = _heapinfo[0].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 _heapinfo[block].free.size += (blocks - lastblocks);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 result = morecore (blocks * BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 block = BLOCK (result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 _heapinfo[block].busy.type = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 _heapinfo[block].busy.info.size = blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 ++_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 _bytes_used += blocks * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 /* At this point we have found a suitable free list entry.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 Figure out how to remove what we need from the list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 result = ADDRESS (block);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 if (_heapinfo[block].free.size > blocks)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 /* The block we found has a bit left over,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 so relink the tail end back into the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 _heapinfo[block + blocks].free.size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 = _heapinfo[block].free.size - blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 _heapinfo[block + blocks].free.next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 = _heapinfo[block].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 _heapinfo[block + blocks].free.prev
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 = _heapinfo[block].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 _heapinfo[_heapinfo[block].free.prev].free.next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 = _heapinfo[_heapinfo[block].free.next].free.prev
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 = _heapindex = block + blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 /* The block exactly matches our requirements,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 so just remove it from the list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 _heapinfo[_heapinfo[block].free.next].free.prev
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 = _heapinfo[block].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 _heapinfo[_heapinfo[block].free.prev].free.next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 = _heapindex = _heapinfo[block].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 --_chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 _heapinfo[block].busy.type = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 _heapinfo[block].busy.info.size = blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 ++_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 _bytes_used += blocks * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 _bytes_free -= blocks * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 #ifndef _LIBC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 /* On some ANSI C systems, some libc functions call _malloc, _free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 and _realloc. Make them use the GNU functions. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 __ptr_t _malloc (__malloc_size_t size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 _malloc (__malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 return malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 void _free (__ptr_t ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 _free (__ptr_t ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 __ptr_t _realloc (__ptr_t ptr, __malloc_size_t size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 _realloc (__ptr_t ptr, __malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 return realloc (ptr, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 /* Free a block of memory allocated by `malloc'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 Copyright 1990, 1991, 1992, 1994 Free Software Foundation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 Written May 1989 by Mike Haertel.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
741 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
742 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
743 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
744 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
746 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
747 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
748 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
749 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
752 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 /* Debugging hook for free. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 void (*__free_hook) __P ((__ptr_t __ptr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 /* List of blocks allocated by memalign. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 struct alignlist *_aligned_blocks = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 /* Return memory to the heap.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 Like `free' but don't call a __free_hook if there is one. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 _free_internal (__ptr_t ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 int type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 __malloc_size_t block, blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 __malloc_size_t i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 struct list *prev, *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 block = BLOCK (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 type = _heapinfo[block].busy.type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 switch (type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 case 0:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 /* Get as many statistics as early as we can. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 --_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 /* Find the free cluster previous to this one in the free list.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 Start searching at the last block referenced; this may benefit
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 programs with locality of allocation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 i = _heapindex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 if (i > block)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 while (i > block)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
795 i = _heapinfo[i].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 do
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 i = _heapinfo[i].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800 while (i > 0 && i < block);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 i = _heapinfo[i].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 /* Determine how to link this block into the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 if (block == i + _heapinfo[i].free.size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 /* Coalesce this block with its predecessor. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 block = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813 /* Really link this block back into the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 _heapinfo[block].free.next = _heapinfo[i].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 _heapinfo[block].free.prev = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 _heapinfo[i].free.next = block;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 _heapinfo[_heapinfo[block].free.next].free.prev = block;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 ++_chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 /* Now that the block is linked in, see if we can coalesce it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 with its successor (by deleting its successor from the list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824 and adding in its size). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 _heapinfo[block].free.size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 += _heapinfo[_heapinfo[block].free.next].free.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 _heapinfo[block].free.next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 = _heapinfo[_heapinfo[block].free.next].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 _heapinfo[_heapinfo[block].free.next].free.prev = block;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 --_chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 /* Now see if we can return stuff to the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 blocks = _heapinfo[block].free.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 && (*__morecore) (0) == ADDRESS (block + blocks))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 __malloc_size_t bytes = blocks * BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 _heaplimit -= blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 (*__morecore) (-(int)bytes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 _heapinfo[_heapinfo[block].free.prev].free.next
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 = _heapinfo[block].free.next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 _heapinfo[_heapinfo[block].free.next].free.prev
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 = _heapinfo[block].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 block = _heapinfo[block].free.prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 --_chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 _bytes_free -= bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 /* Set the next search to begin at this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 _heapindex = block;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 /* Do some of the statistics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 --_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 _bytes_used -= 1 << type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 ++_chunks_free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861 _bytes_free += 1 << type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863 /* Get the address of the first free fragment in this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864 prev = (struct list *) ((char *) ADDRESS (block) +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 (_heapinfo[block].busy.info.frag.first << type));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866
647
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 551
diff changeset
867 if (_heapinfo[block].busy.info.frag.nfree ==
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 551
diff changeset
868 (__malloc_size_t) ((BLOCKSIZE >> type) - 1))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870 /* If all fragments of this block are free, remove them
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 from the fragment list and free the whole block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 next = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 next = next->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 prev->prev->next = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876 if (next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 next->prev = prev->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 _heapinfo[block].busy.type = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 _heapinfo[block].busy.info.size = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881 /* Keep the statistics accurate. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882 ++_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 _bytes_used += BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 _chunks_free -= BLOCKSIZE >> type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 _bytes_free -= BLOCKSIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 free (ADDRESS (block));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 else if (_heapinfo[block].busy.info.frag.nfree != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 /* If some fragments of this block are free, link this
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 fragment into the fragment list after the first free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 fragment of this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 next = (struct list *) ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 next->next = prev->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896 next->prev = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897 prev->next = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 if (next->next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 next->next->prev = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 ++_heapinfo[block].busy.info.frag.nfree;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904 /* No fragments of this block are free, so link this
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 fragment into the fragment list and announce that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 it is the first free fragment of this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 prev = (struct list *) ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908 _heapinfo[block].busy.info.frag.nfree = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 _heapinfo[block].busy.info.frag.first = (unsigned long int)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 ((unsigned long int) ((char *) ptr - (char *) NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911 % BLOCKSIZE >> type);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 prev->next = _fraghead[type].next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913 prev->prev = &_fraghead[type];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 prev->prev->next = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915 if (prev->next != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 prev->next->prev = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 /* Return memory to the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 __free_ret_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 free (__ptr_t ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 struct alignlist *l;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 if (ptr == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 if (PURE_DATA(ptr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936 for (l = _aligned_blocks; l != NULL; l = l->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937 if (l->aligned == ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 l->aligned = NULL; /* Mark the slot in the list as free. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940 ptr = l->exact;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 if (__free_hook != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945 (*__free_hook) (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
946 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
947 _free_internal (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 /* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 This file is part of the GNU C Library.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
952 The GNU C Library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
953 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
954 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
955 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
957 The GNU C Library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
958 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
959 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
960 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
963 along with the GNU C Library. If not, see <http://www.gnu.org/licenses/>. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
967 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 #ifdef _LIBC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 #include <ansidecl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 #include <gnu-stabs.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 #undef cfree
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 function_alias(cfree, free, void, (ptr),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978 DEFUN(cfree, (ptr), PTR ptr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982 void cfree (__ptr_t ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
984 cfree (__ptr_t ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
986 free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990 /* Change the size of a block allocated by `malloc'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
991 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992 Written May 1989 by Mike Haertel.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
994 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
995 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
996 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
997 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
999 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1000 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1001 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1002 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1004 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1005 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1006
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1011 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1012 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 #ifndef min
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 #define min(A, B) ((A) < (B) ? (A) : (B))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1017 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1018
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1019 /* Debugging hook for realloc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022 /* Resize the given region to the new size, returning a pointer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 to the (possibly moved) region. This is optimized for speed;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024 some benchmarks seem to indicate that greater compactness is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025 achieved by unconditionally allocating and copying to a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026 new region. This module has incestuous knowledge of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 internals of both free and malloc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1029 realloc (__ptr_t ptr, __malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1030 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1031 __ptr_t result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1032 int type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1033 __malloc_size_t block, blocks, oldlimit;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1034
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
1035 if (PURE_DATA (ptr))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1036 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1037 result = malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038 memcpy(result, ptr, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1041
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1042 else if (size == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1043 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045 return malloc (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1047 else if (ptr == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1048 return malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1049
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1050 if (__realloc_hook != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1051 return (*__realloc_hook) (ptr, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1052
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1053 block = BLOCK (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1054
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1055 type = _heapinfo[block].busy.type;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1056 switch (type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1057 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1058 case 0:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1059 /* Maybe reallocate a large block to a small fragment. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1060 if (size <= BLOCKSIZE / 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1061 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1062 result = malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1063 if (result != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1064 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1065 memcpy (result, ptr, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1066 _free_internal (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1067 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1068 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1069 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1070
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1071 /* The new size is a large allocation as well;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1072 see if we can hold it in place. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1073 blocks = BLOCKIFY (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1074 if (blocks < _heapinfo[block].busy.info.size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1075 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1076 /* The new size is smaller; return
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1077 excess memory to the free list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1078 _heapinfo[block + blocks].busy.type = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1079 _heapinfo[block + blocks].busy.info.size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1080 = _heapinfo[block].busy.info.size - blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1081 _heapinfo[block].busy.info.size = blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1082 /* We have just created a new chunk by splitting a chunk in two.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1083 Now we will free this chunk; increment the statistics counter
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1084 so it doesn't become wrong when _free_internal decrements it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1085 ++_chunks_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1086 _free_internal (ADDRESS (block + blocks));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1087 result = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1088 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1089 else if (blocks == _heapinfo[block].busy.info.size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1090 /* No size change necessary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1091 result = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1092 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1093 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1094 /* Won't fit, so allocate a new region that will.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1095 Free the old region first in case there is sufficient
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1096 adjacent free space to grow without moving. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1097 blocks = _heapinfo[block].busy.info.size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1098 /* Prevent free from actually returning memory to the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1099 oldlimit = _heaplimit;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1100 _heaplimit = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1101 free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1102 _heaplimit = oldlimit;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1103 result = malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1104 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1105 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1106 /* Now we're really in trouble. We have to unfree
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1107 the thing we just freed. Unfortunately it might
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1108 have been coalesced with its neighbors. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1109 if (_heapindex == block)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1110 (void) malloc (blocks * BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1111 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1112 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1113 __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1114 (void) malloc (blocks * BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1115 free (previous);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1116 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1117 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1118 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1119 if (ptr != result)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1120 memmove (result, ptr, blocks * BLOCKSIZE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1121 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1122 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1123
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1124 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1125 /* Old size is a fragment; type is logarithm
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1126 to base two of the fragment size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1127 if (size > (__malloc_size_t) (1 << (type - 1)) &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1128 size <= (__malloc_size_t) (1 << type))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1129 /* The new size is the same kind of fragment. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1130 result = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1131 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1132 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1133 /* The new size is different; allocate a new space,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1134 and copy the lesser of the new size and the old. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1135 result = malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1136 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1137 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1138 memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1139 free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1140 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1141 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1142 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1143
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1144 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1145 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1146 /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1147
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1148 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1149 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1150 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1151 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1152
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1153 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1154 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1155 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1156 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1157
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1158 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1159 along with this library. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1160
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1161 The author may be reached (Email) at the address mike@ai.mit.edu,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1162 or (US mail) as Mike Haertel c/o Free Software Foundation, Inc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1163
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1164 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1165 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1166 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1167 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1168
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1169 /* Allocate an array of NMEMB elements each SIZE bytes long.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1170 The entire array is initialized to zeros. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1171 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1172 calloc (__malloc_size_t nmemb, __malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1173 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1174 __ptr_t result = malloc (nmemb * size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1175
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1176 if (result != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1177 (void) memset (result, 0, nmemb * size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1179 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1180 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1181 /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1182 This file is part of the GNU C Library.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1183
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1184 The GNU C Library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1185 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1186 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1187 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1188
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1189 The GNU C Library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1190 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1191 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1192 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1193
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1194 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1195 along with the GNU C Library. If not, see <http://www.gnu.org/licenses/>. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1197 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1198 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1199 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1200 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1201
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1202 /* #ifndef __GNU_LIBRARY__ */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1203 #define __sbrk sbrk
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1204 /* #endif */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1205
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1206 #ifdef __GNU_LIBRARY__
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1207 /* It is best not to declare this and cast its result on foreign operating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1208 systems with potentially hostile include files. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1209 #if !(defined(linux) && defined(sparc))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1210 extern __ptr_t __sbrk __P ((int increment));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1211 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1212 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1214 #ifndef NULL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1215 #define NULL 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1216 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1217
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1218 /* Allocate INCREMENT more bytes of data space,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1219 and return the start of data space, or NULL on errors.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1220 If INCREMENT is negative, shrink data space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1221 __ptr_t
452
3d3049ae1304 Import from CVS: tag r21-2-41
cvs
parents: 442
diff changeset
1222 __default_morecore (ptrdiff_t increment)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1223 {
452
3d3049ae1304 Import from CVS: tag r21-2-41
cvs
parents: 442
diff changeset
1224 __ptr_t result = (__ptr_t) __sbrk (increment);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1225 if (result == (__ptr_t) -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1226 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1227 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1228 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1229 /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1230
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1231 This library is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1232 under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1233 Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1234 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1235
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1236 This library is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1237 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1238 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1239 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1240
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1241 You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 4759
diff changeset
1242 along with this library. If not, see <http://www.gnu.org/licenses/>. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1243
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1244 #ifndef _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1245 #define _MALLOC_INTERNAL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1246 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1247 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1248
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1249 __ptr_t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1250 memalign (__malloc_size_t alignment, __malloc_size_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1251 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1252 __ptr_t result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1253 unsigned long int adj;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1254
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1255 size = ((size + alignment - 1) / alignment) * alignment;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1256
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1257 result = malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1258 if (result == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1259 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1260 adj = (unsigned long int) ((unsigned long int) ((char *) result -
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1261 (char *) NULL)) % alignment;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1262 if (adj != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1263 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1264 struct alignlist *l;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1265 for (l = _aligned_blocks; l != NULL; l = l->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1266 if (l->aligned == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1267 /* This slot is free. Use it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1268 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1269 if (l == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1270 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1271 l = (struct alignlist *) malloc (sizeof (struct alignlist));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1272 if (l == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1273 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1274 free (result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1275 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1276 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1277 l->next = _aligned_blocks;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1278 _aligned_blocks = l;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1279 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1280 l->exact = result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1281 result = l->aligned = (char *) result + alignment - adj;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1282 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1283
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1284 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1285 }