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