summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_PlayerHeap.cpp
blob: 2d397baf9f271781f625463f3940653610cab683 (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
#include "nw4r/snd/snd_PlayerHeap.h"

/*******************************************************************************
 * headers
 */

#include <cstddef> // NULL

#include "common.h"

#include "nw4r/snd/snd_DisposeCallbackManager.h"
#include "nw4r/snd/snd_SoundThread.h" // SoundThread::AutoLock

#include "nw4r/ut/ut_algorithm.h"

#include "nw4r/NW4RAssert.hpp"

/*******************************************************************************
 * types
 */

// forward declarations
namespace nw4r { namespace snd { namespace detail { class BasicSound; }}}

/*******************************************************************************
 * functions
 */

namespace nw4r { namespace snd { namespace detail {

PlayerHeap::PlayerHeap() :
	mSound			(nullptr),
	mPlayer			(nullptr),
	mStartAddress	(nullptr),
	mEndAddress		(nullptr),
	mAllocAddress	(nullptr)
{
}

PlayerHeap::~PlayerHeap()
{
	Destroy();
}

bool PlayerHeap::Create(void *startAddress, u32 size)
{
	void *endAddress = ut::AddOffsetToPtr(startAddress, size);
	startAddress = ut::RoundUp(startAddress, 32);

	if (startAddress > endAddress)
		return false;

	mStartAddress	= startAddress;
	mEndAddress		= endAddress;
	mAllocAddress	= mStartAddress;

	return true;
}

void PlayerHeap::Destroy()
{
	Clear();
	mAllocAddress = nullptr;
}

void *PlayerHeap::Alloc(u32 size)
{
	NW4RAssertAligned_Line(108, mAllocAddress, 32);

	void *endp = ut::AddOffsetToPtr(mAllocAddress, size);
	if (endp > mEndAddress)
		return nullptr;

	void *allocAddress = mAllocAddress;
	mAllocAddress = ut::RoundUp(endp, 32);
	return allocAddress;
}

void PlayerHeap::Clear()
{
	SoundThread::AutoLock lockForDispose;

	DisposeCallbackManager::GetInstance().Dispose(
		mStartAddress, ut::GetOffsetFromPtr(mStartAddress, mAllocAddress),
		nullptr);

	DisposeCallbackManager::GetInstance().DisposeWave(
		mStartAddress, ut::GetOffsetFromPtr(mStartAddress, mAllocAddress),
		nullptr);

	mAllocAddress = mStartAddress;
}

u32 PlayerHeap::GetFreeSize() const
{
	s32 offset = ut::GetOffsetFromPtr(mAllocAddress, mEndAddress);
	NW4RAssert_Line(157, offset >= 0);

	return offset;
}

void PlayerHeap::AttachSound(BasicSound *sound)
{
	NW4RAssertPointerNonnull_Line(172, sound);
	NW4RAssert_Line(173, mSound == NULL);

	mSound = sound;
}

void PlayerHeap::DetachSound(BasicSound *sound)
{
	NW4RAssertPointerNonnull_Line(189, sound);
	NW4RAssert_Line(190, sound == mSound);

	mSound = nullptr;
}

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