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