summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/malloc.c')
-rw-r--r--src/lib/libc/stdlib/malloc.c1574
1 files changed, 1220 insertions, 354 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 3c57fad024..c8aef635d4 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,421 +1,1287 @@
1/* 1/*
2 * Copyright (c) 1983 Regents of the University of California. 2 * ----------------------------------------------------------------------------
3 * All rights reserved. 3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * 4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * Redistribution and use in source and binary forms, with or without 5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * modification, are permitted provided that the following conditions 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * are met: 7 * ----------------------------------------------------------------------------
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */ 8 */
33 9
34#if defined(LIBC_SCCS) && !defined(lint) 10#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.54 2003/01/14 02:27:16 millert Exp $";
36static char *rcsid = "$Id: malloc.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
38 13
39/* 14/*
40 * malloc.c (Caltech) 2/21/82 15 * Defining MALLOC_EXTRA_SANITY will enable extra checks which are
41 * Chris Kingsley, kingsley@cit-20. 16 * related to internal conditions and consistency in malloc.c. This has
42 * 17 * a noticeable runtime performance hit, and generally will not do you
43 * This is a very fast storage allocator. It allocates blocks of a small 18 * any good unless you fiddle with the internals of malloc or want
44 * number of different sizes, and keeps free lists of each size. Blocks that 19 * to catch random pointer corruption as early as possible.
45 * don't exactly fit are passed up to the next larger size. In this 20 */
46 * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long. 21#ifndef MALLOC_EXTRA_SANITY
47 * This is designed for use in a virtual memory environment. 22#undef MALLOC_EXTRA_SANITY
23#endif
24
25/*
26 * Defining MALLOC_STATS will enable you to call malloc_dump() and set
27 * the [dD] options in the MALLOC_OPTIONS environment variable.
28 * It has no run-time performance hit, but does pull in stdio...
29 */
30#ifndef MALLOC_STATS
31#undef MALLOC_STATS
32#endif
33
34/*
35 * What to use for Junk. This is the byte value we use to fill with
36 * when the 'J' option is enabled.
48 */ 37 */
38#define SOME_JUNK 0xd0 /* as in "Duh" :-) */
49 39
50#include <sys/types.h> 40#include <sys/types.h>
41#include <sys/param.h>
42#include <sys/mman.h>
43#include <sys/uio.h>
44#include <stdio.h>
51#include <stdlib.h> 45#include <stdlib.h>
52#include <string.h> 46#include <string.h>
53#include <unistd.h> 47#include <unistd.h>
48#include <fcntl.h>
49#include <limits.h>
50#include <errno.h>
51
52#include "thread_private.h"
54 53
55#define NULL 0 54/*
55 * The basic parameters you can tweak.
56 *
57 * malloc_pageshift pagesize = 1 << malloc_pageshift
58 * It's probably best if this is the native
59 * page size, but it shouldn't have to be.
60 *
61 * malloc_minsize minimum size of an allocation in bytes.
62 * If this is too small it's too much work
63 * to manage them. This is also the smallest
64 * unit of alignment used for the storage
65 * returned by malloc/realloc.
66 *
67 */
56 68
57static void morecore(); 69#if defined(__OpenBSD__) && defined(__sparc__)
58static int findbucket(); 70# define malloc_pageshift 13U
71#endif /* __OpenBSD__ */
59 72
60/* 73/*
61 * The overhead on a block is at least 4 bytes. When free, this space 74 * No user serviceable parts behind this point.
62 * contains a pointer to the next free block, and the bottom two bits must 75 *
63 * be zero. When in use, the first byte is set to MAGIC, and the second 76 * This structure describes a page worth of chunks.
64 * byte is the size index. The remaining bytes are for alignment.
65 * If range checking is enabled then a second word holds the size of the
66 * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
67 * The order of elements is critical: ov_magic must overlay the low order
68 * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
69 */ 77 */
70union overhead { 78
71 union overhead *ov_next; /* when free */ 79struct pginfo {
72 struct { 80 struct pginfo *next; /* next on the free list */
73 u_char ovu_magic; /* magic number */ 81 void *page; /* Pointer to the page */
74 u_char ovu_index; /* bucket # */ 82 u_short size; /* size of this page's chunks */
75#ifdef RCHECK 83 u_short shift; /* How far to shift for this size chunks */
76 u_short ovu_rmagic; /* range magic number */ 84 u_short free; /* How many free chunks */
77 u_long ovu_size; /* actual block size */ 85 u_short total; /* How many chunk */
78#endif 86 u_long bits[1]; /* Which chunks are free */
79 } ovu;
80#define ov_magic ovu.ovu_magic
81#define ov_index ovu.ovu_index
82#define ov_rmagic ovu.ovu_rmagic
83#define ov_size ovu.ovu_size
84}; 87};
85 88
86#define MAGIC 0xef /* magic # on accounting info */ 89/*
87#define RMAGIC 0x5555 /* magic # on range info */ 90 * This structure describes a number of free pages.
91 */
92
93struct pgfree {
94 struct pgfree *next; /* next run of free pages */
95 struct pgfree *prev; /* prev run of free pages */
96 void *page; /* pointer to free pages */
97 void *end; /* pointer to end of free pages */
98 u_long size; /* number of bytes free */
99};
88 100
89#ifdef RCHECK 101/*
90#define RSLOP sizeof (u_short) 102 * How many bits per u_long in the bitmap.
103 * Change only if not 8 bits/byte
104 */
105#define MALLOC_BITS (8*sizeof(u_long))
106
107/*
108 * Magic values to put in the page_directory
109 */
110#define MALLOC_NOT_MINE ((struct pginfo*) 0)
111#define MALLOC_FREE ((struct pginfo*) 1)
112#define MALLOC_FIRST ((struct pginfo*) 2)
113#define MALLOC_FOLLOW ((struct pginfo*) 3)
114#define MALLOC_MAGIC ((struct pginfo*) 4)
115
116#ifndef malloc_pageshift
117#define malloc_pageshift (PGSHIFT)
118#endif
119
120#ifndef malloc_minsize
121#define malloc_minsize 16U
122#endif
123
124#ifndef malloc_pageshift
125#error "malloc_pageshift undefined"
126#endif
127
128#if !defined(malloc_pagesize)
129#define malloc_pagesize (1UL<<malloc_pageshift)
130#endif
131
132#if ((1UL<<malloc_pageshift) != malloc_pagesize)
133#error "(1UL<<malloc_pageshift) != malloc_pagesize"
134#endif
135
136#ifndef malloc_maxsize
137#define malloc_maxsize ((malloc_pagesize)>>1)
138#endif
139
140/* A mask for the offset inside a page. */
141#define malloc_pagemask ((malloc_pagesize)-1)
142
143#define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask)))
144#define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)-malloc_origo)
145
146/* fd of /dev/zero */
147#ifdef USE_DEV_ZERO
148static int fdzero;
149#define MMAP_FD fdzero
150#define INIT_MMAP() \
151 { if ((fdzero=open("/dev/zero", O_RDWR, 0000)) == -1) \
152 wrterror("open of /dev/zero"); }
91#else 153#else
92#define RSLOP 0 154#define MMAP_FD (-1)
155#define INIT_MMAP()
93#endif 156#endif
94 157
158/* Set when initialization has been done */
159static unsigned int malloc_started;
160
161/* Number of free pages we cache */
162static unsigned int malloc_cache = 16;
163
164/* The offset from pagenumber to index into the page directory */
165static u_long malloc_origo;
166
167/* The last index in the page directory we care about */
168static u_long last_index;
169
170/* Pointer to page directory. Allocated "as if with" malloc */
171static struct pginfo **page_dir;
172
173/* How many slots in the page directory */
174static size_t malloc_ninfo;
175
176/* Free pages line up here */
177static struct pgfree free_list;
178
179/* Abort(), user doesn't handle problems. */
180static int malloc_abort;
181
182/* Are we trying to die ? */
183static int suicide;
184
185#ifdef MALLOC_STATS
186/* dump statistics */
187static int malloc_stats;
188#endif
189
190/* avoid outputting warnings? */
191static int malloc_silent;
192
193/* always realloc ? */
194static int malloc_realloc;
195
196#if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE))
197/* pass the kernel a hint on free pages ? */
198static int malloc_hint;
199#endif
200
201/* xmalloc behaviour ? */
202static int malloc_xmalloc;
203
204/* zero fill ? */
205static int malloc_zero;
206
207/* junk fill ? */
208static int malloc_junk;
209
210#ifdef __FreeBSD__
211/* utrace ? */
212static int malloc_utrace;
213
214struct ut { void *p; size_t s; void *r; };
215
216void utrace(struct ut *, int);
217
218#define UTRACE(a, b, c) \
219 if (malloc_utrace) \
220 {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);}
221#else /* !__FreeBSD__ */
222#define UTRACE(a,b,c)
223#endif
224
225/* my last break. */
226static void *malloc_brk;
227
228/* one location cache for free-list holders */
229static struct pgfree *px;
230
231/* compile-time options */
232char *malloc_options;
233
234/* Name of the current public function */
235static char *malloc_func;
236
237/* Macro for mmap */
238#define MMAP(size) \
239 mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
240 MMAP_FD, (off_t)0);
241
95/* 242/*
96 * nextf[i] is the pointer to the next free block of size 2^(i+3). The 243 * Necessary function declarations
97 * smallest allocatable block is 8 bytes. The overhead information
98 * precedes the data area returned to the user.
99 */ 244 */
100#define NBUCKETS 30 245static int extend_pgdir(u_long index);
101static union overhead *nextf[NBUCKETS]; 246static void *imalloc(size_t size);
102extern char *sbrk(); 247static void ifree(void *ptr);
248static void *irealloc(void *ptr, size_t size);
249static void *malloc_bytes(size_t size);
250
251#ifdef MALLOC_STATS
252void
253malloc_dump(fd)
254 FILE *fd;
255{
256 struct pginfo **pd;
257 struct pgfree *pf;
258 int j;
259
260 pd = page_dir;
261
262 /* print out all the pages */
263 for(j=0;j<=last_index;j++) {
264 fprintf(fd, "%08lx %5d ", (j+malloc_origo) << malloc_pageshift, j);
265 if (pd[j] == MALLOC_NOT_MINE) {
266 for(j++;j<=last_index && pd[j] == MALLOC_NOT_MINE;j++)
267 ;
268 j--;
269 fprintf(fd, ".. %5d not mine\n", j);
270 } else if (pd[j] == MALLOC_FREE) {
271 for(j++;j<=last_index && pd[j] == MALLOC_FREE;j++)
272 ;
273 j--;
274 fprintf(fd, ".. %5d free\n", j);
275 } else if (pd[j] == MALLOC_FIRST) {
276 for(j++;j<=last_index && pd[j] == MALLOC_FOLLOW;j++)
277 ;
278 j--;
279 fprintf(fd, ".. %5d in use\n", j);
280 } else if (pd[j] < MALLOC_MAGIC) {
281 fprintf(fd, "(%p)\n", pd[j]);
282 } else {
283 fprintf(fd, "%p %d (of %d) x %d @ %p --> %p\n",
284 pd[j], pd[j]->free, pd[j]->total,
285 pd[j]->size, pd[j]->page, pd[j]->next);
286 }
287 }
288
289 for(pf=free_list.next; pf; pf=pf->next) {
290 fprintf(fd, "Free: @%p [%p...%p[ %ld ->%p <-%p\n",
291 pf, pf->page, pf->end, pf->size, pf->prev, pf->next);
292 if (pf == pf->next) {
293 fprintf(fd, "Free_list loops.\n");
294 break;
295 }
296 }
297
298 /* print out various info */
299 fprintf(fd, "Minsize\t%d\n", malloc_minsize);
300 fprintf(fd, "Maxsize\t%d\n", malloc_maxsize);
301 fprintf(fd, "Pagesize\t%lu\n", (u_long)malloc_pagesize);
302 fprintf(fd, "Pageshift\t%d\n", malloc_pageshift);
303 fprintf(fd, "FirstPage\t%ld\n", malloc_origo);
304 fprintf(fd, "LastPage\t%ld %lx\n", last_index+malloc_pageshift,
305 (last_index + malloc_pageshift) << malloc_pageshift);
306 fprintf(fd, "Break\t%ld\n", (u_long)sbrk(0) >> malloc_pageshift);
307}
308#endif /* MALLOC_STATS */
309
310extern char *__progname;
311
312static void
313wrterror(p)
314 char *p;
315{
316 char *q = " error: ";
317 struct iovec iov[4];
318
319 iov[0].iov_base = __progname;
320 iov[0].iov_len = strlen(__progname);
321 iov[1].iov_base = malloc_func;
322 iov[1].iov_len = strlen(malloc_func);
323 iov[2].iov_base = q;
324 iov[2].iov_len = strlen(q);
325 iov[3].iov_base = p;
326 iov[3].iov_len = strlen(p);
327 writev(STDERR_FILENO, iov, 4);
328
329 suicide = 1;
330#ifdef MALLOC_STATS
331 if (malloc_stats)
332 malloc_dump(stderr);
333#endif /* MALLOC_STATS */
334 abort();
335}
336
337static void
338wrtwarning(p)
339 char *p;
340{
341 char *q = " warning: ";
342 struct iovec iov[4];
343
344 if (malloc_abort)
345 wrterror(p);
346 else if (malloc_silent)
347 return;
348
349 iov[0].iov_base = __progname;
350 iov[0].iov_len = strlen(__progname);
351 iov[1].iov_base = malloc_func;
352 iov[1].iov_len = strlen(malloc_func);
353 iov[2].iov_base = q;
354 iov[2].iov_len = strlen(q);
355 iov[3].iov_base = p;
356 iov[3].iov_len = strlen(p);
357 writev(STDERR_FILENO, iov, 4);
358}
359
360#ifdef MALLOC_STATS
361static void
362malloc_exit()
363{
364 FILE *fd = fopen("malloc.out", "a");
365 char *q = "malloc() warning: Couldn't dump stats.\n";
366 if (fd) {
367 malloc_dump(fd);
368 fclose(fd);
369 } else
370 write(STDERR_FILENO, q, strlen(q));
371}
372#endif /* MALLOC_STATS */
103 373
104static int pagesz; /* page size */
105static int pagebucket; /* page size bucket */
106 374
107#ifdef MSTATS
108/* 375/*
109 * nmalloc[i] is the difference between the number of mallocs and frees 376 * Allocate a number of pages from the OS
110 * for a given block size.
111 */ 377 */
112static u_int nmalloc[NBUCKETS]; 378static void *
113#include <stdio.h> 379map_pages(pages)
114#endif 380 size_t pages;
381{
382 caddr_t result, tail;
115 383
116#if defined(DEBUG) || defined(RCHECK) 384 result = (caddr_t)pageround((u_long)sbrk(0));
117#define ASSERT(p) if (!(p)) botch("p") 385 pages <<= malloc_pageshift;
118#include <stdio.h> 386 if (pages > SIZE_T_MAX - (size_t)result) {
119static 387#ifdef MALLOC_EXTRA_SANITY
120botch(s) 388 wrterror("(ES): overflow in map_pages fails\n");
121 char *s; 389#endif /* MALLOC_EXTRA_SANITY */
390 return 0;
391 }
392 tail = result + pages;
393
394 if (brk(tail)) {
395#ifdef MALLOC_EXTRA_SANITY
396 wrterror("(ES): map_pages fails\n");
397#endif /* MALLOC_EXTRA_SANITY */
398 return 0;
399 }
400
401 last_index = ptr2index(tail) - 1;
402 malloc_brk = tail;
403
404 if ((last_index+1) >= malloc_ninfo && !extend_pgdir(last_index))
405 return 0;
406
407 return result;
408}
409
410/*
411 * Extend page directory
412 */
413static int
414extend_pgdir(index)
415 u_long index;
122{ 416{
123 fprintf(stderr, "\r\nassertion botched: %s\r\n", s); 417 struct pginfo **new, **old;
124 (void) fflush(stderr); /* just in case user buffered it */ 418 size_t i, oldlen;
125 abort(); 419
420 /* Make it this many pages */
421 i = index * sizeof *page_dir;
422 i /= malloc_pagesize;
423 i += 2;
424
425 /* remember the old mapping size */
426 oldlen = malloc_ninfo * sizeof *page_dir;
427
428 /*
429 * NOTE: we allocate new pages and copy the directory rather than tempt
430 * fate by trying to "grow" the region.. There is nothing to prevent
431 * us from accidently re-mapping space that's been allocated by our caller
432 * via dlopen() or other mmap().
433 *
434 * The copy problem is not too bad, as there is 4K of page index per
435 * 4MB of malloc arena.
436 *
437 * We can totally avoid the copy if we open a file descriptor to associate
438 * the anon mappings with. Then, when we remap the pages at the new
439 * address, the old pages will be "magically" remapped.. But this means
440 * keeping open a "secret" file descriptor.....
441 */
442
443 /* Get new pages */
444 new = (struct pginfo**) MMAP(i * malloc_pagesize);
445 if (new == MAP_FAILED)
446 return 0;
447
448 /* Copy the old stuff */
449 memcpy(new, page_dir,
450 malloc_ninfo * sizeof *page_dir);
451
452 /* register the new size */
453 malloc_ninfo = i * malloc_pagesize / sizeof *page_dir;
454
455 /* swap the pointers */
456 old = page_dir;
457 page_dir = new;
458
459 /* Now free the old stuff */
460 munmap(old, oldlen);
461 return 1;
126} 462}
127#else
128#define ASSERT(p)
129#endif
130 463
131void * 464/*
132malloc(nbytes) 465 * Initialize the world
133 size_t nbytes; 466 */
467static void
468malloc_init ()
134{ 469{
135 register union overhead *op; 470 char *p, b[64];
136 register long bucket, n; 471 int i, j;
137 register unsigned amt; 472 int save_errno = errno;
138 473
139 /* 474 _MALLOC_LOCK_INIT();
140 * First time malloc is called, setup page size and 475
141 * align break pointer so all data will be page aligned. 476 INIT_MMAP();
142 */ 477
143 if (pagesz == 0) { 478#ifdef MALLOC_EXTRA_SANITY
144 pagesz = n = getpagesize(); 479 malloc_junk = 1;
145 op = (union overhead *)sbrk(0); 480#endif /* MALLOC_EXTRA_SANITY */
146 n = n - sizeof (*op) - ((long)op & (n - 1)); 481
147 if (n < 0) 482 for (i = 0; i < 3; i++) {
148 n += pagesz; 483 if (i == 0) {
149 if (n) { 484 j = readlink("/etc/malloc.conf", b, sizeof b - 1);
150 if (sbrk(n) == (char *)-1) 485 if (j <= 0)
151 return (NULL); 486 continue;
152 } 487 b[j] = '\0';
153 bucket = 0; 488 p = b;
154 amt = 8; 489 } else if (i == 1) {
155 while (pagesz > amt) { 490 if (issetugid() == 0)
156 amt <<= 1; 491 p = getenv("MALLOC_OPTIONS");
157 bucket++; 492 else
158 } 493 continue;
159 pagebucket = bucket; 494 } else if (i == 2) {
160 } 495 p = malloc_options;
161 /*
162 * Convert amount of memory requested into closest block size
163 * stored in hash buckets which satisfies request.
164 * Account for space used per block for accounting.
165 */
166 if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
167#ifndef RCHECK
168 amt = 8; /* size of first bucket */
169 bucket = 0;
170#else
171 amt = 16; /* size of first bucket */
172 bucket = 1;
173#endif
174 n = -((long)sizeof (*op) + RSLOP);
175 } else {
176 amt = pagesz;
177 bucket = pagebucket;
178 } 496 }
179 while (nbytes > amt + n) { 497 for (; p && *p; p++) {
180 amt <<= 1; 498 switch (*p) {
181 if (amt == 0) 499 case '>': malloc_cache <<= 1; break;
182 return (NULL); 500 case '<': malloc_cache >>= 1; break;
183 bucket++; 501 case 'a': malloc_abort = 0; break;
502 case 'A': malloc_abort = 1; break;
503#ifdef MALLOC_STATS
504 case 'd': malloc_stats = 0; break;
505 case 'D': malloc_stats = 1; break;
506#endif /* MALLOC_STATS */
507#if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE))
508 case 'h': malloc_hint = 0; break;
509 case 'H': malloc_hint = 1; break;
510#endif /* __FreeBSD__ */
511 case 'r': malloc_realloc = 0; break;
512 case 'R': malloc_realloc = 1; break;
513 case 'j': malloc_junk = 0; break;
514 case 'J': malloc_junk = 1; break;
515 case 'n': malloc_silent = 0; break;
516 case 'N': malloc_silent = 1; break;
517#ifdef __FreeBSD__
518 case 'u': malloc_utrace = 0; break;
519 case 'U': malloc_utrace = 1; break;
520#endif /* __FreeBSD__ */
521 case 'x': malloc_xmalloc = 0; break;
522 case 'X': malloc_xmalloc = 1; break;
523 case 'z': malloc_zero = 0; break;
524 case 'Z': malloc_zero = 1; break;
525 default:
526 j = malloc_abort;
527 malloc_abort = 0;
528 wrtwarning("unknown char in MALLOC_OPTIONS\n");
529 malloc_abort = j;
530 break;
531 }
184 } 532 }
185 /* 533 }
186 * If nothing in hash bucket right now, 534
187 * request more memory from the system. 535 UTRACE(0, 0, 0);
188 */ 536
189 if ((op = nextf[bucket]) == NULL) { 537 /*
190 morecore(bucket); 538 * We want junk in the entire allocation, and zero only in the part
191 if ((op = nextf[bucket]) == NULL) 539 * the user asked for.
192 return (NULL); 540 */
541 if (malloc_zero)
542 malloc_junk=1;
543
544#ifdef MALLOC_STATS
545 if (malloc_stats && (atexit(malloc_exit) == -1))
546 wrtwarning("atexit(2) failed. Will not be able to dump malloc stats on exit.\n");
547#endif /* MALLOC_STATS */
548
549 /* Allocate one page for the page directory */
550 page_dir = (struct pginfo **) MMAP(malloc_pagesize);
551
552 if (page_dir == MAP_FAILED)
553 wrterror("mmap(2) failed, check limits.\n");
554
555 /*
556 * We need a maximum of malloc_pageshift buckets, steal these from the
557 * front of the page_directory;
558 */
559 malloc_origo = ((u_long)pageround((u_long)sbrk(0))) >> malloc_pageshift;
560 malloc_origo -= malloc_pageshift;
561
562 malloc_ninfo = malloc_pagesize / sizeof *page_dir;
563
564 /* Been here, done that */
565 malloc_started++;
566
567 /* Recalculate the cache size in bytes, and make sure it's nonzero */
568
569 if (!malloc_cache)
570 malloc_cache++;
571
572 malloc_cache <<= malloc_pageshift;
573
574 /*
575 * This is a nice hack from Kaleb Keithly (kaleb@x.org).
576 * We can sbrk(2) further back when we keep this on a low address.
577 */
578 px = (struct pgfree *) imalloc (sizeof *px);
579 errno = save_errno;
580}
581
582/*
583 * Allocate a number of complete pages
584 */
585static void *
586malloc_pages(size)
587 size_t size;
588{
589 void *p, *delay_free = 0;
590 int i;
591 struct pgfree *pf;
592 u_long index;
593
594 size = pageround(size);
595
596 p = 0;
597 /* Look for free pages before asking for more */
598 for(pf = free_list.next; pf; pf = pf->next) {
599
600#ifdef MALLOC_EXTRA_SANITY
601 if (pf->size & malloc_pagemask)
602 wrterror("(ES): junk length entry on free_list\n");
603 if (!pf->size)
604 wrterror("(ES): zero length entry on free_list\n");
605 if (pf->page == pf->end)
606 wrterror("(ES): zero entry on free_list\n");
607 if (pf->page > pf->end)
608 wrterror("(ES): sick entry on free_list\n");
609 if ((void*)pf->page >= (void*)sbrk(0))
610 wrterror("(ES): entry on free_list past brk\n");
611 if (page_dir[ptr2index(pf->page)] != MALLOC_FREE)
612 wrterror("(ES): non-free first page on free-list\n");
613 if (page_dir[ptr2index(pf->end)-1] != MALLOC_FREE)
614 wrterror("(ES): non-free last page on free-list\n");
615#endif /* MALLOC_EXTRA_SANITY */
616
617 if (pf->size < size)
618 continue;
619
620 if (pf->size == size) {
621 p = pf->page;
622 if (pf->next)
623 pf->next->prev = pf->prev;
624 pf->prev->next = pf->next;
625 delay_free = pf;
626 break;
193 } 627 }
194 /* remove from linked list */ 628
195 nextf[bucket] = op->ov_next; 629 p = pf->page;
196 op->ov_magic = MAGIC; 630 pf->page = (char *)pf->page + size;
197 op->ov_index = bucket; 631 pf->size -= size;
198#ifdef MSTATS 632 break;
199 nmalloc[bucket]++; 633 }
200#endif 634
201#ifdef RCHECK 635#ifdef MALLOC_EXTRA_SANITY
202 /* 636 if (p && page_dir[ptr2index(p)] != MALLOC_FREE)
203 * Record allocated size of block and 637 wrterror("(ES): allocated non-free page on free-list\n");
204 * bound space with magic numbers. 638#endif /* MALLOC_EXTRA_SANITY */
205 */ 639
206 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 640 size >>= malloc_pageshift;
207 op->ov_rmagic = RMAGIC; 641
208 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 642 /* Map new pages */
209#endif 643 if (!p)
210 return ((char *)(op + 1)); 644 p = map_pages(size);
645
646 if (p) {
647
648 index = ptr2index(p);
649 page_dir[index] = MALLOC_FIRST;
650 for (i=1;i<size;i++)
651 page_dir[index+i] = MALLOC_FOLLOW;
652
653 if (malloc_junk)
654 memset(p, SOME_JUNK, size << malloc_pageshift);
655 }
656
657 if (delay_free) {
658 if (!px)
659 px = delay_free;
660 else
661 ifree(delay_free);
662 }
663
664 return p;
211} 665}
212 666
213/* 667/*
214 * Allocate more memory to the indicated bucket. 668 * Allocate a page of fragments
215 */ 669 */
216static void 670
217morecore(bucket) 671static __inline__ int
218 int bucket; 672malloc_make_chunks(bits)
673 int bits;
219{ 674{
220 register union overhead *op; 675 struct pginfo *bp;
221 register long sz; /* size of desired block */ 676 void *pp;
222 long amt; /* amount to allocate */ 677 int i, k, l;
223 int nblks; /* how many blocks we get */
224 678
225 /* 679 /* Allocate a new bucket */
226 * sbrk_size <= 0 only for big, FLUFFY, requests (about 680 pp = malloc_pages((size_t)malloc_pagesize);
227 * 2^30 bytes on a VAX, I think) or for a negative arg. 681 if (!pp)
228 */ 682 return 0;
229 sz = 1 << (bucket + 3); 683
230#ifdef DEBUG 684 /* Find length of admin structure */
231 ASSERT(sz > 0); 685 l = sizeof *bp - sizeof(u_long);
232#else 686 l += sizeof(u_long) *
233 if (sz <= 0) 687 (((malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS);
234 return; 688
235#endif 689 /* Don't waste more than two chunks on this */
236 if (sz < pagesz) { 690 /*
237 amt = pagesz; 691 * If we are to allocate a memory protected page for the malloc(0)
238 nblks = amt / sz; 692 * case (when bits=0), it must be from a different page than the
239 } else { 693 * pginfo page.
240 amt = sz + pagesz; 694 * --> Treat it like the big chunk alloc, get a second data page.
241 nblks = 1; 695 */
696 if (bits != 0 && (1UL<<(bits)) <= l+l) {
697 bp = (struct pginfo *)pp;
698 } else {
699 bp = (struct pginfo *)imalloc(l);
700 if (!bp) {
701 ifree(pp);
702 return 0;
242 } 703 }
243 op = (union overhead *)sbrk(amt); 704 }
244 /* no more room! */ 705
245 if ((long)op == -1) 706 /* memory protect the page allocated in the malloc(0) case */
246 return; 707 if (bits == 0) {
247 /* 708
248 * Add new memory allocated to that on 709 bp->size = 0;
249 * free list for this hash bucket. 710 bp->shift = 1;
250 */ 711 i = malloc_minsize-1;
251 nextf[bucket] = op; 712 while (i >>= 1)
252 while (--nblks > 0) { 713 bp->shift++;
253 op->ov_next = (union overhead *)((caddr_t)op + sz); 714 bp->total = bp->free = malloc_pagesize >> bp->shift;
254 op = (union overhead *)((caddr_t)op + sz); 715 bp->page = pp;
255 } 716
717 k = mprotect(pp, malloc_pagesize, PROT_NONE);
718 if (k < 0) {
719 ifree(pp);
720 ifree(bp);
721 return 0;
722 }
723 } else {
724 bp->size = (1UL<<bits);
725 bp->shift = bits;
726 bp->total = bp->free = malloc_pagesize >> bits;
727 bp->page = pp;
728 }
729
730 /* set all valid bits in the bitmap */
731 k = bp->total;
732 i = 0;
733
734 /* Do a bunch at a time */
735 for(;k-i >= MALLOC_BITS; i += MALLOC_BITS)
736 bp->bits[i / MALLOC_BITS] = ~0UL;
737
738 for(; i < k; i++)
739 bp->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS);
740
741 if (bp == bp->page) {
742 /* Mark the ones we stole for ourselves */
743 for(i=0;l > 0;i++) {
744 bp->bits[i/MALLOC_BITS] &= ~(1UL<<(i%MALLOC_BITS));
745 bp->free--;
746 bp->total--;
747 l -= (1 << bits);
748 }
749 }
750
751 /* MALLOC_LOCK */
752
753 page_dir[ptr2index(pp)] = bp;
754
755 bp->next = page_dir[bits];
756 page_dir[bits] = bp;
757
758 /* MALLOC_UNLOCK */
759
760 return 1;
256} 761}
257 762
258void 763/*
259free(cp) 764 * Allocate a fragment
260 void *cp; 765 */
261{ 766static void *
262 register long size; 767malloc_bytes(size)
263 register union overhead *op; 768 size_t size;
264 769{
265 if (cp == NULL) 770 int i,j;
266 return; 771 u_long u;
267 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 772 struct pginfo *bp;
268#ifdef DEBUG 773 int k;
269 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 774 u_long *lp;
270#else 775
271 if (op->ov_magic != MAGIC) 776 /* Don't bother with anything less than this */
272 return; /* sanity */ 777 /* unless we have a malloc(0) requests */
273#endif 778 if (size != 0 && size < malloc_minsize)
274#ifdef RCHECK 779 size = malloc_minsize;
275 ASSERT(op->ov_rmagic == RMAGIC); 780
276 ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 781 /* Find the right bucket */
277#endif 782 if (size == 0)
278 size = op->ov_index; 783 j=0;
279 ASSERT(size < NBUCKETS); 784 else {
280 op->ov_next = nextf[size]; /* also clobbers ov_magic */ 785 j = 1;
281 nextf[size] = op; 786 i = size-1;
282#ifdef MSTATS 787 while (i >>= 1)
283 nmalloc[size]--; 788 j++;
284#endif 789 }
790
791 /* If it's empty, make a page more of that size chunks */
792 if (!page_dir[j] && !malloc_make_chunks(j))
793 return 0;
794
795 bp = page_dir[j];
796
797 /* Find first word of bitmap which isn't empty */
798 for (lp = bp->bits; !*lp; lp++)
799 ;
800
801 /* Find that bit, and tweak it */
802 u = 1;
803 k = 0;
804 while (!(*lp & u)) {
805 u += u;
806 k++;
807 }
808 *lp ^= u;
809
810 /* If there are no more free, remove from free-list */
811 if (!--bp->free) {
812 page_dir[j] = bp->next;
813 bp->next = 0;
814 }
815
816 /* Adjust to the real offset of that chunk */
817 k += (lp-bp->bits)*MALLOC_BITS;
818 k <<= bp->shift;
819
820 if (malloc_junk && bp->size != 0)
821 memset((char *)bp->page + k, SOME_JUNK, bp->size);
822
823 return (u_char *)bp->page + k;
285} 824}
286 825
287/* 826/*
288 * When a program attempts "storage compaction" as mentioned in the 827 * Allocate a piece of memory
289 * old malloc man page, it realloc's an already freed block. Usually
290 * this is the last block it freed; occasionally it might be farther
291 * back. We have to search all the free lists for the block in order
292 * to determine its bucket: 1st we make one pass thru the lists
293 * checking only the first block in each; if that fails we search
294 * ``realloc_srchlen'' blocks in each list for a match (the variable
295 * is extern so the caller can modify it). If that fails we just copy
296 * however many bytes was given to realloc() and hope it's not huge.
297 */ 828 */
298int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 829static void *
830imalloc(size)
831 size_t size;
832{
833 void *result;
299 834
300void * 835 if (!malloc_started)
301realloc(cp, nbytes) 836 malloc_init();
302 void *cp; 837
303 size_t nbytes; 838 if (suicide)
304{ 839 abort();
305 register u_long onb; 840
306 register long i; 841 if ((size + malloc_pagesize) < size) /* Check for overflow */
307 union overhead *op; 842 result = 0;
308 char *res; 843 else if (size <= malloc_maxsize)
309 int was_alloced = 0; 844 result = malloc_bytes(size);
310 845 else
311 if (cp == NULL) 846 result = malloc_pages(size);
312 return (malloc(nbytes)); 847
313 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 848 if (malloc_abort && !result)
314 if (op->ov_magic == MAGIC) { 849 wrterror("allocation failed.\n");
315 was_alloced++; 850
316 i = op->ov_index; 851 if (malloc_zero && result)
317 } else { 852 memset(result, 0, size);
318 /* 853
319 * Already free, doing "compaction". 854 return result;
320 * 855}
321 * Search for the old block of memory on the 856
322 * free list. First, check the most common 857/*
323 * case (last element free'd), then (this failing) 858 * Change the size of an allocation.
324 * the last ``realloc_srchlen'' items free'd. 859 */
325 * If all lookups fail, then assume the size of 860static void *
326 * the memory block being realloc'd is the 861irealloc(ptr, size)
327 * largest possible (so that all "nbytes" of new 862 void *ptr;
328 * memory are copied into). Note that this could cause 863 size_t size;
329 * a memory fault if the old area was tiny, and the moon 864{
330 * is gibbous. However, that is very unlikely. 865 void *p;
331 */ 866 u_long osize, index;
332 if ((i = findbucket(op, 1)) < 0 && 867 struct pginfo **mp;
333 (i = findbucket(op, realloc_srchlen)) < 0) 868 int i;
334 i = NBUCKETS; 869
870 if (suicide)
871 abort();
872
873 if (!malloc_started) {
874 wrtwarning("malloc() has never been called.\n");
875 return 0;
876 }
877
878 index = ptr2index(ptr);
879
880 if (index < malloc_pageshift) {
881 wrtwarning("junk pointer, too low to make sense.\n");
882 return 0;
883 }
884
885 if (index > last_index) {
886 wrtwarning("junk pointer, too high to make sense.\n");
887 return 0;
888 }
889
890 mp = &page_dir[index];
891
892 if (*mp == MALLOC_FIRST) { /* Page allocation */
893
894 /* Check the pointer */
895 if ((u_long)ptr & malloc_pagemask) {
896 wrtwarning("modified (page-) pointer.\n");
897 return 0;
335 } 898 }
336 onb = 1 << (i + 3); 899
337 if (onb < pagesz) 900 /* Find the size in bytes */
338 onb -= sizeof (*op) + RSLOP; 901 for (osize = malloc_pagesize; *++mp == MALLOC_FOLLOW;)
339 else 902 osize += malloc_pagesize;
340 onb += pagesz - sizeof (*op) - RSLOP; 903
341 /* avoid the copy if same size block */ 904 if (!malloc_realloc && /* Unless we have to, */
342 if (was_alloced) { 905 size <= osize && /* .. or are too small, */
343 if (i) { 906 size > (osize - malloc_pagesize)) { /* .. or can free a page, */
344 i = 1 << (i + 2); 907 if (malloc_junk)
345 if (i < pagesz) 908 memset((char *)ptr + size, SOME_JUNK, osize-size);
346 i -= sizeof (*op) + RSLOP; 909 return ptr; /* ..don't do anything else. */
347 else 910 }
348 i += pagesz - sizeof (*op) - RSLOP; 911
349 } 912 } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */
350 if (nbytes <= onb && nbytes > i) { 913
351#ifdef RCHECK 914 /* Check the pointer for sane values */
352 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 915 if ((u_long)ptr & ((1UL<<((*mp)->shift))-1)) {
353 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 916 wrtwarning("modified (chunk-) pointer.\n");
354#endif 917 return 0;
355 return(cp); 918 }
356 } else 919
357 free(cp); 920 /* Find the chunk index in the page */
921 i = ((u_long)ptr & malloc_pagemask) >> (*mp)->shift;
922
923 /* Verify that it isn't a free chunk already */
924 if ((*mp)->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) {
925 wrtwarning("chunk is already free.\n");
926 return 0;
927 }
928
929 osize = (*mp)->size;
930
931 if (!malloc_realloc && /* Unless we have to, */
932 size <= osize && /* ..or are too small, */
933 (size > osize/2 || /* ..or could use a smaller size, */
934 osize == malloc_minsize)) { /* ..(if there is one) */
935 if (malloc_junk)
936 memset((char *)ptr + size, SOME_JUNK, osize-size);
937 return ptr; /* ..don't do anything else. */
938 }
939
940 } else {
941 wrtwarning("pointer to wrong page.\n");
942 return 0;
943 }
944
945 p = imalloc(size);
946
947 if (p) {
948 /* copy the lesser of the two sizes, and free the old one */
949 /* Don't move from/to 0 sized region !!! */
950 if (osize != 0 && size != 0) {
951 if (osize < size)
952 memcpy(p, ptr, osize);
953 else
954 memcpy(p, ptr, size);
358 } 955 }
359 if ((res = malloc(nbytes)) == NULL) 956 ifree(ptr);
360 return (NULL); 957 }
361 if (cp != res) /* common optimization if "compacting" */ 958 return p;
362 bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
363 return (res);
364} 959}
365 960
366/* 961/*
367 * Search ``srchlen'' elements of each free list for a block whose 962 * Free a sequence of pages
368 * header starts at ``freep''. If srchlen is -1 search the whole list.
369 * Return bucket number, or -1 if not found.
370 */ 963 */
371static 964
372findbucket(freep, srchlen) 965static __inline__ void
373 union overhead *freep; 966free_pages(ptr, index, info)
374 int srchlen; 967 void *ptr;
968 int index;
969 struct pginfo *info;
375{ 970{
376 register union overhead *p; 971 int i;
377 register int i, j; 972 struct pgfree *pf, *pt=0;
378 973 u_long l;
379 for (i = 0; i < NBUCKETS; i++) { 974 void *tail;
380 j = 0; 975
381 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 976 if (info == MALLOC_FREE) {
382 if (p == freep) 977 wrtwarning("page is already free.\n");
383 return (i); 978 return;
384 j++; 979 }
385 } 980
981 if (info != MALLOC_FIRST) {
982 wrtwarning("pointer to wrong page.\n");
983 return;
984 }
985
986 if ((u_long)ptr & malloc_pagemask) {
987 wrtwarning("modified (page-) pointer.\n");
988 return;
989 }
990
991 /* Count how many pages and mark them free at the same time */
992 page_dir[index] = MALLOC_FREE;
993 for (i = 1; page_dir[index+i] == MALLOC_FOLLOW; i++)
994 page_dir[index + i] = MALLOC_FREE;
995
996 l = i << malloc_pageshift;
997
998 if (malloc_junk)
999 memset(ptr, SOME_JUNK, l);
1000
1001#if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE))
1002 if (malloc_hint)
1003 madvise(ptr, l, MADV_FREE);
1004#endif
1005
1006 tail = (char *)ptr+l;
1007
1008 /* add to free-list */
1009 if (!px)
1010 px = imalloc(sizeof *px); /* This cannot fail... */
1011 px->page = ptr;
1012 px->end = tail;
1013 px->size = l;
1014 if (!free_list.next) {
1015
1016 /* Nothing on free list, put this at head */
1017 px->next = free_list.next;
1018 px->prev = &free_list;
1019 free_list.next = px;
1020 pf = px;
1021 px = 0;
1022
1023 } else {
1024
1025 /* Find the right spot, leave pf pointing to the modified entry. */
1026 tail = (char *)ptr+l;
1027
1028 for(pf = free_list.next; pf->end < ptr && pf->next; pf = pf->next)
1029 ; /* Race ahead here */
1030
1031 if (pf->page > tail) {
1032 /* Insert before entry */
1033 px->next = pf;
1034 px->prev = pf->prev;
1035 pf->prev = px;
1036 px->prev->next = px;
1037 pf = px;
1038 px = 0;
1039 } else if (pf->end == ptr ) {
1040 /* Append to the previous entry */
1041 pf->end = (char *)pf->end + l;
1042 pf->size += l;
1043 if (pf->next && pf->end == pf->next->page ) {
1044 /* And collapse the next too. */
1045 pt = pf->next;
1046 pf->end = pt->end;
1047 pf->size += pt->size;
1048 pf->next = pt->next;
1049 if (pf->next)
1050 pf->next->prev = pf;
1051 }
1052 } else if (pf->page == tail) {
1053 /* Prepend to entry */
1054 pf->size += l;
1055 pf->page = ptr;
1056 } else if (!pf->next) {
1057 /* Append at tail of chain */
1058 px->next = 0;
1059 px->prev = pf;
1060 pf->next = px;
1061 pf = px;
1062 px = 0;
1063 } else {
1064 wrterror("freelist is destroyed.\n");
386 } 1065 }
387 return (-1); 1066 }
1067
1068 /* Return something to OS ? */
1069 if (!pf->next && /* If we're the last one, */
1070 pf->size > malloc_cache && /* ..and the cache is full, */
1071 pf->end == malloc_brk && /* ..and none behind us, */
1072 malloc_brk == sbrk(0)) { /* ..and it's OK to do... */
1073
1074 /*
1075 * Keep the cache intact. Notice that the '>' above guarantees that
1076 * the pf will always have at least one page afterwards.
1077 */
1078 pf->end = (char *)pf->page + malloc_cache;
1079 pf->size = malloc_cache;
1080
1081 brk(pf->end);
1082 malloc_brk = pf->end;
1083
1084 index = ptr2index(pf->end);
1085
1086 for(i=index;i <= last_index;)
1087 page_dir[i++] = MALLOC_NOT_MINE;
1088
1089 last_index = index - 1;
1090
1091 /* XXX: We could realloc/shrink the pagedir here I guess. */
1092 }
1093 if (pt)
1094 ifree(pt);
388} 1095}
389 1096
390#ifdef MSTATS
391/* 1097/*
392 * mstats - print out statistics about malloc 1098 * Free a chunk, and possibly the page it's on, if the page becomes empty.
393 *
394 * Prints two lines of numbers, one showing the length of the free list
395 * for each size category, the second showing the number of mallocs -
396 * frees for each size category.
397 */ 1099 */
398mstats(s) 1100
399 char *s; 1101/* ARGSUSED */
1102static __inline__ void
1103free_bytes(ptr, index, info)
1104 void *ptr;
1105 int index;
1106 struct pginfo *info;
400{ 1107{
401 register int i, j; 1108 int i;
402 register union overhead *p; 1109 struct pginfo **mp;
403 int totfree = 0, 1110 void *vp;
404 totused = 0; 1111
405 1112 /* Find the chunk number on the page */
406 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 1113 i = ((u_long)ptr & malloc_pagemask) >> info->shift;
407 for (i = 0; i < NBUCKETS; i++) { 1114
408 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 1115 if ((u_long)ptr & ((1UL<<(info->shift))-1)) {
409 ; 1116 wrtwarning("modified (chunk-) pointer.\n");
410 fprintf(stderr, " %d", j); 1117 return;
411 totfree += j * (1 << (i + 3)); 1118 }
412 } 1119
413 fprintf(stderr, "\nused:\t"); 1120 if (info->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) {
414 for (i = 0; i < NBUCKETS; i++) { 1121 wrtwarning("chunk is already free.\n");
415 fprintf(stderr, " %d", nmalloc[i]); 1122 return;
416 totused += nmalloc[i] * (1 << (i + 3)); 1123 }
417 } 1124
418 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 1125 if (malloc_junk && info->size != 0)
419 totused, totfree); 1126 memset(ptr, SOME_JUNK, info->size);
1127
1128 info->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS);
1129 info->free++;
1130
1131 if (info->size != 0)
1132 mp = page_dir + info->shift;
1133 else
1134 mp = page_dir;
1135
1136 if (info->free == 1) {
1137
1138 /* Page became non-full */
1139
1140 /* Insert in address order */
1141 while (*mp && (*mp)->next && (*mp)->next->page < info->page)
1142 mp = &(*mp)->next;
1143 info->next = *mp;
1144 *mp = info;
1145 return;
1146 }
1147
1148 if (info->free != info->total)
1149 return;
1150
1151 /* Find & remove this page in the queue */
1152 while (*mp != info) {
1153 mp = &((*mp)->next);
1154#ifdef MALLOC_EXTRA_SANITY
1155 if (!*mp)
1156 wrterror("(ES): Not on queue\n");
1157#endif /* MALLOC_EXTRA_SANITY */
1158 }
1159 *mp = info->next;
1160
1161 /* Free the page & the info structure if need be */
1162 page_dir[ptr2index(info->page)] = MALLOC_FIRST;
1163
1164 /* If the page was mprotected, unprotect it before releasing it */
1165 if (info->size == 0) {
1166 mprotect(info->page, malloc_pagesize, PROT_READ|PROT_WRITE);
1167 /* Do we have to care if mprotect succeeds here ? */
1168 }
1169
1170 vp = info->page; /* Order is important ! */
1171 if(vp != (void*)info)
1172 ifree(info);
1173 ifree(vp);
1174}
1175
1176static void
1177ifree(ptr)
1178 void *ptr;
1179{
1180 struct pginfo *info;
1181 int index;
1182
1183 /* This is legal */
1184 if (!ptr)
1185 return;
1186
1187 if (!malloc_started) {
1188 wrtwarning("malloc() has never been called.\n");
1189 return;
1190 }
1191
1192 /* If we're already sinking, don't make matters any worse. */
1193 if (suicide)
1194 return;
1195
1196 index = ptr2index(ptr);
1197
1198 if (index < malloc_pageshift) {
1199 wrtwarning("junk pointer, too low to make sense.\n");
1200 return;
1201 }
1202
1203 if (index > last_index) {
1204 wrtwarning("junk pointer, too high to make sense.\n");
1205 return;
1206 }
1207
1208 info = page_dir[index];
1209
1210 if (info < MALLOC_MAGIC)
1211 free_pages(ptr, index, info);
1212 else
1213 free_bytes(ptr, index, info);
1214 return;
1215}
1216
1217/*
1218 * These are the public exported interface routines.
1219 */
1220
1221static int malloc_active;
1222
1223void *
1224malloc(size_t size)
1225{
1226 register void *r;
1227
1228 malloc_func = " in malloc():";
1229 _MALLOC_LOCK();
1230 if (malloc_active++) {
1231 wrtwarning("recursive call.\n");
1232 malloc_active--;
1233 _MALLOC_UNLOCK();
1234 return (0);
1235 }
1236 r = imalloc(size);
1237 UTRACE(0, size, r);
1238 malloc_active--;
1239 _MALLOC_UNLOCK();
1240 if (malloc_xmalloc && !r)
1241 wrterror("out of memory.\n");
1242 return (r);
1243}
1244
1245void
1246free(void *ptr)
1247{
1248 malloc_func = " in free():";
1249 _MALLOC_LOCK();
1250 if (malloc_active++) {
1251 wrtwarning("recursive call.\n");
1252 malloc_active--;
1253 _MALLOC_UNLOCK();
1254 return;
1255 }
1256 ifree(ptr);
1257 UTRACE(ptr, 0, 0);
1258 malloc_active--;
1259 _MALLOC_UNLOCK();
1260 return;
1261}
1262
1263void *
1264realloc(void *ptr, size_t size)
1265{
1266 register void *r;
1267
1268 malloc_func = " in realloc():";
1269 _MALLOC_LOCK();
1270 if (malloc_active++) {
1271 wrtwarning("recursive call.\n");
1272 malloc_active--;
1273 _MALLOC_UNLOCK();
1274 return (0);
1275 }
1276 if (!ptr) {
1277 r = imalloc(size);
1278 } else {
1279 r = irealloc(ptr, size);
1280 }
1281 UTRACE(ptr, size, r);
1282 malloc_active--;
1283 _MALLOC_UNLOCK();
1284 if (malloc_xmalloc && !r)
1285 wrterror("out of memory.\n");
1286 return (r);
420} 1287}
421#endif