1: / storage allocator for use with C 2: / 3: / 4: / 5: / hand-tooled from C compilation to modify save-return 6: / so that it can be called from within the C save 7: / when running with coroutines 8: / 9: /# 10: //* 11: / * C storage allocator 12: / * (circular first fit strategy) 13: / */ 14: /#define BLOK 512 15: /#define BUSY 01 16: / 17: /char *allocs[2] { /*initial empty arena*/ 18: / &allocs[1], 19: / &allocs[0] 20: /}; 21: /struct { int word; }; 22: /char **allocp &allocs[1]; /*current search pointer*/ 23: /char **alloct &allocs[1]; /*top of arena (last cell)*/ 24: / 25: /alloc(nbytes) 26: /{ 27: / register int nwords; 28: / register char **p, **q; 29: / static char **t; 30: / 31: / allocs[0].word =| BUSY; /*static initialization*/ 32: / allocs[1].word =| BUSY; 33: / 34: / nwords = (nbytes+(2*sizeof(p)-1))/sizeof(p); 35: / for(p=allocp;;) { 36: / do { 37: / if((p->word&BUSY)==0) { 38: / while(((q = *p)->word&BUSY)==0) 39: / *p = *q; 40: / if(q >= &p[nwords]) 41: / goto found; 42: / } 43: / q = p; 44: / p = p->word & ~BUSY; 45: / } while(q>=allocp || p<allocp); 46: / if((*alloct=t=sbrk(BLOK*sizeof(p))) == -1) 47: / return(-1); 48: / if(t!=alloct+1) 49: / alloct->word =| BUSY; 50: / alloct = (*t = &t[BLOK]-1); 51: / *alloct = allocs; 52: / alloct->word =| BUSY; 53: / } 54: /found: 55: / allocp = &p[nwords]; 56: / if(q>allocp) 57: / *allocp = *p; 58: / *p = allocp.word|BUSY; 59: / return(p+1); 60: /} 61: / 62: /free(p) 63: /char **p; 64: /{ 65: / allocp = p-1; 66: / allocp->word =& ~BUSY; 67: /} 68: .globl _allocs 69: .data 70: _allocs=. 71: 2+_allocs 72: _allocs 73: .globl _allocp 74: .data 75: _allocp=. 76: 2+_allocs 77: .globl _alloct 78: .data 79: _alloct=. 80: 2+_allocs 81: .globl _alloc 82: 83: .globl _sbrk 84: .text 85: _alloc: 86: mov r5,-(sp) 87: mov sp,r5 88: mov r4,-(sp) 89: mov r3,-(sp) 90: mov r2,-(sp) 91: bis $1,_allocs 92: bis $1,2+_allocs 93: mov 4(r5),r4 94: add $3,r4 95: asr r4 96: mov _allocp,r3 97: jbr L6 98: L7:mov r3,r2 99: mov (r3),r3 100: bic $!177776,r3 101: cmp r2,_allocp 102: jhis L6 103: cmp r3,_allocp 104: jlo L6 105: mov $2000,-(sp) 106: jsr pc,*$_sbrk 107: tst (sp)+ 108: mov r0,t 109: mov r0,*_alloct 110: cmp $177777,r0 111: jeq L11 112: mov _alloct,r0 113: add $2,r0 114: cmp t,r0 115: jeq L12 116: bis $1,*_alloct 117: L12:mov t,r0 118: add $1776,r0 119: mov r0,*t 120: mov r0,_alloct 121: mov $_allocs,*_alloct 122: bis $1,*_alloct 123: L6:bit $1,(r3) 124: jeq L8 125: jbr L7 126: L20001:mov (r2),(r3) 127: L8:mov (r3),r2 128: bit $1,(r2) 129: jeq L20001 130: mov r4,r0 131: asl r0 132: add r3,r0 133: cmp r2,r0 134: jlo L7 135: mov r4,r0 136: asl r0 137: add r3,r0 138: mov r0,_allocp 139: cmp r2,r0 140: jlos L13 141: mov (r3),*_allocp 142: L13:mov _allocp,r0 143: bis $1,r0 144: mov r0,(r3) 145: mov r3,r0 146: add $2,r0 147: L11: 148: mov (sp)+,r2 149: mov (sp)+,r3 150: mov (sp)+,r4 151: mov (sp)+,r5 152: rts pc 153: .globl _free 154: .text 155: _free: 156: mov r5,-(sp) 157: mov sp,r5 158: mov 4(r5),r0 159: add $177776,r0 160: mov r0,_allocp 161: bic $!177776,*_allocp 162: mov (sp)+,r5 163: rts pc 164: .bss 165: t: .=.+2