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