|
|
1.1 ! root 1: /* ! 2: * Space managment for 386 assembler. ! 3: * ! 4: * Normally anything galloc()ed for a line will be free()d ! 5: * at the end of line. This can be avoided by umark() ! 6: */ ! 7: #include <stdio.h> ! 8: #include <asm.h> ! 9: ! 10: static char **mArray; /* all items galloc()ed for this line */ ! 11: static unsigned top = 0; /* highest item galloc()ed this line */ ! 12: static unsigned max = 64; /* count of slots in mArray, may grow */ ! 13: ! 14: #define QTOP 10 /* count of preallocated blocks */ ! 15: typedef union quick quick; ! 16: union quick { /* for allocating quickly but wastefully */ ! 17: expr e; ! 18: data d; ! 19: sym s; ! 20: }; ! 21: ! 22: static quick *qb; /* pre allocated quick blocks */ ! 23: static unsigned qtop = 0; /* highest used quick block */ ! 24: ! 25: /* ! 26: * Allocate a block of space. ! 27: * Leave if there is no space left ! 28: * at all. ! 29: */ ! 30: char * ! 31: alloc(n) ! 32: unsigned n; ! 33: { ! 34: register char *p; ! 35: extern char *calloc(); ! 36: ! 37: if ((p = (char *)calloc(n, 1)) == NULL) ! 38: fatal("Out of space"); ! 39: /* A call to \fBmalloc()\fR failed. ! 40: * The typical large consumers of RAM are macros ! 41: * and \fB.defines\fR; symbols consume less. ! 42: * Can you break your assembly into ! 43: * smaller pieces? ! 44: * Could you be in some sort of ! 45: * endless recursion or loop? */ ! 46: return (p); ! 47: } ! 48: ! 49: /* ! 50: * Init storage functions. ! 51: */ ! 52: void ! 53: initStor() ! 54: { ! 55: extern char *malloc(); ! 56: ! 57: #if 0 ! 58: char *j; ! 59: ! 60: /* This speeds things up on many systems by minamizing ! 61: * traffic with the operating system for space. ! 62: */ ! 63: if(NULL != (j = malloc(16000))) ! 64: free(j); ! 65: #endif ! 66: mArray = (char **)alloc(max * sizeof(*mArray)); ! 67: qb = (quick *)alloc(QTOP * sizeof(*qb)); ! 68: } ! 69: ! 70: /* ! 71: * Expand an area. ! 72: */ ! 73: expand(area, current, by, size) ! 74: char **area; ! 75: unsigned *current, by, size; ! 76: { ! 77: if (NULL == (*area = realloc(*area, size * (*current += by)))) ! 78: fatal("Out of space"); /* NODOC */ ! 79: } ! 80: ! 81: /* ! 82: * Get space and keep track of it on mArray. ! 83: */ ! 84: char * ! 85: galloc(size) ! 86: unsigned size; ! 87: { ! 88: if(top == max) ! 89: expand(&mArray, &max, 10, sizeof(*mArray)); ! 90: ! 91: return (mArray[top++] = alloc(size)); ! 92: } ! 93: ! 94: /* ! 95: * Forget an item on mArray. ! 96: */ ! 97: void ! 98: umark(p) ! 99: char *p; ! 100: { ! 101: register char **t; ! 102: ! 103: for (t = mArray + top; t != mArray;) { ! 104: if(p == *--t) { ! 105: *t = NULL; ! 106: return; ! 107: } ! 108: } ! 109: fatal("Logic error in umark"); /* TECH */ ! 110: } ! 111: ! 112: /* ! 113: * free all items on mArray. ! 114: */ ! 115: void ! 116: freel() ! 117: { ! 118: register char **t; ! 119: ! 120: for(t = mArray + top; t != mArray; ) ! 121: if(NULL != *--t) ! 122: free(*t); ! 123: qtop = top = 0; ! 124: } ! 125: ! 126: /* ! 127: * unmark all the parms. ! 128: */ ! 129: void ! 130: umList(p) ! 131: register parm *p; ! 132: { ! 133: for(; NULL != p; p = p->next) ! 134: umark((char *)p); ! 135: } ! 136: ! 137: /* ! 138: * free list of items connected by next pointers. ! 139: */ ! 140: void ! 141: freeList(p) ! 142: register parm *p; ! 143: { ! 144: register parm *t; ! 145: ! 146: while(NULL != p) { ! 147: p = (t = p)->next; ! 148: free((char *)t); ! 149: } ! 150: } ! 151: ! 152: /* ! 153: * Get an expr block. ! 154: * There can only be more than 4 on an error to be spotted later. ! 155: * This is done because the profiler spotted a large amount of time ! 156: * malloc()ing expr blocks. ! 157: */ ! 158: expr * ! 159: xalloc() ! 160: { ! 161: register expr *tmp; ! 162: ! 163: if(qtop == QTOP) ! 164: return(gnew(expr)); ! 165: tmp = &(qb[qtop++].e); ! 166: return((expr *)clear(tmp)); ! 167: } ! 168: ! 169: /* ! 170: * Make a temporary copy of a sym. ! 171: * Rarely done more than twice a line. ! 172: */ ! 173: sym * ! 174: copySym(s) ! 175: sym *s; ! 176: { ! 177: register sym *n; ! 178: ! 179: if(qtop == QTOP) ! 180: n = gnew(sym); ! 181: else ! 182: n = &(qb[qtop++].s); ! 183: *n = *s; ! 184: return(n); ! 185: } ! 186: ! 187: /* ! 188: * Get space for data item. ! 189: */ ! 190: data * ! 191: gitem(type) ! 192: { ! 193: register data *d; ! 194: ! 195: if(qtop == QTOP) ! 196: d = gnew(data); ! 197: else ! 198: d = &(qb[qtop++].d); ! 199: d->next = NULL; ! 200: d->type = type; ! 201: d->count = 1; ! 202: return(d); ! 203: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.