summaryrefslogtreecommitdiff
path: root/src/SDK/OS/OSAlloc.c
blob: a60f3c02ec5f1a48618d12930b2d99ef26e6b842 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "types.h"
#include "OS/OSAlloc.h"

// TODO: move to header
typedef enum {
    OS_ARENA_MAIN = 0,
    OS_ARENA_MAIN_SUBPRIV = 1,
    OS_ARENA_MAINEX = 2,
    OS_ARENA_ITCM = 3,
    OS_ARENA_DTCM = 4,
    OS_ARENA_SHARED = 5,
    OS_ARENA_WRAM_MAIN = 6,
    OS_ARENA_WRAM_SUB = 7,
    OS_ARENA_WRAM_SUBPRIV = 8,
    OS_ARENA_MAX = 9
} OSArenaId;

#define OSi_ROUND(n, a)            (((u32) (n) + (a) - 1) & ~((a) - 1))

// NOTE: there is just one HeapInfo pointer in PBR, not an array
// void* OSiHeapInfo[OS_ARENA_MAX];

inline Cell* DLAddFront(Cell* list, Cell* cell)
{
    cell->next = list;
    cell->prev = NULL;

    if (list != NULL)
        list->prev = cell;
    return cell;
}

inline Cell* DLExtract(Cell* list, Cell* cell)
{
    if (cell->next) {
        cell->next->prev = cell->prev;
    }
    if (cell->prev == NULL) {
        list = cell->next;
    } else {
        cell->prev->next = cell->next;
    }
    return list;
}

Cell *DLInsert(Cell *original, Cell *inserted)
{
    Cell *prevCell = NULL;
    Cell *nextCell = original;

    for (nextCell = original, prevCell = NULL; nextCell; prevCell = nextCell, nextCell = nextCell->next)
    {
        if (inserted <= nextCell)
        break;
    }

    inserted->next = nextCell;
    inserted->prev = prevCell;

    if (nextCell != NULL)
    {
        nextCell->prev = inserted;
        Cell * temp = (Cell *)((char *)inserted + inserted->size);
        if (temp == nextCell)
        {
            inserted->size += nextCell->size;
            nextCell = nextCell->next;
            inserted->next = nextCell;
            if (nextCell != NULL)
                nextCell->prev = inserted;
        }
    }

    if (prevCell != NULL)
    {
        prevCell->next = inserted;
        Cell * temp = (Cell *)((char *)prevCell + prevCell->size);

        if (temp != inserted)
            return original;

        prevCell->size += inserted->size;
        prevCell->next = nextCell;
        if (nextCell != NULL)
            nextCell->prev = prevCell;

        return original;
    }

    return inserted;
}

#if 0 // not found in DOL
static u32 gUnk8063fa6c; // aligned arenaEnd, 680

// returned by OSInitAlloc, "base address of the new arena,"
// pointer to the beginning of the real data after the heap array header, 684
static void *gUnk8063fa70;

// Table of heaps
static int gUnk8063fa74; // maxHeaps param of OSInitAlloc, 688

volatile OSHeapHandle __OSCurrHeap = -1;

void *OSInitAlloc(void *arenaStart, void *arenaEnd, int maxHeaps)
{
    HeapArray = (HeapDesc *)arenaStart;
    gUnk8063fa74 = maxHeaps;
    for (int i = 0; i < gUnk8063fa74; i++) {
        HeapArray[i].heapSize = -1; // -1 indicates this heap is unused
        HeapArray[i].start = NULL;
        HeapArray[i].freeList = NULL;
    }
    gUnk8063fa6c = (u32)arenaEnd & ~0x1f;
    __OSCurrHeap = -1;
    gUnk8063fa70 = (void *)(((u32)&HeapArray[maxHeaps] + 0x1f) & ~0x1f);
    return gUnk8063fa70;
}

OSHeapHandle OSCreateHeap(void *start, void *end)
{
    start = (void *)((u32)start + 0x1f & ~0x1f);
    end = (void *)((u32)end & ~0x1f);
    for (int i = 0; i < gUnk8063fa74; i++) {
        if (HeapArray[i].heapSize < 0) {
            HeapArray[i].heapSize = end - start;
            ((Cell *)start)->prev = NULL;
            ((Cell *)start)->next = NULL;
            ((Cell *)start)->size = HeapArray[i].heapSize;
            HeapArray[i].start = (Cell *)start;
            HeapArray[i].freeList = NULL;
            return i;
        }
    }
    return -1;
}

#endif

// arenaStart param of OSInitAlloc, points to array of
// maxHeap HeapDescs, 68c
extern HeapDesc *HeapArray; 

#define HEADERSIZE OSi_ROUND(sizeof(Cell), 32)
#define MINOBJSIZE (HEADERSIZE+32)

void* OSAllocFromHeap(/*OSArenaId id,*/ OSHeapHandle heap, u32 size) {
    // OSHeapInfo* heapInfo;
    HeapDesc* hd;
    Cell* cell;
    Cell* newCell;
    long leftoverSize;
    
    /*
    OSIntrMode enabled = OS_DisableInterrupts();
    heapInfo = OSiHeapInfo[id];
    if (!heapInfo) {
        (void)OS_RestoreInterrupts(enabled);
        return NULL;
    }

    if (heap < 0) {
        heap = heapInfo->currentHeap;
    }
    */
    
    //hd = &heapInfo->heapArray[heap];
    hd = &HeapArray[heap];
    
    size += HEADERSIZE;
    size = OSi_ROUND(size, 32);

    for (cell = hd->free; cell != NULL; cell = cell->next) {
        if ((long)size <= cell->size) {
            break;
        }
    }

    if (cell == NULL) {
        //(void)OS_RestoreInterrupts(enabled);
        return NULL;
    }

    leftoverSize = cell->size - (long)size;
    if (leftoverSize < MINOBJSIZE) {
        hd->free = DLExtract(hd->free, cell);
    } else {
        cell->size = (long)size;

        newCell = (Cell *) ((char *)cell + size);
        newCell->size = leftoverSize;

        newCell->prev = cell->prev;
        newCell->next = cell->next;

        if (newCell->next != NULL) {
            newCell->next->prev = newCell;
        }

        if (newCell->prev != NULL) {
            newCell->prev->next = newCell;
        } else {
            hd->free = newCell;
        }
    }

    hd->allocated = DLAddFront(hd->allocated, cell);

    //(void)OS_RestoreInterrupts(enabled);
    return (void *)((char *)cell + HEADERSIZE);
}

void OSFreeToHeap(/*OSArenaId id,*/ OSHeapHandle heap, void* ptr) {
    OSHeapInfo *heapInfo;
    HeapDesc *hd;
    Cell   *cell;
    
    /*
    OSIntrMode enabled = OS_DisableInterrupts();
    heapInfo = OSiHeapInfo[id];

    if (heap < 0) {
        heap = heapInfo->currentHeap;
    }
    */

    cell = (Cell *) ((char *)ptr - HEADERSIZE);
    //hd = &heapInfo->heapArray[heap];
    hd = &HeapArray[heap];
    
    hd->allocated = DLExtract(hd->allocated, cell);
    hd->free = DLInsert(hd->free, cell);

    //(void)OS_RestoreInterrupts(enabled);
}