Annotation of 43BSDReno/sys/netiso/xebec/malloc.c, revision 1.1.1.1

1.1       root        1: /* $Header: malloc.c,v 2.2 88/09/19 12:55:18 nhall Exp $ */
                      2: /* $Source: /var/home/tadl/src/argo/xebec/RCS/malloc.c,v $ */
                      3: /*
                      4:  * This code is such a kludge that I don't want to put my name on it.
                      5:  * It was a ridiculously fast hack and needs rewriting.
                      6:  * However it does work...
                      7:  */
                      8: 
                      9: /* 
                     10:  * a simple malloc
                     11:  * it might be brain-damaged but for the purposes of xebec
                     12:  * it's a whole lot faster than the c library malloc 
                     13:  */
                     14: 
                     15: #include <stdio.h>
                     16: #include "malloc.h"
                     17: #include "debug.h"
                     18: #define CHUNKSIZE 4096*2
                     19: 
                     20: static char *hiwat, *highend;
                     21: int bytesmalloced=0;
                     22: int byteswasted = 0;
                     23: 
                     24: 
                     25: init_alloc()
                     26: {
                     27: #ifdef LINT
                     28:        hiwat = 0;
                     29:        highend = 0;
                     30: #else LINT
                     31:        extern char *sbrk();
                     32: 
                     33:        hiwat = (char *) sbrk(0);
                     34:        hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
                     35:        highend = hiwat;
                     36: #endif LINT
                     37: }
                     38: 
                     39: HIWAT(s)
                     40: char *s;
                     41: {
                     42:        IFDEBUG(M)
                     43:                fprintf(stdout, "HIWAT 0x%x  %s\n", hiwat,s);
                     44:                fflush(stdout);
                     45:        ENDDEBUG
                     46: }
                     47: 
                     48: #define MIN(x,y) ((x<y)?x:y)
                     49: 
                     50: char *Malloc(x)
                     51: int x;
                     52: {
                     53:        char *c;
                     54:        extern char *sbrk();
                     55:        static int firsttime=1;
                     56:        int total = x;
                     57:        int first_iter = 1;
                     58:        char *returnvalue;
                     59: 
                     60:        IFDEBUG(N)
                     61:                fprintf(stdout, "Malloc 0x%x, %d, bytesmalloced %d\n",
                     62:                        total,total, bytesmalloced);
                     63:                fflush(stdout);
                     64:        ENDDEBUG
                     65:        IFDEBUG(M)
                     66:                fprintf(stdout, "Malloc 0x%x, %d, hiwat 0x%x\n",
                     67:                        total,total, hiwat);
                     68:                fflush(stdout);
                     69:        ENDDEBUG
                     70:        if(firsttime) {
                     71:                hiwat = sbrk(0);
                     72:                if(((unsigned)(hiwat) & 0x3)) {
                     73:                        bytesmalloced = 4 - (int) ((unsigned)(hiwat) & 0x3);
                     74:                        hiwat = sbrk( bytesmalloced );
                     75:                } else 
                     76:                        bytesmalloced = 0;
                     77:                firsttime = 0;
                     78:                highend = hiwat;
                     79:        }
                     80:        while( total ) {
                     81:                x = MIN(CHUNKSIZE, total);
                     82:                if(total != x)  {
                     83:                        IFDEBUG(N)
                     84:                                fprintf(stdout, "BIG Malloc tot %d, x %d, left %d net %d\n",
                     85:                                        total,x, total-x, bytesmalloced);
                     86:                                fflush(stdout);
                     87:                        ENDDEBUG
                     88:                }
                     89:                if ( (hiwat + x) > highend) {
                     90:                        c = sbrk(CHUNKSIZE);
                     91:                        IFDEBUG(M)
                     92:                                fprintf(stdout, "hiwat 0x%x, x 0x%x, highend 0x%x, c 0x%x\n",
                     93:                                                hiwat, x, highend, c);
                     94:                                fflush(stdout);
                     95:                        ENDDEBUG
                     96:                        if( c == (char *) -1 ) {
                     97:                                fprintf(stderr, "Ran out of memory!\n");
                     98:                                Exit(-1);
                     99:                        }
                    100:                        if(first_iter) {
                    101:                                returnvalue = c;
                    102:                                first_iter = 0;
                    103:                        }
                    104:                        bytesmalloced +=  CHUNKSIZE;
                    105:                        IFDEBUG(m)
                    106:                                if (highend != c) {
                    107:                                        fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat);
                    108:                                fprintf(OUT, " chunksize 0x%x,  x 0x%x \n", CHUNKSIZE, x);
                    109:                                }
                    110:                        ENDDEBUG
                    111:                        highend = c + CHUNKSIZE;
                    112:                        hiwat = c;
                    113:                }
                    114:                c = hiwat;
                    115:                if(first_iter) {
                    116:                        returnvalue = c;
                    117:                        first_iter = 0;
                    118:                }
                    119:                hiwat += x;
                    120:                total -= x;
                    121:        }
                    122:        if((unsigned)hiwat & 0x3) {
                    123:                byteswasted += (int)((unsigned)(hiwat) & 0x3);
                    124:                hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
                    125:        }
                    126:        IFDEBUG(M)
                    127:                fprintf(stdout, "Malloc = 0x%x, bytesm 0x%x, wasted 0x%x, hiwat 0x%x\n",
                    128:                        returnvalue, bytesmalloced, byteswasted, hiwat);
                    129:        ENDDEBUG
                    130:        IFDEBUG(N)
                    131:                fprintf(stdout, "Malloc returns 0x%x, sbrk(0) 0x%x\n", returnvalue, sbrk(0));
                    132:                fflush(stdout);
                    133:        ENDDEBUG
                    134:        return(returnvalue);
                    135: }
                    136: 

unix.superglobalmegacorp.com

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