1 /* $NetBSD: malloc.c,v 1.5 2008/01/28 06:16:13 dholland Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)malloc.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: malloc.c,v 1.5 2008/01/28 06:16:13 dholland Exp $");
39 #endif /* LIBC_SCCS and not lint */
42 * malloc.c (Caltech) 2/21/82
43 * Chris Kingsley, kingsley@cit-20.
45 * This is a very fast storage allocator. It allocates blocks of a small
46 * number of different sizes, and keeps free lists of each size. Blocks that
47 * don't exactly fit are passed up to the next larger size. In this
48 * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
49 * This is designed for use in a virtual memory environment.
52 #include <sys/types.h>
53 #if defined(DEBUG) || defined(RCHECK)
56 #if defined(RCHECK) || defined(MSTATS)
66 * The overhead on a block is at least 4 bytes. When free, this space
67 * contains a pointer to the next free block, and the bottom two bits must
68 * be zero. When in use, the first byte is set to MAGIC, and the second
69 * byte is the size index. The remaining bytes are for alignment.
70 * If range checking is enabled then a second word holds the size of the
71 * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
72 * The order of elements is critical: ov_magic must overlay the low order
73 * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
76 union overhead
*ov_next
; /* when free */
78 u_char ovu_magic
; /* magic number */
79 u_char ovu_index
; /* bucket # */
81 u_short ovu_rmagic
; /* range magic number */
82 u_long ovu_size
; /* actual block size */
85 #define ov_magic ovu.ovu_magic
86 #define ov_index ovu.ovu_index
87 #define ov_rmagic ovu.ovu_rmagic
88 #define ov_size ovu.ovu_size
91 #define MAGIC 0xef /* magic # on accounting info */
93 #define RMAGIC 0x5555 /* magic # on range info */
97 #define RSLOP sizeof (u_short)
103 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
104 * smallest allocatable block is 8 bytes. The overhead information
105 * precedes the data area returned to the user.
108 static union overhead
*nextf
[NBUCKETS
];
110 static long pagesz
; /* page size */
111 static int pagebucket
; /* page size bucket */
115 * nmalloc[i] is the difference between the number of mallocs and frees
116 * for a given block size.
118 static u_int nmalloc
[NBUCKETS
];
121 static pthread_mutex_t malloc_mutex
= PTHREAD_MUTEX_INITIALIZER
;
123 static void morecore(int);
124 static int findbucket(union overhead
*, int);
126 void mstats(const char *);
129 #if defined(DEBUG) || defined(RCHECK)
130 #define ASSERT(p) if (!(p)) botch(__STRING(p))
132 static void botch(const char *);
135 * NOTE: since this may be called while malloc_mutex is locked, stdio must not
136 * be used in this function.
144 iov
[0].iov_base
= "\nassertion botched: ";
146 iov
[1].iov_base
= (void *)s
;
147 iov
[1].iov_len
= strlen(s
);
148 iov
[2].iov_base
= "\n";
152 * This place deserves a word of warning: a cancellation point will
153 * occur when executing writev(), and we might be still owning
154 * malloc_mutex. At this point we need to disable cancellation
155 * until `after' abort() because i) establishing a cancellation handler
156 * might, depending on the implementation, result in another malloc()
157 * to be executed, and ii) it is really not desirable to let execution
158 * continue. `Fix me.'
160 * Note that holding mutex_lock during abort() is safe.
163 (void)writev(STDERR_FILENO
, iov
, 3);
179 pthread_mutex_lock(&malloc_mutex
);
182 * First time malloc is called, setup page size and
183 * align break pointer so all data will be page aligned.
186 pagesz
= n
= getpagesize();
188 op
= (union overhead
*)(void *)sbrk(0);
189 n
= n
- sizeof (*op
) - ((long)op
& (n
- 1));
193 if (sbrk((int)n
) == (void *)-1) {
194 pthread_mutex_unlock(&malloc_mutex
);
200 while (pagesz
> amt
) {
207 * Convert amount of memory requested into closest block size
208 * stored in hash buckets which satisfies request.
209 * Account for space used per block for accounting.
211 if (nbytes
<= (size_t)(n
= pagesz
- sizeof (*op
) - RSLOP
)) {
213 amt
= 8; /* size of first bucket */
216 amt
= 16; /* size of first bucket */
219 n
= -((long)sizeof (*op
) + RSLOP
);
221 amt
= (unsigned)pagesz
;
224 while (nbytes
> (size_t)(amt
+ n
)) {
231 * If nothing in hash bucket right now,
232 * request more memory from the system.
234 if ((op
= nextf
[bucket
]) == NULL
) {
236 if ((op
= nextf
[bucket
]) == NULL
) {
237 pthread_mutex_unlock(&malloc_mutex
);
241 /* remove from linked list */
242 nextf
[bucket
] = op
->ov_next
;
243 op
->ov_magic
= MAGIC
;
244 op
->ov_index
= bucket
;
248 pthread_mutex_unlock(&malloc_mutex
);
251 * Record allocated size of block and
252 * bound space with magic numbers.
254 op
->ov_size
= (nbytes
+ RSLOP
- 1) & ~(RSLOP
- 1);
255 op
->ov_rmagic
= RMAGIC
;
256 *(u_short
*)((caddr_t
)(op
+ 1) + op
->ov_size
) = RMAGIC
;
258 return ((void *)(op
+ 1));
262 * Allocate more memory to the indicated bucket.
269 long sz
; /* size of desired block */
270 long amt
; /* amount to allocate */
271 long nblks
; /* how many blocks we get */
274 * sbrk_size <= 0 only for big, FLUFFY, requests (about
275 * 2^30 bytes on a VAX, I think) or for a negative arg.
277 sz
= 1 << (bucket
+ 3);
291 op
= (union overhead
*)(void *)sbrk((int)amt
);
296 * Add new memory allocated to that on
297 * free list for this hash bucket.
300 while (--nblks
> 0) {
302 (union overhead
*)(void *)((caddr_t
)(void *)op
+(size_t)sz
);
316 op
= (union overhead
*)(void *)((caddr_t
)cp
- sizeof (union overhead
));
318 ASSERT(op
->ov_magic
== MAGIC
); /* make sure it was in use */
320 if (op
->ov_magic
!= MAGIC
)
324 ASSERT(op
->ov_rmagic
== RMAGIC
);
325 ASSERT(*(u_short
*)((caddr_t
)(op
+ 1) + op
->ov_size
) == RMAGIC
);
328 ASSERT(size
< NBUCKETS
);
329 pthread_mutex_lock(&malloc_mutex
);
330 op
->ov_next
= nextf
[(unsigned int)size
];/* also clobbers ov_magic */
331 nextf
[(unsigned int)size
] = op
;
333 nmalloc
[(size_t)size
]--;
335 pthread_mutex_unlock(&malloc_mutex
);
339 * When a program attempts "storage compaction" as mentioned in the
340 * old malloc man page, it realloc's an already freed block. Usually
341 * this is the last block it freed; occasionally it might be farther
342 * back. We have to search all the free lists for the block in order
343 * to determine its bucket: 1st we make one pass thru the lists
344 * checking only the first block in each; if that fails we search
345 * ``__realloc_srchlen'' blocks in each list for a match (the variable
346 * is extern so the caller can modify it). If that fails we just copy
347 * however many bytes was given to realloc() and hope it's not huge.
349 int __realloc_srchlen
= 4; /* 4 should be plenty, -1 =>'s whole list */
363 return (malloc(nbytes
));
368 op
= (union overhead
*)(void *)((caddr_t
)cp
- sizeof (union overhead
));
369 pthread_mutex_lock(&malloc_mutex
);
370 if (op
->ov_magic
== MAGIC
) {
375 * Already free, doing "compaction".
377 * Search for the old block of memory on the
378 * free list. First, check the most common
379 * case (last element free'd), then (this failing)
380 * the last ``__realloc_srchlen'' items free'd.
381 * If all lookups fail, then assume the size of
382 * the memory block being realloc'd is the
383 * largest possible (so that all "nbytes" of new
384 * memory are copied into). Note that this could cause
385 * a memory fault if the old area was tiny, and the moon
386 * is gibbous. However, that is very unlikely.
388 if ((i
= findbucket(op
, 1)) < 0 &&
389 (i
= findbucket(op
, __realloc_srchlen
)) < 0)
392 onb
= (u_long
)1 << (u_long
)(i
+ 3);
393 if (onb
< (u_long
)pagesz
)
394 onb
-= sizeof (*op
) + RSLOP
;
396 onb
+= pagesz
- sizeof (*op
) - RSLOP
;
397 /* avoid the copy if same size block */
400 i
= (long)1 << (long)(i
+ 2);
402 i
-= sizeof (*op
) + RSLOP
;
404 i
+= pagesz
- sizeof (*op
) - RSLOP
;
406 if (nbytes
<= onb
&& nbytes
> (size_t) i
) {
408 op
->ov_size
= (nbytes
+ RSLOP
- 1) & ~(RSLOP
- 1);
409 *(u_short
*)((caddr_t
)(op
+ 1) + op
->ov_size
) = RMAGIC
;
411 pthread_mutex_unlock(&malloc_mutex
);
420 pthread_mutex_unlock(&malloc_mutex
);
421 if ((res
= malloc(nbytes
)) == NULL
) {
428 if (cp
!= res
) /* common optimization if "compacting" */
429 (void)memmove(res
, cp
, (size_t)((nbytes
< onb
) ? nbytes
: onb
));
431 (void)memmove(res
, cp
, (size_t)((nbytes
< onb
) ? nbytes
: onb
));
438 * Search ``srchlen'' elements of each free list for a block whose
439 * header starts at ``freep''. If srchlen is -1 search the whole list.
440 * Return bucket number, or -1 if not found.
443 findbucket(freep
, srchlen
)
444 union overhead
*freep
;
450 for (i
= 0; i
< NBUCKETS
; i
++) {
452 for (p
= nextf
[i
]; p
&& j
!= srchlen
; p
= p
->ov_next
) {
463 * mstats - print out statistics about malloc
465 * Prints two lines of numbers, one showing the length of the free list
466 * for each size category, the second showing the number of mallocs -
467 * frees for each size category.
478 fprintf(stderr
, "Memory allocation statistics %s\nfree:\t", s
);
479 for (i
= 0; i
< NBUCKETS
; i
++) {
480 for (j
= 0, p
= nextf
[i
]; p
; p
= p
->ov_next
, j
++)
482 fprintf(stderr
, " %d", j
);
483 totfree
+= j
* (1 << (i
+ 3));
485 fprintf(stderr
, "\nused:\t");
486 for (i
= 0; i
< NBUCKETS
; i
++) {
487 fprintf(stderr
, " %d", nmalloc
[i
]);
488 totused
+= nmalloc
[i
] * (1 << (i
+ 3));
490 fprintf(stderr
, "\n\tTotal in use: %d, total free: %d\n",