1232633Smp/* $Header: /p/tcsh/cvsroot/tcsh/tc.alloc.c,v 3.50 2011/12/30 20:55:24 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"
42232633Smp#ifdef HAVE_MALLINFO
43232633Smp#include <malloc.h>
44232633Smp#endif
4559243Sobrien
46232633SmpRCSID("$tcsh: tc.alloc.c,v 3.50 2011/12/30 20:55:24 christos Exp $")
4759243Sobrien
48167465Smp#define RCHECK
49167465Smp#define DEBUG
50167465Smp
5159243Sobrienstatic char   *memtop = NULL;		/* PWP: top of current memory */
5259243Sobrienstatic char   *membot = NULL;		/* PWP: bottom of allocatable memory */
5359243Sobrien
5459243Sobrienint dont_free = 0;
5559243Sobrien
5669408Sache#ifdef WINNT_NATIVE
5759243Sobrien# define malloc		fmalloc
5859243Sobrien# define free		ffree
5959243Sobrien# define calloc		fcalloc
6059243Sobrien# define realloc	frealloc
6169408Sache#endif /* WINNT_NATIVE */
6259243Sobrien
63167465Smp#if !defined(DEBUG) || defined(SYSMALLOC)
64167465Smpstatic void
65167465Smpout_of_memory (void)
66167465Smp{
67167465Smp    static const char msg[] = "Out of memory\n";
68167465Smp
69167465Smp    write(didfds ? 2 : SHDIAG, msg, strlen(msg));
70167465Smp    _exit(1);
71167465Smp}
72167465Smp#endif
73167465Smp
7459243Sobrien#ifndef SYSMALLOC
7559243Sobrien
7659243Sobrien#ifdef SX
7759243Sobrienextern void* sbrk();
7859243Sobrien#endif
7959243Sobrien/*
8059243Sobrien * Lots of os routines are busted and try to free invalid pointers.
8159243Sobrien * Although our free routine is smart enough and it will pick bad
8259243Sobrien * pointers most of the time, in cases where we know we are going to get
8359243Sobrien * a bad pointer, we'd rather leak.
8459243Sobrien */
8559243Sobrien
8659243Sobrien#ifndef NULL
8759243Sobrien#define	NULL 0
8859243Sobrien#endif
8959243Sobrien
9059243Sobrientypedef unsigned char U_char;	/* we don't really have signed chars */
9159243Sobrientypedef unsigned int U_int;
9259243Sobrientypedef unsigned short U_short;
9359243Sobrientypedef unsigned long U_long;
9459243Sobrien
9559243Sobrien
9659243Sobrien/*
9759243Sobrien * The overhead on a block is at least 4 bytes.  When free, this space
9859243Sobrien * contains a pointer to the next free block, and the bottom two bits must
9959243Sobrien * be zero.  When in use, the first byte is set to MAGIC, and the second
10059243Sobrien * byte is the size index.  The remaining bytes are for alignment.
10159243Sobrien * If range checking is enabled and the size of the block fits
10259243Sobrien * in two bytes, then the top two bytes hold the size of the requested block
10359243Sobrien * plus the range checking words, and the header word MINUS ONE.
10459243Sobrien */
10559243Sobrien
10659243Sobrien
10759243Sobrien#define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
10859243Sobrien
10959243Sobrienunion overhead {
11059243Sobrien    union overhead *ov_next;	/* when free */
11159243Sobrien    struct {
11259243Sobrien	U_char  ovu_magic;	/* magic number */
11359243Sobrien	U_char  ovu_index;	/* bucket # */
11459243Sobrien#ifdef RCHECK
11559243Sobrien	U_short ovu_size;	/* actual block size */
11659243Sobrien	U_int   ovu_rmagic;	/* range magic number */
11759243Sobrien#endif
11859243Sobrien    }       ovu;
11959243Sobrien#define	ov_magic	ovu.ovu_magic
12059243Sobrien#define	ov_index	ovu.ovu_index
12159243Sobrien#define	ov_size		ovu.ovu_size
12259243Sobrien#define	ov_rmagic	ovu.ovu_rmagic
12359243Sobrien};
12459243Sobrien
12559243Sobrien#define	MAGIC		0xfd	/* magic # on accounting info */
12659243Sobrien#define RMAGIC		0x55555555	/* magic # on range info */
12759243Sobrien#ifdef RCHECK
12859243Sobrien#define	RSLOP		sizeof (U_int)
12959243Sobrien#else
13059243Sobrien#define	RSLOP		0
13159243Sobrien#endif
13259243Sobrien
13359243Sobrien
13459243Sobrien#define ROUNDUP	7
13559243Sobrien
13659243Sobrien/*
13759243Sobrien * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
13859243Sobrien * smallest allocatable block is 8 bytes.  The overhead information
13959243Sobrien * precedes the data area returned to the user.
14059243Sobrien */
14159243Sobrien#define	NBUCKETS ((sizeof(long) << 3) - 3)
14259243Sobrienstatic union overhead *nextf[NBUCKETS] IZERO_STRUCT;
14359243Sobrien
14459243Sobrien/*
14559243Sobrien * nmalloc[i] is the difference between the number of mallocs and frees
14659243Sobrien * for a given block size.
14759243Sobrien */
14859243Sobrienstatic U_int nmalloc[NBUCKETS] IZERO_STRUCT;
14959243Sobrien
15059243Sobrien#ifndef lint
151167465Smpstatic	int	findbucket	(union overhead *, int);
152167465Smpstatic	void	morecore	(int);
15359243Sobrien#endif
15459243Sobrien
15559243Sobrien
15659243Sobrien#ifdef DEBUG
15759243Sobrien# define CHECK(a, str, p) \
15859243Sobrien    if (a) { \
15959243Sobrien	xprintf(str, p);	\
160167465Smp	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
16159243Sobrien	abort(); \
16259243Sobrien    }
16359243Sobrien#else
16459243Sobrien# define CHECK(a, str, p) \
16559243Sobrien    if (a) { \
16659243Sobrien	xprintf(str, p);	\
167167465Smp	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
16859243Sobrien	return; \
16959243Sobrien    }
17059243Sobrien#endif
17159243Sobrien
17259243Sobrienmemalign_t
173167465Smpmalloc(size_t nbytes)
17459243Sobrien{
17559243Sobrien#ifndef lint
176145479Smp    union overhead *p;
177145479Smp    int bucket = 0;
178145479Smp    unsigned shiftr;
17959243Sobrien
18059243Sobrien    /*
18159243Sobrien     * Convert amount of memory requested into closest block size stored in
18259243Sobrien     * hash buckets which satisfies request.  Account for space used per block
18359243Sobrien     * for accounting.
18459243Sobrien     */
18559243Sobrien#ifdef SUNOS4
18659243Sobrien    /*
18759243Sobrien     * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
18859243Sobrien     * so we get one more...
18959243Sobrien     * From Michael Schroeder: This is not true. It depends on the
19059243Sobrien     * timezone string. In Europe it can overwrite the 13th byte on a
19159243Sobrien     * 12 byte malloc.
19259243Sobrien     * So we punt and we always allocate an extra byte.
19359243Sobrien     */
19459243Sobrien    nbytes++;
19559243Sobrien#endif
19659243Sobrien
19759243Sobrien    nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP);
19859243Sobrien    shiftr = (nbytes - 1) >> 2;
19959243Sobrien
20059243Sobrien    /* apart from this loop, this is O(1) */
20159243Sobrien    while ((shiftr >>= 1) != 0)
20259243Sobrien	bucket++;
20359243Sobrien    /*
20459243Sobrien     * If nothing in hash bucket right now, request more memory from the
20559243Sobrien     * system.
20659243Sobrien     */
20759243Sobrien    if (nextf[bucket] == NULL)
20859243Sobrien	morecore(bucket);
209167465Smp    if ((p = nextf[bucket]) == NULL) {
21059243Sobrien	child++;
21159243Sobrien#ifndef DEBUG
212167465Smp	out_of_memory();
21359243Sobrien#else
21459243Sobrien	showall(NULL, NULL);
215167465Smp	xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes);
21659243Sobrien	abort();
21759243Sobrien#endif
21859243Sobrien	/* fool lint */
21959243Sobrien	return ((memalign_t) 0);
22059243Sobrien    }
22159243Sobrien    /* remove from linked list */
22259243Sobrien    nextf[bucket] = nextf[bucket]->ov_next;
22359243Sobrien    p->ov_magic = MAGIC;
22459243Sobrien    p->ov_index = bucket;
22559243Sobrien    nmalloc[bucket]++;
22659243Sobrien#ifdef RCHECK
22759243Sobrien    /*
22859243Sobrien     * Record allocated size of block and bound space with magic numbers.
22959243Sobrien     */
23059243Sobrien    p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0;
23159243Sobrien    p->ov_rmagic = RMAGIC;
23259243Sobrien    *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
23359243Sobrien#endif
23459243Sobrien    return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead))));
23559243Sobrien#else
23659243Sobrien    if (nbytes)
23759243Sobrien	return ((memalign_t) 0);
23859243Sobrien    else
23959243Sobrien	return ((memalign_t) 0);
24059243Sobrien#endif /* !lint */
24159243Sobrien}
24259243Sobrien
24359243Sobrien#ifndef lint
24459243Sobrien/*
24559243Sobrien * Allocate more memory to the indicated bucket.
24659243Sobrien */
24759243Sobrienstatic void
248167465Smpmorecore(int bucket)
24959243Sobrien{
250145479Smp    union overhead *op;
251145479Smp    int rnu;		/* 2^rnu bytes will be requested */
252145479Smp    int nblks;		/* become nblks blocks of the desired size */
253145479Smp    int siz;
25459243Sobrien
25559243Sobrien    if (nextf[bucket])
25659243Sobrien	return;
25759243Sobrien    /*
25859243Sobrien     * Insure memory is allocated on a page boundary.  Should make getpageize
25959243Sobrien     * call?
26059243Sobrien     */
26159243Sobrien    op = (union overhead *) sbrk(0);
26259243Sobrien    memtop = (char *) op;
26359243Sobrien    if (membot == NULL)
26459243Sobrien	membot = memtop;
26559243Sobrien    if ((long) op & 0x3ff) {
266167465Smp	memtop = sbrk((int) (1024 - ((long) op & 0x3ff)));
26759243Sobrien	memtop += (long) (1024 - ((long) op & 0x3ff));
26859243Sobrien    }
26959243Sobrien
27059243Sobrien    /* take 2k unless the block is bigger than that */
27159243Sobrien    rnu = (bucket <= 8) ? 11 : bucket + 3;
27259243Sobrien    nblks = 1 << (rnu - (bucket + 3));	/* how many blocks to get */
273167465Smp    memtop = sbrk(1 << rnu);	/* PWP */
27459243Sobrien    op = (union overhead *) memtop;
27559243Sobrien    /* no more room! */
27659243Sobrien    if ((long) op == -1)
27759243Sobrien	return;
27859243Sobrien    memtop += (long) (1 << rnu);
27959243Sobrien    /*
28059243Sobrien     * Round up to minimum allocation size boundary and deduct from block count
28159243Sobrien     * to reflect.
28259243Sobrien     */
28359243Sobrien    if (((U_long) op) & ROUNDUP) {
28459243Sobrien	op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP);
28559243Sobrien	nblks--;
28659243Sobrien    }
28759243Sobrien    /*
28859243Sobrien     * Add new memory allocated to that on free list for this hash bucket.
28959243Sobrien     */
29059243Sobrien    nextf[bucket] = op;
29159243Sobrien    siz = 1 << (bucket + 3);
29259243Sobrien    while (--nblks > 0) {
29359243Sobrien	op->ov_next = (union overhead *) (((caddr_t) op) + siz);
29459243Sobrien	op = (union overhead *) (((caddr_t) op) + siz);
29559243Sobrien    }
29659243Sobrien    op->ov_next = NULL;
29759243Sobrien}
29859243Sobrien
29959243Sobrien#endif
30059243Sobrien
30159243Sobrienvoid
302167465Smpfree(ptr_t cp)
30359243Sobrien{
30459243Sobrien#ifndef lint
305145479Smp    int size;
306145479Smp    union overhead *op;
30759243Sobrien
30859243Sobrien    /*
30959243Sobrien     * the don't free flag is there so that we avoid os bugs in routines
31059243Sobrien     * that free invalid pointers!
31159243Sobrien     */
31259243Sobrien    if (cp == NULL || dont_free)
31359243Sobrien	return;
31459243Sobrien    CHECK(!memtop || !membot,
315167465Smp	  CGETS(19, 2, "free(%p) called before any allocations."), cp);
31659243Sobrien    CHECK(cp > (ptr_t) memtop,
317167465Smp	  CGETS(19, 3, "free(%p) above top of memory."), cp);
31859243Sobrien    CHECK(cp < (ptr_t) membot,
319167465Smp	  CGETS(19, 4, "free(%p) below bottom of memory."), cp);
32059243Sobrien    op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
32159243Sobrien    CHECK(op->ov_magic != MAGIC,
322167465Smp	  CGETS(19, 5, "free(%p) bad block."), cp);
32359243Sobrien
32459243Sobrien#ifdef RCHECK
32559243Sobrien    if (op->ov_index <= 13)
32659243Sobrien	CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
327167465Smp	      CGETS(19, 6, "free(%p) bad range check."), cp);
32859243Sobrien#endif
32959243Sobrien    CHECK(op->ov_index >= NBUCKETS,
330167465Smp	  CGETS(19, 7, "free(%p) bad block index."), cp);
33159243Sobrien    size = op->ov_index;
33259243Sobrien    op->ov_next = nextf[size];
33359243Sobrien    nextf[size] = op;
33459243Sobrien
33559243Sobrien    nmalloc[size]--;
33659243Sobrien
33759243Sobrien#else
33859243Sobrien    if (cp == NULL)
33959243Sobrien	return;
34059243Sobrien#endif
34159243Sobrien}
34259243Sobrien
34359243Sobrienmemalign_t
344167465Smpcalloc(size_t i, size_t j)
34559243Sobrien{
34659243Sobrien#ifndef lint
347167465Smp    char *cp;
34859243Sobrien
34959243Sobrien    i *= j;
350167465Smp    cp = xmalloc(i);
351167465Smp    memset(cp, 0, i);
35259243Sobrien
353167465Smp    return ((memalign_t) cp);
35459243Sobrien#else
35559243Sobrien    if (i && j)
35659243Sobrien	return ((memalign_t) 0);
35759243Sobrien    else
35859243Sobrien	return ((memalign_t) 0);
35959243Sobrien#endif
36059243Sobrien}
36159243Sobrien
36259243Sobrien/*
36359243Sobrien * When a program attempts "storage compaction" as mentioned in the
36459243Sobrien * old malloc man page, it realloc's an already freed block.  Usually
36559243Sobrien * this is the last block it freed; occasionally it might be farther
36659243Sobrien * back.  We have to search all the free lists for the block in order
36759243Sobrien * to determine its bucket: 1st we make one pass thru the lists
36859243Sobrien * checking only the first block in each; if that fails we search
36959243Sobrien * ``realloc_srchlen'' blocks in each list for a match (the variable
37059243Sobrien * is extern so the caller can modify it).  If that fails we just copy
37159243Sobrien * however many bytes was given to realloc() and hope it's not huge.
37259243Sobrien */
37359243Sobrien#ifndef lint
37459243Sobrien/* 4 should be plenty, -1 =>'s whole list */
37559243Sobrienstatic int     realloc_srchlen = 4;
37659243Sobrien#endif /* lint */
37759243Sobrien
37859243Sobrienmemalign_t
379167465Smprealloc(ptr_t cp, size_t nbytes)
38059243Sobrien{
38159243Sobrien#ifndef lint
382145479Smp    U_int onb;
38359243Sobrien    union overhead *op;
38459243Sobrien    ptr_t res;
385145479Smp    int i;
38659243Sobrien    int     was_alloced = 0;
38759243Sobrien
38859243Sobrien    if (cp == NULL)
38959243Sobrien	return (malloc(nbytes));
39059243Sobrien    op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
39159243Sobrien    if (op->ov_magic == MAGIC) {
39259243Sobrien	was_alloced++;
39359243Sobrien	i = op->ov_index;
39459243Sobrien    }
39559243Sobrien    else
39659243Sobrien	/*
39759243Sobrien	 * Already free, doing "compaction".
39859243Sobrien	 *
39959243Sobrien	 * Search for the old block of memory on the free list.  First, check the
40059243Sobrien	 * most common case (last element free'd), then (this failing) the last
40159243Sobrien	 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
40259243Sobrien	 * the size of the memory block being realloc'd is the smallest
40359243Sobrien	 * possible.
40459243Sobrien	 */
40559243Sobrien	if ((i = findbucket(op, 1)) < 0 &&
40659243Sobrien	    (i = findbucket(op, realloc_srchlen)) < 0)
40759243Sobrien	    i = 0;
40859243Sobrien
40959243Sobrien    onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP);
41059243Sobrien
41159243Sobrien    /* avoid the copy if same size block */
41259243Sobrien    if (was_alloced && (onb <= (U_int) (1 << (i + 3))) &&
41359243Sobrien	(onb > (U_int) (1 << (i + 2)))) {
41459243Sobrien#ifdef RCHECK
41559243Sobrien	/* JMR: formerly this wasn't updated ! */
41659243Sobrien	nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP);
41759243Sobrien	*((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC;
41859243Sobrien	op->ov_rmagic = RMAGIC;
41959243Sobrien	op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0;
42059243Sobrien#endif
42159243Sobrien	return ((memalign_t) cp);
42259243Sobrien    }
42359243Sobrien    if ((res = malloc(nbytes)) == NULL)
42459243Sobrien	return ((memalign_t) NULL);
42559243Sobrien    if (cp != res) {		/* common optimization */
42659243Sobrien	/*
42759243Sobrien	 * christos: this used to copy nbytes! It should copy the
42859243Sobrien	 * smaller of the old and new size
42959243Sobrien	 */
43059243Sobrien	onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP;
431167465Smp	(void) memmove(res, cp, 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
444232633Smp/*
445232633Smp * On linux, _nss_nis_setnetgrent() calls this function to determine
446232633Smp * the usable size of the pointer passed, but this is not a portable
447232633Smp * API, so we cannot use our malloc replacement without providing one.
448232633Smp * Thanks a lot glibc!
449232633Smp */
450232633Smp#ifdef __linux__
451232633Smp#define M_U_S_CONST
452232633Smp#else
453232633Smp#define M_U_S_CONST
454232633Smp#endif
455232633Smpsize_t malloc_usable_size(M_U_S_CONST void *);
456232633Smpsize_t
457232633Smpmalloc_usable_size(M_U_S_CONST void *ptr)
458232633Smp{
459232633Smp    const union overhead *op = (const union overhead *)
460232633Smp	(((const char *) ptr) - MEMALIGN(sizeof(*op)));
461232633Smp    if (op->ov_magic == MAGIC)
462232633Smp	    return 1 << (op->ov_index + 2);
463232633Smp    else
464232633Smp	    return 0;
465232633Smp}
46659243Sobrien
46759243Sobrien
46859243Sobrien#ifndef lint
46959243Sobrien/*
47059243Sobrien * Search ``srchlen'' elements of each free list for a block whose
47159243Sobrien * header starts at ``freep''.  If srchlen is -1 search the whole list.
47259243Sobrien * Return bucket number, or -1 if not found.
47359243Sobrien */
47459243Sobrienstatic int
475167465Smpfindbucket(union overhead *freep, int srchlen)
47659243Sobrien{
477145479Smp    union overhead *p;
478145479Smp    size_t i;
479145479Smp    int j;
48059243Sobrien
48159243Sobrien    for (i = 0; i < NBUCKETS; i++) {
48259243Sobrien	j = 0;
48359243Sobrien	for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
48459243Sobrien	    if (p == freep)
48559243Sobrien		return (i);
48659243Sobrien	    j++;
48759243Sobrien	}
48859243Sobrien    }
48959243Sobrien    return (-1);
49059243Sobrien}
49159243Sobrien
49259243Sobrien#endif
49359243Sobrien
49459243Sobrien
49559243Sobrien#else				/* SYSMALLOC */
49659243Sobrien
49759243Sobrien/**
49859243Sobrien ** ``Protected versions'' of malloc, realloc, calloc, and free
49959243Sobrien **
50059243Sobrien ** On many systems:
50159243Sobrien **
50259243Sobrien ** 1. malloc(0) is bad
50359243Sobrien ** 2. free(0) is bad
50459243Sobrien ** 3. realloc(0, n) is bad
50559243Sobrien ** 4. realloc(n, 0) is bad
50659243Sobrien **
50759243Sobrien ** Also we call our error routine if we run out of memory.
50859243Sobrien **/
50959243Sobrienmemalign_t
510167465Smpsmalloc(size_t n)
51159243Sobrien{
51259243Sobrien    ptr_t   ptr;
51359243Sobrien
51459243Sobrien    n = n ? n : 1;
51559243Sobrien
516145479Smp#ifdef HAVE_SBRK
51759243Sobrien    if (membot == NULL)
518167465Smp	membot = sbrk(0);
519145479Smp#endif /* HAVE_SBRK */
52059243Sobrien
521167465Smp    if ((ptr = malloc(n)) == NULL)
522167465Smp	out_of_memory();
523145479Smp#ifndef HAVE_SBRK
52459243Sobrien    if (memtop < ((char *) ptr) + n)
52559243Sobrien	memtop = ((char *) ptr) + n;
52659243Sobrien    if (membot == NULL)
527167465Smp	membot = ptr;
528145479Smp#endif /* !HAVE_SBRK */
52959243Sobrien    return ((memalign_t) ptr);
53059243Sobrien}
53159243Sobrien
53259243Sobrienmemalign_t
533167465Smpsrealloc(ptr_t p, size_t n)
53459243Sobrien{
53559243Sobrien    ptr_t   ptr;
53659243Sobrien
53759243Sobrien    n = n ? n : 1;
53859243Sobrien
539145479Smp#ifdef HAVE_SBRK
54059243Sobrien    if (membot == NULL)
541167465Smp	membot = sbrk(0);
542145479Smp#endif /* HAVE_SBRK */
54359243Sobrien
544167465Smp    if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL)
545167465Smp	out_of_memory();
546145479Smp#ifndef HAVE_SBRK
54759243Sobrien    if (memtop < ((char *) ptr) + n)
54859243Sobrien	memtop = ((char *) ptr) + n;
54959243Sobrien    if (membot == NULL)
550167465Smp	membot = ptr;
551145479Smp#endif /* !HAVE_SBRK */
55259243Sobrien    return ((memalign_t) ptr);
55359243Sobrien}
55459243Sobrien
55559243Sobrienmemalign_t
556167465Smpscalloc(size_t s, size_t n)
55759243Sobrien{
55859243Sobrien    ptr_t   ptr;
55959243Sobrien
56059243Sobrien    n *= s;
56159243Sobrien    n = n ? n : 1;
56259243Sobrien
563145479Smp#ifdef HAVE_SBRK
56459243Sobrien    if (membot == NULL)
565167465Smp	membot = sbrk(0);
566145479Smp#endif /* HAVE_SBRK */
56759243Sobrien
568167465Smp    if ((ptr = malloc(n)) == NULL)
569167465Smp	out_of_memory();
57059243Sobrien
571167465Smp    memset (ptr, 0, n);
57259243Sobrien
573145479Smp#ifndef HAVE_SBRK
57459243Sobrien    if (memtop < ((char *) ptr) + n)
57559243Sobrien	memtop = ((char *) ptr) + n;
57659243Sobrien    if (membot == NULL)
577167465Smp	membot = ptr;
578145479Smp#endif /* !HAVE_SBRK */
57959243Sobrien
58059243Sobrien    return ((memalign_t) ptr);
58159243Sobrien}
58259243Sobrien
58359243Sobrienvoid
584167465Smpsfree(ptr_t p)
58559243Sobrien{
58659243Sobrien    if (p && !dont_free)
58759243Sobrien	free(p);
58859243Sobrien}
58959243Sobrien
59059243Sobrien#endif /* SYSMALLOC */
59159243Sobrien
59259243Sobrien/*
59359243Sobrien * mstats - print out statistics about malloc
59459243Sobrien *
59559243Sobrien * Prints two lines of numbers, one showing the length of the free list
59659243Sobrien * for each size category, the second showing the number of mallocs -
59759243Sobrien * frees for each size category.
59859243Sobrien */
59959243Sobrien/*ARGSUSED*/
60059243Sobrienvoid
601167465Smpshowall(Char **v, struct command *c)
60259243Sobrien{
60359243Sobrien#ifndef SYSMALLOC
604145479Smp    size_t i, j;
605145479Smp    union overhead *p;
60659243Sobrien    int     totfree = 0, totused = 0;
60759243Sobrien
60859243Sobrien    xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
60959243Sobrien    for (i = 0; i < NBUCKETS; i++) {
61059243Sobrien	for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
61159243Sobrien	    continue;
612167465Smp	xprintf(" %4zd", j);
61359243Sobrien	totfree += j * (1 << (i + 3));
61459243Sobrien    }
615232633Smp    xprintf("\n%s:\t", CGETS(19, 9, "used"));
61659243Sobrien    for (i = 0; i < NBUCKETS; i++) {
617167465Smp	xprintf(" %4d", nmalloc[i]);
61859243Sobrien	totused += nmalloc[i] * (1 << (i + 3));
61959243Sobrien    }
62059243Sobrien    xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
62159243Sobrien	    totused, totfree);
62259243Sobrien    xprintf(CGETS(19, 11,
62359243Sobrien	    "\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n"),
62459243Sobrien	    (unsigned long) membot, (unsigned long) memtop,
62559243Sobrien	    (unsigned long) sbrk(0));
626232633Smp#else /* SYSMALLOC */
627232633Smp#ifndef HAVE_MALLINFO
628145479Smp#ifdef HAVE_SBRK
629167465Smp    memtop = sbrk(0);
630145479Smp#endif /* HAVE_SBRK */
63159243Sobrien    xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
63259243Sobrien	    (unsigned long) membot, (unsigned long) memtop,
63359243Sobrien	    (unsigned long) (memtop - membot));
634232633Smp#else /* HAVE_MALLINFO */
635232633Smp    struct mallinfo mi;
636232633Smp
637232633Smp    mi = mallinfo();
638232633Smp    xprintf(CGETS(19, 13, "%s current memory allocation:\n"), progname);
639232633Smp    xprintf(CGETS(19, 14, "Total space allocated from system: %d\n"), mi.arena);
640232633Smp    xprintf(CGETS(19, 15, "Number of non-inuse chunks: %d\n"), mi.ordblks);
641232633Smp    xprintf(CGETS(19, 16, "Number of mmapped regions: %d\n"), mi.hblks);
642232633Smp    xprintf(CGETS(19, 17, "Total space in mmapped regions: %d\n"), mi.hblkhd);
643232633Smp    xprintf(CGETS(19, 18, "Total allocated space: %d\n"), mi.uordblks);
644232633Smp    xprintf(CGETS(19, 19, "Total non-inuse space: %d\n"), mi.fordblks);
645232633Smp    xprintf(CGETS(19, 20, "Top-most, releasable space: %d\n"), mi.keepcost);
646232633Smp#endif /* HAVE_MALLINFO */
64759243Sobrien#endif /* SYSMALLOC */
64859243Sobrien    USE(c);
64959243Sobrien    USE(v);
65059243Sobrien}
651