(x_to_w32_font): Initialize dpi from dpyinfo->resy.
[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 defined STDC_HEADERS || defined PROTOTYPES
41 #undef PP
42 #define PP(args) args
43 #undef __ptr_t
44 #define __ptr_t void *
45 #else /* Not C++ or ANSI C. */
46 #undef PP
47 #define PP(args) ()
48 #undef __ptr_t
49 #define __ptr_t char *
50 #endif /* C++ or ANSI C. */
51
52 #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
53 #include <string.h>
54 #else
55 #ifndef memset
56 #define memset(s, zero, n) bzero ((s), (n))
57 #endif
58 #ifndef memcpy
59 #define memcpy(d, s, n) bcopy ((s), (d), (n))
60 #endif
61 #endif
62
63 #ifdef HAVE_LIMITS_H
64 #include <limits.h>
65 #endif
66 #ifndef CHAR_BIT
67 #define CHAR_BIT 8
68 #endif
69
70 #ifdef HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73
74 #endif /* _MALLOC_INTERNAL. */
75
76
77 #ifdef __cplusplus
78 extern "C"
79 {
80 #endif
81
82 #ifdef STDC_HEADERS
83 #include <stddef.h>
84 #define __malloc_size_t size_t
85 #define __malloc_ptrdiff_t ptrdiff_t
86 #else
87 #define __malloc_size_t unsigned int
88 #define __malloc_ptrdiff_t int
89 #endif
90
91 #ifndef NULL
92 #define NULL 0
93 #endif
94
95 #ifndef FREE_RETURN_TYPE
96 #define FREE_RETURN_TYPE void
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 FREE_RETURN_TYPE 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 #ifdef GC_MALLOC_CHECK
682 result = _malloc_internal (BLOCKSIZE);
683 #else
684 result = malloc (BLOCKSIZE);
685 #endif
686 if (result == NULL)
687 return NULL;
688
689 /* Link all fragments but the first into the free list. */
690 next = (struct list *) ((char *) result + (1 << log));
691 next->next = NULL;
692 next->prev = &_fraghead[log];
693 _fraghead[log].next = next;
694
695 for (i = 2; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
696 {
697 next = (struct list *) ((char *) result + (i << log));
698 next->next = _fraghead[log].next;
699 next->prev = &_fraghead[log];
700 next->prev->next = next;
701 next->next->prev = next;
702 }
703
704 /* Initialize the nfree and first counters for this block. */
705 block = BLOCK (result);
706 _heapinfo[block].busy.type = log;
707 _heapinfo[block].busy.info.frag.nfree = i - 1;
708 _heapinfo[block].busy.info.frag.first = i - 1;
709
710 _chunks_free += (BLOCKSIZE >> log) - 1;
711 _bytes_free += BLOCKSIZE - (1 << log);
712 _bytes_used -= BLOCKSIZE - (1 << log);
713 }
714 }
715 else
716 {
717 /* Large allocation to receive one or more blocks.
718 Search the free list in a circle starting at the last place visited.
719 If we loop completely around without finding a large enough
720 space we will have to get more memory from the system. */
721 blocks = BLOCKIFY (size);
722 start = block = _heapindex;
723 while (_heapinfo[block].free.size < blocks)
724 {
725 block = _heapinfo[block].free.next;
726 if (block == start)
727 {
728 /* Need to get more from the system. Get a little extra. */
729 __malloc_size_t wantblocks = blocks + __malloc_extra_blocks;
730 block = _heapinfo[0].free.prev;
731 lastblocks = _heapinfo[block].free.size;
732 /* Check to see if the new core will be contiguous with the
733 final free block; if so we don't need to get as much. */
734 if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
735 /* We can't do this if we will have to make the heap info
736 table bigger to accomodate the new space. */
737 block + wantblocks <= heapsize &&
738 get_contiguous_space ((wantblocks - lastblocks) * BLOCKSIZE,
739 ADDRESS (block + lastblocks)))
740 {
741 /* We got it contiguously. Which block we are extending
742 (the `final free block' referred to above) might have
743 changed, if it got combined with a freed info table. */
744 block = _heapinfo[0].free.prev;
745 _heapinfo[block].free.size += (wantblocks - lastblocks);
746 _bytes_free += (wantblocks - lastblocks) * BLOCKSIZE;
747 _heaplimit += wantblocks - lastblocks;
748 continue;
749 }
750 result = morecore (wantblocks * BLOCKSIZE);
751 if (result == NULL)
752 return NULL;
753 block = BLOCK (result);
754 /* Put the new block at the end of the free list. */
755 _heapinfo[block].free.size = wantblocks;
756 _heapinfo[block].free.prev = _heapinfo[0].free.prev;
757 _heapinfo[block].free.next = 0;
758 _heapinfo[0].free.prev = block;
759 _heapinfo[_heapinfo[block].free.prev].free.next = block;
760 ++_chunks_free;
761 /* Now loop to use some of that block for this allocation. */
762 }
763 }
764
765 /* At this point we have found a suitable free list entry.
766 Figure out how to remove what we need from the list. */
767 result = ADDRESS (block);
768 if (_heapinfo[block].free.size > blocks)
769 {
770 /* The block we found has a bit left over,
771 so relink the tail end back into the free list. */
772 _heapinfo[block + blocks].free.size
773 = _heapinfo[block].free.size - blocks;
774 _heapinfo[block + blocks].free.next
775 = _heapinfo[block].free.next;
776 _heapinfo[block + blocks].free.prev
777 = _heapinfo[block].free.prev;
778 _heapinfo[_heapinfo[block].free.prev].free.next
779 = _heapinfo[_heapinfo[block].free.next].free.prev
780 = _heapindex = block + blocks;
781 }
782 else
783 {
784 /* The block exactly matches our requirements,
785 so just remove it from the list. */
786 _heapinfo[_heapinfo[block].free.next].free.prev
787 = _heapinfo[block].free.prev;
788 _heapinfo[_heapinfo[block].free.prev].free.next
789 = _heapindex = _heapinfo[block].free.next;
790 --_chunks_free;
791 }
792
793 _heapinfo[block].busy.type = 0;
794 _heapinfo[block].busy.info.size = blocks;
795 ++_chunks_used;
796 _bytes_used += blocks * BLOCKSIZE;
797 _bytes_free -= blocks * BLOCKSIZE;
798
799 /* Mark all the blocks of the object just allocated except for the
800 first with a negative number so you can find the first block by
801 adding that adjustment. */
802 while (--blocks > 0)
803 _heapinfo[block + blocks].busy.info.size = -blocks;
804 }
805
806 return result;
807 }
808
809 __ptr_t
810 malloc (size)
811 __malloc_size_t size;
812 {
813 if (!__malloc_initialized && !__malloc_initialize ())
814 return NULL;
815
816 return (__malloc_hook != NULL ? *__malloc_hook : _malloc_internal) (size);
817 }
818 \f
819 #ifndef _LIBC
820
821 /* On some ANSI C systems, some libc functions call _malloc, _free
822 and _realloc. Make them use the GNU functions. */
823
824 __ptr_t
825 _malloc (size)
826 __malloc_size_t size;
827 {
828 return malloc (size);
829 }
830
831 void
832 _free (ptr)
833 __ptr_t ptr;
834 {
835 free (ptr);
836 }
837
838 __ptr_t
839 _realloc (ptr, size)
840 __ptr_t ptr;
841 __malloc_size_t size;
842 {
843 return realloc (ptr, size);
844 }
845
846 #endif
847 /* Free a block of memory allocated by `malloc'.
848 Copyright 1990, 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
849 Written May 1989 by Mike Haertel.
850
851 This library is free software; you can redistribute it and/or
852 modify it under the terms of the GNU Library General Public License as
853 published by the Free Software Foundation; either version 2 of the
854 License, or (at your option) any later version.
855
856 This library is distributed in the hope that it will be useful,
857 but WITHOUT ANY WARRANTY; without even the implied warranty of
858 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
859 Library General Public License for more details.
860
861 You should have received a copy of the GNU Library General Public
862 License along with this library; see the file COPYING.LIB. If
863 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
864 Cambridge, MA 02139, USA.
865
866 The author may be reached (Email) at the address mike@ai.mit.edu,
867 or (US mail) as Mike Haertel c/o Free Software Foundation. */
868
869 #ifndef _MALLOC_INTERNAL
870 #define _MALLOC_INTERNAL
871 #include <malloc.h>
872 #endif
873
874
875 /* Cope with systems lacking `memmove'. */
876 #ifndef memmove
877 #if (defined (MEMMOVE_MISSING) || \
878 !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
879 #ifdef emacs
880 #undef __malloc_safe_bcopy
881 #define __malloc_safe_bcopy safe_bcopy
882 #endif
883 /* This function is defined in realloc.c. */
884 extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
885 #define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
886 #endif
887 #endif
888
889
890 /* Debugging hook for free. */
891 void (*__free_hook) PP ((__ptr_t __ptr));
892
893 /* List of blocks allocated by memalign. */
894 struct alignlist *_aligned_blocks = NULL;
895
896 /* Return memory to the heap.
897 Like `free' but don't call a __free_hook if there is one. */
898 void
899 _free_internal (ptr)
900 __ptr_t ptr;
901 {
902 int type;
903 __malloc_size_t block, blocks;
904 register __malloc_size_t i;
905 struct list *prev, *next;
906 __ptr_t curbrk;
907 const __malloc_size_t lesscore_threshold
908 /* Threshold of free space at which we will return some to the system. */
909 = FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks;
910
911 register struct alignlist *l;
912
913 if (ptr == NULL)
914 return;
915
916 for (l = _aligned_blocks; l != NULL; l = l->next)
917 if (l->aligned == ptr)
918 {
919 l->aligned = NULL; /* Mark the slot in the list as free. */
920 ptr = l->exact;
921 break;
922 }
923
924 block = BLOCK (ptr);
925
926 type = _heapinfo[block].busy.type;
927 switch (type)
928 {
929 case 0:
930 /* Get as many statistics as early as we can. */
931 --_chunks_used;
932 _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
933 _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
934
935 /* Find the free cluster previous to this one in the free list.
936 Start searching at the last block referenced; this may benefit
937 programs with locality of allocation. */
938 i = _heapindex;
939 if (i > block)
940 while (i > block)
941 i = _heapinfo[i].free.prev;
942 else
943 {
944 do
945 i = _heapinfo[i].free.next;
946 while (i > 0 && i < block);
947 i = _heapinfo[i].free.prev;
948 }
949
950 /* Determine how to link this block into the free list. */
951 if (block == i + _heapinfo[i].free.size)
952 {
953 /* Coalesce this block with its predecessor. */
954 _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
955 block = i;
956 }
957 else
958 {
959 /* Really link this block back into the free list. */
960 _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
961 _heapinfo[block].free.next = _heapinfo[i].free.next;
962 _heapinfo[block].free.prev = i;
963 _heapinfo[i].free.next = block;
964 _heapinfo[_heapinfo[block].free.next].free.prev = block;
965 ++_chunks_free;
966 }
967
968 /* Now that the block is linked in, see if we can coalesce it
969 with its successor (by deleting its successor from the list
970 and adding in its size). */
971 if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
972 {
973 _heapinfo[block].free.size
974 += _heapinfo[_heapinfo[block].free.next].free.size;
975 _heapinfo[block].free.next
976 = _heapinfo[_heapinfo[block].free.next].free.next;
977 _heapinfo[_heapinfo[block].free.next].free.prev = block;
978 --_chunks_free;
979 }
980
981 /* How many trailing free blocks are there now? */
982 blocks = _heapinfo[block].free.size;
983
984 /* Where is the current end of accessible core? */
985 curbrk = (*__morecore) (0);
986
987 if (_heaplimit != 0 && curbrk == ADDRESS (_heaplimit))
988 {
989 /* The end of the malloc heap is at the end of accessible core.
990 It's possible that moving _heapinfo will allow us to
991 return some space to the system. */
992
993 __malloc_size_t info_block = BLOCK (_heapinfo);
994 __malloc_size_t info_blocks = _heapinfo[info_block].busy.info.size;
995 __malloc_size_t prev_block = _heapinfo[block].free.prev;
996 __malloc_size_t prev_blocks = _heapinfo[prev_block].free.size;
997 __malloc_size_t next_block = _heapinfo[block].free.next;
998 __malloc_size_t next_blocks = _heapinfo[next_block].free.size;
999
1000 if (/* Win if this block being freed is last in core, the info table
1001 is just before it, the previous free block is just before the
1002 info table, and the two free blocks together form a useful
1003 amount to return to the system. */
1004 (block + blocks == _heaplimit &&
1005 info_block + info_blocks == block &&
1006 prev_block != 0 && prev_block + prev_blocks == info_block &&
1007 blocks + prev_blocks >= lesscore_threshold) ||
1008 /* Nope, not the case. We can also win if this block being
1009 freed is just before the info table, and the table extends
1010 to the end of core or is followed only by a free block,
1011 and the total free space is worth returning to the system. */
1012 (block + blocks == info_block &&
1013 ((info_block + info_blocks == _heaplimit &&
1014 blocks >= lesscore_threshold) ||
1015 (info_block + info_blocks == next_block &&
1016 next_block + next_blocks == _heaplimit &&
1017 blocks + next_blocks >= lesscore_threshold)))
1018 )
1019 {
1020 malloc_info *newinfo;
1021 __malloc_size_t oldlimit = _heaplimit;
1022
1023 /* Free the old info table, clearing _heaplimit to avoid
1024 recursion into this code. We don't want to return the
1025 table's blocks to the system before we have copied them to
1026 the new location. */
1027 _heaplimit = 0;
1028 _free_internal (_heapinfo);
1029 _heaplimit = oldlimit;
1030
1031 /* Tell malloc to search from the beginning of the heap for
1032 free blocks, so it doesn't reuse the ones just freed. */
1033 _heapindex = 0;
1034
1035 /* Allocate new space for the info table and move its data. */
1036 newinfo = (malloc_info *) _malloc_internal (info_blocks
1037 * BLOCKSIZE);
1038 memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE);
1039 _heapinfo = newinfo;
1040
1041 /* We should now have coalesced the free block with the
1042 blocks freed from the old info table. Examine the entire
1043 trailing free block to decide below whether to return some
1044 to the system. */
1045 block = _heapinfo[0].free.prev;
1046 blocks = _heapinfo[block].free.size;
1047 }
1048
1049 /* Now see if we can return stuff to the system. */
1050 if (block + blocks == _heaplimit && blocks >= lesscore_threshold)
1051 {
1052 register __malloc_size_t bytes = blocks * BLOCKSIZE;
1053 _heaplimit -= blocks;
1054 (*__morecore) (-bytes);
1055 _heapinfo[_heapinfo[block].free.prev].free.next
1056 = _heapinfo[block].free.next;
1057 _heapinfo[_heapinfo[block].free.next].free.prev
1058 = _heapinfo[block].free.prev;
1059 block = _heapinfo[block].free.prev;
1060 --_chunks_free;
1061 _bytes_free -= bytes;
1062 }
1063 }
1064
1065 /* Set the next search to begin at this block. */
1066 _heapindex = block;
1067 break;
1068
1069 default:
1070 /* Do some of the statistics. */
1071 --_chunks_used;
1072 _bytes_used -= 1 << type;
1073 ++_chunks_free;
1074 _bytes_free += 1 << type;
1075
1076 /* Get the address of the first free fragment in this block. */
1077 prev = (struct list *) ((char *) ADDRESS (block) +
1078 (_heapinfo[block].busy.info.frag.first << type));
1079
1080 if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
1081 {
1082 /* If all fragments of this block are free, remove them
1083 from the fragment list and free the whole block. */
1084 next = prev;
1085 for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
1086 next = next->next;
1087 prev->prev->next = next;
1088 if (next != NULL)
1089 next->prev = prev->prev;
1090 _heapinfo[block].busy.type = 0;
1091 _heapinfo[block].busy.info.size = 1;
1092
1093 /* Keep the statistics accurate. */
1094 ++_chunks_used;
1095 _bytes_used += BLOCKSIZE;
1096 _chunks_free -= BLOCKSIZE >> type;
1097 _bytes_free -= BLOCKSIZE;
1098
1099 #ifdef GC_MALLOC_CHECK
1100 _free_internal (ADDRESS (block));
1101 #else
1102 free (ADDRESS (block));
1103 #endif
1104 }
1105 else if (_heapinfo[block].busy.info.frag.nfree != 0)
1106 {
1107 /* If some fragments of this block are free, link this
1108 fragment into the fragment list after the first free
1109 fragment of this block. */
1110 next = (struct list *) ptr;
1111 next->next = prev->next;
1112 next->prev = prev;
1113 prev->next = next;
1114 if (next->next != NULL)
1115 next->next->prev = next;
1116 ++_heapinfo[block].busy.info.frag.nfree;
1117 }
1118 else
1119 {
1120 /* No fragments of this block are free, so link this
1121 fragment into the fragment list and announce that
1122 it is the first free fragment of this block. */
1123 prev = (struct list *) ptr;
1124 _heapinfo[block].busy.info.frag.nfree = 1;
1125 _heapinfo[block].busy.info.frag.first = (unsigned long int)
1126 ((unsigned long int) ((char *) ptr - (char *) NULL)
1127 % BLOCKSIZE >> type);
1128 prev->next = _fraghead[type].next;
1129 prev->prev = &_fraghead[type];
1130 prev->prev->next = prev;
1131 if (prev->next != NULL)
1132 prev->next->prev = prev;
1133 }
1134 break;
1135 }
1136 }
1137
1138 /* Return memory to the heap. */
1139
1140 FREE_RETURN_TYPE
1141 free (ptr)
1142 __ptr_t ptr;
1143 {
1144 if (__free_hook != NULL)
1145 (*__free_hook) (ptr);
1146 else
1147 _free_internal (ptr);
1148 }
1149
1150 /* Define the `cfree' alias for `free'. */
1151 #ifdef weak_alias
1152 weak_alias (free, cfree)
1153 #else
1154 void
1155 cfree (ptr)
1156 __ptr_t ptr;
1157 {
1158 free (ptr);
1159 }
1160 #endif
1161 /* Change the size of a block allocated by `malloc'.
1162 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1163 Written May 1989 by Mike Haertel.
1164
1165 This library is free software; you can redistribute it and/or
1166 modify it under the terms of the GNU Library General Public License as
1167 published by the Free Software Foundation; either version 2 of the
1168 License, or (at your option) any later version.
1169
1170 This library is distributed in the hope that it will be useful,
1171 but WITHOUT ANY WARRANTY; without even the implied warranty of
1172 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1173 Library General Public License for more details.
1174
1175 You should have received a copy of the GNU Library General Public
1176 License along with this library; see the file COPYING.LIB. If
1177 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
1178 Cambridge, MA 02139, USA.
1179
1180 The author may be reached (Email) at the address mike@ai.mit.edu,
1181 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1182
1183 #ifndef _MALLOC_INTERNAL
1184 #define _MALLOC_INTERNAL
1185 #include <malloc.h>
1186 #endif
1187
1188
1189
1190 /* Cope with systems lacking `memmove'. */
1191 #if (defined (MEMMOVE_MISSING) || \
1192 !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
1193
1194 #ifdef emacs
1195 #undef __malloc_safe_bcopy
1196 #define __malloc_safe_bcopy safe_bcopy
1197 #else
1198
1199 /* Snarfed directly from Emacs src/dispnew.c:
1200 XXX Should use system bcopy if it handles overlap. */
1201
1202 /* Like bcopy except never gets confused by overlap. */
1203
1204 void
1205 __malloc_safe_bcopy (afrom, ato, size)
1206 __ptr_t afrom;
1207 __ptr_t ato;
1208 __malloc_size_t size;
1209 {
1210 char *from = afrom, *to = ato;
1211
1212 if (size <= 0 || from == to)
1213 return;
1214
1215 /* If the source and destination don't overlap, then bcopy can
1216 handle it. If they do overlap, but the destination is lower in
1217 memory than the source, we'll assume bcopy can handle that. */
1218 if (to < from || from + size <= to)
1219 bcopy (from, to, size);
1220
1221 /* Otherwise, we'll copy from the end. */
1222 else
1223 {
1224 register char *endf = from + size;
1225 register char *endt = to + size;
1226
1227 /* If TO - FROM is large, then we should break the copy into
1228 nonoverlapping chunks of TO - FROM bytes each. However, if
1229 TO - FROM is small, then the bcopy function call overhead
1230 makes this not worth it. The crossover point could be about
1231 anywhere. Since I don't think the obvious copy loop is too
1232 bad, I'm trying to err in its favor. */
1233 if (to - from < 64)
1234 {
1235 do
1236 *--endt = *--endf;
1237 while (endf != from);
1238 }
1239 else
1240 {
1241 for (;;)
1242 {
1243 endt -= (to - from);
1244 endf -= (to - from);
1245
1246 if (endt < to)
1247 break;
1248
1249 bcopy (endf, endt, to - from);
1250 }
1251
1252 /* If SIZE wasn't a multiple of TO - FROM, there will be a
1253 little left over. The amount left over is
1254 (endt + (to - from)) - to, which is endt - from. */
1255 bcopy (from, to, endt - from);
1256 }
1257 }
1258 }
1259 #endif /* emacs */
1260
1261 #ifndef memmove
1262 extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
1263 #define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
1264 #endif
1265
1266 #endif
1267
1268
1269 #define min(A, B) ((A) < (B) ? (A) : (B))
1270
1271 /* Debugging hook for realloc. */
1272 __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
1273
1274 /* Resize the given region to the new size, returning a pointer
1275 to the (possibly moved) region. This is optimized for speed;
1276 some benchmarks seem to indicate that greater compactness is
1277 achieved by unconditionally allocating and copying to a
1278 new region. This module has incestuous knowledge of the
1279 internals of both free and malloc. */
1280 __ptr_t
1281 _realloc_internal (ptr, size)
1282 __ptr_t ptr;
1283 __malloc_size_t size;
1284 {
1285 __ptr_t result;
1286 int type;
1287 __malloc_size_t block, blocks, oldlimit;
1288
1289 if (size == 0)
1290 {
1291 _free_internal (ptr);
1292 return _malloc_internal (0);
1293 }
1294 else if (ptr == NULL)
1295 return _malloc_internal (size);
1296
1297 block = BLOCK (ptr);
1298
1299 type = _heapinfo[block].busy.type;
1300 switch (type)
1301 {
1302 case 0:
1303 /* Maybe reallocate a large block to a small fragment. */
1304 if (size <= BLOCKSIZE / 2)
1305 {
1306 result = _malloc_internal (size);
1307 if (result != NULL)
1308 {
1309 memcpy (result, ptr, size);
1310 _free_internal (ptr);
1311 return result;
1312 }
1313 }
1314
1315 /* The new size is a large allocation as well;
1316 see if we can hold it in place. */
1317 blocks = BLOCKIFY (size);
1318 if (blocks < _heapinfo[block].busy.info.size)
1319 {
1320 /* The new size is smaller; return
1321 excess memory to the free list. */
1322 _heapinfo[block + blocks].busy.type = 0;
1323 _heapinfo[block + blocks].busy.info.size
1324 = _heapinfo[block].busy.info.size - blocks;
1325 _heapinfo[block].busy.info.size = blocks;
1326 /* We have just created a new chunk by splitting a chunk in two.
1327 Now we will free this chunk; increment the statistics counter
1328 so it doesn't become wrong when _free_internal decrements it. */
1329 ++_chunks_used;
1330 _free_internal (ADDRESS (block + blocks));
1331 result = ptr;
1332 }
1333 else if (blocks == _heapinfo[block].busy.info.size)
1334 /* No size change necessary. */
1335 result = ptr;
1336 else
1337 {
1338 /* Won't fit, so allocate a new region that will.
1339 Free the old region first in case there is sufficient
1340 adjacent free space to grow without moving. */
1341 blocks = _heapinfo[block].busy.info.size;
1342 /* Prevent free from actually returning memory to the system. */
1343 oldlimit = _heaplimit;
1344 _heaplimit = 0;
1345 _free_internal (ptr);
1346 result = _malloc_internal (size);
1347 if (_heaplimit == 0)
1348 _heaplimit = oldlimit;
1349 if (result == NULL)
1350 {
1351 /* Now we're really in trouble. We have to unfree
1352 the thing we just freed. Unfortunately it might
1353 have been coalesced with its neighbors. */
1354 if (_heapindex == block)
1355 (void) _malloc_internal (blocks * BLOCKSIZE);
1356 else
1357 {
1358 __ptr_t previous
1359 = _malloc_internal ((block - _heapindex) * BLOCKSIZE);
1360 (void) _malloc_internal (blocks * BLOCKSIZE);
1361 _free_internal (previous);
1362 }
1363 return NULL;
1364 }
1365 if (ptr != result)
1366 memmove (result, ptr, blocks * BLOCKSIZE);
1367 }
1368 break;
1369
1370 default:
1371 /* Old size is a fragment; type is logarithm
1372 to base two of the fragment size. */
1373 if (size > (__malloc_size_t) (1 << (type - 1)) &&
1374 size <= (__malloc_size_t) (1 << type))
1375 /* The new size is the same kind of fragment. */
1376 result = ptr;
1377 else
1378 {
1379 /* The new size is different; allocate a new space,
1380 and copy the lesser of the new size and the old. */
1381 result = _malloc_internal (size);
1382 if (result == NULL)
1383 return NULL;
1384 memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
1385 _free_internal (ptr);
1386 }
1387 break;
1388 }
1389
1390 return result;
1391 }
1392
1393 __ptr_t
1394 realloc (ptr, size)
1395 __ptr_t ptr;
1396 __malloc_size_t size;
1397 {
1398 if (!__malloc_initialized && !__malloc_initialize ())
1399 return NULL;
1400
1401 return (__realloc_hook != NULL ? *__realloc_hook : _realloc_internal)
1402 (ptr, size);
1403 }
1404 /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
1405
1406 This library is free software; you can redistribute it and/or
1407 modify it under the terms of the GNU Library General Public License as
1408 published by the Free Software Foundation; either version 2 of the
1409 License, or (at your option) any later version.
1410
1411 This library is distributed in the hope that it will be useful,
1412 but WITHOUT ANY WARRANTY; without even the implied warranty of
1413 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1414 Library General Public License for more details.
1415
1416 You should have received a copy of the GNU Library General Public
1417 License along with this library; see the file COPYING.LIB. If
1418 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
1419 Cambridge, MA 02139, USA.
1420
1421 The author may be reached (Email) at the address mike@ai.mit.edu,
1422 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1423
1424 #ifndef _MALLOC_INTERNAL
1425 #define _MALLOC_INTERNAL
1426 #include <malloc.h>
1427 #endif
1428
1429 /* Allocate an array of NMEMB elements each SIZE bytes long.
1430 The entire array is initialized to zeros. */
1431 __ptr_t
1432 calloc (nmemb, size)
1433 register __malloc_size_t nmemb;
1434 register __malloc_size_t size;
1435 {
1436 register __ptr_t result = malloc (nmemb * size);
1437
1438 if (result != NULL)
1439 (void) memset (result, 0, nmemb * size);
1440
1441 return result;
1442 }
1443 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1444 This file is part of the GNU C Library.
1445
1446 The GNU C Library is free software; you can redistribute it and/or modify
1447 it under the terms of the GNU General Public License as published by
1448 the Free Software Foundation; either version 2, or (at your option)
1449 any later version.
1450
1451 The GNU C Library is distributed in the hope that it will be useful,
1452 but WITHOUT ANY WARRANTY; without even the implied warranty of
1453 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1454 GNU General Public License for more details.
1455
1456 You should have received a copy of the GNU General Public License
1457 along with the GNU C Library; see the file COPYING. If not, write to
1458 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
1459
1460 #ifndef _MALLOC_INTERNAL
1461 #define _MALLOC_INTERNAL
1462 #include <malloc.h>
1463 #endif
1464
1465 #ifndef __GNU_LIBRARY__
1466 #define __sbrk sbrk
1467 #endif
1468
1469 #ifdef __GNU_LIBRARY__
1470 /* It is best not to declare this and cast its result on foreign operating
1471 systems with potentially hostile include files. */
1472
1473 #include <stddef.h>
1474 extern __ptr_t __sbrk PP ((ptrdiff_t increment));
1475 #endif
1476
1477 #ifndef NULL
1478 #define NULL 0
1479 #endif
1480
1481 /* Allocate INCREMENT more bytes of data space,
1482 and return the start of data space, or NULL on errors.
1483 If INCREMENT is negative, shrink data space. */
1484 __ptr_t
1485 __default_morecore (increment)
1486 __malloc_ptrdiff_t increment;
1487 {
1488 __ptr_t result = (__ptr_t) __sbrk (increment);
1489 if (result == (__ptr_t) -1)
1490 return NULL;
1491 return result;
1492 }
1493 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
1494
1495 This library is free software; you can redistribute it and/or
1496 modify it under the terms of the GNU Library General Public License as
1497 published by the Free Software Foundation; either version 2 of the
1498 License, or (at your option) any later version.
1499
1500 This library is distributed in the hope that it will be useful,
1501 but WITHOUT ANY WARRANTY; without even the implied warranty of
1502 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1503 Library General Public License for more details.
1504
1505 You should have received a copy of the GNU Library General Public
1506 License along with this library; see the file COPYING.LIB. If
1507 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
1508 Cambridge, MA 02139, USA. */
1509
1510 #ifndef _MALLOC_INTERNAL
1511 #define _MALLOC_INTERNAL
1512 #include <malloc.h>
1513 #endif
1514
1515 #if __DJGPP__ - 0 == 1
1516
1517 /* There is some problem with memalign in DJGPP v1 and we are supposed
1518 to omit it. Noone told me why, they just told me to do it. */
1519
1520 #else
1521
1522 __ptr_t (*__memalign_hook) PP ((size_t __size, size_t __alignment));
1523
1524 __ptr_t
1525 memalign (alignment, size)
1526 __malloc_size_t alignment;
1527 __malloc_size_t size;
1528 {
1529 __ptr_t result;
1530 unsigned long int adj, lastadj;
1531
1532 if (__memalign_hook)
1533 return (*__memalign_hook) (alignment, size);
1534
1535 /* Allocate a block with enough extra space to pad the block with up to
1536 (ALIGNMENT - 1) bytes if necessary. */
1537 result = malloc (size + alignment - 1);
1538 if (result == NULL)
1539 return NULL;
1540
1541 /* Figure out how much we will need to pad this particular block
1542 to achieve the required alignment. */
1543 adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
1544
1545 do
1546 {
1547 /* Reallocate the block with only as much excess as it needs. */
1548 free (result);
1549 result = malloc (adj + size);
1550 if (result == NULL) /* Impossible unless interrupted. */
1551 return NULL;
1552
1553 lastadj = adj;
1554 adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
1555 /* It's conceivable we might have been so unlucky as to get a
1556 different block with weaker alignment. If so, this block is too
1557 short to contain SIZE after alignment correction. So we must
1558 try again and get another block, slightly larger. */
1559 } while (adj > lastadj);
1560
1561 if (adj != 0)
1562 {
1563 /* Record this block in the list of aligned blocks, so that `free'
1564 can identify the pointer it is passed, which will be in the middle
1565 of an allocated block. */
1566
1567 struct alignlist *l;
1568 for (l = _aligned_blocks; l != NULL; l = l->next)
1569 if (l->aligned == NULL)
1570 /* This slot is free. Use it. */
1571 break;
1572 if (l == NULL)
1573 {
1574 l = (struct alignlist *) malloc (sizeof (struct alignlist));
1575 if (l == NULL)
1576 {
1577 free (result);
1578 return NULL;
1579 }
1580 l->next = _aligned_blocks;
1581 _aligned_blocks = l;
1582 }
1583 l->exact = result;
1584 result = l->aligned = (char *) result + alignment - adj;
1585 }
1586
1587 return result;
1588 }
1589
1590 #endif /* Not DJGPP v1 */
1591 /* Allocate memory on a page boundary.
1592 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
1593
1594 This library is free software; you can redistribute it and/or
1595 modify it under the terms of the GNU Library General Public License as
1596 published by the Free Software Foundation; either version 2 of the
1597 License, or (at your option) any later version.
1598
1599 This library is distributed in the hope that it will be useful,
1600 but WITHOUT ANY WARRANTY; without even the implied warranty of
1601 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1602 Library General Public License for more details.
1603
1604 You should have received a copy of the GNU Library General Public
1605 License along with this library; see the file COPYING.LIB. If
1606 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
1607 Cambridge, MA 02139, USA.
1608
1609 The author may be reached (Email) at the address mike@ai.mit.edu,
1610 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1611
1612 #if defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC)
1613
1614 /* Emacs defines GMALLOC_INHIBIT_VALLOC to avoid this definition
1615 on MSDOS, where it conflicts with a system header file. */
1616
1617 #define ELIDE_VALLOC
1618
1619 #endif
1620
1621 #ifndef ELIDE_VALLOC
1622
1623 #if defined (__GNU_LIBRARY__) || defined (_LIBC)
1624 #include <stddef.h>
1625 #include <sys/cdefs.h>
1626 #if defined (__GLIBC__) && __GLIBC__ >= 2
1627 /* __getpagesize is already declared in <unistd.h> with return type int */
1628 #else
1629 extern size_t __getpagesize PP ((void));
1630 #endif
1631 #else
1632 #include "getpagesize.h"
1633 #define __getpagesize() getpagesize()
1634 #endif
1635
1636 #ifndef _MALLOC_INTERNAL
1637 #define _MALLOC_INTERNAL
1638 #include <malloc.h>
1639 #endif
1640
1641 static __malloc_size_t pagesize;
1642
1643 __ptr_t
1644 valloc (size)
1645 __malloc_size_t size;
1646 {
1647 if (pagesize == 0)
1648 pagesize = __getpagesize ();
1649
1650 return memalign (pagesize, size);
1651 }
1652
1653 #endif /* Not ELIDE_VALLOC. */