forked from albe/libpspvram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvramalloc.c
284 lines (216 loc) · 6.28 KB
/
vramalloc.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Helper for use with the PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed as 'free to use and modify as long as credited appropriately'
*
* vramalloc.c - Standard C library VRAM allocation routines.
*
* Copyright (c) 2006 Alexander Berl <[email protected]>
*
*/
//#include <psptypes.h>
//#include <pspkernel.h>
#include <pspgu.h>
#include <malloc.h>
#include "vramalloc.h"
/* Use this to set the default vramalloc() alignment. */
#define DEFAULT_VALIGNMENT 16
#ifndef ALIGN
#define ALIGN(x, align) (((x)+((align)-1))&~((align)-1))
#endif
#define VRAM_SIZE 0x200000
#define VRAM_BASE ((unsigned int)sceGeEdramGetAddr())
/* _vram_mem_block_header structure. */
typedef struct _vram_mem_header {
void * ptr;
size_t size;
struct _vram_mem_header * prev;
struct _vram_mem_header * next;
} vram_mem_header_t;
void * __vramalloc_vram_base = (void*)0;
vram_mem_header_t *__vramalloc_vram_head = NULL;
vram_mem_header_t *__vramalloc_vram_tail = NULL;
size_t vgetMemorySize(unsigned int width, unsigned int height, unsigned int psm)
{
switch (psm)
{
case GU_PSM_T4:
return (width * height) >> 1;
case GU_PSM_T8:
return width * height;
case GU_PSM_5650:
case GU_PSM_5551:
case GU_PSM_4444:
case GU_PSM_T16:
return 2 * width * height;
case GU_PSM_8888:
case GU_PSM_T32:
return 4 * width * height;
default:
return 0;
}
}
inline void* vGuPointer( void *ptr )
{
return (void*)((u32)ptr & ~VRAM_BASE);
}
inline void* vCPUPointer( void *ptr )
{
return (void*)((u32)ptr | VRAM_BASE);
}
/* Find the smallest block that we can allocate AFTER, returning NULL if there
are none. */
vram_mem_header_t * _vram_mem_fit(vram_mem_header_t *head, size_t size)
{
vram_mem_header_t *prev_mem = head, *best_fit = NULL;
u32 prev_top, next_bot;
size_t best_size = 0;
if (((u32)head->ptr+head->size+size)<VRAM_SIZE) {
best_fit = head;
best_size = VRAM_SIZE - ((u32)head->ptr+head->size);
}
while (prev_mem != NULL) {
if (prev_mem->next != NULL) {
prev_top = (u32)prev_mem->ptr;
next_bot = prev_top - ((u32)prev_mem->next->ptr + prev_mem->next->size);
if (next_bot >= size) {
if (best_fit==NULL || next_bot<best_size) {
best_fit = prev_mem->next;
best_size = next_bot;
}
}
}
prev_mem = prev_mem->next;
}
return best_fit;
}
void * vramalloc(size_t size)
{
void *ptr = NULL;
vram_mem_header_t *new_mem, *prev_mem;
size_t mem_sz;
mem_sz = size;
if ((mem_sz & (DEFAULT_VALIGNMENT - 1)) != 0)
mem_sz = ALIGN(mem_sz, DEFAULT_VALIGNMENT);
/* If we don't have any allocated blocks, reserve the first block
and initialize __vramalloc_vram_tail. */
if (__vramalloc_vram_head == NULL) {
if (size>VRAM_SIZE)
return ptr;
__vramalloc_vram_head = (vram_mem_header_t *)malloc( sizeof(vram_mem_header_t) );
if (__vramalloc_vram_head == NULL)
return ptr;
ptr = (void *)__vramalloc_vram_base;
__vramalloc_vram_head->ptr = ptr;
__vramalloc_vram_head->size = mem_sz;
__vramalloc_vram_head->prev = NULL;
__vramalloc_vram_head->next = NULL;
__vramalloc_vram_tail = __vramalloc_vram_head;
return vabsptr(ptr);
}
/* Check to see if there's free space at the bottom of the heap.
NOTE: This case is now already handled in _vram_mem_fit */
/*if (((u32)__vramalloc_vram_head->ptr + __vramalloc_vram_head->size + mem_sz) < VRAM_SIZE) {
new_mem = (vram_mem_header_t *)malloc( sizeof(vram_mem_header_t) );
if (new_mem == NULL)
return ptr;
ptr = (void *)((u32)__vramalloc_vram_head->ptr + __vramalloc_vram_head->size);
new_mem->ptr = ptr;
new_mem->size = mem_sz;
new_mem->prev = NULL;
new_mem->next = __vramalloc_vram_head;
new_mem->next->prev = new_mem;
__vramalloc_vram_head = new_mem;
return ptr;
}*/
/* See if we can allocate the block anywhere. */
prev_mem = _vram_mem_fit(__vramalloc_vram_head, mem_sz);
if (prev_mem != NULL) {
new_mem = (vram_mem_header_t *)malloc( sizeof(vram_mem_header_t) );
if (new_mem == NULL)
return ptr;
ptr = (void *)((u32)prev_mem->ptr + prev_mem->size);
new_mem->ptr = ptr;
new_mem->size = mem_sz;
new_mem->prev = prev_mem->prev;
if (new_mem->prev!=NULL)
new_mem->prev->next = new_mem;
new_mem->next = prev_mem;
prev_mem->prev = new_mem;
if (prev_mem == __vramalloc_vram_head)
__vramalloc_vram_head = new_mem;
return vabsptr(ptr);
}
/* Now we have a problem: There's no room at the bottom and also no room in between.
So either we do compact the memory (time critical because memcopies needed) or we
just return NULL so the application has to handle this case itself.
For now we'll just return NULL
*/
return ptr;
}
void vfree(void *ptr)
{
vram_mem_header_t *cur;
if (!ptr)
return;
if (!__vramalloc_vram_head)
return;
ptr = vrelptr(ptr);
/* Freeing the head pointer is a special case. */
if (ptr == __vramalloc_vram_head->ptr) {
cur = __vramalloc_vram_head->next;
free(__vramalloc_vram_head);
__vramalloc_vram_head = cur;
if (__vramalloc_vram_head != NULL) {
__vramalloc_vram_head->prev = NULL;
} else {
__vramalloc_vram_tail = NULL;
}
return;
}
cur = __vramalloc_vram_head;
while (ptr != cur->ptr) {
/* ptr isn't in our list */
if (cur->next == NULL) {
return;
}
cur = cur->next;
}
/* Deallocate the block. */
if (cur->next != NULL) {
cur->next->prev = cur->prev;
} else {
/* If this block was the last one in the list, shrink the heap. */
__vramalloc_vram_tail = cur->prev;
}
cur->prev->next = cur->next;
free( cur );
}
size_t vmemavail()
{
if (__vramalloc_vram_head==NULL)
return VRAM_SIZE;
vram_mem_header_t *cur;
size_t size = VRAM_SIZE - ((u32)__vramalloc_vram_head->ptr + __vramalloc_vram_head->size);
cur = __vramalloc_vram_head;
while (cur->next!=NULL) {
size += (u32)cur->ptr - ((u32)cur->next->ptr + cur->next->size);
cur = cur->next;
}
return size;
}
size_t vlargestblock()
{
if (__vramalloc_vram_head==NULL)
return VRAM_SIZE;
vram_mem_header_t *cur;
size_t size = VRAM_SIZE - ((u32)__vramalloc_vram_head->ptr + __vramalloc_vram_head->size);
size_t new_size;
cur = __vramalloc_vram_head;
while (cur->next!=NULL) {
new_size = (u32)cur->ptr - ((u32)cur->next->ptr + cur->next->size);
if (new_size>size) size = new_size;
cur = cur->next;
}
return size;
}