Annotation of coherent/b/lib/libc/gen/malloc/realloc.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * realloc.c
                      3:  * Fits System V requirements.
                      4:  * Knows format of malloc arena.
                      5:  */
                      6: 
                      7: #include <stdio.h>
                      8: #include <sys/malloc.h>
                      9: 
                     10: extern char *malloc();
                     11: 
                     12: char *
                     13: realloc(cp, size) register char *cp; unsigned size;
                     14: {
                     15:        register unsigned len, needed, nlen;
                     16:        register MBLOCK *mp, *nmp;
                     17: 
                     18:        if (cp == NULL)
                     19:                return malloc(size);
                     20:        else if (size == 0) {
                     21:                free(cp);
                     22:                return NULL;
                     23:        }
                     24: 
                     25:        /* Add space for blksize, round up to word boundary, fail if too big. */
                     26:        needed = roundup(size + sizeof(unsigned), BLOCKSIZE);
                     27:        if (needed < size)
                     28:                return NULL;
                     29: 
                     30:        /* Find mblock of the given cp and its length. */
                     31:        mp = mblockp(cp);
                     32:        len = realsize(mp->blksize);
                     33: 
                     34:        /* Request to shrink. */
                     35:        if (len >= needed) {
                     36:                if ((len -= needed) > LEASTFREE) {
                     37:                        /* Split block into used and free parts. */
                     38:                        mp->blksize = needed;
                     39:                        mp = bumpp(mp, needed);
                     40:                        mp->blksize = (len|FREE);
                     41:                        __a_count++;
                     42:                }
                     43:                return cp;
                     44:        }
                     45: 
                     46:        /* Request to grow. */
                     47:        nmp = bumpp(mp, len);           /* next block */
                     48:        nlen = nmp->blksize;            /* length of next block (+1 if free) */
                     49:        if (isfree(nlen) && (nlen += len) > needed) {
                     50:                /* The next block is free and the two are big enough. */
                     51:                if ((nlen - needed) > LEASTFREE) {
                     52:                        /* Combine blocks, split into used and free parts. */
                     53:                        mp->blksize = needed;
                     54:                        mp = bumpp(mp, needed);
                     55:                        mp->blksize = nlen - needed;
                     56:                }
                     57:                else {  /* Combine blocks, using all of both. */
                     58:                        mp->blksize = nlen = realsize(nlen);
                     59:                        mp = bumpp(mp, nlen);
                     60:                        __a_count--;
                     61:                }
                     62:                if (nmp == __a_scanp)
                     63:                        __a_scanp = mp;
                     64:                return cp;
                     65:        }
                     66: 
                     67:        /* Otherwise, malloc new arena, copy current to it, free current. */
                     68:        if ((mp = (MBLOCK *)malloc(size)) == NULL)
                     69:                return NULL;
                     70:        memcpy(mp, cp, len - sizeof(unsigned));
                     71:        free(cp);
                     72:        return((char *)mp);
                     73: }

unix.superglobalmegacorp.com

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