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