Update contrib.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\cbase\ub_circ.cpp
20 EXPORT_C CCirBufBase::CCirBufBase(TInt aSize)
22 Constructor taking the size of an object within the buffer.
24 @param aSize The size of an object in the buffer.
26 @panic E32USER-CBase 72, if aSize is zero or negative.
31 __ASSERT_ALWAYS(iSize>0,Panic(ECircItemSizeNegativeOrZero));
40 EXPORT_C CCirBufBase::~CCirBufBase()
44 This frees the memory allocated to the buffer.
51 EXPORT_C void CCirBufBase::SetLengthL(TInt aLength)
53 Sets the maximum capacity of this circular buffer, and resets all
54 of the buffer pointers.
56 The capacity is the maximum number of elements that the buffer can hold.
58 The buffer itself is allocated as a result of a call to this function. If
59 the function has previously been called, then any existing buffer is freed and
60 any information in it is lost.
64 1. This function must be called before attempting to add any objects to
67 2. The function can leave if there is insufficient memory available to
70 @param aLength The maximum capacity of the circular buffer.
72 @panic E32USER-CBase 73, if aLength is zero or negative.
76 __ASSERT_ALWAYS(aLength>0,Panic(ECircSetLengthNegativeOrZero));
77 iPtr=(TUint8 *)User::ReAllocL(iPtr,aLength*iSize);
78 iPtrE=iPtr+(aLength*iSize);
84 EXPORT_C void CCirBufBase::Reset()
93 Mem::FillZ(iPtr,iLength*iSize);
97 EXPORT_C TInt CCirBufBase::DoAdd(const TUint8 *aPtr)
99 Implementation function for CCirBuf::Add(const T*)
101 Adds a single object to the circular buffer, but only if there is
104 @param aPtr A pointer to the object to be added.
106 @return 1 if the object is successfully added. 0 if the object cannot be added
107 because the circular buffer is full.
109 @panic E32USER-CBase 74, if a call to CCirBufBase::SetLengthL() has not been
110 made before calling this function.
113 @see CCirBufBase::SetLengthL
117 __ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
120 Mem::Copy(iHead,aPtr,iSize);
128 EXPORT_C TInt CCirBufBase::DoAdd(const TUint8 *aPtr,TInt aCount)
130 Implementation function for CCirBuf::Add(const T*,TInt)
132 Adds multiple objects to the circular buffer, but only if there is
135 @param aPtr A pointer to a set of contiguous objects to be added.
137 @param aCount The number of objects to be added.
139 @return The number of objects successfully added to the buffer. This value
140 may be less than the number requested and can range from 0 to aCount.
142 @panic E32USER-CBase 74, if a call to CCirBufBase::SetLengthL() has not been
143 made before calling this function.
144 @panic E32USER-CBase 75, if aCount is not a positive value.
147 @see CCirBufBase::SetLengthL
151 __ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
152 __ASSERT_ALWAYS(aCount>0,Panic(ECircAddCountNegative));
153 TInt rem=iLength-iCount;
156 aCount=Min(aCount,rem);
157 rem=(iPtrE-iHead)/iSize;
159 iHead=Mem::Copy(iHead,aPtr,aCount*iSize);
162 TInt len=(rem*iSize);
163 Mem::Copy(iHead,aPtr,len);
164 iHead=Mem::Copy(iPtr,aPtr+len,(aCount*iSize)-len);
172 EXPORT_C TInt CCirBufBase::DoRemove(TUint8 *aPtr)
174 Implementation function for CCirBuf::Remove(T*)
176 Removes a single object from the circular buffer, but only if there are
177 objects in the buffer.
179 A binary copy of the object is made to aPtr.
181 @param aPtr A pointer to a location supplied by the caller.
183 @return 1 if an object is successfully removed. 0 if an object cannot be removed
184 because the circular buffer is empty.
192 Mem::Copy(aPtr,iTail,iSize);
200 EXPORT_C TInt CCirBufBase::DoRemove(TUint8 *aPtr,TInt aCount)
202 Implementation function for CCirBuf::Remove(T*,TInt)
204 Attempts to remove aCount objects from the circular buffer, but only if there
205 are objects in the buffer.
207 A binary copy of the objects is made to aPtr.
209 @param aPtr A pointer to a location supplied by the caller capable of
210 holding aCount objects.
212 @param aCount The number of objects to be removed from the circular buffer.
214 @return The number of objects successfully removed from the buffer. This value
215 may be less than the number requested, and can range from 0 to aCount.
217 @panic E32USER-CBase 76, if aCount is not a positive value.
225 __ASSERT_ALWAYS(aCount>0,Panic(ECircRemoveCountNegative));
226 aCount=Min(aCount,iCount);
227 TInt rem=(iPtrE-iTail)/iSize;
231 Mem::Copy(aPtr,iTail,aCount*iSize);
236 Mem::Copy(aPtr,iTail,len);
237 rem=(aCount*iSize)-len;
238 Mem::Copy(aPtr+len,iPtr,rem);
247 EXPORT_C CCirBuffer::CCirBuffer()
250 Default C++ constructor.
254 EXPORT_C CCirBuffer::~CCirBuffer()
261 EXPORT_C TInt CCirBuffer::Get()
263 Removes an unsigned 8-bit integer value from the circular buffer and returns
266 The returned TUint8 is promoted to a TInt to allow for negative error codes,
269 @return The unsigned 8-bit integer value removed from the circular buffer.
270 KErrGeneral, if the circular buffer is empty.
283 EXPORT_C TInt CCirBuffer::Put(TInt aVal)
285 Adds an unsigned 8-bit integer value in the range 0 to 255 to the circular buffer.
287 If the specified integer is outside the range 0 to 255,
288 this method discards all but the lowest 8 bits and treats those
289 as the unsigned integer to store.
290 For example, specifying -2 (or 510, or -258, etc) will result in 254 being stored,
291 and therefore in 254 being returned by the Get() method
292 (and not the number passed to Put()).
294 @param aVal The unsigned 8-bit integer value to be added.
295 @return KErrNone, if the unsigned integer is successfully added.
296 KErrGeneral, if the unsigned integer cannnot be added because
297 the circular buffer is full.
299 @see CCirBuffer::Get()
303 __ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
306 *iHead++=(TUint8)aVal;