tc.alloc.c revision 145479
1145479Smp/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $ */ 259243Sobrien/* 359243Sobrien * tc.alloc.c (Caltech) 2/21/82 459243Sobrien * Chris Kingsley, kingsley@cit-20. 559243Sobrien * 659243Sobrien * This is a very fast storage allocator. It allocates blocks of a small 759243Sobrien * number of different sizes, and keeps free lists of each size. Blocks that 859243Sobrien * don't exactly fit are passed up to the next larger size. In this 959243Sobrien * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. 1059243Sobrien * This is designed for use in a program that uses vast quantities of memory, 1159243Sobrien * but bombs when it runs out. 1259243Sobrien */ 1359243Sobrien/*- 1459243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California. 1559243Sobrien * All rights reserved. 1659243Sobrien * 1759243Sobrien * Redistribution and use in source and binary forms, with or without 1859243Sobrien * modification, are permitted provided that the following conditions 1959243Sobrien * are met: 2059243Sobrien * 1. Redistributions of source code must retain the above copyright 2159243Sobrien * notice, this list of conditions and the following disclaimer. 2259243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 2359243Sobrien * notice, this list of conditions and the following disclaimer in the 2459243Sobrien * documentation and/or other materials provided with the distribution. 25100616Smp * 3. Neither the name of the University nor the names of its contributors 2659243Sobrien * may be used to endorse or promote products derived from this software 2759243Sobrien * without specific prior written permission. 2859243Sobrien * 2959243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3059243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3159243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3259243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3359243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3459243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3559243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3659243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3759243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3859243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3959243Sobrien * SUCH DAMAGE. 4059243Sobrien */ 4159243Sobrien#include "sh.h" 4259243Sobrien 43145479SmpRCSID("$Id: tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $") 4459243Sobrien 4559243Sobrienstatic char *memtop = NULL; /* PWP: top of current memory */ 4659243Sobrienstatic char *membot = NULL; /* PWP: bottom of allocatable memory */ 4759243Sobrien 4859243Sobrienint dont_free = 0; 4959243Sobrien 5069408Sache#ifdef WINNT_NATIVE 5159243Sobrien# define malloc fmalloc 5259243Sobrien# define free ffree 5359243Sobrien# define calloc fcalloc 5459243Sobrien# define realloc frealloc 5569408Sache#endif /* WINNT_NATIVE */ 5659243Sobrien 5759243Sobrien#ifndef SYSMALLOC 5859243Sobrien 5959243Sobrien#undef RCHECK 6059243Sobrien#undef DEBUG 6159243Sobrien 6259243Sobrien#ifdef SX 6359243Sobrienextern void* sbrk(); 6459243Sobrien#endif 6559243Sobrien/* 6659243Sobrien * Lots of os routines are busted and try to free invalid pointers. 6759243Sobrien * Although our free routine is smart enough and it will pick bad 6859243Sobrien * pointers most of the time, in cases where we know we are going to get 6959243Sobrien * a bad pointer, we'd rather leak. 7059243Sobrien */ 7159243Sobrien 7259243Sobrien#ifndef NULL 7359243Sobrien#define NULL 0 7459243Sobrien#endif 7559243Sobrien 7659243Sobrientypedef unsigned char U_char; /* we don't really have signed chars */ 7759243Sobrientypedef unsigned int U_int; 7859243Sobrientypedef unsigned short U_short; 7959243Sobrientypedef unsigned long U_long; 8059243Sobrien 8159243Sobrien 8259243Sobrien/* 8359243Sobrien * The overhead on a block is at least 4 bytes. When free, this space 8459243Sobrien * contains a pointer to the next free block, and the bottom two bits must 8559243Sobrien * be zero. When in use, the first byte is set to MAGIC, and the second 8659243Sobrien * byte is the size index. The remaining bytes are for alignment. 8759243Sobrien * If range checking is enabled and the size of the block fits 8859243Sobrien * in two bytes, then the top two bytes hold the size of the requested block 8959243Sobrien * plus the range checking words, and the header word MINUS ONE. 9059243Sobrien */ 9159243Sobrien 9259243Sobrien 9359243Sobrien#define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP) 9459243Sobrien 9559243Sobrienunion overhead { 9659243Sobrien union overhead *ov_next; /* when free */ 9759243Sobrien struct { 9859243Sobrien U_char ovu_magic; /* magic number */ 9959243Sobrien U_char ovu_index; /* bucket # */ 10059243Sobrien#ifdef RCHECK 10159243Sobrien U_short ovu_size; /* actual block size */ 10259243Sobrien U_int ovu_rmagic; /* range magic number */ 10359243Sobrien#endif 10459243Sobrien } ovu; 10559243Sobrien#define ov_magic ovu.ovu_magic 10659243Sobrien#define ov_index ovu.ovu_index 10759243Sobrien#define ov_size ovu.ovu_size 10859243Sobrien#define ov_rmagic ovu.ovu_rmagic 10959243Sobrien}; 11059243Sobrien 11159243Sobrien#define MAGIC 0xfd /* magic # on accounting info */ 11259243Sobrien#define RMAGIC 0x55555555 /* magic # on range info */ 11359243Sobrien#ifdef RCHECK 11459243Sobrien#define RSLOP sizeof (U_int) 11559243Sobrien#else 11659243Sobrien#define RSLOP 0 11759243Sobrien#endif 11859243Sobrien 11959243Sobrien 12059243Sobrien#define ROUNDUP 7 12159243Sobrien 12259243Sobrien/* 12359243Sobrien * nextf[i] is the pointer to the next free block of size 2^(i+3). The 12459243Sobrien * smallest allocatable block is 8 bytes. The overhead information 12559243Sobrien * precedes the data area returned to the user. 12659243Sobrien */ 12759243Sobrien#define NBUCKETS ((sizeof(long) << 3) - 3) 12859243Sobrienstatic union overhead *nextf[NBUCKETS] IZERO_STRUCT; 12959243Sobrien 13059243Sobrien/* 13159243Sobrien * nmalloc[i] is the difference between the number of mallocs and frees 13259243Sobrien * for a given block size. 13359243Sobrien */ 13459243Sobrienstatic U_int nmalloc[NBUCKETS] IZERO_STRUCT; 13559243Sobrien 13659243Sobrien#ifndef lint 13759243Sobrienstatic int findbucket __P((union overhead *, int)); 13859243Sobrienstatic void morecore __P((int)); 13959243Sobrien#endif 14059243Sobrien 14159243Sobrien 14259243Sobrien#ifdef DEBUG 14359243Sobrien# define CHECK(a, str, p) \ 14459243Sobrien if (a) { \ 14559243Sobrien xprintf(str, p); \ 14659243Sobrien xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ 14759243Sobrien abort(); \ 14859243Sobrien } 14959243Sobrien#else 15059243Sobrien# define CHECK(a, str, p) \ 15159243Sobrien if (a) { \ 15259243Sobrien xprintf(str, p); \ 15359243Sobrien xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ 15459243Sobrien return; \ 15559243Sobrien } 15659243Sobrien#endif 15759243Sobrien 15859243Sobrienmemalign_t 15959243Sobrienmalloc(nbytes) 160145479Smp size_t nbytes; 16159243Sobrien{ 16259243Sobrien#ifndef lint 163145479Smp union overhead *p; 164145479Smp int bucket = 0; 165145479Smp unsigned shiftr; 16659243Sobrien 16759243Sobrien /* 16859243Sobrien * Convert amount of memory requested into closest block size stored in 16959243Sobrien * hash buckets which satisfies request. Account for space used per block 17059243Sobrien * for accounting. 17159243Sobrien */ 17259243Sobrien#ifdef SUNOS4 17359243Sobrien /* 17459243Sobrien * SunOS localtime() overwrites the 9th byte on an 8 byte malloc().... 17559243Sobrien * so we get one more... 17659243Sobrien * From Michael Schroeder: This is not true. It depends on the 17759243Sobrien * timezone string. In Europe it can overwrite the 13th byte on a 17859243Sobrien * 12 byte malloc. 17959243Sobrien * So we punt and we always allocate an extra byte. 18059243Sobrien */ 18159243Sobrien nbytes++; 18259243Sobrien#endif 18359243Sobrien 18459243Sobrien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP); 18559243Sobrien shiftr = (nbytes - 1) >> 2; 18659243Sobrien 18759243Sobrien /* apart from this loop, this is O(1) */ 18859243Sobrien while ((shiftr >>= 1) != 0) 18959243Sobrien bucket++; 19059243Sobrien /* 19159243Sobrien * If nothing in hash bucket right now, request more memory from the 19259243Sobrien * system. 19359243Sobrien */ 19459243Sobrien if (nextf[bucket] == NULL) 19559243Sobrien morecore(bucket); 19659243Sobrien if ((p = (union overhead *) nextf[bucket]) == NULL) { 19759243Sobrien child++; 19859243Sobrien#ifndef DEBUG 19959243Sobrien stderror(ERR_NOMEM); 20059243Sobrien#else 20159243Sobrien showall(NULL, NULL); 20259243Sobrien xprintf(CGETS(19, 1, "nbytes=%d: Out of memory\n"), nbytes); 20359243Sobrien abort(); 20459243Sobrien#endif 20559243Sobrien /* fool lint */ 20659243Sobrien return ((memalign_t) 0); 20759243Sobrien } 20859243Sobrien /* remove from linked list */ 20959243Sobrien nextf[bucket] = nextf[bucket]->ov_next; 21059243Sobrien p->ov_magic = MAGIC; 21159243Sobrien p->ov_index = bucket; 21259243Sobrien nmalloc[bucket]++; 21359243Sobrien#ifdef RCHECK 21459243Sobrien /* 21559243Sobrien * Record allocated size of block and bound space with magic numbers. 21659243Sobrien */ 21759243Sobrien p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0; 21859243Sobrien p->ov_rmagic = RMAGIC; 21959243Sobrien *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC; 22059243Sobrien#endif 22159243Sobrien return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead)))); 22259243Sobrien#else 22359243Sobrien if (nbytes) 22459243Sobrien return ((memalign_t) 0); 22559243Sobrien else 22659243Sobrien return ((memalign_t) 0); 22759243Sobrien#endif /* !lint */ 22859243Sobrien} 22959243Sobrien 23059243Sobrien#ifndef lint 23159243Sobrien/* 23259243Sobrien * Allocate more memory to the indicated bucket. 23359243Sobrien */ 23459243Sobrienstatic void 23559243Sobrienmorecore(bucket) 236145479Smp int bucket; 23759243Sobrien{ 238145479Smp union overhead *op; 239145479Smp int rnu; /* 2^rnu bytes will be requested */ 240145479Smp int nblks; /* become nblks blocks of the desired size */ 241145479Smp int siz; 24259243Sobrien 24359243Sobrien if (nextf[bucket]) 24459243Sobrien return; 24559243Sobrien /* 24659243Sobrien * Insure memory is allocated on a page boundary. Should make getpageize 24759243Sobrien * call? 24859243Sobrien */ 24959243Sobrien op = (union overhead *) sbrk(0); 25059243Sobrien memtop = (char *) op; 25159243Sobrien if (membot == NULL) 25259243Sobrien membot = memtop; 25359243Sobrien if ((long) op & 0x3ff) { 25459243Sobrien memtop = (char *) sbrk((int) (1024 - ((long) op & 0x3ff))); 25559243Sobrien memtop += (long) (1024 - ((long) op & 0x3ff)); 25659243Sobrien } 25759243Sobrien 25859243Sobrien /* take 2k unless the block is bigger than that */ 25959243Sobrien rnu = (bucket <= 8) ? 11 : bucket + 3; 26059243Sobrien nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ 26159243Sobrien memtop = (char *) sbrk(1 << rnu); /* PWP */ 26259243Sobrien op = (union overhead *) memtop; 26359243Sobrien /* no more room! */ 26459243Sobrien if ((long) op == -1) 26559243Sobrien return; 26659243Sobrien memtop += (long) (1 << rnu); 26759243Sobrien /* 26859243Sobrien * Round up to minimum allocation size boundary and deduct from block count 26959243Sobrien * to reflect. 27059243Sobrien */ 27159243Sobrien if (((U_long) op) & ROUNDUP) { 27259243Sobrien op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP); 27359243Sobrien nblks--; 27459243Sobrien } 27559243Sobrien /* 27659243Sobrien * Add new memory allocated to that on free list for this hash bucket. 27759243Sobrien */ 27859243Sobrien nextf[bucket] = op; 27959243Sobrien siz = 1 << (bucket + 3); 28059243Sobrien while (--nblks > 0) { 28159243Sobrien op->ov_next = (union overhead *) (((caddr_t) op) + siz); 28259243Sobrien op = (union overhead *) (((caddr_t) op) + siz); 28359243Sobrien } 28459243Sobrien op->ov_next = NULL; 28559243Sobrien} 28659243Sobrien 28759243Sobrien#endif 28859243Sobrien 28959243Sobrienvoid 29059243Sobrienfree(cp) 29159243Sobrien ptr_t cp; 29259243Sobrien{ 29359243Sobrien#ifndef lint 294145479Smp int size; 295145479Smp union overhead *op; 29659243Sobrien 29759243Sobrien /* 29859243Sobrien * the don't free flag is there so that we avoid os bugs in routines 29959243Sobrien * that free invalid pointers! 30059243Sobrien */ 30159243Sobrien if (cp == NULL || dont_free) 30259243Sobrien return; 30359243Sobrien CHECK(!memtop || !membot, 30459243Sobrien CGETS(19, 2, "free(%lx) called before any allocations."), cp); 30559243Sobrien CHECK(cp > (ptr_t) memtop, 30659243Sobrien CGETS(19, 3, "free(%lx) above top of memory."), cp); 30759243Sobrien CHECK(cp < (ptr_t) membot, 30859243Sobrien CGETS(19, 4, "free(%lx) below bottom of memory."), cp); 30959243Sobrien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 31059243Sobrien CHECK(op->ov_magic != MAGIC, 31159243Sobrien CGETS(19, 5, "free(%lx) bad block."), cp); 31259243Sobrien 31359243Sobrien#ifdef RCHECK 31459243Sobrien if (op->ov_index <= 13) 31559243Sobrien CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC, 31659243Sobrien CGETS(19, 6, "free(%lx) bad range check."), cp); 31759243Sobrien#endif 31859243Sobrien CHECK(op->ov_index >= NBUCKETS, 31959243Sobrien CGETS(19, 7, "free(%lx) bad block index."), cp); 32059243Sobrien size = op->ov_index; 32159243Sobrien op->ov_next = nextf[size]; 32259243Sobrien nextf[size] = op; 32359243Sobrien 32459243Sobrien nmalloc[size]--; 32559243Sobrien 32659243Sobrien#else 32759243Sobrien if (cp == NULL) 32859243Sobrien return; 32959243Sobrien#endif 33059243Sobrien} 33159243Sobrien 33259243Sobrienmemalign_t 33359243Sobriencalloc(i, j) 33459243Sobrien size_t i, j; 33559243Sobrien{ 33659243Sobrien#ifndef lint 337145479Smp char *cp, *scp; 33859243Sobrien 33959243Sobrien i *= j; 34059243Sobrien scp = cp = (char *) xmalloc((size_t) i); 34159243Sobrien if (i != 0) 34259243Sobrien do 34359243Sobrien *cp++ = 0; 34459243Sobrien while (--i); 34559243Sobrien 34659243Sobrien return ((memalign_t) scp); 34759243Sobrien#else 34859243Sobrien if (i && j) 34959243Sobrien return ((memalign_t) 0); 35059243Sobrien else 35159243Sobrien return ((memalign_t) 0); 35259243Sobrien#endif 35359243Sobrien} 35459243Sobrien 35559243Sobrien/* 35659243Sobrien * When a program attempts "storage compaction" as mentioned in the 35759243Sobrien * old malloc man page, it realloc's an already freed block. Usually 35859243Sobrien * this is the last block it freed; occasionally it might be farther 35959243Sobrien * back. We have to search all the free lists for the block in order 36059243Sobrien * to determine its bucket: 1st we make one pass thru the lists 36159243Sobrien * checking only the first block in each; if that fails we search 36259243Sobrien * ``realloc_srchlen'' blocks in each list for a match (the variable 36359243Sobrien * is extern so the caller can modify it). If that fails we just copy 36459243Sobrien * however many bytes was given to realloc() and hope it's not huge. 36559243Sobrien */ 36659243Sobrien#ifndef lint 36759243Sobrien/* 4 should be plenty, -1 =>'s whole list */ 36859243Sobrienstatic int realloc_srchlen = 4; 36959243Sobrien#endif /* lint */ 37059243Sobrien 37159243Sobrienmemalign_t 37259243Sobrienrealloc(cp, nbytes) 37359243Sobrien ptr_t cp; 37459243Sobrien size_t nbytes; 37559243Sobrien{ 37659243Sobrien#ifndef lint 377145479Smp U_int onb; 37859243Sobrien union overhead *op; 37959243Sobrien ptr_t res; 380145479Smp int i; 38159243Sobrien int was_alloced = 0; 38259243Sobrien 38359243Sobrien if (cp == NULL) 38459243Sobrien return (malloc(nbytes)); 38559243Sobrien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 38659243Sobrien if (op->ov_magic == MAGIC) { 38759243Sobrien was_alloced++; 38859243Sobrien i = op->ov_index; 38959243Sobrien } 39059243Sobrien else 39159243Sobrien /* 39259243Sobrien * Already free, doing "compaction". 39359243Sobrien * 39459243Sobrien * Search for the old block of memory on the free list. First, check the 39559243Sobrien * most common case (last element free'd), then (this failing) the last 39659243Sobrien * ``realloc_srchlen'' items free'd. If all lookups fail, then assume 39759243Sobrien * the size of the memory block being realloc'd is the smallest 39859243Sobrien * possible. 39959243Sobrien */ 40059243Sobrien if ((i = findbucket(op, 1)) < 0 && 40159243Sobrien (i = findbucket(op, realloc_srchlen)) < 0) 40259243Sobrien i = 0; 40359243Sobrien 40459243Sobrien onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP); 40559243Sobrien 40659243Sobrien /* avoid the copy if same size block */ 40759243Sobrien if (was_alloced && (onb <= (U_int) (1 << (i + 3))) && 40859243Sobrien (onb > (U_int) (1 << (i + 2)))) { 40959243Sobrien#ifdef RCHECK 41059243Sobrien /* JMR: formerly this wasn't updated ! */ 41159243Sobrien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP); 41259243Sobrien *((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC; 41359243Sobrien op->ov_rmagic = RMAGIC; 41459243Sobrien op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0; 41559243Sobrien#endif 41659243Sobrien return ((memalign_t) cp); 41759243Sobrien } 41859243Sobrien if ((res = malloc(nbytes)) == NULL) 41959243Sobrien return ((memalign_t) NULL); 42059243Sobrien if (cp != res) { /* common optimization */ 42159243Sobrien /* 42259243Sobrien * christos: this used to copy nbytes! It should copy the 42359243Sobrien * smaller of the old and new size 42459243Sobrien */ 42559243Sobrien onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP; 42659243Sobrien (void) memmove((ptr_t) res, (ptr_t) cp, 42759243Sobrien (size_t) (onb < nbytes ? onb : nbytes)); 42859243Sobrien } 42959243Sobrien if (was_alloced) 43059243Sobrien free(cp); 43159243Sobrien return ((memalign_t) res); 43259243Sobrien#else 43359243Sobrien if (cp && nbytes) 43459243Sobrien return ((memalign_t) 0); 43559243Sobrien else 43659243Sobrien return ((memalign_t) 0); 43759243Sobrien#endif /* !lint */ 43859243Sobrien} 43959243Sobrien 44059243Sobrien 44159243Sobrien 44259243Sobrien#ifndef lint 44359243Sobrien/* 44459243Sobrien * Search ``srchlen'' elements of each free list for a block whose 44559243Sobrien * header starts at ``freep''. If srchlen is -1 search the whole list. 44659243Sobrien * Return bucket number, or -1 if not found. 44759243Sobrien */ 44859243Sobrienstatic int 44959243Sobrienfindbucket(freep, srchlen) 45059243Sobrien union overhead *freep; 45159243Sobrien int srchlen; 45259243Sobrien{ 453145479Smp union overhead *p; 454145479Smp size_t i; 455145479Smp int j; 45659243Sobrien 45759243Sobrien for (i = 0; i < NBUCKETS; i++) { 45859243Sobrien j = 0; 45959243Sobrien for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 46059243Sobrien if (p == freep) 46159243Sobrien return (i); 46259243Sobrien j++; 46359243Sobrien } 46459243Sobrien } 46559243Sobrien return (-1); 46659243Sobrien} 46759243Sobrien 46859243Sobrien#endif 46959243Sobrien 47059243Sobrien 47159243Sobrien#else /* SYSMALLOC */ 47259243Sobrien 47359243Sobrien/** 47459243Sobrien ** ``Protected versions'' of malloc, realloc, calloc, and free 47559243Sobrien ** 47659243Sobrien ** On many systems: 47759243Sobrien ** 47859243Sobrien ** 1. malloc(0) is bad 47959243Sobrien ** 2. free(0) is bad 48059243Sobrien ** 3. realloc(0, n) is bad 48159243Sobrien ** 4. realloc(n, 0) is bad 48259243Sobrien ** 48359243Sobrien ** Also we call our error routine if we run out of memory. 48459243Sobrien **/ 48559243Sobrienmemalign_t 48659243Sobriensmalloc(n) 48759243Sobrien size_t n; 48859243Sobrien{ 48959243Sobrien ptr_t ptr; 49059243Sobrien 49159243Sobrien n = n ? n : 1; 49259243Sobrien 493145479Smp#ifdef HAVE_SBRK 49459243Sobrien if (membot == NULL) 49559243Sobrien membot = (char*) sbrk(0); 496145479Smp#endif /* HAVE_SBRK */ 49759243Sobrien 49859243Sobrien if ((ptr = malloc(n)) == (ptr_t) 0) { 49959243Sobrien child++; 50059243Sobrien stderror(ERR_NOMEM); 50159243Sobrien } 502145479Smp#ifndef HAVE_SBRK 50359243Sobrien if (memtop < ((char *) ptr) + n) 50459243Sobrien memtop = ((char *) ptr) + n; 50559243Sobrien if (membot == NULL) 50659243Sobrien membot = (char*) ptr; 507145479Smp#endif /* !HAVE_SBRK */ 50859243Sobrien return ((memalign_t) ptr); 50959243Sobrien} 51059243Sobrien 51159243Sobrienmemalign_t 51259243Sobriensrealloc(p, n) 51359243Sobrien ptr_t p; 51459243Sobrien size_t n; 51559243Sobrien{ 51659243Sobrien ptr_t ptr; 51759243Sobrien 51859243Sobrien n = n ? n : 1; 51959243Sobrien 520145479Smp#ifdef HAVE_SBRK 52159243Sobrien if (membot == NULL) 52259243Sobrien membot = (char*) sbrk(0); 523145479Smp#endif /* HAVE_SBRK */ 52459243Sobrien 52559243Sobrien if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) { 52659243Sobrien child++; 52759243Sobrien stderror(ERR_NOMEM); 52859243Sobrien } 529145479Smp#ifndef HAVE_SBRK 53059243Sobrien if (memtop < ((char *) ptr) + n) 53159243Sobrien memtop = ((char *) ptr) + n; 53259243Sobrien if (membot == NULL) 53359243Sobrien membot = (char*) ptr; 534145479Smp#endif /* !HAVE_SBRK */ 53559243Sobrien return ((memalign_t) ptr); 53659243Sobrien} 53759243Sobrien 53859243Sobrienmemalign_t 53959243Sobrienscalloc(s, n) 54059243Sobrien size_t s, n; 54159243Sobrien{ 54259243Sobrien char *sptr; 54359243Sobrien ptr_t ptr; 54459243Sobrien 54559243Sobrien n *= s; 54659243Sobrien n = n ? n : 1; 54759243Sobrien 548145479Smp#ifdef HAVE_SBRK 54959243Sobrien if (membot == NULL) 55059243Sobrien membot = (char*) sbrk(0); 551145479Smp#endif /* HAVE_SBRK */ 55259243Sobrien 55359243Sobrien if ((ptr = malloc(n)) == (ptr_t) 0) { 55459243Sobrien child++; 55559243Sobrien stderror(ERR_NOMEM); 55659243Sobrien } 55759243Sobrien 55859243Sobrien sptr = (char *) ptr; 55959243Sobrien if (n != 0) 56059243Sobrien do 56159243Sobrien *sptr++ = 0; 56259243Sobrien while (--n); 56359243Sobrien 564145479Smp#ifndef HAVE_SBRK 56559243Sobrien if (memtop < ((char *) ptr) + n) 56659243Sobrien memtop = ((char *) ptr) + n; 56759243Sobrien if (membot == NULL) 56859243Sobrien membot = (char*) ptr; 569145479Smp#endif /* !HAVE_SBRK */ 57059243Sobrien 57159243Sobrien return ((memalign_t) ptr); 57259243Sobrien} 57359243Sobrien 57459243Sobrienvoid 57559243Sobriensfree(p) 57659243Sobrien ptr_t p; 57759243Sobrien{ 57859243Sobrien if (p && !dont_free) 57959243Sobrien free(p); 58059243Sobrien} 58159243Sobrien 58259243Sobrien#endif /* SYSMALLOC */ 58359243Sobrien 58459243Sobrien/* 58559243Sobrien * mstats - print out statistics about malloc 58659243Sobrien * 58759243Sobrien * Prints two lines of numbers, one showing the length of the free list 58859243Sobrien * for each size category, the second showing the number of mallocs - 58959243Sobrien * frees for each size category. 59059243Sobrien */ 59159243Sobrien/*ARGSUSED*/ 59259243Sobrienvoid 59359243Sobrienshowall(v, c) 59459243Sobrien Char **v; 59559243Sobrien struct command *c; 59659243Sobrien{ 59759243Sobrien#ifndef SYSMALLOC 598145479Smp size_t i, j; 599145479Smp union overhead *p; 60059243Sobrien int totfree = 0, totused = 0; 60159243Sobrien 60259243Sobrien xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname); 60359243Sobrien for (i = 0; i < NBUCKETS; i++) { 60459243Sobrien for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 60559243Sobrien continue; 60659243Sobrien xprintf(" %4d", j); 60759243Sobrien totfree += j * (1 << (i + 3)); 60859243Sobrien } 60959243Sobrien xprintf(CGETS(19, 9, "\nused:\t")); 61059243Sobrien for (i = 0; i < NBUCKETS; i++) { 61159243Sobrien xprintf(" %4u", nmalloc[i]); 61259243Sobrien totused += nmalloc[i] * (1 << (i + 3)); 61359243Sobrien } 61459243Sobrien xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"), 61559243Sobrien totused, totfree); 61659243Sobrien xprintf(CGETS(19, 11, 61759243Sobrien "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"), 61859243Sobrien (unsigned long) membot, (unsigned long) memtop, 61959243Sobrien (unsigned long) sbrk(0)); 62059243Sobrien#else 621145479Smp#ifdef HAVE_SBRK 62259243Sobrien memtop = (char *) sbrk(0); 623145479Smp#endif /* HAVE_SBRK */ 62459243Sobrien xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"), 62559243Sobrien (unsigned long) membot, (unsigned long) memtop, 62659243Sobrien (unsigned long) (memtop - membot)); 62759243Sobrien#endif /* SYSMALLOC */ 62859243Sobrien USE(c); 62959243Sobrien USE(v); 63059243Sobrien} 631