summaryrefslogtreecommitdiff
path: root/src/boot/O2/system_heap.c
blob: f25a01a979a2bba06559b184ae98b8ee796a6af9 (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
/**
 * @file system_heap.c
 *
 * Original file name unknown, as well as all function names.
 *
 * @note:
 *  Only SystemHeap_Init() is used, and is essentially just a wrapper for SystemArena_Init().
 *
 */

#include "system_heap.h"
#include "libc/stdint.h"
#include "libc64/osmalloc.h"
#include "libc64/malloc.h"
#include "unk.h"

typedef void (*BlockFunc)(void*);
typedef void (*BlockFunc1)(void*, u32);
typedef void (*BlockFunc8)(void*, u32, u32, u32, u32, u32, u32, u32, u32);

typedef struct InitFunc {
    /* 0x0 */ uintptr_t nextOffset;
    /* 0x4 */ void (*func)(void);
} InitFunc; // size = 0x8

void* sInitFuncs = NULL;

char sNew[] = "";

void* SystemHeap_Malloc(size_t size) {
    if (size == 0) {
        size = 1;
    }
    return __osMalloc(&malloc_arena, size);
}

void SystemHeap_Free(void* ptr) {
    if (ptr != NULL) {
        __osFree(&malloc_arena, ptr);
    }
}

void SystemHeap_RunBlockFunc(void* blk, size_t nBlk, size_t blkSize, BlockFunc blockFunc) {
    uintptr_t pos = (uintptr_t)blk;

    for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) {
        blockFunc((void*)pos);
    }
}

void SystemHeap_RunBlockFunc1(void* blk, size_t nBlk, size_t blkSize, BlockFunc1 blockFunc) {
    uintptr_t pos = (uintptr_t)blk;

    for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) {
        blockFunc((void*)pos, 2);
    }
}

void* SystemHeap_RunBlockFunc8(void* blk, size_t nBlk, size_t blkSize, BlockFunc8 blockFunc) {
    if (blk == NULL) {
        blk = SystemHeap_Malloc(nBlk * blkSize);
    }

    if ((blk != NULL) && (blockFunc != NULL)) {
        uintptr_t pos = (uintptr_t)blk;

        for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) {
            blockFunc((void*)pos, 0, 0, 0, 0, 0, 0, 0, 0);
        }
    }

    return blk;
}

void SystemHeap_RunBlockFunc1Reverse(void* blk, size_t nBlk, size_t blkSize, BlockFunc1 blockFunc, s32 shouldFree) {
    uintptr_t pos;
    uintptr_t start;
    size_t maskedBlkSize;

    if (blk == NULL) {
        return;
    }

    if (blockFunc != NULL) {
        start = (uintptr_t)blk;
        maskedBlkSize = (blkSize & ~0);
        pos = (uintptr_t)start + (nBlk * blkSize);

        while (pos > start) {
            pos -= maskedBlkSize;
            blockFunc((void*)pos, 2);
        }
    }

    if (shouldFree) {
        SystemHeap_Free(blk);
    }
}

void SystemHeap_RunInits(void) {
    InitFunc* initFunc = (InitFunc*)&sInitFuncs;
    u32 nextOffset = initFunc->nextOffset;
    InitFunc* prev = NULL;

    while (nextOffset != 0) {
        initFunc = (InitFunc*)((uintptr_t)initFunc + nextOffset);

        if (initFunc->func != NULL) {
            (*initFunc->func)();
        }

        nextOffset = initFunc->nextOffset;
        initFunc->nextOffset = (uintptr_t)prev;
        prev = initFunc;
    }

    sInitFuncs = prev;
}

void SystemHeap_Init(void* heap, size_t size) {
    MallocInit(heap, size);
    SystemHeap_RunInits();
}