Annotation of researchv10no/cmd/f2c/malloc.c, revision 1.1

1.1     ! root        1: /****************************************************************
        !             2: Copyright 1990 by AT&T Bell Laboratories and Bellcore.
        !             3: 
        !             4: Permission to use, copy, modify, and distribute this software
        !             5: and its documentation for any purpose and without fee is hereby
        !             6: granted, provided that the above copyright notice appear in all
        !             7: copies and that both that the copyright notice and this
        !             8: permission notice and warranty disclaimer appear in supporting
        !             9: documentation, and that the names of AT&T Bell Laboratories or
        !            10: Bellcore or any of their entities not be used in advertising or
        !            11: publicity pertaining to distribution of the software without
        !            12: specific, written prior permission.
        !            13: 
        !            14: AT&T and Bellcore disclaim all warranties with regard to this
        !            15: software, including all implied warranties of merchantability
        !            16: and fitness.  In no event shall AT&T or Bellcore be liable for
        !            17: any special, indirect or consequential damages or any damages
        !            18: whatsoever resulting from loss of use, data or profits, whether
        !            19: in an action of contract, negligence or other tortious action,
        !            20: arising out of or in connection with the use or performance of
        !            21: this software.
        !            22: ****************************************************************/
        !            23: 
        !            24: #ifndef CRAY
        !            25: #define STACKMIN 512
        !            26: #define MINBLK (2*sizeof(struct mem) + 16)
        !            27: #define MSTUFF _malloc_stuff_
        !            28: #define F MSTUFF.free
        !            29: #define B MSTUFF.busy
        !            30: #define SBGULP 8192
        !            31: char *memcpy();
        !            32: 
        !            33: struct mem {
        !            34:        struct mem *next;
        !            35:        unsigned len;
        !            36:        };
        !            37: 
        !            38: struct {
        !            39:        struct mem *free;
        !            40:        char *busy;
        !            41:        } MSTUFF;
        !            42: 
        !            43: char *
        !            44: malloc(size)
        !            45: register unsigned size;
        !            46: {
        !            47:        register struct mem *p, *q, *r, *s;
        !            48:        unsigned register k, m;
        !            49:        extern char *sbrk();
        !            50:        char *top, *top1;
        !            51: 
        !            52:        size = (size+7) & ~7;
        !            53:        r = (struct mem *) &F;
        !            54:        for (p = F, q = 0; p; r = p, p = p->next) {
        !            55:                if ((k = p->len) >= size && (!q || m > k)) { m = k; q = p; s = r; }
        !            56:                }
        !            57:        if (q) {
        !            58:                if (q->len - size >= MINBLK) { /* split block */
        !            59:                        p = (struct mem *) (((char *) (q+1)) + size);
        !            60:                        p->next = q->next;
        !            61:                        p->len = q->len - size - sizeof(struct mem);
        !            62:                        s->next = p;
        !            63:                        q->len = size;
        !            64:                        }
        !            65:                else s->next = q->next;
        !            66:                }
        !            67:        else {
        !            68:                top = B ? B : (char *)(((long)sbrk(0) + 7) & ~7);
        !            69:                if (F && (char *)(F+1) + F->len == B)
        !            70:                        { q = F; F = F->next; }
        !            71:                else q = (struct mem *) top;
        !            72:                top1 = (char *)(q+1) + size;
        !            73:                if (top1 > top) {
        !            74:                        if (sbrk((int)(top1-top+SBGULP)) == (char *) -1)
        !            75:                                return 0;
        !            76:                        r = (struct mem *)top1;
        !            77:                        r->len = SBGULP - sizeof(struct mem);
        !            78:                        r->next = F;
        !            79:                        F = r;
        !            80:                        top1 += SBGULP;
        !            81:                        }
        !            82:                q->len = size;
        !            83:                B = top1;
        !            84:                }
        !            85:        return (char *) (q+1);
        !            86:        }
        !            87: 
        !            88: free(f)
        !            89: char *f;
        !            90: {
        !            91:        struct mem *p, *q, *r;
        !            92:        char *pn, *qn;
        !            93: 
        !            94:        if (!f) return;
        !            95:        q = (struct mem *) (f - sizeof(struct mem));
        !            96:        qn = f + q->len;
        !            97:        for (p = F, r = (struct mem *) &F; ; r = p, p = p->next) {
        !            98:                if (qn == (char *) p) {
        !            99:                        q->len += p->len + sizeof(struct mem);
        !           100:                        p = p->next;
        !           101:                        }
        !           102:                pn = p ? ((char *) (p+1)) + p->len : 0;
        !           103:                if (pn == (char *) q) {
        !           104:                        p->len += sizeof(struct mem) + q->len;
        !           105:                        q->len = 0;
        !           106:                        q->next = p;
        !           107:                        r->next = p;
        !           108:                        break;
        !           109:                        }
        !           110:                if (pn < (char *) q) {
        !           111:                        r->next = q;
        !           112:                        q->next = p;
        !           113:                        break;
        !           114:                        }
        !           115:                }
        !           116:        }
        !           117: 
        !           118: char *
        !           119: realloc(f, size)
        !           120: char *f;
        !           121: unsigned size;
        !           122: {
        !           123:        struct mem *p;
        !           124:        char *q, *f1;
        !           125:        unsigned s1;
        !           126: 
        !           127:        if (!f) return malloc(size);
        !           128:        p = (struct mem *) (f - sizeof(struct mem));
        !           129:        s1 = p->len;
        !           130:        free(f);
        !           131:        if (s1 > size) s1 = size + 7 & ~7;
        !           132:        if (!p->len) {
        !           133:                f1 = (char *)(p->next + 1);
        !           134:                memcpy(f1, f, s1);
        !           135:                f = f1;
        !           136:                }
        !           137:        q = malloc(size);
        !           138:        if (q && q != f)
        !           139:                memcpy(q, f, s1);
        !           140:        return q;
        !           141:        }
        !           142: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.