xref: /haiku/src/kits/debugger/model/TeamMemoryBlock.cpp (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent  * Copyright 2011, Rene Gollent, rene@gollent.com.
3*fce4895dSRene Gollent  * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent  */
5*fce4895dSRene Gollent 
6*fce4895dSRene Gollent 
7*fce4895dSRene Gollent #include "TeamMemoryBlock.h"
8*fce4895dSRene Gollent 
9*fce4895dSRene Gollent 
10*fce4895dSRene Gollent #include <AutoLocker.h>
11*fce4895dSRene Gollent 
12*fce4895dSRene Gollent #include "TeamMemoryBlockManager.h"
13*fce4895dSRene Gollent 
14*fce4895dSRene Gollent 
15*fce4895dSRene Gollent // #pragma mark - TeamMemoryBlock
16*fce4895dSRene Gollent 
17*fce4895dSRene Gollent 
TeamMemoryBlock(target_addr_t baseAddress,TeamMemoryBlockOwner * owner)18*fce4895dSRene Gollent TeamMemoryBlock::TeamMemoryBlock(target_addr_t baseAddress,
19*fce4895dSRene Gollent 	TeamMemoryBlockOwner* owner)
20*fce4895dSRene Gollent 	:
21*fce4895dSRene Gollent 	fValid(false),
22*fce4895dSRene Gollent 	fWritable(false),
23*fce4895dSRene Gollent 	fBaseAddress(baseAddress),
24*fce4895dSRene Gollent 	fBlockOwner(owner)
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent }
27*fce4895dSRene Gollent 
28*fce4895dSRene Gollent 
~TeamMemoryBlock()29*fce4895dSRene Gollent TeamMemoryBlock::~TeamMemoryBlock()
30*fce4895dSRene Gollent {
31*fce4895dSRene Gollent 	delete fBlockOwner;
32*fce4895dSRene Gollent }
33*fce4895dSRene Gollent 
34*fce4895dSRene Gollent 
35*fce4895dSRene Gollent void
AddListener(Listener * listener)36*fce4895dSRene Gollent TeamMemoryBlock::AddListener(Listener* listener)
37*fce4895dSRene Gollent {
38*fce4895dSRene Gollent 	AutoLocker<BLocker> lock(fLock);
39*fce4895dSRene Gollent 	fListeners.Add(listener);
40*fce4895dSRene Gollent }
41*fce4895dSRene Gollent 
42*fce4895dSRene Gollent 
43*fce4895dSRene Gollent bool
HasListener(Listener * listener)44*fce4895dSRene Gollent TeamMemoryBlock::HasListener(Listener* listener)
45*fce4895dSRene Gollent {
46*fce4895dSRene Gollent 	AutoLocker<BLocker> lock(fLock);
47*fce4895dSRene Gollent 	ListenerList::Iterator iterator = fListeners.GetIterator();
48*fce4895dSRene Gollent 	while (iterator.HasNext()) {
49*fce4895dSRene Gollent 		if (iterator.Next() == listener)
50*fce4895dSRene Gollent 			return true;
51*fce4895dSRene Gollent 	}
52*fce4895dSRene Gollent 
53*fce4895dSRene Gollent 	return false;
54*fce4895dSRene Gollent }
55*fce4895dSRene Gollent 
56*fce4895dSRene Gollent 
57*fce4895dSRene Gollent void
RemoveListener(Listener * listener)58*fce4895dSRene Gollent TeamMemoryBlock::RemoveListener(Listener* listener)
59*fce4895dSRene Gollent {
60*fce4895dSRene Gollent 	AutoLocker<BLocker> lock(fLock);
61*fce4895dSRene Gollent 	fListeners.Remove(listener);
62*fce4895dSRene Gollent }
63*fce4895dSRene Gollent 
64*fce4895dSRene Gollent 
65*fce4895dSRene Gollent void
MarkValid()66*fce4895dSRene Gollent TeamMemoryBlock::MarkValid()
67*fce4895dSRene Gollent {
68*fce4895dSRene Gollent 	fValid = true;
69*fce4895dSRene Gollent 	NotifyDataRetrieved();
70*fce4895dSRene Gollent }
71*fce4895dSRene Gollent 
72*fce4895dSRene Gollent 
73*fce4895dSRene Gollent void
Invalidate()74*fce4895dSRene Gollent TeamMemoryBlock::Invalidate()
75*fce4895dSRene Gollent {
76*fce4895dSRene Gollent 	fValid = false;
77*fce4895dSRene Gollent }
78*fce4895dSRene Gollent 
79*fce4895dSRene Gollent 
80*fce4895dSRene Gollent bool
Contains(target_addr_t address) const81*fce4895dSRene Gollent TeamMemoryBlock::Contains(target_addr_t address) const
82*fce4895dSRene Gollent {
83*fce4895dSRene Gollent 	return fValid && address >= fBaseAddress
84*fce4895dSRene Gollent 		&& address < (fBaseAddress + sizeof(fData));
85*fce4895dSRene Gollent }
86*fce4895dSRene Gollent 
87*fce4895dSRene Gollent 
88*fce4895dSRene Gollent void
SetWritable(bool writable)89*fce4895dSRene Gollent TeamMemoryBlock::SetWritable(bool writable)
90*fce4895dSRene Gollent {
91*fce4895dSRene Gollent 	fWritable = writable;
92*fce4895dSRene Gollent }
93*fce4895dSRene Gollent 
94*fce4895dSRene Gollent 
95*fce4895dSRene Gollent void
NotifyDataRetrieved(status_t result)96*fce4895dSRene Gollent TeamMemoryBlock::NotifyDataRetrieved(status_t result)
97*fce4895dSRene Gollent {
98*fce4895dSRene Gollent 	for (ListenerList::Iterator it = fListeners.GetIterator();
99*fce4895dSRene Gollent 			Listener* listener = it.Next();) {
100*fce4895dSRene Gollent 		if (result == B_OK)
101*fce4895dSRene Gollent 			listener->MemoryBlockRetrieved(this);
102*fce4895dSRene Gollent 		else
103*fce4895dSRene Gollent 			listener->MemoryBlockRetrievalFailed(this, result);
104*fce4895dSRene Gollent 	}
105*fce4895dSRene Gollent }
106*fce4895dSRene Gollent 
107*fce4895dSRene Gollent 
108*fce4895dSRene Gollent void
LastReferenceReleased()109*fce4895dSRene Gollent TeamMemoryBlock::LastReferenceReleased()
110*fce4895dSRene Gollent {
111*fce4895dSRene Gollent 	fBlockOwner->RemoveBlock(this);
112*fce4895dSRene Gollent 
113*fce4895dSRene Gollent 	delete this;
114*fce4895dSRene Gollent }
115*fce4895dSRene Gollent 
116*fce4895dSRene Gollent 
117*fce4895dSRene Gollent // #pragma mark - TeamMemoryBlock
118*fce4895dSRene Gollent 
119*fce4895dSRene Gollent 
~Listener()120*fce4895dSRene Gollent TeamMemoryBlock::Listener::~Listener()
121*fce4895dSRene Gollent {
122*fce4895dSRene Gollent }
123*fce4895dSRene Gollent 
124*fce4895dSRene Gollent 
125*fce4895dSRene Gollent void
MemoryBlockRetrieved(TeamMemoryBlock * block)126*fce4895dSRene Gollent TeamMemoryBlock::Listener::MemoryBlockRetrieved(TeamMemoryBlock* block)
127*fce4895dSRene Gollent {
128*fce4895dSRene Gollent }
129*fce4895dSRene Gollent 
130*fce4895dSRene Gollent 
131*fce4895dSRene Gollent void
MemoryBlockRetrievalFailed(TeamMemoryBlock * block,status_t result)132*fce4895dSRene Gollent TeamMemoryBlock::Listener::MemoryBlockRetrievalFailed(TeamMemoryBlock* block,
133*fce4895dSRene Gollent 	status_t result)
134*fce4895dSRene Gollent {
135*fce4895dSRene Gollent }
136