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