1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.alloc.c,v 3.53 2015/02/22 16:31:54 christos Exp $ */
3 * tc.alloc.c (Caltech) 2/21/82
4 * Chris Kingsley, kingsley@cit-20.
6 * This is a very fast storage allocator. It allocates blocks of a small
7 * number of different sizes, and keeps free lists of each size. Blocks that
8 * don't exactly fit are passed up to the next larger size. In this
9 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
10 * This is designed for use in a program that uses vast quantities of memory,
11 * but bombs when it runs out.
14 * Copyright (c) 1980, 1991 The Regents of the University of California.
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #if defined(HAVE_SBRK) && !defined(__APPLE__)
49 RCSID("$tcsh: tc.alloc.c,v 3.53 2015/02/22 16:31:54 christos Exp $")
54 static char *memtop = NULL; /* PWP: top of current memory */
55 static char *membot = NULL; /* PWP: bottom of allocatable memory */
60 # define malloc fmalloc
62 # define calloc fcalloc
63 # define realloc frealloc
64 #endif /* WINNT_NATIVE */
66 #if !defined(DEBUG) || defined(SYSMALLOC)
70 static const char msg[] = "Out of memory\n";
72 TCSH_IGNORE(write(didfds ? 2 : SHDIAG, msg, strlen(msg)));
83 * Lots of os routines are busted and try to free invalid pointers.
84 * Although our free routine is smart enough and it will pick bad
85 * pointers most of the time, in cases where we know we are going to get
86 * a bad pointer, we'd rather leak.
93 typedef unsigned char U_char; /* we don't really have signed chars */
94 typedef unsigned int U_int;
95 typedef unsigned short U_short;
96 typedef unsigned long U_long;
100 * The overhead on a block is at least 4 bytes. When free, this space
101 * contains a pointer to the next free block, and the bottom two bits must
102 * be zero. When in use, the first byte is set to MAGIC, and the second
103 * byte is the size index. The remaining bytes are for alignment.
104 * If range checking is enabled and the size of the block fits
105 * in two bytes, then the top two bytes hold the size of the requested block
106 * plus the range checking words, and the header word MINUS ONE.
110 #define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
113 union overhead *ov_next; /* when free */
115 U_char ovu_magic; /* magic number */
116 U_char ovu_index; /* bucket # */
118 U_short ovu_size; /* actual block size */
119 U_int ovu_rmagic; /* range magic number */
122 #define ov_magic ovu.ovu_magic
123 #define ov_index ovu.ovu_index
124 #define ov_size ovu.ovu_size
125 #define ov_rmagic ovu.ovu_rmagic
128 #define MAGIC 0xfd /* magic # on accounting info */
129 #define RMAGIC 0x55555555 /* magic # on range info */
131 #define RSLOP sizeof (U_int)
140 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
141 * smallest allocatable block is 8 bytes. The overhead information
142 * precedes the data area returned to the user.
144 #define NBUCKETS ((sizeof(long) << 3) - 3)
145 static union overhead *nextf[NBUCKETS] IZERO_STRUCT;
148 * nmalloc[i] is the difference between the number of mallocs and frees
149 * for a given block size.
151 static U_int nmalloc[NBUCKETS] IZERO_STRUCT;
154 static int findbucket (union overhead *, int);
155 static void morecore (int);
160 # define CHECK(a, str, p) \
163 xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \
167 # define CHECK(a, str, p) \
170 xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \
176 malloc(size_t nbytes)
184 * Convert amount of memory requested into closest block size stored in
185 * hash buckets which satisfies request. Account for space used per block
190 * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
191 * so we get one more...
192 * From Michael Schroeder: This is not true. It depends on the
193 * timezone string. In Europe it can overwrite the 13th byte on a
195 * So we punt and we always allocate an extra byte.
200 nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP);
201 shiftr = (nbytes - 1) >> 2;
203 /* apart from this loop, this is O(1) */
204 while ((shiftr >>= 1) != 0)
207 * If nothing in hash bucket right now, request more memory from the
210 if (nextf[bucket] == NULL)
212 if ((p = nextf[bucket]) == NULL) {
218 xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes);
222 return ((memalign_t) 0);
224 /* remove from linked list */
225 nextf[bucket] = nextf[bucket]->ov_next;
227 p->ov_index = bucket;
231 * Record allocated size of block and bound space with magic numbers.
233 p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0;
234 p->ov_rmagic = RMAGIC;
235 *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
237 return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead))));
240 return ((memalign_t) 0);
242 return ((memalign_t) 0);
248 * Allocate more memory to the indicated bucket.
254 int rnu; /* 2^rnu bytes will be requested */
255 int nblks; /* become nblks blocks of the desired size */
261 * Insure memory is allocated on a page boundary. Should make getpageize
264 op = (union overhead *) sbrk(0);
265 memtop = (char *) op;
268 if ((long) op & 0x3ff) {
269 memtop = sbrk((int) (1024 - ((long) op & 0x3ff)));
270 memtop += (long) (1024 - ((long) op & 0x3ff));
273 /* take 2k unless the block is bigger than that */
274 rnu = (bucket <= 8) ? 11 : bucket + 3;
275 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
276 memtop = sbrk(1 << rnu); /* PWP */
277 op = (union overhead *) memtop;
281 memtop += (long) (1 << rnu);
283 * Round up to minimum allocation size boundary and deduct from block count
286 if (((U_long) op) & ROUNDUP) {
287 op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP);
291 * Add new memory allocated to that on free list for this hash bucket.
294 siz = 1 << (bucket + 3);
295 while (--nblks > 0) {
296 op->ov_next = (union overhead *) (((caddr_t) op) + siz);
297 op = (union overhead *) (((caddr_t) op) + siz);
312 * the don't free flag is there so that we avoid os bugs in routines
313 * that free invalid pointers!
315 if (cp == NULL || dont_free)
317 CHECK(!memtop || !membot,
318 CGETS(19, 2, "free(%p) called before any allocations."), cp);
319 CHECK(cp > (ptr_t) memtop,
320 CGETS(19, 3, "free(%p) above top of memory."), cp);
321 CHECK(cp < (ptr_t) membot,
322 CGETS(19, 4, "free(%p) below bottom of memory."), cp);
323 op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
324 CHECK(op->ov_magic != MAGIC,
325 CGETS(19, 5, "free(%p) bad block."), cp);
328 if (op->ov_index <= 13)
329 CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
330 CGETS(19, 6, "free(%p) bad range check."), cp);
332 CHECK(op->ov_index >= NBUCKETS,
333 CGETS(19, 7, "free(%p) bad block index."), cp);
335 op->ov_next = nextf[size];
347 calloc(size_t i, size_t j)
356 return ((memalign_t) cp);
359 return ((memalign_t) 0);
361 return ((memalign_t) 0);
366 * When a program attempts "storage compaction" as mentioned in the
367 * old malloc man page, it realloc's an already freed block. Usually
368 * this is the last block it freed; occasionally it might be farther
369 * back. We have to search all the free lists for the block in order
370 * to determine its bucket: 1st we make one pass thru the lists
371 * checking only the first block in each; if that fails we search
372 * ``realloc_srchlen'' blocks in each list for a match (the variable
373 * is extern so the caller can modify it). If that fails we just copy
374 * however many bytes was given to realloc() and hope it's not huge.
377 /* 4 should be plenty, -1 =>'s whole list */
378 static int realloc_srchlen = 4;
382 realloc(ptr_t cp, size_t nbytes)
392 return (malloc(nbytes));
393 op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
394 if (op->ov_magic == MAGIC) {
400 * Already free, doing "compaction".
402 * Search for the old block of memory on the free list. First, check the
403 * most common case (last element free'd), then (this failing) the last
404 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
405 * the size of the memory block being realloc'd is the smallest
408 if ((i = findbucket(op, 1)) < 0 &&
409 (i = findbucket(op, realloc_srchlen)) < 0)
412 onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP);
414 /* avoid the copy if same size block */
415 if (was_alloced && (onb <= (U_int) (1 << (i + 3))) &&
416 (onb > (U_int) (1 << (i + 2)))) {
418 /* JMR: formerly this wasn't updated ! */
419 nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP);
420 *((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC;
421 op->ov_rmagic = RMAGIC;
422 op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0;
424 return ((memalign_t) cp);
426 if ((res = malloc(nbytes)) == NULL)
427 return ((memalign_t) NULL);
428 if (cp != res) { /* common optimization */
430 * christos: this used to copy nbytes! It should copy the
431 * smaller of the old and new size
433 onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP;
434 (void) memmove(res, cp, onb < nbytes ? onb : nbytes);
438 return ((memalign_t) res);
441 return ((memalign_t) 0);
443 return ((memalign_t) 0);
448 * On linux, _nss_nis_setnetgrent() calls this function to determine
449 * the usable size of the pointer passed, but this is not a portable
450 * API, so we cannot use our malloc replacement without providing one.
451 * Thanks a lot glibc!
458 size_t malloc_usable_size(M_U_S_CONST void *);
460 malloc_usable_size(M_U_S_CONST void *ptr)
462 const union overhead *op = (const union overhead *)
463 (((const char *) ptr) - MEMALIGN(sizeof(*op)));
464 if (op->ov_magic == MAGIC)
465 return 1 << (op->ov_index + 3);
473 * Search ``srchlen'' elements of each free list for a block whose
474 * header starts at ``freep''. If srchlen is -1 search the whole list.
475 * Return bucket number, or -1 if not found.
478 findbucket(union overhead *freep, int srchlen)
484 for (i = 0; i < NBUCKETS; i++) {
486 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
498 #else /* SYSMALLOC */
501 ** ``Protected versions'' of malloc, realloc, calloc, and free
505 ** 1. malloc(0) is bad
507 ** 3. realloc(0, n) is bad
508 ** 4. realloc(n, 0) is bad
510 ** Also we call our error routine if we run out of memory.
522 #endif /* USE_SBRK */
524 if ((ptr = malloc(n)) == NULL)
527 if (memtop < ((char *) ptr) + n)
528 memtop = ((char *) ptr) + n;
531 #endif /* !USE_SBRK */
532 return ((memalign_t) ptr);
536 srealloc(ptr_t p, size_t n)
545 #endif /* USE_SBRK */
547 if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL)
550 if (memtop < ((char *) ptr) + n)
551 memtop = ((char *) ptr) + n;
554 #endif /* !USE_SBRK */
555 return ((memalign_t) ptr);
559 scalloc(size_t s, size_t n)
569 #endif /* USE_SBRK */
571 if ((ptr = malloc(n)) == NULL)
577 if (memtop < ((char *) ptr) + n)
578 memtop = ((char *) ptr) + n;
581 #endif /* !USE_SBRK */
583 return ((memalign_t) ptr);
593 #endif /* SYSMALLOC */
596 * mstats - print out statistics about malloc
598 * Prints two lines of numbers, one showing the length of the free list
599 * for each size category, the second showing the number of mallocs -
600 * frees for each size category.
604 showall(Char **v, struct command *c)
609 int totfree = 0, totused = 0;
611 xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
612 for (i = 0; i < NBUCKETS; i++) {
613 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
616 totfree += j * (1 << (i + 3));
618 xprintf("\n%s:\t", CGETS(19, 9, "used"));
619 for (i = 0; i < NBUCKETS; i++) {
620 xprintf(" %4d", nmalloc[i]);
621 totused += nmalloc[i] * (1 << (i + 3));
623 xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
625 xprintf(CGETS(19, 11,
626 "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"),
627 (unsigned long) membot, (unsigned long) memtop,
628 (unsigned long) sbrk(0));
629 #else /* SYSMALLOC */
630 #ifndef HAVE_MALLINFO
633 #endif /* USE_SBRK */
634 xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
635 (unsigned long) membot, (unsigned long) memtop,
636 (unsigned long) (memtop - membot));
637 #else /* HAVE_MALLINFO */
641 xprintf(CGETS(19, 13, "%s current memory allocation:\n"), progname);
642 xprintf(CGETS(19, 14, "Total space allocated from system: %d\n"), mi.arena);
643 xprintf(CGETS(19, 15, "Number of non-inuse chunks: %d\n"), mi.ordblks);
644 xprintf(CGETS(19, 16, "Number of mmapped regions: %d\n"), mi.hblks);
645 xprintf(CGETS(19, 17, "Total space in mmapped regions: %d\n"), mi.hblkhd);
646 xprintf(CGETS(19, 18, "Total allocated space: %d\n"), mi.uordblks);
647 xprintf(CGETS(19, 19, "Total non-inuse space: %d\n"), mi.fordblks);
648 xprintf(CGETS(19, 20, "Top-most, releasable space: %d\n"), mi.keepcost);
649 #endif /* HAVE_MALLINFO */
650 #endif /* SYSMALLOC */