-
Notifications
You must be signed in to change notification settings - Fork 5
/
memorystack.cpp
43 lines (34 loc) · 989 Bytes
/
memorystack.cpp
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
#include "prism/memorystack.h"
#include <stdio.h>
#include <prism/memoryhandler.h>
MemoryStack createMemoryStack(uint32_t tSize)
{
MemoryStack ret;
ret.mAddress = allocMemory(tSize);
ret.mSize = tSize;
ret.mOffset = 0;
ret.mAmount = 0;
return ret;
}
void destroyMemoryStack(MemoryStack * tStack)
{
freeMemory(tStack->mAddress);
}
void resizeMemoryStackToCurrentSize(MemoryStack* tStack) {
if (tStack->mOffset == tStack->mSize) return;
tStack->mAddress = reallocMemory(tStack->mAddress, tStack->mOffset);
tStack->mSize = tStack->mOffset;
}
void * allocMemoryOnMemoryStack(MemoryStack * tStack, uint32_t tSize)
{
void* ret = (void*)((uintptr_t)tStack->mAddress + tStack->mOffset);
const auto padding = (4 - (tSize % 4)) % 4;
tStack->mOffset += tSize + padding;
tStack->mAmount++;
return ret;
}
int canFitOnMemoryStack(MemoryStack * tStack, uint32_t tSize)
{
const auto padding = (4 - (tSize % 4)) % 4;
return (tStack->mOffset + tSize + padding) <= tStack->mSize;
}