summaryrefslogtreecommitdiff
path: root/as/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'as/alloc.c')
-rw-r--r--as/alloc.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/as/alloc.c b/as/alloc.c
new file mode 100644
index 0000000..6f64ccc
--- /dev/null
+++ b/as/alloc.c
@@ -0,0 +1,100 @@
+
+#include "syshead.h"
+#include "const.h"
+#include "type.h"
+#include "align.h"
+
+PRIVATE char NOMEMEORY[] = "Cannot allocate sufficient memory";
+
+#ifdef USE_FIXED_HEAP
+PRIVATE char *heapend; /* end of free space for symbol list */
+PRIVATE char *heapptr; /* next free space in symbol list */
+#endif
+
+#ifndef USE_FIXED_HEAP
+PRIVATE char tempbuf[2048];
+#endif
+
+void
+init_heap()
+{
+#ifdef USE_FIXED_HEAP
+#ifndef USERMEM
+#define USERMEM (unsigned) 0xAC00U
+#endif
+
+#ifdef __AS386_16__
+ heapptr = sbrk(0);
+ heapend = ((char*)&argc) - STAKSIZ - 16;
+ brk(heapend);
+ if(sbrk(0) != heapend)
+ as_abort(NOMEMEORY);
+#else
+#ifdef SOS_EDOS
+ heapend = stackreg() - STAKSIZ;
+#else
+ heapptr = malloc(USERMEM);
+ heapend = heapptr + USERMEM;
+ if (heapptr == 0)
+ as_abort(NOMEMEORY);
+#endif
+#endif
+#endif
+}
+
+void * temp_buf()
+{
+#ifdef USE_FIXED_HEAP
+ return heapptr;
+#else
+ return tempbuf;
+#endif
+}
+
+void *
+asalloc(size)
+unsigned int size;
+{
+ void * rv;
+#ifdef USE_FIXED_HEAP
+ align(heapptr);
+ if (heapptr+size < heapend)
+ {
+ rv = heapptr;
+ heapptr += size;
+ }
+ else
+ rv = 0;
+#else
+ rv = malloc(size);
+#endif
+
+ if (rv == 0) as_abort(NOMEMEORY);
+ return rv;
+}
+
+
+void *
+asrealloc(oldptr, size)
+void * oldptr;
+unsigned int size;
+{
+ void * rv;
+#ifdef USE_FIXED_HEAP
+ if (oldptr == 0) return asalloc(size);
+
+ if ((char*)oldptr+size < heapend)
+ {
+ heapptr = oldptr + size;
+ rv = oldptr;
+ }
+ else
+ rv = 0;
+#else
+ rv = realloc(oldptr, size);
+#endif
+
+ if (rv == 0) as_abort(NOMEMEORY);
+ return rv;
+}
+