annotate src/vmsgmalloc.c @ 26440:1e3acb0878a2

*** empty log message ***
author Gerd Moellmann <gerd@gnu.org>
date Sat, 13 Nov 1999 23:19:56 +0000
parents b7aa6ac26872
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 /* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 /* The malloc headers and source files from the C library follow here. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 /* Declarations for `malloc' and friends.
26088
b7aa6ac26872 Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents: 14186
diff changeset
8 Copyright 1990, 1991, 1992, 1993, 1999 Free Software Foundation, Inc.
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
11 This file is part of GNU Emacs.
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
13 GNU Emacs is free software; you can redistribute it and/or modify
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
14 it under the terms of the GNU General Public License as published by
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
15 the Free Software Foundation; either version 2, or (at your option)
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
16 any later version.
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
17
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
18 GNU Emacs is distributed in the hope that it will be useful,
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
21 GNU General Public License for more details.
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
23 You should have received a copy of the GNU General Public License
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
24 along with GNU Emacs; see the file COPYING. If not, write to
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
25 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
26 Boston, MA 02111-1307, USA.
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 #ifndef _MALLOC_H
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 #define _MALLOC_H 1
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 #ifdef __cplusplus
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 extern "C"
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 #undef __P
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 #define __P(args) args
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 #undef __const
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 #define __const const
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 #undef __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 #define __ptr_t void *
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 #else /* Not C++ or ANSI C. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 #undef __P
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 #define __P(args) ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 #undef __const
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 #define __const
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 #undef __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 #define __ptr_t char *
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 #endif /* C++ or ANSI C. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 #ifndef NULL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 #define NULL 0
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59
26088
b7aa6ac26872 Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents: 14186
diff changeset
60 #if defined (HAVE_CONFIG_H) || defined (emacs)
b7aa6ac26872 Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents: 14186
diff changeset
61 #include <config.h>
b7aa6ac26872 Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents: 14186
diff changeset
62 #endif
b7aa6ac26872 Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents: 14186
diff changeset
63
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 #ifdef __STDC__
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 #include <stddef.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 #ifdef VMS /* The following are defined in stdio.h, but we need it NOW!
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 But do NOT do it with defines here, for then, VAX C is going
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 to barf when it gets to stdio.h and the typedefs in there! */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 typedef unsigned int size_t;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 typedef int ptrdiff_t;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 #else /* not VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 #undef size_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 #define size_t unsigned int
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 #undef ptrdiff_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 #define ptrdiff_t int
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 #endif /* VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 /* Allocate SIZE bytes of memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 extern __ptr_t malloc __P ((size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 /* Re-allocate the previously allocated block
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 in __ptr_t, making the new block SIZE bytes long. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 extern void free __P ((__ptr_t __ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 /* Allocate SIZE bytes on a page boundary. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 extern __ptr_t valloc __P ((size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 /* VMS hooks to deal with two heaps */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 /* Allocate SIZE bytes of memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 extern __ptr_t __vms_malloc __P ((size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 /* Re-allocate the previously allocated block
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 in __ptr_t, making the new block SIZE bytes long. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 extern __ptr_t __vms_realloc __P ((__ptr_t __ptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 extern void __vms_free __P ((__ptr_t __ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 #ifdef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 #include <stdio.h> /* Harmless, gets __GNU_LIBRARY__ defined. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 #if defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 #include <string.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 #ifndef memset
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 #define memset(s, zero, n) bzero ((s), (n))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 #ifndef memcpy
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 #define memcpy(d, s, n) bcopy ((s), (d), (n))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 #ifndef memmove
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 #define memmove(d, s, n) bcopy ((s), (d), (n))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 #if defined(__GNU_LIBRARY__) || defined(__STDC__)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 #include <limits.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 #define CHAR_BIT 8
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 /* The allocator divides the heap into blocks of fixed size; large
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 requests receive one or more whole blocks, and small requests
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 receive a fragment of a block. Fragment sizes are powers of two,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 and all fragments of a block are the same size. When all the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 fragments in a block have been freed, the block itself is freed. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 #define INT_BIT (CHAR_BIT * sizeof(int))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 #define BLOCKLOG 9
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 #define BLOCKSIZE (1 << BLOCKLOG)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 /* Determine the amount of memory spanned by the initial heap table
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 (not an absolute limit). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 /* Number of contiguous free blocks allowed to build up at the end of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 memory before they will be returned to the system. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 #define FINAL_FREE_BLOCKS 8
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 /* Data structure giving per-block information. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 typedef union
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 /* Heap information for a busy block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 struct
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 /* Zero for a large block, or positive giving the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 logarithm to the base two of the fragment size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 int type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 union
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 struct
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 size_t nfree; /* Free fragments in a fragmented block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 size_t first; /* First free fragment of the block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 } frag;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 /* Size (in blocks) of a large cluster. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 } info;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 } busy;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 /* Heap information for a free block
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 (that may be the first of a free cluster). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 struct
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 size_t size; /* Size (in blocks) of a free cluster. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 size_t next; /* Index of next free cluster. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 size_t prev; /* Index of previous free cluster. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 } free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 } malloc_info;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 /* Pointer to first block of the heap. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 extern char *_heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 /* Table indexed by block number giving per-block information. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 extern malloc_info *_heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 /* Address to block number and vice versa. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 /* Current search index for the heap table. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 extern size_t _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 /* Limit of valid info table indices. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 extern size_t _heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 /* Doubly linked lists of free fragments. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 struct list
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 struct list *next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 struct list *prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 /* Free list headers for each fragment size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 extern struct list _fraghead[];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 /* List of blocks allocated with `memalign' (or `valloc'). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 struct alignlist
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 struct alignlist *next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 __ptr_t aligned; /* The address that memaligned returned. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 __ptr_t exact; /* The address that malloc returned. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 extern struct alignlist *_aligned_blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 /* Instrumentation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 extern size_t _chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 extern size_t _bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 extern size_t _chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 extern size_t _bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 /* Internal version of `free' used in `morecore' (malloc.c). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 extern void _free_internal __P ((__ptr_t __ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 #endif /* _MALLOC_INTERNAL. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 /* Underlying allocation function; successive calls should
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 return contiguous pieces of memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 /* It does NOT always return contiguous pieces of memory on VMS. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 /* Underlying deallocation function. It accepts both a pointer and
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 a size to back up. It is implementation dependent what is really
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 used. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 extern __ptr_t (*__lesscore) __P ((__ptr_t __ptr, ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 /* Default value of `__morecore'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 /* Default value of `__lesscore'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 extern __ptr_t __default_lesscore __P ((__ptr_t __ptr, ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 /* Default value of `__morecore'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 extern __ptr_t __vms_morecore __P ((ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 /* Default value of `__lesscore'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 extern __ptr_t __vms_lesscore __P ((__ptr_t __ptr, ptrdiff_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 /* If not NULL, this function is called after each time
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 `__morecore' is called to increase the data size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 extern void (*__after_morecore_hook) __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 /* If not NULL, this function is called after each time
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 `__lesscore' is called to increase the data size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 extern void (*__after_lesscore_hook) __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 /* Nonzero if `malloc' has been called and done its initialization. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 extern int __malloc_initialized;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 /* Hooks for debugging versions. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 extern void (*__free_hook) __P ((__ptr_t __ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 extern __ptr_t (*__malloc_hook) __P ((size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 /* Activate a standard collection of debugging hooks. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 extern int mcheck __P ((void (*__func) __P ((void))));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 /* Activate a standard collection of tracing hooks. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 extern void mtrace __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 /* Statistics available to the user. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 struct mstats
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 size_t bytes_total; /* Total size of the heap. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 size_t chunks_used; /* Chunks allocated by the user. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 size_t bytes_used; /* Byte total of user-allocated chunks. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 size_t chunks_free; /* Chunks in the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 size_t bytes_free; /* Byte total of chunks in the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 /* Pick up the current statistics. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 extern struct mstats mstats __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290 /* Call WARNFUN with a warning message when memory usage is high. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 extern void memory_warnings __P ((__ptr_t __start,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 void (*__warnfun) __P ((__const char *))));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 /* Relocating allocator. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
298 extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
299
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
300 /* Free the storage allocated in HANDLEPTR. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
301 extern void r_alloc_free __P ((__ptr_t *__handleptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
302
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
303 /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
304 extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
305
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
306
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
307 #ifdef __cplusplus
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
308 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
309 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
310
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
311 #endif /* malloc.h */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
312 /* Memory allocator `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
313 Copyright 1990, 1991, 1992, 1993 Free Software Foundation
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
314 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
315
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
316 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
317 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
318 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
319 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
320
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
321 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
322 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
323 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
324 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
325
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
326 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
327 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
328 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
329 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
330
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
331 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
332 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
333
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
334 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
335 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
336 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
337 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
338
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
339 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
340 /* How to really get more memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
341 __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __vms_morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
342 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
343 /* How to really get more memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
344 __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
345 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
346
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
347 /* Debugging hook for `malloc'. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
348 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
349 __ptr_t (*__malloc_hook) __P ((size_t __size)) = __vms_malloc;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
350 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
351 __ptr_t (*__malloc_hook) __P ((size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
352 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
353
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
354 /* Pointer to the base of the first block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
355 char *_heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
356
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
357 /* Block information table. Allocated with align/__free (not malloc/free). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
358 malloc_info *_heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
359
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
360 /* Number of info entries. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
361 static size_t heapsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
362
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
363 /* Search index in the info table. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
364 size_t _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
365
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
366 /* Limit of valid info table indices. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
367 size_t _heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
368
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
369 /* Free lists for each fragment size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
370 struct list _fraghead[BLOCKLOG];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
371
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
372 /* Instrumentation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
373 size_t _chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
374 size_t _bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
375 size_t _chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
376 size_t _bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
377
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
378 /* Are you experienced? */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
379 int __malloc_initialized;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
380
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
381 void (*__after_morecore_hook) __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
382
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
383 /* Aligned allocation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
384 static __ptr_t align __P ((size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
385 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
386 align (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
387 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
388 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
389 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
390 unsigned long int adj;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
391
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
392 result = (*__morecore) (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
393 adj = (unsigned long int) ((unsigned long int) ((char *) result -
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
394 (char *) NULL)) % BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
395 if (adj != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
396 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
397 adj = BLOCKSIZE - adj;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
398 (void) (*__morecore) (adj);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
399 result = (char *) result + adj;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
400 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
401
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
402 if (__after_morecore_hook)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
403 (*__after_morecore_hook) ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
404
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
405 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
406 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
407
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
408 /* Set everything up and remember that we have. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
409 static int initialize __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
410 static int
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
411 initialize ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
412 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
413 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
414 extern VMS_present_buffer();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
415 printf("__malloc_initialized = %d\n", __malloc_initialized);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
416 VMS_present_buffer();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
417 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
418 heapsize = HEAP / BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
419 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
420 if (_heapinfo == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
421 return 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
422 memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
423 _heapinfo[0].free.size = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
424 _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
425 _heapindex = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
426 _heapbase = (char *) _heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
427 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
428 /* debug */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
429 printf("_heapbase = 0%o/0x%x/%d\n", _heapbase, _heapbase, _heapbase);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
430 /* end debug */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
431 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
432 __malloc_initialized = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
433 return 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
434 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
435
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
436 /* Get neatly aligned memory, initializing or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
437 growing the heap info table as necessary. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
438 static __ptr_t morecore __P ((size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
439 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
440 morecore (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
441 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
442 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
443 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
444 malloc_info *newinfo, *oldinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
445 size_t newsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
446
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
447 result = align (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
448 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
449 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
450
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
451 /* Check if we need to grow the info table. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
452 if ((size_t) BLOCK ((char *) result + size) > heapsize)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
453 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
454 newsize = heapsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
455 while ((size_t) BLOCK ((char *) result + size) > newsize)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
456 newsize *= 2;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
457 newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
458 if (newinfo == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
459 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
460 (*__lesscore) (result, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
461 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
462 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
463 memset (newinfo, 0, newsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
464 memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
465 oldinfo = _heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
466 newinfo[BLOCK (oldinfo)].busy.type = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
467 newinfo[BLOCK (oldinfo)].busy.info.size
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
468 = BLOCKIFY (heapsize * sizeof (malloc_info));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
469 _heapinfo = newinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
470 _free_internal (oldinfo);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
471 heapsize = newsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
472 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
473
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
474 _heaplimit = BLOCK ((char *) result + size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
475 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
476 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
477
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
478 /* Allocate memory from the heap. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
479 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
480 malloc (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
481 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
482 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
483 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
484 size_t block, blocks, lastblocks, start;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
485 register size_t i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
486 struct list *next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
487
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
488 if (size == 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
489 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
490
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
491 if (__malloc_hook != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
492 return (*__malloc_hook) (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
493
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
494 if (!__malloc_initialized)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
495 if (!initialize ())
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
496 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
497
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
498 if (size < sizeof (struct list))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
499 size = sizeof (struct list);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
500
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
501 /* Determine the allocation policy based on the request size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
502 if (size <= BLOCKSIZE / 2)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
503 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
504 /* Small allocation to receive a fragment of a block.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
505 Determine the logarithm to base two of the fragment size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
506 register size_t log = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
507 --size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
508 while ((size /= 2) != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
509 ++log;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
510
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
511 /* Look in the fragment lists for a
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
512 free fragment of the desired size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
513 next = _fraghead[log].next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
514 if (next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
515 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
516 /* There are free fragments of this size.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
517 Pop a fragment out of the fragment list and return it.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
518 Update the block's nfree and first counters. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
519 result = (__ptr_t) next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
520 next->prev->next = next->next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
521 if (next->next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
522 next->next->prev = next->prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
523 block = BLOCK (result);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
524 if (--_heapinfo[block].busy.info.frag.nfree != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
525 _heapinfo[block].busy.info.frag.first = (unsigned long int)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
526 ((unsigned long int) ((char *) next->next - (char *) NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
527 % BLOCKSIZE) >> log;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
528
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
529 /* Update the statistics. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
530 ++_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
531 _bytes_used += 1 << log;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
532 --_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
533 _bytes_free -= 1 << log;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
534 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
535 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
536 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
537 /* No free fragments of the desired size, so get a new block
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
538 and break it into fragments, returning the first. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
539 result = malloc (BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
540 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
541 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
542
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
543 /* Link all fragments but the first into the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
544 for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
545 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
546 next = (struct list *) ((char *) result + (i << log));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
547 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
548 printf("DEBUG: malloc (%d): next = %p\n", size, next);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
549 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
550 next->next = _fraghead[log].next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
551 next->prev = &_fraghead[log];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
552 next->prev->next = next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
553 if (next->next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
554 next->next->prev = next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
555 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
556
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
557 /* Initialize the nfree and first counters for this block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
558 block = BLOCK (result);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
559 _heapinfo[block].busy.type = log;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
560 _heapinfo[block].busy.info.frag.nfree = i - 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
561 _heapinfo[block].busy.info.frag.first = i - 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
562
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
563 _chunks_free += (BLOCKSIZE >> log) - 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
564 _bytes_free += BLOCKSIZE - (1 << log);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
565 _bytes_used -= BLOCKSIZE - (1 << log);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
566 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
567 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
568 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
569 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
570 /* Large allocation to receive one or more blocks.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
571 Search the free list in a circle starting at the last place visited.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
572 If we loop completely around without finding a large enough
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
573 space we will have to get more memory from the system. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
574 blocks = BLOCKIFY (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
575 start = block = _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
576 while (_heapinfo[block].free.size < blocks)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
577 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
578 block = _heapinfo[block].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
579 if (block == start)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
580 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
581 /* Need to get more from the system. Check to see if
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
582 the new core will be contiguous with the final free
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
583 block; if so we don't need to get as much. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
584 block = _heapinfo[0].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
585 lastblocks = _heapinfo[block].free.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
586 if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
587 (*__morecore) (0) == ADDRESS (block + lastblocks) &&
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
588 (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
589 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
590 _heapinfo[block].free.size = blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
591 _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
592 continue;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
593 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
594 result = morecore (blocks * BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
595 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
596 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
597 block = BLOCK (result);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
598 _heapinfo[block].busy.type = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
599 _heapinfo[block].busy.info.size = blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
600 ++_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
601 _bytes_used += blocks * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
602 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
603 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
604 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
605
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
606 /* At this point we have found a suitable free list entry.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
607 Figure out how to remove what we need from the list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
608 result = ADDRESS (block);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
609 if (_heapinfo[block].free.size > blocks)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
610 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
611 /* The block we found has a bit left over,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
612 so relink the tail end back into the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
613 _heapinfo[block + blocks].free.size
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
614 = _heapinfo[block].free.size - blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
615 _heapinfo[block + blocks].free.next
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
616 = _heapinfo[block].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
617 _heapinfo[block + blocks].free.prev
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
618 = _heapinfo[block].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
619 _heapinfo[_heapinfo[block].free.prev].free.next
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
620 = _heapinfo[_heapinfo[block].free.next].free.prev
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
621 = _heapindex = block + blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
622 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
623 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
624 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
625 /* The block exactly matches our requirements,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
626 so just remove it from the list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
627 _heapinfo[_heapinfo[block].free.next].free.prev
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
628 = _heapinfo[block].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
629 _heapinfo[_heapinfo[block].free.prev].free.next
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
630 = _heapindex = _heapinfo[block].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
631 --_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
632 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
633
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
634 _heapinfo[block].busy.type = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
635 _heapinfo[block].busy.info.size = blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
636 ++_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
637 _bytes_used += blocks * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
638 _bytes_free -= blocks * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
639 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
640
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
641 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
642 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
643 /* Free a block of memory allocated by `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
644 Copyright 1990, 1991, 1992 Free Software Foundation
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
645 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
646
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
647 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
648 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
649 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
650 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
651
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
652 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
653 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
654 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
655 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
656
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
657 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
658 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
659 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
660 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
661
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
662 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
663 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
664
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
665 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
666 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
667 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
668 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
669
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
670 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
671 /* How to really get more memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
672 __ptr_t (*__lesscore) __P ((__ptr_t __ptr,ptrdiff_t __size)) = __vms_lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
673 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
674 /* How to really get more memory. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
675 __ptr_t (*__lesscore) __P ((__ptr_t __ptr,ptrdiff_t __size)) = __default_lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
676 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
677
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
678 /* Debugging hook for free. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
679 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
680 void (*__free_hook) __P ((__ptr_t __ptr)) = __vms_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
681 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
682 void (*__free_hook) __P ((__ptr_t __ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
683 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
684
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
685 /* List of blocks allocated by memalign. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
686 struct alignlist *_aligned_blocks = NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
687
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
688 /* Return memory to the heap.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
689 Like `free' but don't call a __free_hook if there is one. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
690 void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
691 _free_internal (ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
692 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
693 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
694 int type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
695 size_t block, blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
696 register size_t i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
697 struct list *prev, *next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
698
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
699 block = BLOCK (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
700
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
701 type = _heapinfo[block].busy.type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
702 switch (type)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
703 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
704 case 0:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
705 /* Get as many statistics as early as we can. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
706 --_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
707 _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
708 _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
709
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
710 /* Find the free cluster previous to this one in the free list.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
711 Start searching at the last block referenced; this may benefit
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
712 programs with locality of allocation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
713 i = _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
714 if (i > block)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
715 while (i > block)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
716 i = _heapinfo[i].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
717 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
718 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
719 do
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
720 i = _heapinfo[i].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
721 while (i > 0 && i < block);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
722 i = _heapinfo[i].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
723 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
724
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
725 /* Determine how to link this block into the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
726 if (block == i + _heapinfo[i].free.size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
727 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
728 /* Coalesce this block with its predecessor. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
729 _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
730 block = i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
731 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
732 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
733 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
734 /* Really link this block back into the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
735 _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
736 _heapinfo[block].free.next = _heapinfo[i].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
737 _heapinfo[block].free.prev = i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
738 _heapinfo[i].free.next = block;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
739 _heapinfo[_heapinfo[block].free.next].free.prev = block;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
740 ++_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
741 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
742
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
743 /* Now that the block is linked in, see if we can coalesce it
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
744 with its successor (by deleting its successor from the list
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
745 and adding in its size). */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
746 if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
747 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
748 _heapinfo[block].free.size
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
749 += _heapinfo[_heapinfo[block].free.next].free.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
750 _heapinfo[block].free.next
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
751 = _heapinfo[_heapinfo[block].free.next].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
752 _heapinfo[_heapinfo[block].free.next].free.prev = block;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
753 --_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
754 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
755
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
756 /* Now see if we can return stuff to the system. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
757 blocks = _heapinfo[block].free.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
758 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
759 && (*__morecore) (0) == ADDRESS (block + blocks))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
760 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
761 register size_t bytes = blocks * BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
762 _heaplimit -= blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
763 (*__lesscore) (ADDRESS(block), bytes);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
764 _heapinfo[_heapinfo[block].free.prev].free.next
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
765 = _heapinfo[block].free.next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
766 _heapinfo[_heapinfo[block].free.next].free.prev
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
767 = _heapinfo[block].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
768 block = _heapinfo[block].free.prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
769 --_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
770 _bytes_free -= bytes;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
771 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
772
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
773 /* Set the next search to begin at this block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
774 _heapindex = block;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
775 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
776
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
777 default:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
778 /* Do some of the statistics. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
779 --_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
780 _bytes_used -= 1 << type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
781 ++_chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
782 _bytes_free += 1 << type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
783
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
784 /* Get the address of the first free fragment in this block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
785 prev = (struct list *) ((char *) ADDRESS (block) +
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
786 (_heapinfo[block].busy.info.frag.first << type));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
787 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
788 printf("_free_internal(0%o/0x%x/%d) :\n", ptr, ptr, ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
789 printf(" block = %d, type = %d, prev = 0%o/0x%x/%d\n",
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
790 block, type, prev, prev, prev);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
791 printf(" _heapinfo[block=%d].busy.info.frag.nfree = %d\n",
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
792 block,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
793 _heapinfo[block].busy.info.frag.nfree);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
794 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
795
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
796 if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
797 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
798 /* If all fragments of this block are free, remove them
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
799 from the fragment list and free the whole block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
800 next = prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
801 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
802 next = next->next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
803 prev->prev->next = next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
804 if (next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
805 next->prev = prev->prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
806 _heapinfo[block].busy.type = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
807 _heapinfo[block].busy.info.size = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
808
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
809 /* Keep the statistics accurate. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
810 ++_chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
811 _bytes_used += BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
812 _chunks_free -= BLOCKSIZE >> type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
813 _bytes_free -= BLOCKSIZE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
814
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
815 free (ADDRESS (block));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
816 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
817 else if (_heapinfo[block].busy.info.frag.nfree != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
818 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
819 /* If some fragments of this block are free, link this
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
820 fragment into the fragment list after the first free
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
821 fragment of this block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
822 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
823 printf("There's a bug hiding here (%s:%d), so I will print some values\n", __FILE__, __LINE__);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
824 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
825 next = (struct list *) ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
826 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
827 printf(" (struct list *)next (0%o / 0x%x / %d) ->\n", next, next, next);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
828 printf(" next = 0%o / 0x%x / %d\n", next->next,next->next,next->next);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
829 printf(" prev = 0%o / 0x%x / %d\n", next->prev,next->prev,next->prev);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
830 printf(" (struct list *)prev (0%o / 0x%x / %d)->\n", prev, prev, prev);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
831 printf(" next = 0%o / 0x%x / %d\n", prev->next,prev->next,prev->next);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
832 printf(" prev = 0%o / 0x%x / %d\n", prev->prev,prev->prev,prev->prev);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
833 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
834 next->next = prev->next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
835 next->prev = prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
836 prev->next = next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
837 if (next->next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
838 next->next->prev = next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
839 ++_heapinfo[block].busy.info.frag.nfree;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
840 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
841 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
842 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
843 /* No fragments of this block are free, so link this
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
844 fragment into the fragment list and announce that
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
845 it is the first free fragment of this block. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
846 prev = (struct list *) ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
847 _heapinfo[block].busy.info.frag.nfree = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
848 _heapinfo[block].busy.info.frag.first = (unsigned long int)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
849 ((unsigned long int) ((char *) ptr - (char *) NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
850 % BLOCKSIZE >> type);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
851 prev->next = _fraghead[type].next;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
852 prev->prev = &_fraghead[type];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
853 prev->prev->next = prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
854 if (prev->next != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
855 prev->next->prev = prev;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
856 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
857 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
858 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
859 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
860
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
861 /* Return memory to the heap. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
862 void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
863 free (ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
864 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
865 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
866 register struct alignlist *l;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
867
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
868 if (ptr == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
869 return;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
870
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
871 for (l = _aligned_blocks; l != NULL; l = l->next)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
872 if (l->aligned == ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
873 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
874 l->aligned = NULL; /* Mark the slot in the list as free. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
875 ptr = l->exact;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
876 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
877 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
878
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
879 if (__free_hook != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
880 (*__free_hook) (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
881 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
882 _free_internal (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
883 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
884 /* Change the size of a block allocated by `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
885 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
886 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
887
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
888 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
889 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
890 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
891 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
892
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
893 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
894 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
895 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
896 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
897
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
898 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
899 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
900 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
901 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
902
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
903 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
904 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
905
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
906 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
907 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
908 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
909 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
910
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
911 #define min(A, B) ((A) < (B) ? (A) : (B))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
912
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
913 /* Debugging hook for realloc. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
914 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
915 __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size)) = __vms_realloc;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
916 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
917 __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
918 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
919
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
920 /* Resize the given region to the new size, returning a pointer
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
921 to the (possibly moved) region. This is optimized for speed;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
922 some benchmarks seem to indicate that greater compactness is
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
923 achieved by unconditionally allocating and copying to a
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
924 new region. This module has incestuous knowledge of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
925 internals of both free and malloc. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
926 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
927 realloc (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
928 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
929 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
930 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
931 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
932 int type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
933 size_t block, blocks, oldlimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
934
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
935 if (size == 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
936 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
937 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
938 return malloc (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
939 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
940 else if (ptr == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
941 return malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
942
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
943 if (__realloc_hook != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
944 return (*__realloc_hook) (ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
945
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
946 block = BLOCK (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
947
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
948 type = _heapinfo[block].busy.type;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
949 switch (type)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
950 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
951 case 0:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
952 /* Maybe reallocate a large block to a small fragment. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
953 if (size <= BLOCKSIZE / 2)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
954 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
955 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
956 if (result != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
957 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
958 memcpy (result, ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
959 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
960 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
961 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
962 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
963
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
964 /* The new size is a large allocation as well;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
965 see if we can hold it in place. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
966 blocks = BLOCKIFY (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
967 if (blocks < _heapinfo[block].busy.info.size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
968 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
969 /* The new size is smaller; return
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
970 excess memory to the free list. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
971 _heapinfo[block + blocks].busy.type = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
972 _heapinfo[block + blocks].busy.info.size
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
973 = _heapinfo[block].busy.info.size - blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
974 _heapinfo[block].busy.info.size = blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
975 free (ADDRESS (block + blocks));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
976 result = ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
977 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
978 else if (blocks == _heapinfo[block].busy.info.size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
979 /* No size change necessary. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
980 result = ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
981 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
982 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
983 /* Won't fit, so allocate a new region that will.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
984 Free the old region first in case there is sufficient
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
985 adjacent free space to grow without moving. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
986 blocks = _heapinfo[block].busy.info.size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
987 /* Prevent free from actually returning memory to the system. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
988 oldlimit = _heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
989 _heaplimit = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
990 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
991 _heaplimit = oldlimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
992 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
993 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
994 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
995 /* Now we're really in trouble. We have to unfree
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
996 the thing we just freed. Unfortunately it might
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
997 have been coalesced with its neighbors. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
998 if (_heapindex == block)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
999 (void) malloc (blocks * BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1000 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1001 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1002 __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1003 (void) malloc (blocks * BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1004 free (previous);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1005 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1006 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1007 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1008 if (ptr != result)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1009 memmove (result, ptr, blocks * BLOCKSIZE);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1010 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1011 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1012
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1013 default:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1014 /* Old size is a fragment; type is logarithm
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1015 to base two of the fragment size. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1016 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1017 /* The new size is the same kind of fragment. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1018 result = ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1019 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1020 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1021 /* The new size is different; allocate a new space,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1022 and copy the lesser of the new size and the old. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1023 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1024 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1025 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1026 memcpy (result, ptr, min (size, (size_t) 1 << type));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1027 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1028 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1029 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1030 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1031
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1032 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1033 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1034 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1035
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1036 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1037 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1038 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1039 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1040
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1041 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1042 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1043 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1044 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1045
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1046 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1047 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1048 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1049 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1050
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1051 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1052 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1053
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1054 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1055 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1056 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1057 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1058
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1059 /* Allocate an array of NMEMB elements each SIZE bytes long.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1060 The entire array is initialized to zeros. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1061 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1062 calloc (nmemb, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1063 register size_t nmemb;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1064 register size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1065 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1066 register __ptr_t result = malloc (nmemb * size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1067
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1068 if (result != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1069 (void) memset (result, 0, nmemb * size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1070
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1071 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1072 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1073 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1074 This file is part of the GNU C Library.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1075
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1076 The GNU C Library is free software; you can redistribute it and/or modify
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1077 it under the terms of the GNU General Public License as published by
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1078 the Free Software Foundation; either version 2, or (at your option)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1079 any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1080
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1081 The GNU C Library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1082 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1083 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1084 GNU General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1085
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1086 You should have received a copy of the GNU General Public License
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1087 along with the GNU C Library; see the file COPYING. If not, write to
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1088 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1089
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1090 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1091 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1092 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1093 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1094
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1095 #ifndef __GNU_LIBRARY__
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1096 #define __sbrk sbrk
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1097 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1098 #define __brk brk
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1099 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1100 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1101
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1102 extern __ptr_t __sbrk __P ((int increment));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1103
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1104 #ifndef NULL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1105 #define NULL 0
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1106 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1107
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1108 #if defined(emacs) && defined(VMS)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1109 /* Dumping of Emacs on VMS does not include the heap!
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1110 So let's make a huge array from which initial data will be
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1111 allocated.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1112
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1113 VMS_ALLOCATION_SIZE is the amount of memory we preallocate.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1114 We don't want it to be too large, because it only gives a larger
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1115 dump file. The way to check how much is really used is to
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1116 make VMS_ALLOCATION_SIZE very large, to link Emacs with the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1117 debugger, run Emacs, check how much was allocated. Then set
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1118 VMS_ALLOCATION_SIZE to something suitable, recompile gmalloc,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1119 relink Emacs, and you should be off.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1120
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1121 N.B. This is experimental, but it worked quite fine on Emacs 18.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1122 */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1123 #ifndef VMS_ALLOCATION_SIZE
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1124 #define VMS_ALLOCATION_SIZE (512*(512+128))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1125 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1126
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1127 int vms_out_initial = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1128 char vms_initial_buffer[VMS_ALLOCATION_SIZE];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1129 char *vms_current_brk = vms_initial_buffer;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1130 char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1131
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1132 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1133 __vms_initial_morecore (increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1134 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1135 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1136 __ptr_t result = NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1137 __ptr_t temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1138
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1139 /* It's far easier to make the alignment here than to make a
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1140 kludge in align () */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1141 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1142 printf(">>>foo... %p...", vms_current_brk);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1143 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1144 vms_current_brk += (BLOCKSIZE - ((unsigned long) vms_current_brk
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1145 & (BLOCKSIZE - 1))) & (BLOCKSIZE - 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1146 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1147 printf("bar... %p. (%d)\n", vms_current_brk, increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1148 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1149 temp = vms_current_brk + (int) increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1150 if (temp <= vms_end_brk)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1151 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1152 if (increment >= 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1153 result = vms_current_brk;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1154 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1155 result = temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1156 vms_current_brk = temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1157 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1158 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1159 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1160
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1161 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1162 __vms_initial_lesscore (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1163 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1164 ptrdiff_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1165 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1166 if (ptr >= vms_initial_buffer
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1167 && ptr < vms_initial_buffer+VMS_ALLOCATION_SIZE)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1168 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1169 vms_current_brk = ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1170 return vms_current_brk;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1171 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1172 return vms_current_brk;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1173 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1174
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1175 VMS_present_buffer()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1176 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1177 printf("Vms initial buffer starts at 0%o/0x%x/%d and ends at 0%o/0x%x/%d\n",
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1178 vms_initial_buffer, vms_initial_buffer, vms_initial_buffer,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1179 vms_end_brk, vms_end_brk, vms_end_brk);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1180 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1181 #endif /* defined(emacs) && defined(VMS) */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1182
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1183 #ifdef VMS
14036
621a575db6f7 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 4696
diff changeset
1184 /* Unfortunately, the VAX C sbrk() is buggy. For example, it returns
2877
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1185 memory in 512 byte chunks (not a bug, but there's more), AND it
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1186 adds an extra 512 byte chunk if you ask for a multiple of 512
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1187 bytes (you ask for 512 bytes, you get 1024 bytes...). And also,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1188 the VAX C sbrk does not handle negative increments...
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1189 There's a similar problem with brk(). Even if you set the break
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1190 to an even page boundary, it gives you one extra page... */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1191
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1192 static char vms_brk_info_fetched = -1; /* -1 if this is the first time, otherwise
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1193 bit 0 set if 'increment' needs adjustment
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1194 bit 1 set if the value to brk() needs adjustment */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1195 static char *vms_brk_start = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1196 static char *vms_brk_end = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1197 static char *vms_brk_current = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1198 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1199
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1200 /* Allocate INCREMENT more bytes of data space,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1201 and return the start of data space, or NULL on errors.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1202 If INCREMENT is negative, shrink data space. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1203 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1204 __default_morecore (increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1205 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1206 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1207 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1208 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1209 __ptr_t temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1210
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1211 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1212 printf("DEBUG: morecore: increment = %x\n", increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1213 printf(" @ start: vms_brk_info_fetched = %x\n", vms_brk_info_fetched);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1214 printf(" vms_brk_start = %p\n", vms_brk_start);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1215 printf(" vms_brk_current = %p\n", vms_brk_current);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1216 printf(" vms_brk_end = %p\n", vms_brk_end);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1217 printf(" @ end: ");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1218 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1219
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1220 if (vms_brk_info_fetched < 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1221 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1222 vms_brk_current = vms_brk_start = __sbrk (512);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1223 vms_brk_end = __sbrk (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1224 if (vms_brk_end - vms_brk_current == 1024)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1225 vms_brk_info_fetched = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1226 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1227 vms_brk_info_fetched = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1228 vms_brk_end = brk(vms_brk_start);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1229 if (vms_brk_end != vms_brk_start)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1230 vms_brk_info_fetched |= 2;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1231 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1232 printf("vms_brk_info_fetched = %x\n", vms_brk_info_fetched);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1233 printf(" vms_brk_start = %p\n", vms_brk_start);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1234 printf(" vms_brk_current = %p\n", vms_brk_current);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1235 printf(" vms_brk_end = %p\n", vms_brk_end);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1236 printf(" ");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1237 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1238 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1239
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1240 if (increment < 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1241 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1242 printf("BZZZZZT! ERROR: __default_morecore does NOT take negative args\n");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1243 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1244 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1245
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1246 if (increment > 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1247 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1248 result = vms_brk_current;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1249 temp = vms_brk_current + increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1250
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1251 if (temp > vms_brk_end)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1252 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1253 __ptr_t foo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1254
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1255 foo = __sbrk (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1256 if (foo == vms_brk_end)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1257 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1258 increment = temp - vms_brk_end;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1259 if (increment > (vms_brk_info_fetched & 1))
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1260 increment -= (vms_brk_info_fetched & 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1261 foo = __sbrk(increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1262 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1263 printf("__sbrk(%d) --> %p\n", increment, foo);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1264 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1265 if (foo == (__ptr_t) -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1266 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1267 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1268 printf(" ");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1269 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1270 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1271 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1272 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1273 result = __sbrk (increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1274
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1275 if (result == (__ptr_t) -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1276 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1277
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1278 temp = result + increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1279 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1280
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1281 vms_brk_end = __sbrk(0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1282 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1283 vms_brk_current = temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1284 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1285 printf("vms_brk_current = %p\n", vms_brk_current);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1286 printf(" vms_brk_end = %p\n", vms_brk_end);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1287 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1288 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1289 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1290 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1291 printf(" nothing more...\n");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1292 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1293
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1294 /* OK, so the user wanted to check where the heap limit is. Let's
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1295 see if the system thinks it is where we think it is. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1296 temp = __sbrk (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1297 if (temp != vms_brk_end)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1298 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1299 /* the value has changed.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1300 Let's trust the system and modify our value */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1301 vms_brk_current = vms_brk_end = temp;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1302 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1303 return vms_brk_current;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1304
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1305 #else /* not VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1306 result = __sbrk ((int) increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1307 if (result == (__ptr_t) -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1308 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1309 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1310 #endif /* VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1311 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1312
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1313 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1314 __default_lesscore (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1315 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1316 ptrdiff_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1317 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1318 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1319 if (vms_brk_end != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1320 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1321 vms_brk_current = ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1322 if (vms_brk_current < vms_brk_start)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1323 vms_brk_current = vms_brk_start;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1324 vms_brk_end = (char *) vms_brk_current -
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1325 ((vms_brk_info_fetched >> 1) & 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1326 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1327 printf("<<<bar... %p (%p (%p, %d))...",
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1328 vms_brk_end, vms_brk_current, ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1329 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1330 vms_brk_end = __brk (vms_brk_end);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1331 #ifdef RL_DEBUG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1332 printf("foo... %p.\n", vms_brk_end);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1333 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1334 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1335
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1336 return vms_brk_current;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1337 #else /* not VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1338 __default_morecore (-size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1339 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1340 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1341
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1342 /* Allocate memory on a page boundary.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1343 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1344
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1345 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1346 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1347 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1348 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1349
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1350 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1351 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1352 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1353 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1354
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1355 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1356 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1357 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1358 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1359
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1360 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1361 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1362
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1363 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1364 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1365 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1366 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1367
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1368 #if defined (emacs) || defined (HAVE_CONFIG_H)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1369 #include "config.h"
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1370 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1371
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1372 #ifdef __GNU_LIBRARY__
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1373 extern size_t __getpagesize __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1374 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1375 #if !defined(USG) && !defined(VMS)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1376 extern size_t getpagesize __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1377 #define __getpagesize() getpagesize()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1378 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1379 #include <sys/param.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1380 #ifdef EXEC_PAGESIZE
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1381 #define __getpagesize() EXEC_PAGESIZE
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1382 #else /* No EXEC_PAGESIZE. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1383 #ifdef NBPG
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1384 #ifndef CLSIZE
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1385 #define CLSIZE 1
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1386 #endif /* No CLSIZE. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1387 #define __getpagesize() (NBPG * CLSIZE)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1388 #else /* No NBPG. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1389 #define __getpagesize() NBPC
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1390 #endif /* NBPG. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1391 #endif /* EXEC_PAGESIZE. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1392 #endif /* USG. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1393 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1394
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1395 static size_t pagesize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1396
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1397 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1398 valloc (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1399 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1400 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1401 if (pagesize == 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1402 pagesize = __getpagesize ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1403
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1404 return memalign (pagesize, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1405 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1406 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1407
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1408 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1409 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1410 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1411 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1412
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1413 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1414 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1415 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1416 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1417
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1418 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1419 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1420 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1421 Cambridge, MA 02139, USA. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1422
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1423 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1424 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1425 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1426 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1427
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1428 __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1429 memalign (alignment, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1430 size_t alignment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1431 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1432 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1433 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1434 unsigned long int adj;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1435
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1436 size = ((size + alignment - 1) / alignment) * alignment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1437
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1438 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1439 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1440 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1441 adj = (unsigned long int) ((unsigned long int) ((char *) result -
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1442 (char *) NULL)) % alignment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1443 if (adj != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1444 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1445 struct alignlist *l;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1446 for (l = _aligned_blocks; l != NULL; l = l->next)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1447 if (l->aligned == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1448 /* This slot is free. Use it. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1449 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1450 if (l == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1451 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1452 l = (struct alignlist *) malloc (sizeof (struct alignlist));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1453 if (l == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1454 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1455 free (result);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1456 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1457 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1458 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1459 l->exact = result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1460 result = l->aligned = (char *) result + alignment - adj;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1461 l->next = _aligned_blocks;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1462 _aligned_blocks = l;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1463 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1464
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1465 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1466 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1467
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1468 #ifdef VMS
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1469 struct vms_malloc_data
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1470 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1471 int __malloc_initialized;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1472 char *_heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1473 malloc_info *_heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1474 size_t heapsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1475 size_t _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1476 size_t _heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1477 size_t _chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1478 size_t _bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1479 size_t _chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1480 size_t _bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1481 } ____vms_malloc_data[] =
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1482 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1483 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1484 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1485 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1486
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1487 struct vms_core_routines
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1488 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1489 __ptr_t (*__morecore) __P ((ptrdiff_t increment));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1490 __ptr_t (*__lesscore) __P ((__ptr_t address, ptrdiff_t increment));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1491 } ____vms_core_routines[] =
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1492 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1493 { __vms_initial_morecore, __vms_initial_lesscore },
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1494 { __default_morecore, __default_lesscore },
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1495 { 0, 0 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1496 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1497
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1498 static int current_vms_data = -1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1499 static int current_vms_core_routines = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1500
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1501 static void use_vms_core_routines (int i)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1502 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1503 current_vms_core_routines = i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1504 current_vms_data = i;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1505 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1506
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1507 static void use_vms_data (int i)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1508 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1509 use_vms_core_routines (i);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1510 __malloc_initialized = ____vms_malloc_data[i].__malloc_initialized;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1511 _heapbase = ____vms_malloc_data[i]._heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1512 _heapinfo = ____vms_malloc_data[i]._heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1513 heapsize = ____vms_malloc_data[i].heapsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1514 _heapindex = ____vms_malloc_data[i]._heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1515 _heaplimit = ____vms_malloc_data[i]._heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1516 _chunks_used = ____vms_malloc_data[i]._chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1517 _bytes_used = ____vms_malloc_data[i]._bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1518 _chunks_free = ____vms_malloc_data[i]._chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1519 _bytes_free = ____vms_malloc_data[i]._bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1520 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1521
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1522 static void store_vms_data (int i)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1523 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1524 ____vms_malloc_data[i].__malloc_initialized = __malloc_initialized;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1525 ____vms_malloc_data[i]._heapbase = _heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1526 ____vms_malloc_data[i]._heapinfo = _heapinfo;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1527 ____vms_malloc_data[i].heapsize = heapsize;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1528 ____vms_malloc_data[i]._heapindex = _heapindex;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1529 ____vms_malloc_data[i]._heaplimit = _heaplimit;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1530 ____vms_malloc_data[i]._chunks_used = _chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1531 ____vms_malloc_data[i]._bytes_used = _bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1532 ____vms_malloc_data[i]._chunks_free = _chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1533 ____vms_malloc_data[i]._bytes_free = _bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1534 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1535
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1536 static void store_current_vms_data ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1537 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1538 switch (current_vms_data)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1539 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1540 case 0:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1541 case 1:
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1542 store_vms_data (current_vms_data);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1543 break;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1544 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1545 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1546
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1547 __ptr_t __vms_morecore (increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1548 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1549 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1550 return
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1551 (*____vms_core_routines[current_vms_core_routines].__morecore) (increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1552 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1553
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1554 __ptr_t __vms_lesscore (ptr, increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1555 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1556 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1557 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1558 return
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1559 (*____vms_core_routines[current_vms_core_routines].__lesscore) (ptr,increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1560 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1561
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1562 __ptr_t __vms_malloc (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1563 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1564 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1565 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1566 int old_current_vms_data = current_vms_data;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1567
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1568 __malloc_hook = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1569
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1570 store_current_vms_data ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1571
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1572 if (____vms_malloc_data[0]._heapbase != 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1573 use_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1574 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1575 use_vms_core_routines (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1576 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1577 store_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1578 if (result == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1579 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1580 use_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1581 result = malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1582 store_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1583 vms_out_initial = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1584 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1585 __malloc_hook = __vms_malloc;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1586 if (old_current_vms_data != -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1587 use_vms_data (current_vms_data);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1588 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1589 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1590
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1591 void __vms_free (ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1592 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1593 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1594 int old_current_vms_data = current_vms_data;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1595
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1596 __free_hook = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1597
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1598 store_current_vms_data ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1599
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1600 if (ptr >= vms_initial_buffer && ptr <= vms_end_brk)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1601 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1602 use_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1603 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1604 store_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1605 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1606 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1607 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1608 use_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1609 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1610 store_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1611 if (_chunks_free == 0 && _chunks_used == 0)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1612 vms_out_initial = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1613 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1614 __free_hook = __vms_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1615 if (old_current_vms_data != -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1616 use_vms_data (current_vms_data);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1617 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1618
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1619 __ptr_t __vms_realloc (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1620 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1621 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1622 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1623 __ptr_t result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1624 int old_current_vms_data = current_vms_data;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1625
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1626 __realloc_hook = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1627
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1628 store_current_vms_data ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1629
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1630 if (ptr >= vms_initial_buffer && ptr <= vms_end_brk)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1631 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1632 use_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1633 result = realloc (ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1634 store_vms_data (0);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1635 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1636 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1637 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1638 use_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1639 result = realloc (ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1640 store_vms_data (1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1641 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1642 __realloc_hook = __vms_realloc;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1643 if (old_current_vms_data != -1)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1644 use_vms_data (current_vms_data);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1645 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1646 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1647 #endif /* VMS */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1648 /* Standard debugging hooks for `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1649 Copyright 1990, 1991, 1992 Free Software Foundation
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1650 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1651
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1652 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1653 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1654 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1655 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1656
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1657 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1658 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1659 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1660 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1661
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1662 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1663 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1664 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1665 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1666
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1667 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1668 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1669
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1670 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1671 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1672 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1673 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1674
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1675 /* Old hook values. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1676 static void (*old_free_hook) __P ((__ptr_t ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1677 static __ptr_t (*old_malloc_hook) __P ((size_t size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1678 static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, size_t size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1679
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1680 /* Function to call when something awful happens. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1681 static void (*abortfunc) __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1682
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1683 /* Arbitrary magical numbers. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1684 #define MAGICWORD 0xfedabeeb
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1685 #define MAGICBYTE ((char) 0xd7)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1686
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1687 struct hdr
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1688 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1689 size_t size; /* Exact size requested by user. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1690 unsigned long int magic; /* Magic number to check header integrity. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1691 };
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1692
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1693 static void checkhdr __P ((__const struct hdr *));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1694 static void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1695 checkhdr (hdr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1696 __const struct hdr *hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1697 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1698 if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1699 (*abortfunc) ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1700 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1701
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1702 static void freehook __P ((__ptr_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1703 static void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1704 freehook (ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1705 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1706 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1707 struct hdr *hdr = ((struct hdr *) ptr) - 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1708 checkhdr (hdr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1709 hdr->magic = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1710 __free_hook = old_free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1711 free (hdr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1712 __free_hook = freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1713 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1714
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1715 static __ptr_t mallochook __P ((size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1716 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1717 mallochook (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1718 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1719 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1720 struct hdr *hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1721
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1722 __malloc_hook = old_malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1723 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1724 __malloc_hook = mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1725 if (hdr == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1726 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1727
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1728 hdr->size = size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1729 hdr->magic = MAGICWORD;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1730 ((char *) &hdr[1])[size] = MAGICBYTE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1731 return (__ptr_t) (hdr + 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1732 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1733
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1734 static __ptr_t reallochook __P ((__ptr_t, size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1735 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1736 reallochook (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1737 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1738 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1739 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1740 struct hdr *hdr = ((struct hdr *) ptr) - 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1741
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1742 checkhdr (hdr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1743 __free_hook = old_free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1744 __malloc_hook = old_malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1745 __realloc_hook = old_realloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1746 hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1747 __free_hook = freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1748 __malloc_hook = mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1749 __realloc_hook = reallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1750 if (hdr == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1751 return NULL;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1752
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1753 hdr->size = size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1754 ((char *) &hdr[1])[size] = MAGICBYTE;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1755 return (__ptr_t) (hdr + 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1756 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1757
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1758 int
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1759 mcheck (func)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1760 void (*func) __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1761 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1762 extern void abort __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1763 static int mcheck_used = 0;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1764
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1765 abortfunc = (func != NULL) ? func : abort;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1766
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1767 /* These hooks may not be safely inserted if malloc is already in use. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1768 if (!__malloc_initialized && !mcheck_used)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1769 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1770 old_free_hook = __free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1771 __free_hook = freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1772 old_malloc_hook = __malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1773 __malloc_hook = mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1774 old_realloc_hook = __realloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1775 __realloc_hook = reallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1776 mcheck_used = 1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1777 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1778
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1779 return mcheck_used ? 0 : -1;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1780 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1781 /* More debugging hooks for `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1782 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1783 Written April 2, 1991 by John Gilmore of Cygnus Support.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1784 Based on mcheck.c by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1785
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1786 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1787 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1788 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1789 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1790
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1791 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1792 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1793 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1794 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1795
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1796 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1797 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1798 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1799 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1800
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1801 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1802 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1803
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1804 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1805 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1806 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1807 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1808
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1809 #include <stdio.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1810
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1811 #ifndef __GNU_LIBRARY__
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1812 extern char *getenv ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1813 #else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1814 #include <stdlib.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1815 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1816
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1817 static FILE *mallstream;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1818 static char mallenv[]= "MALLOC_TRACE";
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1819 static char mallbuf[BUFSIZ]; /* Buffer for the output. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1820
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1821 /* Address to breakpoint on accesses to... */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1822 __ptr_t mallwatch;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1823
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1824 /* Old hook values. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1825 static __ptr_t (*tr_old_morecore) __P ((ptrdiff_t increment));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1826 static __ptr_t (*tr_old_lesscore) __P ((__ptr_t ptr, ptrdiff_t increment));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1827 static void (*tr_old_free_hook) __P ((__ptr_t ptr));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1828 static __ptr_t (*tr_old_malloc_hook) __P ((size_t size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1829 static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, size_t size));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1830
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1831 /* This function is called when the block being alloc'd, realloc'd, or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1832 freed has an address matching the variable "mallwatch". In a debugger,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1833 set "mallwatch" to the address of interest, then put a breakpoint on
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1834 tr_break. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1835
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1836 void tr_break __P ((void));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1837 void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1838 tr_break ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1839 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1840 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1841
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1842 static void tr_freehook __P ((__ptr_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1843 static void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1844 tr_freehook (ptr)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1845 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1846 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1847 fprintf (mallstream, "- %p\n", ptr); /* Be sure to print it first. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1848 if (ptr == mallwatch)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1849 tr_break ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1850 __free_hook = tr_old_free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1851 free (ptr);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1852 __free_hook = tr_freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1853 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1854
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1855 static __ptr_t tr_morecore __P ((ptrdiff_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1856 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1857 tr_morecore (increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1858 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1859 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1860 __ptr_t p;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1861
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1862 __morecore = tr_old_morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1863 p = (__ptr_t) (*__morecore) (increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1864 __morecore = tr_morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1865
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1866 fprintf (mallstream, "$ %p %d\n", p, increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1867
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1868 return p;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1869 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1870
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1871 static __ptr_t tr_lesscore __P ((__ptr_t, ptrdiff_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1872 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1873 tr_lesscore (ptr, increment)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1874 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1875 ptrdiff_t increment;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1876 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1877 __ptr_t p;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1878
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1879 __lesscore = tr_old_lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1880 p = (__ptr_t) (*__lesscore) (ptr, increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1881 __lesscore = tr_lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1882
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1883 fprintf (mallstream, "* %p (%p, %d)\n", p, ptr, increment);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1884
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1885 return p;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1886 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1887
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1888 static __ptr_t tr_mallochook __P ((size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1889 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1890 tr_mallochook (size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1891 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1892 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1893 __ptr_t hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1894
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1895 __malloc_hook = tr_old_malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1896 hdr = (__ptr_t) malloc (size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1897 __malloc_hook = tr_mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1898
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1899 /* We could be printing a NULL here; that's OK. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1900 fprintf (mallstream, "+ %p %x\n", hdr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1901
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1902 if (hdr == mallwatch)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1903 tr_break ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1904
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1905 return hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1906 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1907
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1908 static __ptr_t tr_reallochook __P ((__ptr_t, size_t));
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1909 static __ptr_t
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1910 tr_reallochook (ptr, size)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1911 __ptr_t ptr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1912 size_t size;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1913 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1914 __ptr_t hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1915
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1916 if (ptr == mallwatch)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1917 tr_break ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1918
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1919 __free_hook = tr_old_free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1920 __malloc_hook = tr_old_malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1921 __realloc_hook = tr_old_realloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1922 hdr = (__ptr_t) realloc (ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1923 __free_hook = tr_freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1924 __malloc_hook = tr_mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1925 __realloc_hook = tr_reallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1926 if (hdr == NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1927 /* Failed realloc. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1928 fprintf (mallstream, "! %p %x\n", ptr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1929 else
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1930 fprintf (mallstream, "< %p\n> %p %x\n", ptr, hdr, size);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1931
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1932 if (hdr == mallwatch)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1933 tr_break ();
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1934
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1935 return hdr;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1936 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1937
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1938 /* We enable tracing if either the environment variable MALLOC_TRACE
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1939 is set, or if the variable mallwatch has been patched to an address
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1940 that the debugging user wants us to stop on. When patching mallwatch,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1941 don't forget to set a breakpoint on tr_break! */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1942
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1943 void
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1944 mtrace ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1945 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1946 char *mallfile;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1947
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1948 mallfile = getenv (mallenv);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1949 if (mallfile != NULL || mallwatch != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1950 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1951 mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1952 if (mallstream != NULL)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1953 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1954 /* Be sure it doesn't malloc its buffer! */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1955 setbuf (mallstream, mallbuf);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1956 fprintf (mallstream, "= Start\n");
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1957 #if defined(emacs) && defined(VMS)
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1958 fprintf (mallstream, "= Initial buffer spans %p -- %p\n",
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1959 vms_initial_buffer, vms_end_brk + 1);
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1960 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1961 tr_old_morecore = __morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1962 __morecore = tr_morecore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1963 tr_old_lesscore = __lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1964 __lesscore = tr_lesscore;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1965 tr_old_free_hook = __free_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1966 __free_hook = tr_freehook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1967 tr_old_malloc_hook = __malloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1968 __malloc_hook = tr_mallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1969 tr_old_realloc_hook = __realloc_hook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1970 __realloc_hook = tr_reallochook;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1971 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1972 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1973 }
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1974 /* Access the statistics maintained by `malloc'.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1975 Copyright 1990, 1991, 1992 Free Software Foundation
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1976 Written May 1989 by Mike Haertel.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1977
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1978 This library is free software; you can redistribute it and/or
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1979 modify it under the terms of the GNU Library General Public License as
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1980 published by the Free Software Foundation; either version 2 of the
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1981 License, or (at your option) any later version.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1982
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1983 This library is distributed in the hope that it will be useful,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1984 but WITHOUT ANY WARRANTY; without even the implied warranty of
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1985 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1986 Library General Public License for more details.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1987
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1988 You should have received a copy of the GNU Library General Public
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1989 License along with this library; see the file COPYING.LIB. If
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1990 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1991 Cambridge, MA 02139, USA.
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1992
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1993 The author may be reached (Email) at the address mike@ai.mit.edu,
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1994 or (US mail) as Mike Haertel c/o Free Software Foundation. */
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1995
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1996 #ifndef _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1997 #define _MALLOC_INTERNAL
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1998 #include <malloc.h>
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1999 #endif
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2000
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2001 struct mstats
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2002 mstats ()
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2003 {
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2004 struct mstats result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2005
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2006 result.bytes_total = (char *) (*__morecore) (0) - _heapbase;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2007 result.chunks_used = _chunks_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2008 result.bytes_used = _bytes_used;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2009 result.chunks_free = _chunks_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2010 result.bytes_free = _bytes_free;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2011 return result;
daa4784ba863 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2012 }