1 2 #include "syshead.h" 3 #include "const.h" 4 #include "type.h" 5 #include "align.h" 6 7 PRIVATE char NOMEMEORY[] = "Cannot allocate sufficient memory"; 8 9 #ifdef USE_FIXED_HEAP 10 PRIVATE char *heapend; /* end of free space for symbol list */ 11 PRIVATE char *heapptr; /* next free space in symbol list */ 12 #endif 13 14 #ifndef USE_FIXED_HEAP 15 PRIVATE char tempbuf[2048]; 16 #endif 17 18 void init_heap()19init_heap() 20 { 21 #ifdef USE_FIXED_HEAP 22 #ifndef USERMEM 23 #define USERMEM 0xAC00U 24 #endif 25 26 #ifdef __AS386_16__ 27 int stk; 28 heapptr = sbrk(0); 29 heapend = ((char*)&stk) - STAKSIZ - 16; 30 brk(heapend); 31 if(sbrk(0) != heapend) 32 as_abort(NOMEMEORY); 33 #else 34 #ifdef SOS_EDOS 35 heapend = stackreg() - STAKSIZ; 36 #else 37 heapptr = malloc(USERMEM); 38 heapend = heapptr + USERMEM; 39 if (heapptr == 0) 40 as_abort(NOMEMEORY); 41 #endif 42 #endif 43 #endif 44 } 45 temp_buf()46void * temp_buf() 47 { 48 #ifdef USE_FIXED_HEAP 49 return heapptr; 50 #else 51 return tempbuf; 52 #endif 53 } 54 55 void * asalloc(size)56asalloc(size) 57 unsigned int size; 58 { 59 void * rv; 60 #ifdef USE_FIXED_HEAP 61 align(heapptr); 62 if (heapptr+size < heapend) 63 { 64 rv = heapptr; 65 heapptr += size; 66 } 67 else 68 rv = 0; 69 #else 70 rv = malloc(size); 71 #endif 72 if (rv == 0 && size) as_abort(NOMEMEORY); 73 return rv; 74 } 75 76 77 void * asrealloc(oldptr,size)78asrealloc(oldptr, size) 79 void * oldptr; 80 unsigned int size; 81 { 82 void * rv; 83 #ifdef USE_FIXED_HEAP 84 if (oldptr == 0) return asalloc(size); 85 86 if ((char*)oldptr+size < heapend) 87 { 88 heapptr = (char*)oldptr + size; 89 rv = oldptr; 90 } 91 else 92 rv = 0; 93 #else 94 rv = realloc(oldptr, size); 95 #endif 96 97 if (rv == 0) as_abort(NOMEMEORY); 98 return rv; 99 } 100 101