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