summaryrefslogtreecommitdiff
path: root/as/alloc.c
blob: 6f64ccc937a6615e3e72c32b14d79ea853db7d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
}