summaryrefslogtreecommitdiff
path: root/src/pso/THeap.cpp
blob: 3a6552d8999e105bed7438ea49a3b2c04464ea14 (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
#include "pso/THeap.h"
#include <stdlib.h>
#include <string.h>
#include <global_types.h>

THeap *obj_heap;
THeap *alt_heap;

void THeap::operator delete(void *ptr) {

}

void *THeap::operator new(size_t size) {
	return NULL;
}

void xfree(void *ptr) {

}

void *xmalloc(size_t size) {
	return NULL;
}

void THeap::heap_free(void *ptr) {
	#define heap_ptr &ptr_u8[-sizeof(heap_node)]
	#define ptr_heap ((heap_node *)heap_ptr)
	const u8 *ptr_u8 = (u8 *)ptr;
	if (ptr == NULL) {
		return;
	} else {
		heap_node *prev_node = heap_nodes;
		heap_node *node;

		for (; node = prev_node->next, node < ptr_heap; prev_node = node);

		if (((u8 *)&ptr_heap->next + ptr_heap->remaining_size) == (u8 *)&node->next) {
			ptr_heap->next = node->next;
			ptr_heap->remaining_size += node->remaining_size;
		} else {
			ptr_heap->next = node;
		}

		if (((u8 *)&prev_node->next + prev_node->remaining_size) == heap_ptr) {
			prev_node->next = ptr_heap->next;
			prev_node->remaining_size += ptr_heap->remaining_size;
		} else {
			prev_node->next = ptr_heap;
		}
	}
	#undef ptr_heap
	#undef heap_ptr
}

void *THeap::heap_zalloc(size_t size) {
	void *ptr = heap_alloc(size);
	if (ptr != NULL) {
		memset(ptr, 0, size);
	}
	return ptr;
}

void *THeap::heap_alloc(size_t size) {
	heap_node *prev_node = heap_nodes;
	heap_node *node;
	heap_node *tmp;

	u32 node_size = (sizeof(heap_node) - 1) + align + size;
	node_size &= -align;

	for (; node = prev_node->next, node != NULL; prev_node = node) {
		if (node->remaining_size >= node_size) {
			if (node_size == node->remaining_size) {
				prev_node->next = node->next;
			} else {
				const u8 *next_u8 = (u8 *)&node->next;
				tmp = (heap_node *)&next_u8[node_size];

				tmp->next = node->next;
				tmp->remaining_size = node->remaining_size - node_size;

				node->remaining_size = node_size;
				prev_node->next = tmp;
			}
			return node + 1;
		}
	}
	return NULL;
}

THeap::~THeap() {
	xfree(heap_nodes);
}

THeap::THeap(size_t size, int alignment) {
	mbr_0x0C = 0;
	mbr_0x10 = 0;
	align = alignment;
	heap_nodes = (heap_node *)xmalloc(size);
	if (heap_nodes != NULL) {
		heap_node *tmp_node;
		heap_node *next_tmp_node;

		memset(heap_nodes, 0, size);

		tmp_node = heap_nodes;
		next_tmp_node = &tmp_node[1];
		tmp_node->next = next_tmp_node;

		heap_nodes->remaining_size = 0;

		next_tmp_node->next = NULL;
		next_tmp_node->remaining_size = size - sizeof(heap_node);
	}
}