Annotation of Net2/netiso/xebec/malloc.c, revision 1.1.1.3

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

unix.superglobalmegacorp.com

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