1*19d2e3deSDmitry Chagin /* $Header: /p/tcsh/cvsroot/tcsh/tc.alloc.c,v 3.56 2016/03/08 12:47:43 christos Exp $ */ 2c80476e4SDavid E. O'Brien /* 3c80476e4SDavid E. O'Brien * tc.alloc.c (Caltech) 2/21/82 4c80476e4SDavid E. O'Brien * Chris Kingsley, kingsley@cit-20. 5c80476e4SDavid E. O'Brien * 6c80476e4SDavid E. O'Brien * This is a very fast storage allocator. It allocates blocks of a small 7c80476e4SDavid E. O'Brien * number of different sizes, and keeps free lists of each size. Blocks that 8c80476e4SDavid E. O'Brien * don't exactly fit are passed up to the next larger size. In this 9c80476e4SDavid E. O'Brien * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. 10c80476e4SDavid E. O'Brien * This is designed for use in a program that uses vast quantities of memory, 11c80476e4SDavid E. O'Brien * but bombs when it runs out. 12c80476e4SDavid E. O'Brien */ 13c80476e4SDavid E. O'Brien /*- 14c80476e4SDavid E. O'Brien * Copyright (c) 1980, 1991 The Regents of the University of California. 15c80476e4SDavid E. O'Brien * All rights reserved. 16c80476e4SDavid E. O'Brien * 17c80476e4SDavid E. O'Brien * Redistribution and use in source and binary forms, with or without 18c80476e4SDavid E. O'Brien * modification, are permitted provided that the following conditions 19c80476e4SDavid E. O'Brien * are met: 20c80476e4SDavid E. O'Brien * 1. Redistributions of source code must retain the above copyright 21c80476e4SDavid E. O'Brien * notice, this list of conditions and the following disclaimer. 22c80476e4SDavid E. O'Brien * 2. Redistributions in binary form must reproduce the above copyright 23c80476e4SDavid E. O'Brien * notice, this list of conditions and the following disclaimer in the 24c80476e4SDavid E. O'Brien * documentation and/or other materials provided with the distribution. 2529301572SMark Peek * 3. Neither the name of the University nor the names of its contributors 26c80476e4SDavid E. O'Brien * may be used to endorse or promote products derived from this software 27c80476e4SDavid E. O'Brien * without specific prior written permission. 28c80476e4SDavid E. O'Brien * 29c80476e4SDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30c80476e4SDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31c80476e4SDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32c80476e4SDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33c80476e4SDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34c80476e4SDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35c80476e4SDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36c80476e4SDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37c80476e4SDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38c80476e4SDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39c80476e4SDavid E. O'Brien * SUCH DAMAGE. 40c80476e4SDavid E. O'Brien */ 41c80476e4SDavid E. O'Brien #include "sh.h" 429ccc37e3SMark Peek #ifdef HAVE_MALLINFO 439ccc37e3SMark Peek #include <malloc.h> 449ccc37e3SMark Peek #endif 45*19d2e3deSDmitry Chagin #if defined(HAVE_SBRK) && !defined(__APPLE__) 46*19d2e3deSDmitry Chagin #define USE_SBRK 47*19d2e3deSDmitry Chagin #endif 48c80476e4SDavid E. O'Brien 49*19d2e3deSDmitry Chagin RCSID("$tcsh: tc.alloc.c,v 3.56 2016/03/08 12:47:43 christos Exp $") 5045e5710bSMark Peek 5145e5710bSMark Peek #define RCHECK 5245e5710bSMark Peek #define DEBUG 53c80476e4SDavid E. O'Brien 54c80476e4SDavid E. O'Brien static char *memtop = NULL; /* PWP: top of current memory */ 55c80476e4SDavid E. O'Brien static char *membot = NULL; /* PWP: bottom of allocatable memory */ 56c80476e4SDavid E. O'Brien 57c80476e4SDavid E. O'Brien int dont_free = 0; 58c80476e4SDavid E. O'Brien 593b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE 60c80476e4SDavid E. O'Brien # define malloc fmalloc 61c80476e4SDavid E. O'Brien # define free ffree 62c80476e4SDavid E. O'Brien # define calloc fcalloc 63c80476e4SDavid E. O'Brien # define realloc frealloc 643b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */ 65c80476e4SDavid E. O'Brien 6645e5710bSMark Peek #if !defined(DEBUG) || defined(SYSMALLOC) 6745e5710bSMark Peek static void 6845e5710bSMark Peek out_of_memory (void) 6945e5710bSMark Peek { 7045e5710bSMark Peek static const char msg[] = "Out of memory\n"; 71c80476e4SDavid E. O'Brien 72*19d2e3deSDmitry Chagin TCSH_IGNORE(write(didfds ? 2 : SHDIAG, msg, strlen(msg))); 7345e5710bSMark Peek _exit(1); 7445e5710bSMark Peek } 7545e5710bSMark Peek #endif 7645e5710bSMark Peek 7745e5710bSMark Peek #ifndef SYSMALLOC 78c80476e4SDavid E. O'Brien 79c80476e4SDavid E. O'Brien #ifdef SX 80c80476e4SDavid E. O'Brien extern void* sbrk(); 81c80476e4SDavid E. O'Brien #endif 82c80476e4SDavid E. O'Brien /* 83c80476e4SDavid E. O'Brien * Lots of os routines are busted and try to free invalid pointers. 84c80476e4SDavid E. O'Brien * Although our free routine is smart enough and it will pick bad 85c80476e4SDavid E. O'Brien * pointers most of the time, in cases where we know we are going to get 86c80476e4SDavid E. O'Brien * a bad pointer, we'd rather leak. 87c80476e4SDavid E. O'Brien */ 88c80476e4SDavid E. O'Brien 89c80476e4SDavid E. O'Brien #ifndef NULL 90c80476e4SDavid E. O'Brien #define NULL 0 91c80476e4SDavid E. O'Brien #endif 92c80476e4SDavid E. O'Brien 93c80476e4SDavid E. O'Brien typedef unsigned char U_char; /* we don't really have signed chars */ 94c80476e4SDavid E. O'Brien typedef unsigned int U_int; 95c80476e4SDavid E. O'Brien typedef unsigned short U_short; 96c80476e4SDavid E. O'Brien typedef unsigned long U_long; 97c80476e4SDavid E. O'Brien 98c80476e4SDavid E. O'Brien 99c80476e4SDavid E. O'Brien /* 100c80476e4SDavid E. O'Brien * The overhead on a block is at least 4 bytes. When free, this space 101c80476e4SDavid E. O'Brien * contains a pointer to the next free block, and the bottom two bits must 102c80476e4SDavid E. O'Brien * be zero. When in use, the first byte is set to MAGIC, and the second 103c80476e4SDavid E. O'Brien * byte is the size index. The remaining bytes are for alignment. 104c80476e4SDavid E. O'Brien * If range checking is enabled and the size of the block fits 105c80476e4SDavid E. O'Brien * in two bytes, then the top two bytes hold the size of the requested block 106c80476e4SDavid E. O'Brien * plus the range checking words, and the header word MINUS ONE. 107c80476e4SDavid E. O'Brien */ 108c80476e4SDavid E. O'Brien 109c80476e4SDavid E. O'Brien 110c80476e4SDavid E. O'Brien #define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP) 111c80476e4SDavid E. O'Brien 112c80476e4SDavid E. O'Brien union overhead { 113c80476e4SDavid E. O'Brien union overhead *ov_next; /* when free */ 114c80476e4SDavid E. O'Brien struct { 115c80476e4SDavid E. O'Brien U_char ovu_magic; /* magic number */ 116c80476e4SDavid E. O'Brien U_char ovu_index; /* bucket # */ 117c80476e4SDavid E. O'Brien #ifdef RCHECK 118c80476e4SDavid E. O'Brien U_short ovu_size; /* actual block size */ 119c80476e4SDavid E. O'Brien U_int ovu_rmagic; /* range magic number */ 120c80476e4SDavid E. O'Brien #endif 121c80476e4SDavid E. O'Brien } ovu; 122c80476e4SDavid E. O'Brien #define ov_magic ovu.ovu_magic 123c80476e4SDavid E. O'Brien #define ov_index ovu.ovu_index 124c80476e4SDavid E. O'Brien #define ov_size ovu.ovu_size 125c80476e4SDavid E. O'Brien #define ov_rmagic ovu.ovu_rmagic 126c80476e4SDavid E. O'Brien }; 127c80476e4SDavid E. O'Brien 128c80476e4SDavid E. O'Brien #define MAGIC 0xfd /* magic # on accounting info */ 129c80476e4SDavid E. O'Brien #define RMAGIC 0x55555555 /* magic # on range info */ 130c80476e4SDavid E. O'Brien #ifdef RCHECK 131c80476e4SDavid E. O'Brien #define RSLOP sizeof (U_int) 132c80476e4SDavid E. O'Brien #else 133c80476e4SDavid E. O'Brien #define RSLOP 0 134c80476e4SDavid E. O'Brien #endif 135c80476e4SDavid E. O'Brien 136c80476e4SDavid E. O'Brien 137*19d2e3deSDmitry Chagin #ifdef _LP64 138*19d2e3deSDmitry Chagin #define ROUNDUP 15 139*19d2e3deSDmitry Chagin #else 140c80476e4SDavid E. O'Brien #define ROUNDUP 7 141*19d2e3deSDmitry Chagin #endif 142c80476e4SDavid E. O'Brien 143c80476e4SDavid E. O'Brien /* 144c80476e4SDavid E. O'Brien * nextf[i] is the pointer to the next free block of size 2^(i+3). The 145c80476e4SDavid E. O'Brien * smallest allocatable block is 8 bytes. The overhead information 146c80476e4SDavid E. O'Brien * precedes the data area returned to the user. 147c80476e4SDavid E. O'Brien */ 148c80476e4SDavid E. O'Brien #define NBUCKETS ((sizeof(long) << 3) - 3) 149c80476e4SDavid E. O'Brien static union overhead *nextf[NBUCKETS] IZERO_STRUCT; 150c80476e4SDavid E. O'Brien 151c80476e4SDavid E. O'Brien /* 152c80476e4SDavid E. O'Brien * nmalloc[i] is the difference between the number of mallocs and frees 153c80476e4SDavid E. O'Brien * for a given block size. 154c80476e4SDavid E. O'Brien */ 155c80476e4SDavid E. O'Brien static U_int nmalloc[NBUCKETS] IZERO_STRUCT; 156c80476e4SDavid E. O'Brien 157c80476e4SDavid E. O'Brien #ifndef lint 15845e5710bSMark Peek static int findbucket (union overhead *, int); 15945e5710bSMark Peek static void morecore (int); 160c80476e4SDavid E. O'Brien #endif 161c80476e4SDavid E. O'Brien 162c80476e4SDavid E. O'Brien 163c80476e4SDavid E. O'Brien #ifdef DEBUG 164c80476e4SDavid E. O'Brien # define CHECK(a, str, p) \ 165c80476e4SDavid E. O'Brien if (a) { \ 166c80476e4SDavid E. O'Brien xprintf(str, p); \ 16745e5710bSMark Peek xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \ 168c80476e4SDavid E. O'Brien abort(); \ 169c80476e4SDavid E. O'Brien } 170c80476e4SDavid E. O'Brien #else 171c80476e4SDavid E. O'Brien # define CHECK(a, str, p) \ 172c80476e4SDavid E. O'Brien if (a) { \ 173c80476e4SDavid E. O'Brien xprintf(str, p); \ 17445e5710bSMark Peek xprintf(" (memtop = %p membot = %p)\n", memtop, membot); \ 175c80476e4SDavid E. O'Brien return; \ 176c80476e4SDavid E. O'Brien } 177c80476e4SDavid E. O'Brien #endif 178c80476e4SDavid E. O'Brien 179c80476e4SDavid E. O'Brien memalign_t 18045e5710bSMark Peek malloc(size_t nbytes) 181c80476e4SDavid E. O'Brien { 182c80476e4SDavid E. O'Brien #ifndef lint 18323338178SMark Peek union overhead *p; 18423338178SMark Peek int bucket = 0; 18523338178SMark Peek unsigned shiftr; 186c80476e4SDavid E. O'Brien 187c80476e4SDavid E. O'Brien /* 188c80476e4SDavid E. O'Brien * Convert amount of memory requested into closest block size stored in 189c80476e4SDavid E. O'Brien * hash buckets which satisfies request. Account for space used per block 190c80476e4SDavid E. O'Brien * for accounting. 191c80476e4SDavid E. O'Brien */ 192c80476e4SDavid E. O'Brien #ifdef SUNOS4 193c80476e4SDavid E. O'Brien /* 194c80476e4SDavid E. O'Brien * SunOS localtime() overwrites the 9th byte on an 8 byte malloc().... 195c80476e4SDavid E. O'Brien * so we get one more... 196c80476e4SDavid E. O'Brien * From Michael Schroeder: This is not true. It depends on the 197c80476e4SDavid E. O'Brien * timezone string. In Europe it can overwrite the 13th byte on a 198c80476e4SDavid E. O'Brien * 12 byte malloc. 199c80476e4SDavid E. O'Brien * So we punt and we always allocate an extra byte. 200c80476e4SDavid E. O'Brien */ 201c80476e4SDavid E. O'Brien nbytes++; 202c80476e4SDavid E. O'Brien #endif 203c80476e4SDavid E. O'Brien 204c80476e4SDavid E. O'Brien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP); 205c80476e4SDavid E. O'Brien shiftr = (nbytes - 1) >> 2; 206c80476e4SDavid E. O'Brien 207c80476e4SDavid E. O'Brien /* apart from this loop, this is O(1) */ 208c80476e4SDavid E. O'Brien while ((shiftr >>= 1) != 0) 209c80476e4SDavid E. O'Brien bucket++; 210c80476e4SDavid E. O'Brien /* 211c80476e4SDavid E. O'Brien * If nothing in hash bucket right now, request more memory from the 212c80476e4SDavid E. O'Brien * system. 213c80476e4SDavid E. O'Brien */ 214c80476e4SDavid E. O'Brien if (nextf[bucket] == NULL) 215c80476e4SDavid E. O'Brien morecore(bucket); 21645e5710bSMark Peek if ((p = nextf[bucket]) == NULL) { 217c80476e4SDavid E. O'Brien child++; 218c80476e4SDavid E. O'Brien #ifndef DEBUG 21945e5710bSMark Peek out_of_memory(); 220c80476e4SDavid E. O'Brien #else 221c80476e4SDavid E. O'Brien showall(NULL, NULL); 22245e5710bSMark Peek xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes); 223c80476e4SDavid E. O'Brien abort(); 224c80476e4SDavid E. O'Brien #endif 225c80476e4SDavid E. O'Brien /* fool lint */ 226c80476e4SDavid E. O'Brien return ((memalign_t) 0); 227c80476e4SDavid E. O'Brien } 228c80476e4SDavid E. O'Brien /* remove from linked list */ 229c80476e4SDavid E. O'Brien nextf[bucket] = nextf[bucket]->ov_next; 230c80476e4SDavid E. O'Brien p->ov_magic = MAGIC; 231c80476e4SDavid E. O'Brien p->ov_index = bucket; 232c80476e4SDavid E. O'Brien nmalloc[bucket]++; 233c80476e4SDavid E. O'Brien #ifdef RCHECK 234c80476e4SDavid E. O'Brien /* 235c80476e4SDavid E. O'Brien * Record allocated size of block and bound space with magic numbers. 236c80476e4SDavid E. O'Brien */ 237c80476e4SDavid E. O'Brien p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0; 238c80476e4SDavid E. O'Brien p->ov_rmagic = RMAGIC; 239c80476e4SDavid E. O'Brien *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC; 240c80476e4SDavid E. O'Brien #endif 241c80476e4SDavid E. O'Brien return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead)))); 242c80476e4SDavid E. O'Brien #else 243c80476e4SDavid E. O'Brien if (nbytes) 244c80476e4SDavid E. O'Brien return ((memalign_t) 0); 245c80476e4SDavid E. O'Brien else 246c80476e4SDavid E. O'Brien return ((memalign_t) 0); 247c80476e4SDavid E. O'Brien #endif /* !lint */ 248c80476e4SDavid E. O'Brien } 249c80476e4SDavid E. O'Brien 250c80476e4SDavid E. O'Brien #ifndef lint 251c80476e4SDavid E. O'Brien /* 252c80476e4SDavid E. O'Brien * Allocate more memory to the indicated bucket. 253c80476e4SDavid E. O'Brien */ 254c80476e4SDavid E. O'Brien static void 25545e5710bSMark Peek morecore(int bucket) 256c80476e4SDavid E. O'Brien { 25723338178SMark Peek union overhead *op; 25823338178SMark Peek int rnu; /* 2^rnu bytes will be requested */ 25923338178SMark Peek int nblks; /* become nblks blocks of the desired size */ 26023338178SMark Peek int siz; 261c80476e4SDavid E. O'Brien 262c80476e4SDavid E. O'Brien if (nextf[bucket]) 263c80476e4SDavid E. O'Brien return; 264c80476e4SDavid E. O'Brien /* 265c80476e4SDavid E. O'Brien * Insure memory is allocated on a page boundary. Should make getpageize 266c80476e4SDavid E. O'Brien * call? 267c80476e4SDavid E. O'Brien */ 268c80476e4SDavid E. O'Brien op = (union overhead *) sbrk(0); 269c80476e4SDavid E. O'Brien memtop = (char *) op; 270c80476e4SDavid E. O'Brien if (membot == NULL) 271c80476e4SDavid E. O'Brien membot = memtop; 272c80476e4SDavid E. O'Brien if ((long) op & 0x3ff) { 27345e5710bSMark Peek memtop = sbrk((int) (1024 - ((long) op & 0x3ff))); 274c80476e4SDavid E. O'Brien memtop += (long) (1024 - ((long) op & 0x3ff)); 275c80476e4SDavid E. O'Brien } 276c80476e4SDavid E. O'Brien 277c80476e4SDavid E. O'Brien /* take 2k unless the block is bigger than that */ 278c80476e4SDavid E. O'Brien rnu = (bucket <= 8) ? 11 : bucket + 3; 279c80476e4SDavid E. O'Brien nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ 28045e5710bSMark Peek memtop = sbrk(1 << rnu); /* PWP */ 281c80476e4SDavid E. O'Brien op = (union overhead *) memtop; 282c80476e4SDavid E. O'Brien /* no more room! */ 283c80476e4SDavid E. O'Brien if ((long) op == -1) 284c80476e4SDavid E. O'Brien return; 285c80476e4SDavid E. O'Brien memtop += (long) (1 << rnu); 286c80476e4SDavid E. O'Brien /* 287c80476e4SDavid E. O'Brien * Round up to minimum allocation size boundary and deduct from block count 288c80476e4SDavid E. O'Brien * to reflect. 289c80476e4SDavid E. O'Brien */ 290c80476e4SDavid E. O'Brien if (((U_long) op) & ROUNDUP) { 291c80476e4SDavid E. O'Brien op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP); 292c80476e4SDavid E. O'Brien nblks--; 293c80476e4SDavid E. O'Brien } 294c80476e4SDavid E. O'Brien /* 295c80476e4SDavid E. O'Brien * Add new memory allocated to that on free list for this hash bucket. 296c80476e4SDavid E. O'Brien */ 297c80476e4SDavid E. O'Brien nextf[bucket] = op; 298c80476e4SDavid E. O'Brien siz = 1 << (bucket + 3); 299c80476e4SDavid E. O'Brien while (--nblks > 0) { 300c80476e4SDavid E. O'Brien op->ov_next = (union overhead *) (((caddr_t) op) + siz); 301c80476e4SDavid E. O'Brien op = (union overhead *) (((caddr_t) op) + siz); 302c80476e4SDavid E. O'Brien } 303c80476e4SDavid E. O'Brien op->ov_next = NULL; 304c80476e4SDavid E. O'Brien } 305c80476e4SDavid E. O'Brien 306c80476e4SDavid E. O'Brien #endif 307c80476e4SDavid E. O'Brien 308c80476e4SDavid E. O'Brien void 30945e5710bSMark Peek free(ptr_t cp) 310c80476e4SDavid E. O'Brien { 311c80476e4SDavid E. O'Brien #ifndef lint 31223338178SMark Peek int size; 31323338178SMark Peek union overhead *op; 314c80476e4SDavid E. O'Brien 315c80476e4SDavid E. O'Brien /* 316c80476e4SDavid E. O'Brien * the don't free flag is there so that we avoid os bugs in routines 317c80476e4SDavid E. O'Brien * that free invalid pointers! 318c80476e4SDavid E. O'Brien */ 319c80476e4SDavid E. O'Brien if (cp == NULL || dont_free) 320c80476e4SDavid E. O'Brien return; 321c80476e4SDavid E. O'Brien CHECK(!memtop || !membot, 32245e5710bSMark Peek CGETS(19, 2, "free(%p) called before any allocations."), cp); 323c80476e4SDavid E. O'Brien CHECK(cp > (ptr_t) memtop, 32445e5710bSMark Peek CGETS(19, 3, "free(%p) above top of memory."), cp); 325c80476e4SDavid E. O'Brien CHECK(cp < (ptr_t) membot, 32645e5710bSMark Peek CGETS(19, 4, "free(%p) below bottom of memory."), cp); 327c80476e4SDavid E. O'Brien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 328c80476e4SDavid E. O'Brien CHECK(op->ov_magic != MAGIC, 32945e5710bSMark Peek CGETS(19, 5, "free(%p) bad block."), cp); 330c80476e4SDavid E. O'Brien 331c80476e4SDavid E. O'Brien #ifdef RCHECK 332c80476e4SDavid E. O'Brien if (op->ov_index <= 13) 333c80476e4SDavid E. O'Brien CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC, 33445e5710bSMark Peek CGETS(19, 6, "free(%p) bad range check."), cp); 335c80476e4SDavid E. O'Brien #endif 336c80476e4SDavid E. O'Brien CHECK(op->ov_index >= NBUCKETS, 33745e5710bSMark Peek CGETS(19, 7, "free(%p) bad block index."), cp); 338c80476e4SDavid E. O'Brien size = op->ov_index; 339c80476e4SDavid E. O'Brien op->ov_next = nextf[size]; 340c80476e4SDavid E. O'Brien nextf[size] = op; 341c80476e4SDavid E. O'Brien 342c80476e4SDavid E. O'Brien nmalloc[size]--; 343c80476e4SDavid E. O'Brien 344c80476e4SDavid E. O'Brien #else 345c80476e4SDavid E. O'Brien if (cp == NULL) 346c80476e4SDavid E. O'Brien return; 347c80476e4SDavid E. O'Brien #endif 348c80476e4SDavid E. O'Brien } 349c80476e4SDavid E. O'Brien 350c80476e4SDavid E. O'Brien memalign_t 35145e5710bSMark Peek calloc(size_t i, size_t j) 352c80476e4SDavid E. O'Brien { 353c80476e4SDavid E. O'Brien #ifndef lint 35445e5710bSMark Peek char *cp; 355*19d2e3deSDmitry Chagin volatile size_t k; 356c80476e4SDavid E. O'Brien 357c80476e4SDavid E. O'Brien i *= j; 35845e5710bSMark Peek cp = xmalloc(i); 359*19d2e3deSDmitry Chagin /* Stop gcc 5.x from optimizing malloc+memset = calloc */ 360*19d2e3deSDmitry Chagin k = i; 361*19d2e3deSDmitry Chagin memset(cp, 0, k); 362c80476e4SDavid E. O'Brien 36345e5710bSMark Peek return ((memalign_t) cp); 364c80476e4SDavid E. O'Brien #else 365c80476e4SDavid E. O'Brien if (i && j) 366c80476e4SDavid E. O'Brien return ((memalign_t) 0); 367c80476e4SDavid E. O'Brien else 368c80476e4SDavid E. O'Brien return ((memalign_t) 0); 369c80476e4SDavid E. O'Brien #endif 370c80476e4SDavid E. O'Brien } 371c80476e4SDavid E. O'Brien 372c80476e4SDavid E. O'Brien /* 373c80476e4SDavid E. O'Brien * When a program attempts "storage compaction" as mentioned in the 374c80476e4SDavid E. O'Brien * old malloc man page, it realloc's an already freed block. Usually 375c80476e4SDavid E. O'Brien * this is the last block it freed; occasionally it might be farther 376c80476e4SDavid E. O'Brien * back. We have to search all the free lists for the block in order 377c80476e4SDavid E. O'Brien * to determine its bucket: 1st we make one pass thru the lists 378c80476e4SDavid E. O'Brien * checking only the first block in each; if that fails we search 379c80476e4SDavid E. O'Brien * ``realloc_srchlen'' blocks in each list for a match (the variable 380c80476e4SDavid E. O'Brien * is extern so the caller can modify it). If that fails we just copy 381c80476e4SDavid E. O'Brien * however many bytes was given to realloc() and hope it's not huge. 382c80476e4SDavid E. O'Brien */ 383c80476e4SDavid E. O'Brien #ifndef lint 384c80476e4SDavid E. O'Brien /* 4 should be plenty, -1 =>'s whole list */ 385c80476e4SDavid E. O'Brien static int realloc_srchlen = 4; 386c80476e4SDavid E. O'Brien #endif /* lint */ 387c80476e4SDavid E. O'Brien 388c80476e4SDavid E. O'Brien memalign_t 38945e5710bSMark Peek realloc(ptr_t cp, size_t nbytes) 390c80476e4SDavid E. O'Brien { 391c80476e4SDavid E. O'Brien #ifndef lint 39223338178SMark Peek U_int onb; 393c80476e4SDavid E. O'Brien union overhead *op; 394c80476e4SDavid E. O'Brien ptr_t res; 39523338178SMark Peek int i; 396c80476e4SDavid E. O'Brien int was_alloced = 0; 397c80476e4SDavid E. O'Brien 398c80476e4SDavid E. O'Brien if (cp == NULL) 399c80476e4SDavid E. O'Brien return (malloc(nbytes)); 400c80476e4SDavid E. O'Brien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 401c80476e4SDavid E. O'Brien if (op->ov_magic == MAGIC) { 402c80476e4SDavid E. O'Brien was_alloced++; 403c80476e4SDavid E. O'Brien i = op->ov_index; 404c80476e4SDavid E. O'Brien } 405c80476e4SDavid E. O'Brien else 406c80476e4SDavid E. O'Brien /* 407c80476e4SDavid E. O'Brien * Already free, doing "compaction". 408c80476e4SDavid E. O'Brien * 409c80476e4SDavid E. O'Brien * Search for the old block of memory on the free list. First, check the 410c80476e4SDavid E. O'Brien * most common case (last element free'd), then (this failing) the last 411c80476e4SDavid E. O'Brien * ``realloc_srchlen'' items free'd. If all lookups fail, then assume 412c80476e4SDavid E. O'Brien * the size of the memory block being realloc'd is the smallest 413c80476e4SDavid E. O'Brien * possible. 414c80476e4SDavid E. O'Brien */ 415c80476e4SDavid E. O'Brien if ((i = findbucket(op, 1)) < 0 && 416c80476e4SDavid E. O'Brien (i = findbucket(op, realloc_srchlen)) < 0) 417c80476e4SDavid E. O'Brien i = 0; 418c80476e4SDavid E. O'Brien 419c80476e4SDavid E. O'Brien onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP); 420c80476e4SDavid E. O'Brien 421c80476e4SDavid E. O'Brien /* avoid the copy if same size block */ 422c80476e4SDavid E. O'Brien if (was_alloced && (onb <= (U_int) (1 << (i + 3))) && 423c80476e4SDavid E. O'Brien (onb > (U_int) (1 << (i + 2)))) { 424c80476e4SDavid E. O'Brien #ifdef RCHECK 425c80476e4SDavid E. O'Brien /* JMR: formerly this wasn't updated ! */ 426c80476e4SDavid E. O'Brien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP); 427c80476e4SDavid E. O'Brien *((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC; 428c80476e4SDavid E. O'Brien op->ov_rmagic = RMAGIC; 429c80476e4SDavid E. O'Brien op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0; 430c80476e4SDavid E. O'Brien #endif 431c80476e4SDavid E. O'Brien return ((memalign_t) cp); 432c80476e4SDavid E. O'Brien } 433c80476e4SDavid E. O'Brien if ((res = malloc(nbytes)) == NULL) 434c80476e4SDavid E. O'Brien return ((memalign_t) NULL); 435c80476e4SDavid E. O'Brien if (cp != res) { /* common optimization */ 436c80476e4SDavid E. O'Brien /* 437c80476e4SDavid E. O'Brien * christos: this used to copy nbytes! It should copy the 438c80476e4SDavid E. O'Brien * smaller of the old and new size 439c80476e4SDavid E. O'Brien */ 440c80476e4SDavid E. O'Brien onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP; 44145e5710bSMark Peek (void) memmove(res, cp, onb < nbytes ? onb : nbytes); 442c80476e4SDavid E. O'Brien } 443c80476e4SDavid E. O'Brien if (was_alloced) 444c80476e4SDavid E. O'Brien free(cp); 445c80476e4SDavid E. O'Brien return ((memalign_t) res); 446c80476e4SDavid E. O'Brien #else 447c80476e4SDavid E. O'Brien if (cp && nbytes) 448c80476e4SDavid E. O'Brien return ((memalign_t) 0); 449c80476e4SDavid E. O'Brien else 450c80476e4SDavid E. O'Brien return ((memalign_t) 0); 451c80476e4SDavid E. O'Brien #endif /* !lint */ 452c80476e4SDavid E. O'Brien } 453c80476e4SDavid E. O'Brien 4549ccc37e3SMark Peek /* 4559ccc37e3SMark Peek * On linux, _nss_nis_setnetgrent() calls this function to determine 4569ccc37e3SMark Peek * the usable size of the pointer passed, but this is not a portable 4579ccc37e3SMark Peek * API, so we cannot use our malloc replacement without providing one. 4589ccc37e3SMark Peek * Thanks a lot glibc! 4599ccc37e3SMark Peek */ 4609ccc37e3SMark Peek #ifdef __linux__ 4619ccc37e3SMark Peek #define M_U_S_CONST 4629ccc37e3SMark Peek #else 4639ccc37e3SMark Peek #define M_U_S_CONST 4649ccc37e3SMark Peek #endif 4659ccc37e3SMark Peek size_t malloc_usable_size(M_U_S_CONST void *); 4669ccc37e3SMark Peek size_t 4679ccc37e3SMark Peek malloc_usable_size(M_U_S_CONST void *ptr) 4689ccc37e3SMark Peek { 4699ccc37e3SMark Peek const union overhead *op = (const union overhead *) 4709ccc37e3SMark Peek (((const char *) ptr) - MEMALIGN(sizeof(*op))); 4719ccc37e3SMark Peek if (op->ov_magic == MAGIC) 472*19d2e3deSDmitry Chagin return 1 << (op->ov_index + 3); 4739ccc37e3SMark Peek else 4749ccc37e3SMark Peek return 0; 4759ccc37e3SMark Peek } 476c80476e4SDavid E. O'Brien 477c80476e4SDavid E. O'Brien 478c80476e4SDavid E. O'Brien #ifndef lint 479c80476e4SDavid E. O'Brien /* 480c80476e4SDavid E. O'Brien * Search ``srchlen'' elements of each free list for a block whose 481c80476e4SDavid E. O'Brien * header starts at ``freep''. If srchlen is -1 search the whole list. 482c80476e4SDavid E. O'Brien * Return bucket number, or -1 if not found. 483c80476e4SDavid E. O'Brien */ 484c80476e4SDavid E. O'Brien static int 48545e5710bSMark Peek findbucket(union overhead *freep, int srchlen) 486c80476e4SDavid E. O'Brien { 48723338178SMark Peek union overhead *p; 48823338178SMark Peek size_t i; 48923338178SMark Peek int j; 490c80476e4SDavid E. O'Brien 491c80476e4SDavid E. O'Brien for (i = 0; i < NBUCKETS; i++) { 492c80476e4SDavid E. O'Brien j = 0; 493c80476e4SDavid E. O'Brien for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 494c80476e4SDavid E. O'Brien if (p == freep) 495c80476e4SDavid E. O'Brien return (i); 496c80476e4SDavid E. O'Brien j++; 497c80476e4SDavid E. O'Brien } 498c80476e4SDavid E. O'Brien } 499c80476e4SDavid E. O'Brien return (-1); 500c80476e4SDavid E. O'Brien } 501c80476e4SDavid E. O'Brien 502c80476e4SDavid E. O'Brien #endif 503c80476e4SDavid E. O'Brien 504c80476e4SDavid E. O'Brien 505c80476e4SDavid E. O'Brien #else /* SYSMALLOC */ 506c80476e4SDavid E. O'Brien 507c80476e4SDavid E. O'Brien /** 508c80476e4SDavid E. O'Brien ** ``Protected versions'' of malloc, realloc, calloc, and free 509c80476e4SDavid E. O'Brien ** 510c80476e4SDavid E. O'Brien ** On many systems: 511c80476e4SDavid E. O'Brien ** 512c80476e4SDavid E. O'Brien ** 1. malloc(0) is bad 513c80476e4SDavid E. O'Brien ** 2. free(0) is bad 514c80476e4SDavid E. O'Brien ** 3. realloc(0, n) is bad 515c80476e4SDavid E. O'Brien ** 4. realloc(n, 0) is bad 516c80476e4SDavid E. O'Brien ** 517c80476e4SDavid E. O'Brien ** Also we call our error routine if we run out of memory. 518c80476e4SDavid E. O'Brien **/ 519c80476e4SDavid E. O'Brien memalign_t 52045e5710bSMark Peek smalloc(size_t n) 521c80476e4SDavid E. O'Brien { 522c80476e4SDavid E. O'Brien ptr_t ptr; 523c80476e4SDavid E. O'Brien 524c80476e4SDavid E. O'Brien n = n ? n : 1; 525c80476e4SDavid E. O'Brien 526*19d2e3deSDmitry Chagin #ifdef USE_SBRK 527c80476e4SDavid E. O'Brien if (membot == NULL) 52845e5710bSMark Peek membot = sbrk(0); 529*19d2e3deSDmitry Chagin #endif /* USE_SBRK */ 530c80476e4SDavid E. O'Brien 53145e5710bSMark Peek if ((ptr = malloc(n)) == NULL) 53245e5710bSMark Peek out_of_memory(); 533*19d2e3deSDmitry Chagin #ifndef USE_SBRK 534c80476e4SDavid E. O'Brien if (memtop < ((char *) ptr) + n) 535c80476e4SDavid E. O'Brien memtop = ((char *) ptr) + n; 536c80476e4SDavid E. O'Brien if (membot == NULL) 53745e5710bSMark Peek membot = ptr; 538*19d2e3deSDmitry Chagin #endif /* !USE_SBRK */ 539c80476e4SDavid E. O'Brien return ((memalign_t) ptr); 540c80476e4SDavid E. O'Brien } 541c80476e4SDavid E. O'Brien 542c80476e4SDavid E. O'Brien memalign_t 54345e5710bSMark Peek srealloc(ptr_t p, size_t n) 544c80476e4SDavid E. O'Brien { 545c80476e4SDavid E. O'Brien ptr_t ptr; 546c80476e4SDavid E. O'Brien 547c80476e4SDavid E. O'Brien n = n ? n : 1; 548c80476e4SDavid E. O'Brien 549*19d2e3deSDmitry Chagin #ifdef USE_SBRK 550c80476e4SDavid E. O'Brien if (membot == NULL) 55145e5710bSMark Peek membot = sbrk(0); 552*19d2e3deSDmitry Chagin #endif /* USE_SBRK */ 553c80476e4SDavid E. O'Brien 55445e5710bSMark Peek if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL) 55545e5710bSMark Peek out_of_memory(); 556*19d2e3deSDmitry Chagin #ifndef USE_SBRK 557c80476e4SDavid E. O'Brien if (memtop < ((char *) ptr) + n) 558c80476e4SDavid E. O'Brien memtop = ((char *) ptr) + n; 559c80476e4SDavid E. O'Brien if (membot == NULL) 56045e5710bSMark Peek membot = ptr; 561*19d2e3deSDmitry Chagin #endif /* !USE_SBRK */ 562c80476e4SDavid E. O'Brien return ((memalign_t) ptr); 563c80476e4SDavid E. O'Brien } 564c80476e4SDavid E. O'Brien 565c80476e4SDavid E. O'Brien memalign_t 56645e5710bSMark Peek scalloc(size_t s, size_t n) 567c80476e4SDavid E. O'Brien { 568c80476e4SDavid E. O'Brien ptr_t ptr; 569c80476e4SDavid E. O'Brien 570c80476e4SDavid E. O'Brien n *= s; 571c80476e4SDavid E. O'Brien n = n ? n : 1; 572c80476e4SDavid E. O'Brien 573*19d2e3deSDmitry Chagin #ifdef USE_SBRK 574c80476e4SDavid E. O'Brien if (membot == NULL) 57545e5710bSMark Peek membot = sbrk(0); 576*19d2e3deSDmitry Chagin #endif /* USE_SBRK */ 577c80476e4SDavid E. O'Brien 57845e5710bSMark Peek if ((ptr = malloc(n)) == NULL) 57945e5710bSMark Peek out_of_memory(); 580c80476e4SDavid E. O'Brien 58145e5710bSMark Peek memset (ptr, 0, n); 582c80476e4SDavid E. O'Brien 583*19d2e3deSDmitry Chagin #ifndef USE_SBRK 584c80476e4SDavid E. O'Brien if (memtop < ((char *) ptr) + n) 585c80476e4SDavid E. O'Brien memtop = ((char *) ptr) + n; 586c80476e4SDavid E. O'Brien if (membot == NULL) 58745e5710bSMark Peek membot = ptr; 588*19d2e3deSDmitry Chagin #endif /* !USE_SBRK */ 589c80476e4SDavid E. O'Brien 590c80476e4SDavid E. O'Brien return ((memalign_t) ptr); 591c80476e4SDavid E. O'Brien } 592c80476e4SDavid E. O'Brien 593c80476e4SDavid E. O'Brien void 59445e5710bSMark Peek sfree(ptr_t p) 595c80476e4SDavid E. O'Brien { 596c80476e4SDavid E. O'Brien if (p && !dont_free) 597c80476e4SDavid E. O'Brien free(p); 598c80476e4SDavid E. O'Brien } 599c80476e4SDavid E. O'Brien 600c80476e4SDavid E. O'Brien #endif /* SYSMALLOC */ 601c80476e4SDavid E. O'Brien 602c80476e4SDavid E. O'Brien /* 603c80476e4SDavid E. O'Brien * mstats - print out statistics about malloc 604c80476e4SDavid E. O'Brien * 605c80476e4SDavid E. O'Brien * Prints two lines of numbers, one showing the length of the free list 606c80476e4SDavid E. O'Brien * for each size category, the second showing the number of mallocs - 607c80476e4SDavid E. O'Brien * frees for each size category. 608c80476e4SDavid E. O'Brien */ 609c80476e4SDavid E. O'Brien /*ARGSUSED*/ 610c80476e4SDavid E. O'Brien void 61145e5710bSMark Peek showall(Char **v, struct command *c) 612c80476e4SDavid E. O'Brien { 613c80476e4SDavid E. O'Brien #ifndef SYSMALLOC 61423338178SMark Peek size_t i, j; 61523338178SMark Peek union overhead *p; 616c80476e4SDavid E. O'Brien int totfree = 0, totused = 0; 617c80476e4SDavid E. O'Brien 618c80476e4SDavid E. O'Brien xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname); 619c80476e4SDavid E. O'Brien for (i = 0; i < NBUCKETS; i++) { 620c80476e4SDavid E. O'Brien for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 621c80476e4SDavid E. O'Brien continue; 62245e5710bSMark Peek xprintf(" %4zd", j); 623c80476e4SDavid E. O'Brien totfree += j * (1 << (i + 3)); 624c80476e4SDavid E. O'Brien } 6259ccc37e3SMark Peek xprintf("\n%s:\t", CGETS(19, 9, "used")); 626c80476e4SDavid E. O'Brien for (i = 0; i < NBUCKETS; i++) { 62745e5710bSMark Peek xprintf(" %4d", nmalloc[i]); 628c80476e4SDavid E. O'Brien totused += nmalloc[i] * (1 << (i + 3)); 629c80476e4SDavid E. O'Brien } 630c80476e4SDavid E. O'Brien xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"), 631c80476e4SDavid E. O'Brien totused, totfree); 632c80476e4SDavid E. O'Brien xprintf(CGETS(19, 11, 633c80476e4SDavid E. O'Brien "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"), 634c80476e4SDavid E. O'Brien (unsigned long) membot, (unsigned long) memtop, 635c80476e4SDavid E. O'Brien (unsigned long) sbrk(0)); 6369ccc37e3SMark Peek #else /* SYSMALLOC */ 6379ccc37e3SMark Peek #ifndef HAVE_MALLINFO 638*19d2e3deSDmitry Chagin #ifdef USE_SBRK 63945e5710bSMark Peek memtop = sbrk(0); 640*19d2e3deSDmitry Chagin #endif /* USE_SBRK */ 641c80476e4SDavid E. O'Brien xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"), 642c80476e4SDavid E. O'Brien (unsigned long) membot, (unsigned long) memtop, 643c80476e4SDavid E. O'Brien (unsigned long) (memtop - membot)); 6449ccc37e3SMark Peek #else /* HAVE_MALLINFO */ 6459ccc37e3SMark Peek struct mallinfo mi; 6469ccc37e3SMark Peek 6479ccc37e3SMark Peek mi = mallinfo(); 6489ccc37e3SMark Peek xprintf(CGETS(19, 13, "%s current memory allocation:\n"), progname); 6499ccc37e3SMark Peek xprintf(CGETS(19, 14, "Total space allocated from system: %d\n"), mi.arena); 6509ccc37e3SMark Peek xprintf(CGETS(19, 15, "Number of non-inuse chunks: %d\n"), mi.ordblks); 6519ccc37e3SMark Peek xprintf(CGETS(19, 16, "Number of mmapped regions: %d\n"), mi.hblks); 6529ccc37e3SMark Peek xprintf(CGETS(19, 17, "Total space in mmapped regions: %d\n"), mi.hblkhd); 6539ccc37e3SMark Peek xprintf(CGETS(19, 18, "Total allocated space: %d\n"), mi.uordblks); 6549ccc37e3SMark Peek xprintf(CGETS(19, 19, "Total non-inuse space: %d\n"), mi.fordblks); 6559ccc37e3SMark Peek xprintf(CGETS(19, 20, "Top-most, releasable space: %d\n"), mi.keepcost); 6569ccc37e3SMark Peek #endif /* HAVE_MALLINFO */ 657c80476e4SDavid E. O'Brien #endif /* SYSMALLOC */ 658c80476e4SDavid E. O'Brien USE(c); 659c80476e4SDavid E. O'Brien USE(v); 660c80476e4SDavid E. O'Brien } 661