|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "MemSpyDriverXferBuffer.h" |
|
19 |
|
20 // System includes |
|
21 #include <u32hal.h> |
|
22 |
|
23 // Shared includes |
|
24 #include <memspy/driver/memspydriverobjectsshared.h> |
|
25 #include "MemSpyDriverObjectsInternal.h" |
|
26 |
|
27 // User includes |
|
28 #include "MemSpyDriverUtils.h" |
|
29 #include "MemSpyDriverDevice.h" |
|
30 |
|
31 |
|
32 |
|
33 DMemSpyDriverXferBuffer::DMemSpyDriverXferBuffer( DMemSpyDriverDevice& aDevice, DThread& aThread ) |
|
34 : iDevice( aDevice ), iClientThread( aThread ) |
|
35 { |
|
36 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::DMemSpyDriverXferBuffer() - this: 0x%08x", this )); |
|
37 } |
|
38 |
|
39 |
|
40 DMemSpyDriverXferBuffer::~DMemSpyDriverXferBuffer() |
|
41 { |
|
42 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::~DMemSpyDriverXferBuffer() - START - this: 0x%08x", this )); |
|
43 |
|
44 NKern::ThreadEnterCS(); |
|
45 delete iBuffer; |
|
46 NKern::ThreadLeaveCS(); |
|
47 |
|
48 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::~DMemSpyDriverXferBuffer() - END - this: 0x%08x", this )); |
|
49 } |
|
50 |
|
51 |
|
52 TInt DMemSpyDriverXferBuffer::Construct( TInt aSize ) |
|
53 { |
|
54 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::Construct() - START - this: 0x%08x, newSize: %d, iSize: %d", this, aSize, iSize )); |
|
55 TInt ret = KErrNone; |
|
56 // |
|
57 iBuffer = Kern::AllocZ( aSize ); |
|
58 if ( iBuffer == NULL ) |
|
59 { |
|
60 ret = KErrNoMemory; |
|
61 } |
|
62 else |
|
63 { |
|
64 iSize = aSize; |
|
65 } |
|
66 // |
|
67 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::Construct() - END - this: 0x%08x, newSize: %d, iSize: %d", this, aSize, iSize )); |
|
68 return ret; |
|
69 } |
|
70 |
|
71 |
|
72 |
|
73 TInt DMemSpyDriverXferBuffer::Size() const |
|
74 { |
|
75 return iSize; |
|
76 } |
|
77 |
|
78 |
|
79 TInt DMemSpyDriverXferBuffer::GrowBy( TInt aSize ) |
|
80 { |
|
81 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::GrowBy() - START - this: 0x%08x, newSize: %d, iSize: %d", this, iSize + aSize, iSize )); |
|
82 // |
|
83 TInt ret = KErrNone; |
|
84 const TInt newSize = iSize + aSize; |
|
85 if ( newSize > iSize ) |
|
86 { |
|
87 NKern::ThreadEnterCS(); |
|
88 ret = Kern::SafeReAlloc( iBuffer, iSize, newSize ); |
|
89 if ( ret == KErrNone ) |
|
90 { |
|
91 iSize = newSize; |
|
92 } |
|
93 NKern::ThreadLeaveCS(); |
|
94 } |
|
95 else |
|
96 { |
|
97 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::GrowBy() - WARNING - new size would shrink buffer (or have no effect)!" )); |
|
98 } |
|
99 // |
|
100 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::GrowBy() - END - this: 0x%08x, err: %d, iSize: %d", this, ret, iSize )); |
|
101 return ret; |
|
102 } |
|
103 |
|
104 |
|
105 TInt DMemSpyDriverXferBuffer::EnsureCapacity( TInt aSize ) |
|
106 { |
|
107 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::EnsureCapacity() - START - this: 0x%08x, aSize: %d, iSize: %d", this, aSize, iSize )); |
|
108 // |
|
109 TInt ret = KErrNone; |
|
110 if ( aSize > iSize ) |
|
111 { |
|
112 const TInt newSize = aSize; |
|
113 // |
|
114 NKern::ThreadEnterCS(); |
|
115 ret = Kern::SafeReAlloc( iBuffer, iSize, newSize ); |
|
116 if ( ret == KErrNone ) |
|
117 { |
|
118 iSize = newSize; |
|
119 } |
|
120 NKern::ThreadLeaveCS(); |
|
121 } |
|
122 else |
|
123 { |
|
124 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::EnsureCapacity() - WARNING - new size would shrink buffer (or have no effect)!" )); |
|
125 } |
|
126 // |
|
127 TRACE( Kern::Printf("DMemSpyDriverXferBuffer::EnsureCapacity() - END - this: 0x%08x, err: %d, iSize: %d", this, ret, iSize )); |
|
128 return ret; |
|
129 } |
|
130 |
|
131 |
|
132 void DMemSpyDriverXferBuffer::Reset() |
|
133 { |
|
134 memclr( iBuffer, iSize ); |
|
135 } |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 TInt DMemSpyDriverXferBuffer::WriteToClient( TDes8* aDestinationPointer, TInt aLength ) |
|
147 { |
|
148 // Check the client has enough space |
|
149 TInt err = Kern::ThreadGetDesMaxLength( &iClientThread, aDestinationPointer ); |
|
150 if ( err >= 0 ) |
|
151 { |
|
152 if ( aLength > err ) |
|
153 { |
|
154 Kern::Printf("DMemSpyDriverXferBuffer::WriteToClient() - client descriptor max len: %d, amount to write: %d => KErrOverflow", err, aLength ); |
|
155 err = KErrOverflow; |
|
156 } |
|
157 else |
|
158 { |
|
159 const TPtrC8 pData( (const TUint8*) iBuffer, aLength ); |
|
160 err = Kern::ThreadDesWrite( &iClientThread, aDestinationPointer, pData, 0, KChunkShiftBy0 | KTruncateToMaxLength, &iClientThread ); |
|
161 } |
|
162 } |
|
163 |
|
164 // Check for bad descriptor and panic the client |
|
165 if ( err == KErrBadDescriptor ) |
|
166 { |
|
167 MemSpyDriverUtils::PanicThread( iClientThread, EPanicBadDescriptor ); |
|
168 } |
|
169 // |
|
170 return err; |
|
171 } |
|
172 |
|
173 |
|
174 DThread& DMemSpyDriverXferBuffer::ClientThread() |
|
175 { |
|
176 return iClientThread; |
|
177 } |
|
178 |
|
179 |
|
180 TUint8* DMemSpyDriverXferBuffer::Ptr() |
|
181 { |
|
182 return (TUint8*) iBuffer; |
|
183 } |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |