summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_FrameHeap.cpp
blob: 3120290636518d1aabaec5dae3a318e96831a945 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "nw4r/snd/snd_FrameHeap.h"
#include <new>

namespace nw4r {
namespace snd {
namespace detail {

FrameHeap::FrameHeap() : mHandle(NULL) {}

FrameHeap::~FrameHeap() {
    if (IsValid()) {
        Destroy();
    }
}

bool FrameHeap::Create(void* pBase, u32 size) {
    if (IsValid()) {
        Destroy();
    }

    void* pEnd = static_cast<u8*>(pBase) + size;
    pBase = ut::RoundUp(pBase, 4);
    if (pBase > pEnd) {
        return false;
    }

    mHandle = MEMCreateFrmHeap(pBase, ut::GetOffsetFromPtr(pBase, pEnd));
    if (mHandle == NULL) {
        return false;
    }

    if (!NewSection()) {
        return false;
    }

    return true;
}

void FrameHeap::Destroy() {
    if (!IsValid()) {
        return;
    }

    ClearSection();
    MEMFreeToFrmHeap(mHandle, MEM_FRM_HEAP_FREE_ALL);

    MEMDestroyFrmHeap(mHandle);
    mHandle = NULL;
}

void FrameHeap::Clear() {
    ClearSection();
    MEMFreeToFrmHeap(mHandle, MEM_FRM_HEAP_FREE_ALL);

    NewSection();
}

void* FrameHeap::Alloc(u32 size, FreeCallback pCallback, void* pCallbackArg) {
    void* pBuffer = MEMAllocFromFrmHeapEx(
        mHandle, BLOCK_BUFFER_SIZE + ut::RoundUp(size, HEAP_ALIGN), HEAP_ALIGN);

    if (pBuffer == NULL) {
        return NULL;
    }

    void *pBuffer2 = ut::AddOffsetToPtr(pBuffer, BLOCK_BUFFER_SIZE);
    Block* pBlock = new (pBuffer) Block(pBuffer2, size, pCallback, pCallbackArg);
    mSectionList.GetBack().AppendBlock(pBlock);

    return pBuffer2;
}

int FrameHeap::SaveState() {
    if (!MEMRecordStateForFrmHeap(mHandle, mSectionList.GetSize())) {
        return -1;
    }

    if (!NewSection()) {
        MEMFreeByStateToFrmHeap(mHandle, 0);
        return -1;
    }

    return GetCurrentLevel();
}

void FrameHeap::LoadState(int id) {
    if (id == 0) {
        Clear();
        return;
    }

    while (id < static_cast<int>(mSectionList.GetSize())) {
        Section& rSection = mSectionList.GetBack();
        rSection.~Section();
        mSectionList.Erase(&rSection);
    }

    MEMFreeByStateToFrmHeap(mHandle, id);
    MEMRecordStateForFrmHeap(mHandle, mSectionList.GetSize());

    NewSection();
}

int FrameHeap::GetCurrentLevel() const {
    return mSectionList.GetSize() - 1;
}

u32 FrameHeap::GetFreeSize() const {
    u32 freeSize = MEMGetAllocatableSizeForFrmHeapEx(mHandle, HEAP_ALIGN);
    if (freeSize < sizeof(Block)) {
        return 0;
    }

    return ut::RoundDown(freeSize - sizeof(Block), HEAP_ALIGN);
}

bool FrameHeap::NewSection() {
    void* pSection = MEMAllocFromFrmHeap(mHandle, sizeof(Section));
    if (pSection == NULL) {
        return false;
    }

    mSectionList.PushBack(new (pSection) Section());
    return true;
}

void FrameHeap::ClearSection() {
    while (!mSectionList.IsEmpty()) {
        Section& rSection = mSectionList.GetBack();
        rSection.~Section();
        mSectionList.Erase(&rSection);
    }
}

} // namespace detail
} // namespace snd
} // namespace nw4r