1 /* This file is no longer automatically generated from libc. */
3 #define _MALLOC_INTERNAL
5 /* The malloc headers and source files from the C library follow here. */
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.
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.
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.
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.
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. */
33 #ifdef _MALLOC_INTERNAL
39 #if defined __cplusplus || (defined (__STDC__) && __STDC__) || \
40 defined STDC_HEADERS || defined PROTOTYPES
44 #define __ptr_t void *
45 #else /* Not C++ or ANSI C. */
49 #define __ptr_t char *
50 #endif /* C++ or ANSI C. */
52 #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
56 #define memset(s, zero, n) bzero ((s), (n))
59 #define memcpy(d, s, n) bcopy ((s), (d), (n))
74 #endif /* _MALLOC_INTERNAL. */
84 #define __malloc_size_t size_t
85 #define __malloc_ptrdiff_t ptrdiff_t
87 #define __malloc_size_t unsigned int
88 #define __malloc_ptrdiff_t int
95 #ifndef FREE_RETURN_TYPE
96 #define FREE_RETURN_TYPE void
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
));
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
));
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
));
122 #ifdef _MALLOC_INTERNAL
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)
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)
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
142 /* Data structure giving per-block information. */
145 /* Heap information for a busy block. */
148 /* Zero for a large (multiblock) object, or positive giving the
149 logarithm to the base two of the fragment size. */
155 __malloc_size_t nfree
; /* Free frags in a fragmented block. */
156 __malloc_size_t first
; /* First free fragment of the block. */
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
;
164 /* Heap information for a free block
165 (that may be the first of a free cluster). */
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. */
174 /* Pointer to first block of the heap. */
175 extern char *_heapbase
;
177 /* Table indexed by block number giving per-block information. */
178 extern malloc_info
*_heapinfo
;
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))
184 /* Current search index for the heap table. */
185 extern __malloc_size_t _heapindex
;
187 /* Limit of valid info table indices. */
188 extern __malloc_size_t _heaplimit
;
190 /* Doubly linked lists of free fragments. */
197 /* Free list headers for each fragment size. */
198 extern struct list _fraghead
[];
200 /* List of blocks allocated with `memalign' (or `valloc'). */
203 struct alignlist
*next
;
204 __ptr_t aligned
; /* The address that memaligned returned. */
205 __ptr_t exact
; /* The address that malloc returned. */
207 extern struct alignlist
*_aligned_blocks
;
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
;
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
));
222 #endif /* _MALLOC_INTERNAL. */
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
));
228 /* Underlying allocation function; successive calls should
229 return contiguous pieces of memory. */
230 extern __ptr_t (*__morecore
) PP ((__malloc_ptrdiff_t __size
));
232 /* Default value of `__morecore'. */
233 extern __ptr_t __default_morecore
PP ((__malloc_ptrdiff_t __size
));
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));
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
;
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));
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
));
256 /* Return values for `mprobe': these are the kinds of inconsistencies that
257 `mcheck' enables detection of. */
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. */
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
))));
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
));
278 /* Activate a standard collection of tracing hooks. */
279 extern void mtrace
PP ((void));
280 extern void muntrace
PP ((void));
282 /* Statistics available to the user. */
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. */
292 /* Pick up the current statistics. */
293 extern struct mstats mstats
PP ((void));
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 *))));
300 /* Relocating allocator. */
302 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
303 extern __ptr_t r_alloc
PP ((__ptr_t
*__handleptr
, __malloc_size_t __size
));
305 /* Free the storage allocated in HANDLEPTR. */
306 extern void r_alloc_free
PP ((__ptr_t
*__handleptr
));
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
));
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.
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.
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.
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.
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. */
339 #ifndef _MALLOC_INTERNAL
340 #define _MALLOC_INTERNAL
345 /* How to really get more memory. */
346 __ptr_t (*__morecore
) PP ((ptrdiff_t __size
)) = __default_morecore
;
348 /* Debugging hook for `malloc'. */
349 __ptr_t (*__malloc_hook
) PP ((__malloc_size_t __size
));
351 /* Pointer to the base of the first block. */
354 /* Block information table. Allocated with align/__free (not malloc/free). */
355 malloc_info
*_heapinfo
;
357 /* Number of info entries. */
358 static __malloc_size_t heapsize
;
360 /* Search index in the info table. */
361 __malloc_size_t _heapindex
;
363 /* Limit of valid info table indices. */
364 __malloc_size_t _heaplimit
;
366 /* Free lists for each fragment size. */
367 struct list _fraghead
[BLOCKLOG
];
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
;
375 /* Are you experienced? */
376 int __malloc_initialized
;
378 __malloc_size_t __malloc_extra_blocks
;
380 void (*__malloc_initialize_hook
) PP ((void));
381 void (*__after_morecore_hook
) PP ((void));
384 /* Aligned allocation. */
385 static __ptr_t align
PP ((__malloc_size_t
));
388 __malloc_size_t size
;
391 unsigned long int adj
;
393 result
= (*__morecore
) (size
);
394 adj
= (unsigned long int) ((unsigned long int) ((char *) result
-
395 (char *) NULL
)) % BLOCKSIZE
;
399 adj
= BLOCKSIZE
- adj
;
400 new = (*__morecore
) (adj
);
401 result
= (char *) result
+ adj
;
404 if (__after_morecore_hook
)
405 (*__after_morecore_hook
) ();
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
));
415 get_contiguous_space (size
, position
)
416 __malloc_ptrdiff_t size
;
422 before
= (*__morecore
) (0);
423 /* If we can tell in advance that the break is at the wrong place,
425 if (before
!= position
)
428 /* Allocate SIZE bytes and get the address of them. */
429 after
= (*__morecore
) (size
);
433 /* It was not contiguous--reject it. */
434 if (after
!= position
)
436 (*__morecore
) (- size
);
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));
454 __malloc_size_t block
, blocks
;
456 block
= BLOCK (_heapinfo
);
457 blocks
= BLOCKIFY (heapsize
* sizeof (malloc_info
));
459 /* Account for the _heapinfo block itself in the statistics. */
460 _bytes_used
+= blocks
* BLOCKSIZE
;
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. */
468 _heapinfo
[block
+ blocks
].busy
.info
.size
= -blocks
;
471 /* Set everything up and remember that we have. */
473 __malloc_initialize ()
475 if (__malloc_initialized
)
478 if (__malloc_initialize_hook
)
479 (*__malloc_initialize_hook
) ();
481 heapsize
= HEAP
/ BLOCKSIZE
;
482 _heapinfo
= (malloc_info
*) align (heapsize
* sizeof (malloc_info
));
483 if (_heapinfo
== NULL
)
485 memset (_heapinfo
, 0, heapsize
* sizeof (malloc_info
));
486 _heapinfo
[0].free
.size
= 0;
487 _heapinfo
[0].free
.next
= _heapinfo
[0].free
.prev
= 0;
489 _heapbase
= (char *) _heapinfo
;
490 _heaplimit
= BLOCK (_heapbase
+ heapsize
* sizeof (malloc_info
));
492 register_heapinfo ();
494 __malloc_initialized
= 1;
498 static int morecore_recursing
;
500 /* Get neatly aligned memory, initializing or
501 growing the heap info table as necessary. */
502 static __ptr_t morecore
PP ((__malloc_size_t
));
505 __malloc_size_t size
;
508 malloc_info
*newinfo
, *oldinfo
;
509 __malloc_size_t newsize
;
511 if (morecore_recursing
)
512 /* Avoid recursion. The caller will know how to handle a null return. */
515 result
= align (size
);
519 /* Check if we need to grow the info table. */
520 if ((__malloc_size_t
) BLOCK ((char *) result
+ size
) > heapsize
)
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
529 while ((__malloc_size_t
) BLOCK ((char *) result
+ size
) > newsize
);
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
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;
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
));
563 /* Allocate new space for the malloc info table. */
566 newinfo
= (malloc_info
*) align (newsize
* sizeof (malloc_info
));
571 (*__morecore
) (-size
);
575 /* Is it big enough to record status for its own space?
577 if ((__malloc_size_t
) BLOCK ((char *) newinfo
578 + newsize
* sizeof (malloc_info
))
582 /* Must try again. First give back most of what we just got. */
583 (*__morecore
) (- newsize
* sizeof (malloc_info
));
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
));
596 register_heapinfo ();
598 /* Reset _heaplimit so _free_internal never decides
599 it can relocate or resize the info table. */
601 _free_internal (oldinfo
);
603 /* The new heap limit includes the new table just allocated. */
604 _heaplimit
= BLOCK ((char *) newinfo
+ heapsize
* sizeof (malloc_info
));
609 _heaplimit
= BLOCK ((char *) result
+ size
);
613 /* Allocate memory from the heap. */
615 _malloc_internal (size
)
616 __malloc_size_t size
;
619 __malloc_size_t block
, blocks
, lastblocks
, start
;
620 register __malloc_size_t i
;
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).
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.
635 if (size
< sizeof (struct list
))
636 size
= sizeof (struct list
);
638 #ifdef SUNOS_LOCALTIME_BUG
643 /* Determine the allocation policy based on the request size. */
644 if (size
<= BLOCKSIZE
/ 2)
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;
650 while ((size
/= 2) != 0)
653 /* Look in the fragment lists for a
654 free fragment of the desired size. */
655 next
= _fraghead
[log
].next
;
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
)
671 /* Update the statistics. */
673 _bytes_used
+= 1 << log
;
675 _bytes_free
-= 1 << log
;
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
);
684 result
= malloc (BLOCKSIZE
);
689 /* Link all fragments but the first into the free list. */
690 next
= (struct list
*) ((char *) result
+ (1 << log
));
692 next
->prev
= &_fraghead
[log
];
693 _fraghead
[log
].next
= next
;
695 for (i
= 2; i
< (__malloc_size_t
) (BLOCKSIZE
>> log
); ++i
)
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
;
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;
710 _chunks_free
+= (BLOCKSIZE
>> log
) - 1;
711 _bytes_free
+= BLOCKSIZE
- (1 << log
);
712 _bytes_used
-= BLOCKSIZE
- (1 << log
);
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
)
725 block
= _heapinfo
[block
].free
.next
;
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
)))
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
;
750 result
= morecore (wantblocks
* BLOCKSIZE
);
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
;
761 /* Now loop to use some of that block for this allocation. */
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
)
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
;
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
;
793 _heapinfo
[block
].busy
.type
= 0;
794 _heapinfo
[block
].busy
.info
.size
= blocks
;
796 _bytes_used
+= blocks
* BLOCKSIZE
;
797 _bytes_free
-= blocks
* BLOCKSIZE
;
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. */
803 _heapinfo
[block
+ blocks
].busy
.info
.size
= -blocks
;
811 __malloc_size_t size
;
813 if (!__malloc_initialized
&& !__malloc_initialize ())
816 return (__malloc_hook
!= NULL
? *__malloc_hook
: _malloc_internal
) (size
);
821 /* On some ANSI C systems, some libc functions call _malloc, _free
822 and _realloc. Make them use the GNU functions. */
826 __malloc_size_t size
;
828 return malloc (size
);
841 __malloc_size_t size
;
843 return realloc (ptr
, size
);
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.
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.
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.
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.
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. */
869 #ifndef _MALLOC_INTERNAL
870 #define _MALLOC_INTERNAL
875 /* Cope with systems lacking `memmove'. */
877 #if (defined (MEMMOVE_MISSING) || \
878 !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
880 #undef __malloc_safe_bcopy
881 #define __malloc_safe_bcopy safe_bcopy
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))
890 /* Debugging hook for free. */
891 void (*__free_hook
) PP ((__ptr_t __ptr
));
893 /* List of blocks allocated by memalign. */
894 struct alignlist
*_aligned_blocks
= NULL
;
896 /* Return memory to the heap.
897 Like `free' but don't call a __free_hook if there is one. */
903 __malloc_size_t block
, blocks
;
904 register __malloc_size_t i
;
905 struct list
*prev
, *next
;
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
;
911 register struct alignlist
*l
;
916 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
917 if (l
->aligned
== ptr
)
919 l
->aligned
= NULL
; /* Mark the slot in the list as free. */
926 type
= _heapinfo
[block
].busy
.type
;
930 /* Get as many statistics as early as we can. */
932 _bytes_used
-= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
933 _bytes_free
+= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
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. */
941 i
= _heapinfo
[i
].free
.prev
;
945 i
= _heapinfo
[i
].free
.next
;
946 while (i
> 0 && i
< block
);
947 i
= _heapinfo
[i
].free
.prev
;
950 /* Determine how to link this block into the free list. */
951 if (block
== i
+ _heapinfo
[i
].free
.size
)
953 /* Coalesce this block with its predecessor. */
954 _heapinfo
[i
].free
.size
+= _heapinfo
[block
].busy
.info
.size
;
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
;
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
)
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
;
981 /* How many trailing free blocks are there now? */
982 blocks
= _heapinfo
[block
].free
.size
;
984 /* Where is the current end of accessible core? */
985 curbrk
= (*__morecore
) (0);
987 if (_heaplimit
!= 0 && curbrk
== ADDRESS (_heaplimit
))
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. */
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
;
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
)))
1020 malloc_info
*newinfo
;
1021 __malloc_size_t oldlimit
= _heaplimit
;
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. */
1028 _free_internal (_heapinfo
);
1029 _heaplimit
= oldlimit
;
1031 /* Tell malloc to search from the beginning of the heap for
1032 free blocks, so it doesn't reuse the ones just freed. */
1035 /* Allocate new space for the info table and move its data. */
1036 newinfo
= (malloc_info
*) _malloc_internal (info_blocks
1038 memmove (newinfo
, _heapinfo
, info_blocks
* BLOCKSIZE
);
1039 _heapinfo
= newinfo
;
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
1045 block
= _heapinfo
[0].free
.prev
;
1046 blocks
= _heapinfo
[block
].free
.size
;
1049 /* Now see if we can return stuff to the system. */
1050 if (block
+ blocks
== _heaplimit
&& blocks
>= lesscore_threshold
)
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
;
1061 _bytes_free
-= bytes
;
1065 /* Set the next search to begin at this block. */
1070 /* Do some of the statistics. */
1072 _bytes_used
-= 1 << type
;
1074 _bytes_free
+= 1 << type
;
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
));
1080 if (_heapinfo
[block
].busy
.info
.frag
.nfree
== (BLOCKSIZE
>> type
) - 1)
1082 /* If all fragments of this block are free, remove them
1083 from the fragment list and free the whole block. */
1085 for (i
= 1; i
< (__malloc_size_t
) (BLOCKSIZE
>> type
); ++i
)
1087 prev
->prev
->next
= next
;
1089 next
->prev
= prev
->prev
;
1090 _heapinfo
[block
].busy
.type
= 0;
1091 _heapinfo
[block
].busy
.info
.size
= 1;
1093 /* Keep the statistics accurate. */
1095 _bytes_used
+= BLOCKSIZE
;
1096 _chunks_free
-= BLOCKSIZE
>> type
;
1097 _bytes_free
-= BLOCKSIZE
;
1099 #ifdef GC_MALLOC_CHECK
1100 _free_internal (ADDRESS (block
));
1102 free (ADDRESS (block
));
1105 else if (_heapinfo
[block
].busy
.info
.frag
.nfree
!= 0)
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
;
1114 if (next
->next
!= NULL
)
1115 next
->next
->prev
= next
;
1116 ++_heapinfo
[block
].busy
.info
.frag
.nfree
;
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
;
1138 /* Return memory to the heap. */
1144 if (__free_hook
!= NULL
)
1145 (*__free_hook
) (ptr
);
1147 _free_internal (ptr
);
1150 /* Define the `cfree' alias for `free'. */
1152 weak_alias (free
, cfree
)
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.
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.
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.
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.
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. */
1183 #ifndef _MALLOC_INTERNAL
1184 #define _MALLOC_INTERNAL
1190 /* Cope with systems lacking `memmove'. */
1191 #if (defined (MEMMOVE_MISSING) || \
1192 !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
1195 #undef __malloc_safe_bcopy
1196 #define __malloc_safe_bcopy safe_bcopy
1199 /* Snarfed directly from Emacs src/dispnew.c:
1200 XXX Should use system bcopy if it handles overlap. */
1202 /* Like bcopy except never gets confused by overlap. */
1205 __malloc_safe_bcopy (afrom
, ato
, size
)
1208 __malloc_size_t size
;
1210 char *from
= afrom
, *to
= ato
;
1212 if (size
<= 0 || from
== to
)
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
);
1221 /* Otherwise, we'll copy from the end. */
1224 register char *endf
= from
+ size
;
1225 register char *endt
= to
+ size
;
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. */
1237 while (endf
!= from
);
1243 endt
-= (to
- from
);
1244 endf
-= (to
- from
);
1249 bcopy (endf
, endt
, to
- from
);
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
);
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))
1269 #define min(A, B) ((A) < (B) ? (A) : (B))
1271 /* Debugging hook for realloc. */
1272 __ptr_t (*__realloc_hook
) PP ((__ptr_t __ptr
, __malloc_size_t __size
));
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. */
1281 _realloc_internal (ptr
, size
)
1283 __malloc_size_t size
;
1287 __malloc_size_t block
, blocks
, oldlimit
;
1291 _free_internal (ptr
);
1292 return _malloc_internal (0);
1294 else if (ptr
== NULL
)
1295 return _malloc_internal (size
);
1297 block
= BLOCK (ptr
);
1299 type
= _heapinfo
[block
].busy
.type
;
1303 /* Maybe reallocate a large block to a small fragment. */
1304 if (size
<= BLOCKSIZE
/ 2)
1306 result
= _malloc_internal (size
);
1309 memcpy (result
, ptr
, size
);
1310 _free_internal (ptr
);
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
)
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. */
1330 _free_internal (ADDRESS (block
+ blocks
));
1333 else if (blocks
== _heapinfo
[block
].busy
.info
.size
)
1334 /* No size change necessary. */
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
;
1345 _free_internal (ptr
);
1346 result
= _malloc_internal (size
);
1347 if (_heaplimit
== 0)
1348 _heaplimit
= oldlimit
;
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
);
1359 = _malloc_internal ((block
- _heapindex
) * BLOCKSIZE
);
1360 (void) _malloc_internal (blocks
* BLOCKSIZE
);
1361 _free_internal (previous
);
1366 memmove (result
, ptr
, blocks
* BLOCKSIZE
);
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. */
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
);
1384 memcpy (result
, ptr
, min (size
, (__malloc_size_t
) 1 << type
));
1385 _free_internal (ptr
);
1396 __malloc_size_t size
;
1398 if (!__malloc_initialized
&& !__malloc_initialize ())
1401 return (__realloc_hook
!= NULL
? *__realloc_hook
: _realloc_internal
)
1404 /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
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.
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.
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.
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. */
1424 #ifndef _MALLOC_INTERNAL
1425 #define _MALLOC_INTERNAL
1429 /* Allocate an array of NMEMB elements each SIZE bytes long.
1430 The entire array is initialized to zeros. */
1432 calloc (nmemb
, size
)
1433 register __malloc_size_t nmemb
;
1434 register __malloc_size_t size
;
1436 register __ptr_t result
= malloc (nmemb
* size
);
1439 (void) memset (result
, 0, nmemb
* size
);
1443 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1444 This file is part of the GNU C Library.
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)
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.
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. */
1460 #ifndef _MALLOC_INTERNAL
1461 #define _MALLOC_INTERNAL
1465 #ifndef __GNU_LIBRARY__
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. */
1474 extern __ptr_t __sbrk
PP ((ptrdiff_t increment
));
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. */
1485 __default_morecore (increment
)
1486 __malloc_ptrdiff_t increment
;
1488 __ptr_t result
= (__ptr_t
) __sbrk (increment
);
1489 if (result
== (__ptr_t
) -1)
1493 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
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.
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.
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. */
1510 #ifndef _MALLOC_INTERNAL
1511 #define _MALLOC_INTERNAL
1515 #if __DJGPP__ - 0 == 1
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. */
1522 __ptr_t (*__memalign_hook
) PP ((size_t __size
, size_t __alignment
));
1525 memalign (alignment
, size
)
1526 __malloc_size_t alignment
;
1527 __malloc_size_t size
;
1530 unsigned long int adj
, lastadj
;
1532 if (__memalign_hook
)
1533 return (*__memalign_hook
) (alignment
, size
);
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);
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
;
1547 /* Reallocate the block with only as much excess as it needs. */
1549 result
= malloc (adj
+ size
);
1550 if (result
== NULL
) /* Impossible unless interrupted. */
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
);
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. */
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. */
1574 l
= (struct alignlist
*) malloc (sizeof (struct alignlist
));
1580 l
->next
= _aligned_blocks
;
1581 _aligned_blocks
= l
;
1584 result
= l
->aligned
= (char *) result
+ alignment
- adj
;
1590 #endif /* Not DJGPP v1 */
1591 /* Allocate memory on a page boundary.
1592 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
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.
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.
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.
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. */
1612 #if defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC)
1614 /* Emacs defines GMALLOC_INHIBIT_VALLOC to avoid this definition
1615 on MSDOS, where it conflicts with a system header file. */
1617 #define ELIDE_VALLOC
1621 #ifndef ELIDE_VALLOC
1623 #if defined (__GNU_LIBRARY__) || defined (_LIBC)
1625 #include <sys/cdefs.h>
1626 #if defined (__GLIBC__) && __GLIBC__ >= 2
1627 /* __getpagesize is already declared in <unistd.h> with return type int */
1629 extern size_t __getpagesize
PP ((void));
1632 #include "getpagesize.h"
1633 #define __getpagesize() getpagesize()
1636 #ifndef _MALLOC_INTERNAL
1637 #define _MALLOC_INTERNAL
1641 static __malloc_size_t pagesize
;
1645 __malloc_size_t size
;
1648 pagesize
= __getpagesize ();
1650 return memalign (pagesize
, size
);
1653 #endif /* Not ELIDE_VALLOC. */