tc.alloc.c revision 100616
1100616Smp/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.36 2002/03/08 17:36:47 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 43100616SmpRCSID("$Id: tc.alloc.c,v 3.36 2002/03/08 17:36:47 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 5059243Sobrien#if defined(_VMS_POSIX) || defined(_AMIGA_MEMORY) 5159243Sobrien# define NO_SBRK 5259243Sobrien#endif 5359243Sobrien 5469408Sache#ifdef WINNT_NATIVE 5559243Sobrien# define malloc fmalloc 5659243Sobrien# define free ffree 5759243Sobrien# define calloc fcalloc 5859243Sobrien# define realloc frealloc 5969408Sache#endif /* WINNT_NATIVE */ 6059243Sobrien 6159243Sobrien#ifndef SYSMALLOC 6259243Sobrien 6359243Sobrien#undef RCHECK 6459243Sobrien#undef DEBUG 6559243Sobrien 6659243Sobrien#ifdef SX 6759243Sobrienextern void* sbrk(); 6859243Sobrien#endif 6959243Sobrien/* 7059243Sobrien * Lots of os routines are busted and try to free invalid pointers. 7159243Sobrien * Although our free routine is smart enough and it will pick bad 7259243Sobrien * pointers most of the time, in cases where we know we are going to get 7359243Sobrien * a bad pointer, we'd rather leak. 7459243Sobrien */ 7559243Sobrien 7659243Sobrien#ifndef NULL 7759243Sobrien#define NULL 0 7859243Sobrien#endif 7959243Sobrien 8059243Sobrientypedef unsigned char U_char; /* we don't really have signed chars */ 8159243Sobrientypedef unsigned int U_int; 8259243Sobrientypedef unsigned short U_short; 8359243Sobrientypedef unsigned long U_long; 8459243Sobrien 8559243Sobrien 8659243Sobrien/* 8759243Sobrien * The overhead on a block is at least 4 bytes. When free, this space 8859243Sobrien * contains a pointer to the next free block, and the bottom two bits must 8959243Sobrien * be zero. When in use, the first byte is set to MAGIC, and the second 9059243Sobrien * byte is the size index. The remaining bytes are for alignment. 9159243Sobrien * If range checking is enabled and the size of the block fits 9259243Sobrien * in two bytes, then the top two bytes hold the size of the requested block 9359243Sobrien * plus the range checking words, and the header word MINUS ONE. 9459243Sobrien */ 9559243Sobrien 9659243Sobrien 9759243Sobrien#define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP) 9859243Sobrien 9959243Sobrienunion overhead { 10059243Sobrien union overhead *ov_next; /* when free */ 10159243Sobrien struct { 10259243Sobrien U_char ovu_magic; /* magic number */ 10359243Sobrien U_char ovu_index; /* bucket # */ 10459243Sobrien#ifdef RCHECK 10559243Sobrien U_short ovu_size; /* actual block size */ 10659243Sobrien U_int ovu_rmagic; /* range magic number */ 10759243Sobrien#endif 10859243Sobrien } ovu; 10959243Sobrien#define ov_magic ovu.ovu_magic 11059243Sobrien#define ov_index ovu.ovu_index 11159243Sobrien#define ov_size ovu.ovu_size 11259243Sobrien#define ov_rmagic ovu.ovu_rmagic 11359243Sobrien}; 11459243Sobrien 11559243Sobrien#define MAGIC 0xfd /* magic # on accounting info */ 11659243Sobrien#define RMAGIC 0x55555555 /* magic # on range info */ 11759243Sobrien#ifdef RCHECK 11859243Sobrien#define RSLOP sizeof (U_int) 11959243Sobrien#else 12059243Sobrien#define RSLOP 0 12159243Sobrien#endif 12259243Sobrien 12359243Sobrien 12459243Sobrien#define ROUNDUP 7 12559243Sobrien 12659243Sobrien/* 12759243Sobrien * nextf[i] is the pointer to the next free block of size 2^(i+3). The 12859243Sobrien * smallest allocatable block is 8 bytes. The overhead information 12959243Sobrien * precedes the data area returned to the user. 13059243Sobrien */ 13159243Sobrien#define NBUCKETS ((sizeof(long) << 3) - 3) 13259243Sobrienstatic union overhead *nextf[NBUCKETS] IZERO_STRUCT; 13359243Sobrien 13459243Sobrien/* 13559243Sobrien * nmalloc[i] is the difference between the number of mallocs and frees 13659243Sobrien * for a given block size. 13759243Sobrien */ 13859243Sobrienstatic U_int nmalloc[NBUCKETS] IZERO_STRUCT; 13959243Sobrien 14059243Sobrien#ifndef lint 14159243Sobrienstatic int findbucket __P((union overhead *, int)); 14259243Sobrienstatic void morecore __P((int)); 14359243Sobrien#endif 14459243Sobrien 14559243Sobrien 14659243Sobrien#ifdef DEBUG 14759243Sobrien# define CHECK(a, str, p) \ 14859243Sobrien if (a) { \ 14959243Sobrien xprintf(str, p); \ 15059243Sobrien xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ 15159243Sobrien abort(); \ 15259243Sobrien } 15359243Sobrien#else 15459243Sobrien# define CHECK(a, str, p) \ 15559243Sobrien if (a) { \ 15659243Sobrien xprintf(str, p); \ 15759243Sobrien xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \ 15859243Sobrien return; \ 15959243Sobrien } 16059243Sobrien#endif 16159243Sobrien 16259243Sobrienmemalign_t 16359243Sobrienmalloc(nbytes) 16459243Sobrien register size_t nbytes; 16559243Sobrien{ 16659243Sobrien#ifndef lint 16759243Sobrien register union overhead *p; 16859243Sobrien register int bucket = 0; 16959243Sobrien register unsigned shiftr; 17059243Sobrien 17159243Sobrien /* 17259243Sobrien * Convert amount of memory requested into closest block size stored in 17359243Sobrien * hash buckets which satisfies request. Account for space used per block 17459243Sobrien * for accounting. 17559243Sobrien */ 17659243Sobrien#ifdef SUNOS4 17759243Sobrien /* 17859243Sobrien * SunOS localtime() overwrites the 9th byte on an 8 byte malloc().... 17959243Sobrien * so we get one more... 18059243Sobrien * From Michael Schroeder: This is not true. It depends on the 18159243Sobrien * timezone string. In Europe it can overwrite the 13th byte on a 18259243Sobrien * 12 byte malloc. 18359243Sobrien * So we punt and we always allocate an extra byte. 18459243Sobrien */ 18559243Sobrien nbytes++; 18659243Sobrien#endif 18759243Sobrien 18859243Sobrien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP); 18959243Sobrien shiftr = (nbytes - 1) >> 2; 19059243Sobrien 19159243Sobrien /* apart from this loop, this is O(1) */ 19259243Sobrien while ((shiftr >>= 1) != 0) 19359243Sobrien bucket++; 19459243Sobrien /* 19559243Sobrien * If nothing in hash bucket right now, request more memory from the 19659243Sobrien * system. 19759243Sobrien */ 19859243Sobrien if (nextf[bucket] == NULL) 19959243Sobrien morecore(bucket); 20059243Sobrien if ((p = (union overhead *) nextf[bucket]) == NULL) { 20159243Sobrien child++; 20259243Sobrien#ifndef DEBUG 20359243Sobrien stderror(ERR_NOMEM); 20459243Sobrien#else 20559243Sobrien showall(NULL, NULL); 20659243Sobrien xprintf(CGETS(19, 1, "nbytes=%d: Out of memory\n"), nbytes); 20759243Sobrien abort(); 20859243Sobrien#endif 20959243Sobrien /* fool lint */ 21059243Sobrien return ((memalign_t) 0); 21159243Sobrien } 21259243Sobrien /* remove from linked list */ 21359243Sobrien nextf[bucket] = nextf[bucket]->ov_next; 21459243Sobrien p->ov_magic = MAGIC; 21559243Sobrien p->ov_index = bucket; 21659243Sobrien nmalloc[bucket]++; 21759243Sobrien#ifdef RCHECK 21859243Sobrien /* 21959243Sobrien * Record allocated size of block and bound space with magic numbers. 22059243Sobrien */ 22159243Sobrien p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0; 22259243Sobrien p->ov_rmagic = RMAGIC; 22359243Sobrien *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC; 22459243Sobrien#endif 22559243Sobrien return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead)))); 22659243Sobrien#else 22759243Sobrien if (nbytes) 22859243Sobrien return ((memalign_t) 0); 22959243Sobrien else 23059243Sobrien return ((memalign_t) 0); 23159243Sobrien#endif /* !lint */ 23259243Sobrien} 23359243Sobrien 23459243Sobrien#ifndef lint 23559243Sobrien/* 23659243Sobrien * Allocate more memory to the indicated bucket. 23759243Sobrien */ 23859243Sobrienstatic void 23959243Sobrienmorecore(bucket) 24059243Sobrien register int bucket; 24159243Sobrien{ 24259243Sobrien register union overhead *op; 24359243Sobrien register int rnu; /* 2^rnu bytes will be requested */ 24459243Sobrien register int nblks; /* become nblks blocks of the desired size */ 24559243Sobrien register int siz; 24659243Sobrien 24759243Sobrien if (nextf[bucket]) 24859243Sobrien return; 24959243Sobrien /* 25059243Sobrien * Insure memory is allocated on a page boundary. Should make getpageize 25159243Sobrien * call? 25259243Sobrien */ 25359243Sobrien op = (union overhead *) sbrk(0); 25459243Sobrien memtop = (char *) op; 25559243Sobrien if (membot == NULL) 25659243Sobrien membot = memtop; 25759243Sobrien if ((long) op & 0x3ff) { 25859243Sobrien memtop = (char *) sbrk((int) (1024 - ((long) op & 0x3ff))); 25959243Sobrien memtop += (long) (1024 - ((long) op & 0x3ff)); 26059243Sobrien } 26159243Sobrien 26259243Sobrien /* take 2k unless the block is bigger than that */ 26359243Sobrien rnu = (bucket <= 8) ? 11 : bucket + 3; 26459243Sobrien nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ 26559243Sobrien memtop = (char *) sbrk(1 << rnu); /* PWP */ 26659243Sobrien op = (union overhead *) memtop; 26759243Sobrien /* no more room! */ 26859243Sobrien if ((long) op == -1) 26959243Sobrien return; 27059243Sobrien memtop += (long) (1 << rnu); 27159243Sobrien /* 27259243Sobrien * Round up to minimum allocation size boundary and deduct from block count 27359243Sobrien * to reflect. 27459243Sobrien */ 27559243Sobrien if (((U_long) op) & ROUNDUP) { 27659243Sobrien op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP); 27759243Sobrien nblks--; 27859243Sobrien } 27959243Sobrien /* 28059243Sobrien * Add new memory allocated to that on free list for this hash bucket. 28159243Sobrien */ 28259243Sobrien nextf[bucket] = op; 28359243Sobrien siz = 1 << (bucket + 3); 28459243Sobrien while (--nblks > 0) { 28559243Sobrien op->ov_next = (union overhead *) (((caddr_t) op) + siz); 28659243Sobrien op = (union overhead *) (((caddr_t) op) + siz); 28759243Sobrien } 28859243Sobrien op->ov_next = NULL; 28959243Sobrien} 29059243Sobrien 29159243Sobrien#endif 29259243Sobrien 29359243Sobrienvoid 29459243Sobrienfree(cp) 29559243Sobrien ptr_t cp; 29659243Sobrien{ 29759243Sobrien#ifndef lint 29859243Sobrien register int size; 29959243Sobrien register union overhead *op; 30059243Sobrien 30159243Sobrien /* 30259243Sobrien * the don't free flag is there so that we avoid os bugs in routines 30359243Sobrien * that free invalid pointers! 30459243Sobrien */ 30559243Sobrien if (cp == NULL || dont_free) 30659243Sobrien return; 30759243Sobrien CHECK(!memtop || !membot, 30859243Sobrien CGETS(19, 2, "free(%lx) called before any allocations."), cp); 30959243Sobrien CHECK(cp > (ptr_t) memtop, 31059243Sobrien CGETS(19, 3, "free(%lx) above top of memory."), cp); 31159243Sobrien CHECK(cp < (ptr_t) membot, 31259243Sobrien CGETS(19, 4, "free(%lx) below bottom of memory."), cp); 31359243Sobrien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 31459243Sobrien CHECK(op->ov_magic != MAGIC, 31559243Sobrien CGETS(19, 5, "free(%lx) bad block."), cp); 31659243Sobrien 31759243Sobrien#ifdef RCHECK 31859243Sobrien if (op->ov_index <= 13) 31959243Sobrien CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC, 32059243Sobrien CGETS(19, 6, "free(%lx) bad range check."), cp); 32159243Sobrien#endif 32259243Sobrien CHECK(op->ov_index >= NBUCKETS, 32359243Sobrien CGETS(19, 7, "free(%lx) bad block index."), cp); 32459243Sobrien size = op->ov_index; 32559243Sobrien op->ov_next = nextf[size]; 32659243Sobrien nextf[size] = op; 32759243Sobrien 32859243Sobrien nmalloc[size]--; 32959243Sobrien 33059243Sobrien#else 33159243Sobrien if (cp == NULL) 33259243Sobrien return; 33359243Sobrien#endif 33459243Sobrien} 33559243Sobrien 33659243Sobrienmemalign_t 33759243Sobriencalloc(i, j) 33859243Sobrien size_t i, j; 33959243Sobrien{ 34059243Sobrien#ifndef lint 34159243Sobrien register char *cp, *scp; 34259243Sobrien 34359243Sobrien i *= j; 34459243Sobrien scp = cp = (char *) xmalloc((size_t) i); 34559243Sobrien if (i != 0) 34659243Sobrien do 34759243Sobrien *cp++ = 0; 34859243Sobrien while (--i); 34959243Sobrien 35059243Sobrien return ((memalign_t) scp); 35159243Sobrien#else 35259243Sobrien if (i && j) 35359243Sobrien return ((memalign_t) 0); 35459243Sobrien else 35559243Sobrien return ((memalign_t) 0); 35659243Sobrien#endif 35759243Sobrien} 35859243Sobrien 35959243Sobrien/* 36059243Sobrien * When a program attempts "storage compaction" as mentioned in the 36159243Sobrien * old malloc man page, it realloc's an already freed block. Usually 36259243Sobrien * this is the last block it freed; occasionally it might be farther 36359243Sobrien * back. We have to search all the free lists for the block in order 36459243Sobrien * to determine its bucket: 1st we make one pass thru the lists 36559243Sobrien * checking only the first block in each; if that fails we search 36659243Sobrien * ``realloc_srchlen'' blocks in each list for a match (the variable 36759243Sobrien * is extern so the caller can modify it). If that fails we just copy 36859243Sobrien * however many bytes was given to realloc() and hope it's not huge. 36959243Sobrien */ 37059243Sobrien#ifndef lint 37159243Sobrien/* 4 should be plenty, -1 =>'s whole list */ 37259243Sobrienstatic int realloc_srchlen = 4; 37359243Sobrien#endif /* lint */ 37459243Sobrien 37559243Sobrienmemalign_t 37659243Sobrienrealloc(cp, nbytes) 37759243Sobrien ptr_t cp; 37859243Sobrien size_t nbytes; 37959243Sobrien{ 38059243Sobrien#ifndef lint 38159243Sobrien register U_int onb; 38259243Sobrien union overhead *op; 38359243Sobrien ptr_t res; 38459243Sobrien register int i; 38559243Sobrien int was_alloced = 0; 38659243Sobrien 38759243Sobrien if (cp == NULL) 38859243Sobrien return (malloc(nbytes)); 38959243Sobrien op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead))); 39059243Sobrien if (op->ov_magic == MAGIC) { 39159243Sobrien was_alloced++; 39259243Sobrien i = op->ov_index; 39359243Sobrien } 39459243Sobrien else 39559243Sobrien /* 39659243Sobrien * Already free, doing "compaction". 39759243Sobrien * 39859243Sobrien * Search for the old block of memory on the free list. First, check the 39959243Sobrien * most common case (last element free'd), then (this failing) the last 40059243Sobrien * ``realloc_srchlen'' items free'd. If all lookups fail, then assume 40159243Sobrien * the size of the memory block being realloc'd is the smallest 40259243Sobrien * possible. 40359243Sobrien */ 40459243Sobrien if ((i = findbucket(op, 1)) < 0 && 40559243Sobrien (i = findbucket(op, realloc_srchlen)) < 0) 40659243Sobrien i = 0; 40759243Sobrien 40859243Sobrien onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP); 40959243Sobrien 41059243Sobrien /* avoid the copy if same size block */ 41159243Sobrien if (was_alloced && (onb <= (U_int) (1 << (i + 3))) && 41259243Sobrien (onb > (U_int) (1 << (i + 2)))) { 41359243Sobrien#ifdef RCHECK 41459243Sobrien /* JMR: formerly this wasn't updated ! */ 41559243Sobrien nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP); 41659243Sobrien *((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC; 41759243Sobrien op->ov_rmagic = RMAGIC; 41859243Sobrien op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0; 41959243Sobrien#endif 42059243Sobrien return ((memalign_t) cp); 42159243Sobrien } 42259243Sobrien if ((res = malloc(nbytes)) == NULL) 42359243Sobrien return ((memalign_t) NULL); 42459243Sobrien if (cp != res) { /* common optimization */ 42559243Sobrien /* 42659243Sobrien * christos: this used to copy nbytes! It should copy the 42759243Sobrien * smaller of the old and new size 42859243Sobrien */ 42959243Sobrien onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP; 43059243Sobrien (void) memmove((ptr_t) res, (ptr_t) cp, 43159243Sobrien (size_t) (onb < nbytes ? onb : nbytes)); 43259243Sobrien } 43359243Sobrien if (was_alloced) 43459243Sobrien free(cp); 43559243Sobrien return ((memalign_t) res); 43659243Sobrien#else 43759243Sobrien if (cp && nbytes) 43859243Sobrien return ((memalign_t) 0); 43959243Sobrien else 44059243Sobrien return ((memalign_t) 0); 44159243Sobrien#endif /* !lint */ 44259243Sobrien} 44359243Sobrien 44459243Sobrien 44559243Sobrien 44659243Sobrien#ifndef lint 44759243Sobrien/* 44859243Sobrien * Search ``srchlen'' elements of each free list for a block whose 44959243Sobrien * header starts at ``freep''. If srchlen is -1 search the whole list. 45059243Sobrien * Return bucket number, or -1 if not found. 45159243Sobrien */ 45259243Sobrienstatic int 45359243Sobrienfindbucket(freep, srchlen) 45459243Sobrien union overhead *freep; 45559243Sobrien int srchlen; 45659243Sobrien{ 45759243Sobrien register union overhead *p; 45859243Sobrien register int i, j; 45959243Sobrien 46059243Sobrien for (i = 0; i < NBUCKETS; i++) { 46159243Sobrien j = 0; 46259243Sobrien for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 46359243Sobrien if (p == freep) 46459243Sobrien return (i); 46559243Sobrien j++; 46659243Sobrien } 46759243Sobrien } 46859243Sobrien return (-1); 46959243Sobrien} 47059243Sobrien 47159243Sobrien#endif 47259243Sobrien 47359243Sobrien 47459243Sobrien#else /* SYSMALLOC */ 47559243Sobrien 47659243Sobrien/** 47759243Sobrien ** ``Protected versions'' of malloc, realloc, calloc, and free 47859243Sobrien ** 47959243Sobrien ** On many systems: 48059243Sobrien ** 48159243Sobrien ** 1. malloc(0) is bad 48259243Sobrien ** 2. free(0) is bad 48359243Sobrien ** 3. realloc(0, n) is bad 48459243Sobrien ** 4. realloc(n, 0) is bad 48559243Sobrien ** 48659243Sobrien ** Also we call our error routine if we run out of memory. 48759243Sobrien **/ 48859243Sobrienmemalign_t 48959243Sobriensmalloc(n) 49059243Sobrien size_t n; 49159243Sobrien{ 49259243Sobrien ptr_t ptr; 49359243Sobrien 49459243Sobrien n = n ? n : 1; 49559243Sobrien 49659243Sobrien#ifndef NO_SBRK 49759243Sobrien if (membot == NULL) 49859243Sobrien membot = (char*) sbrk(0); 49959243Sobrien#endif /* !NO_SBRK */ 50059243Sobrien 50159243Sobrien if ((ptr = malloc(n)) == (ptr_t) 0) { 50259243Sobrien child++; 50359243Sobrien stderror(ERR_NOMEM); 50459243Sobrien } 50559243Sobrien#ifdef NO_SBRK 50659243Sobrien if (memtop < ((char *) ptr) + n) 50759243Sobrien memtop = ((char *) ptr) + n; 50859243Sobrien if (membot == NULL) 50959243Sobrien membot = (char*) ptr; 51059243Sobrien#endif /* NO_SBRK */ 51159243Sobrien return ((memalign_t) ptr); 51259243Sobrien} 51359243Sobrien 51459243Sobrienmemalign_t 51559243Sobriensrealloc(p, n) 51659243Sobrien ptr_t p; 51759243Sobrien size_t n; 51859243Sobrien{ 51959243Sobrien ptr_t ptr; 52059243Sobrien 52159243Sobrien n = n ? n : 1; 52259243Sobrien 52359243Sobrien#ifndef NO_SBRK 52459243Sobrien if (membot == NULL) 52559243Sobrien membot = (char*) sbrk(0); 52659243Sobrien#endif /* NO_SBRK */ 52759243Sobrien 52859243Sobrien if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) { 52959243Sobrien child++; 53059243Sobrien stderror(ERR_NOMEM); 53159243Sobrien } 53259243Sobrien#ifdef NO_SBRK 53359243Sobrien if (memtop < ((char *) ptr) + n) 53459243Sobrien memtop = ((char *) ptr) + n; 53559243Sobrien if (membot == NULL) 53659243Sobrien membot = (char*) ptr; 53759243Sobrien#endif /* NO_SBRK */ 53859243Sobrien return ((memalign_t) ptr); 53959243Sobrien} 54059243Sobrien 54159243Sobrienmemalign_t 54259243Sobrienscalloc(s, n) 54359243Sobrien size_t s, n; 54459243Sobrien{ 54559243Sobrien char *sptr; 54659243Sobrien ptr_t ptr; 54759243Sobrien 54859243Sobrien n *= s; 54959243Sobrien n = n ? n : 1; 55059243Sobrien 55159243Sobrien#ifndef NO_SBRK 55259243Sobrien if (membot == NULL) 55359243Sobrien membot = (char*) sbrk(0); 55459243Sobrien#endif /* NO_SBRK */ 55559243Sobrien 55659243Sobrien if ((ptr = malloc(n)) == (ptr_t) 0) { 55759243Sobrien child++; 55859243Sobrien stderror(ERR_NOMEM); 55959243Sobrien } 56059243Sobrien 56159243Sobrien sptr = (char *) ptr; 56259243Sobrien if (n != 0) 56359243Sobrien do 56459243Sobrien *sptr++ = 0; 56559243Sobrien while (--n); 56659243Sobrien 56759243Sobrien#ifdef NO_SBRK 56859243Sobrien if (memtop < ((char *) ptr) + n) 56959243Sobrien memtop = ((char *) ptr) + n; 57059243Sobrien if (membot == NULL) 57159243Sobrien membot = (char*) ptr; 57259243Sobrien#endif /* NO_SBRK */ 57359243Sobrien 57459243Sobrien return ((memalign_t) ptr); 57559243Sobrien} 57659243Sobrien 57759243Sobrienvoid 57859243Sobriensfree(p) 57959243Sobrien ptr_t p; 58059243Sobrien{ 58159243Sobrien if (p && !dont_free) 58259243Sobrien free(p); 58359243Sobrien} 58459243Sobrien 58559243Sobrien#endif /* SYSMALLOC */ 58659243Sobrien 58759243Sobrien/* 58859243Sobrien * mstats - print out statistics about malloc 58959243Sobrien * 59059243Sobrien * Prints two lines of numbers, one showing the length of the free list 59159243Sobrien * for each size category, the second showing the number of mallocs - 59259243Sobrien * frees for each size category. 59359243Sobrien */ 59459243Sobrien/*ARGSUSED*/ 59559243Sobrienvoid 59659243Sobrienshowall(v, c) 59759243Sobrien Char **v; 59859243Sobrien struct command *c; 59959243Sobrien{ 60059243Sobrien#ifndef SYSMALLOC 60159243Sobrien register int i, j; 60259243Sobrien register union overhead *p; 60359243Sobrien int totfree = 0, totused = 0; 60459243Sobrien 60559243Sobrien xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname); 60659243Sobrien for (i = 0; i < NBUCKETS; i++) { 60759243Sobrien for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 60859243Sobrien continue; 60959243Sobrien xprintf(" %4d", j); 61059243Sobrien totfree += j * (1 << (i + 3)); 61159243Sobrien } 61259243Sobrien xprintf(CGETS(19, 9, "\nused:\t")); 61359243Sobrien for (i = 0; i < NBUCKETS; i++) { 61459243Sobrien xprintf(" %4u", nmalloc[i]); 61559243Sobrien totused += nmalloc[i] * (1 << (i + 3)); 61659243Sobrien } 61759243Sobrien xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"), 61859243Sobrien totused, totfree); 61959243Sobrien xprintf(CGETS(19, 11, 62059243Sobrien "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"), 62159243Sobrien (unsigned long) membot, (unsigned long) memtop, 62259243Sobrien (unsigned long) sbrk(0)); 62359243Sobrien#else 62459243Sobrien#ifndef NO_SBRK 62559243Sobrien memtop = (char *) sbrk(0); 62659243Sobrien#endif /* !NO_SBRK */ 62759243Sobrien xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"), 62859243Sobrien (unsigned long) membot, (unsigned long) memtop, 62959243Sobrien (unsigned long) (memtop - membot)); 63059243Sobrien#endif /* SYSMALLOC */ 63159243Sobrien USE(c); 63259243Sobrien USE(v); 63359243Sobrien} 634