summaryrefslogtreecommitdiff
path: root/src/code/gamealloc.c
blob: acf55ee0caed0bcc5fe22bbb624476dad3ba9b4b (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
#include "gamealloc.h"
#include "libc64/malloc.h"
#include "libc/stdint.h"

void func_800D3720_jp(GameAlloc* this) {
    GameAllocEntry* iter = this->base.next;

    while (&this->base != iter) {
        iter = iter->next;
    }
}

void* gamealloc_malloc(GameAlloc* this, size_t size) {
    GameAllocEntry* ptr = malloc(size + sizeof(GameAllocEntry));

    if (ptr != NULL) {
        ptr->size = size;
        ptr->prev = this->head;
        this->head->next = ptr;
        this->head = ptr;
        ptr->next = &this->base;
        this->base.prev = this->head;

        return ptr + 1;
    }

    return NULL;
}

void gamealloc_free(GameAlloc* this, void* ptr) {
    if (ptr != NULL) {
        GameAllocEntry* entry = (GameAllocEntry*)ptr - 1;

        entry->prev->next = entry->next;
        entry->next->prev = entry->prev;
        this->head = this->base.prev;
        free(entry);
    }
}

void gamealloc_cleanup(GameAlloc* this) {
    GameAllocEntry* iter = this->base.next;

    while (&this->base != iter) {
        GameAllocEntry* cur = iter;

        iter = iter->next;
        free(cur);
    }

    this->head = &this->base;
    this->base.next = &this->base;
    this->base.prev = &this->base;
}

void gamealloc_init(GameAlloc* this) {
    this->head = &this->base;
    this->base.next = &this->base;
    this->base.prev = &this->base;
}