os/kernelhwsrv/kernel/eka/euser/cbase/ub_circ.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\cbase\ub_circ.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "ub_std.h"
sl@0
    19
sl@0
    20
EXPORT_C CCirBufBase::CCirBufBase(TInt aSize)
sl@0
    21
/**
sl@0
    22
Constructor taking the size of an object within the buffer.
sl@0
    23
sl@0
    24
@param aSize The size of an object in the buffer.
sl@0
    25
sl@0
    26
@panic E32USER-CBase 72, if aSize is zero or negative. 
sl@0
    27
*/
sl@0
    28
	: iSize(aSize)
sl@0
    29
	{
sl@0
    30
sl@0
    31
	__ASSERT_ALWAYS(iSize>0,Panic(ECircItemSizeNegativeOrZero));
sl@0
    32
//	iCount=0;
sl@0
    33
//	iLength=0;
sl@0
    34
//	iPtr=NULL;
sl@0
    35
//	iPtrE=NULL;
sl@0
    36
//	iHead=NULL;
sl@0
    37
//	iTail=NULL;
sl@0
    38
	}
sl@0
    39
sl@0
    40
EXPORT_C CCirBufBase::~CCirBufBase()
sl@0
    41
/**
sl@0
    42
Destructor.
sl@0
    43
sl@0
    44
This frees the memory allocated to the buffer.
sl@0
    45
*/
sl@0
    46
	{
sl@0
    47
sl@0
    48
	User::Free(iPtr);
sl@0
    49
	}
sl@0
    50
sl@0
    51
EXPORT_C void CCirBufBase::SetLengthL(TInt aLength)
sl@0
    52
/**
sl@0
    53
Sets the maximum capacity of this circular buffer, and resets all
sl@0
    54
of the buffer pointers.
sl@0
    55
sl@0
    56
The capacity is the maximum number of elements that the buffer can hold.
sl@0
    57
sl@0
    58
The buffer itself is allocated as a result of a call to this function. If 
sl@0
    59
the function has previously been called, then any existing buffer is freed and 
sl@0
    60
any information in it is lost.
sl@0
    61
sl@0
    62
Notes:
sl@0
    63
sl@0
    64
1. This function must be called before attempting to add any objects to
sl@0
    65
   the buffer.
sl@0
    66
sl@0
    67
2. The function can leave if there is insufficient memory available to
sl@0
    68
   allocate the buffer.
sl@0
    69
sl@0
    70
@param aLength The maximum capacity of the circular buffer.
sl@0
    71
sl@0
    72
@panic E32USER-CBase 73, if aLength is zero or negative. 
sl@0
    73
*/
sl@0
    74
	{
sl@0
    75
sl@0
    76
	__ASSERT_ALWAYS(aLength>0,Panic(ECircSetLengthNegativeOrZero));
sl@0
    77
	iPtr=(TUint8 *)User::ReAllocL(iPtr,aLength*iSize);
sl@0
    78
	iPtrE=iPtr+(aLength*iSize);
sl@0
    79
	iHead=iTail=iPtr;
sl@0
    80
	iLength=aLength;
sl@0
    81
	iCount=0;
sl@0
    82
	}
sl@0
    83
sl@0
    84
EXPORT_C void CCirBufBase::Reset()
sl@0
    85
/**
sl@0
    86
Empties the buffer.
sl@0
    87
*/
sl@0
    88
	{
sl@0
    89
sl@0
    90
	iHead=iTail=iPtr;
sl@0
    91
	iCount=0;
sl@0
    92
#if defined(_DEBUG)
sl@0
    93
	Mem::FillZ(iPtr,iLength*iSize);
sl@0
    94
#endif
sl@0
    95
	}
sl@0
    96
sl@0
    97
EXPORT_C TInt CCirBufBase::DoAdd(const TUint8 *aPtr)
sl@0
    98
/**
sl@0
    99
Implementation function for CCirBuf::Add(const T*)
sl@0
   100
sl@0
   101
Adds a single object to the circular buffer, but only if there is
sl@0
   102
space available.
sl@0
   103
sl@0
   104
@param aPtr A pointer to the object to be added.
sl@0
   105
sl@0
   106
@return 1 if the object is successfully added. 0 if the object cannot be added 
sl@0
   107
        because the circular buffer is full.
sl@0
   108
sl@0
   109
@panic E32USER-CBase 74, if a call to CCirBufBase::SetLengthL() has not been
sl@0
   110
                         made before calling this function.
sl@0
   111
sl@0
   112
@see CCirBuf::Add
sl@0
   113
@see CCirBufBase::SetLengthL
sl@0
   114
*/
sl@0
   115
	{
sl@0
   116
sl@0
   117
	__ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
sl@0
   118
	if (iCount>=iLength)
sl@0
   119
		return(KErrNone);
sl@0
   120
	Mem::Copy(iHead,aPtr,iSize);
sl@0
   121
	iCount++;
sl@0
   122
	iHead+=iSize;
sl@0
   123
	if (iHead>=iPtrE)
sl@0
   124
		iHead=iPtr;
sl@0
   125
	return(1);
sl@0
   126
	}
sl@0
   127
sl@0
   128
EXPORT_C TInt CCirBufBase::DoAdd(const TUint8 *aPtr,TInt aCount)
sl@0
   129
/**
sl@0
   130
Implementation function for CCirBuf::Add(const T*,TInt)
sl@0
   131
sl@0
   132
Adds multiple objects to the circular buffer, but only if there is
sl@0
   133
space available.
sl@0
   134
sl@0
   135
@param aPtr   A pointer to a set of contiguous objects to be added.
sl@0
   136
sl@0
   137
@param aCount The number of objects to be added.
sl@0
   138
sl@0
   139
@return The number of objects successfully added to the buffer. This value 
sl@0
   140
        may be less than the number requested and can range from 0 to aCount. 
sl@0
   141
sl@0
   142
@panic E32USER-CBase 74, if a call to CCirBufBase::SetLengthL() has not been
sl@0
   143
                         made before calling this function.
sl@0
   144
@panic E32USER-CBase 75, if aCount is not a positive value. 
sl@0
   145
sl@0
   146
@see CCirBuf::Add
sl@0
   147
@see CCirBufBase::SetLengthL
sl@0
   148
*/
sl@0
   149
	{
sl@0
   150
sl@0
   151
	__ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
sl@0
   152
	__ASSERT_ALWAYS(aCount>0,Panic(ECircAddCountNegative));
sl@0
   153
	TInt rem=iLength-iCount;
sl@0
   154
	if (rem==0)
sl@0
   155
		return(0);
sl@0
   156
	aCount=Min(aCount,rem);
sl@0
   157
	rem=(iPtrE-iHead)/iSize;
sl@0
   158
	if (aCount<=rem)
sl@0
   159
		iHead=Mem::Copy(iHead,aPtr,aCount*iSize);
sl@0
   160
	else
sl@0
   161
		{
sl@0
   162
		TInt len=(rem*iSize);
sl@0
   163
		Mem::Copy(iHead,aPtr,len);
sl@0
   164
		iHead=Mem::Copy(iPtr,aPtr+len,(aCount*iSize)-len);
sl@0
   165
		}
sl@0
   166
	if (iHead>=iPtrE)
sl@0
   167
		iHead=iPtr;
sl@0
   168
	iCount+=aCount;
sl@0
   169
	return(aCount);
sl@0
   170
	}
sl@0
   171
sl@0
   172
EXPORT_C TInt CCirBufBase::DoRemove(TUint8 *aPtr)
sl@0
   173
/**
sl@0
   174
Implementation function for CCirBuf::Remove(T*)
sl@0
   175
sl@0
   176
Removes a single object from the circular buffer, but only if there are
sl@0
   177
objects in the buffer.
sl@0
   178
sl@0
   179
A binary copy of the object is made to aPtr.
sl@0
   180
sl@0
   181
@param aPtr A pointer to a location supplied by the caller.
sl@0
   182
sl@0
   183
@return 1 if an object is successfully removed. 0 if an object cannot be removed 
sl@0
   184
        because the circular buffer is empty.
sl@0
   185
sl@0
   186
@see CCirBuf::Remove
sl@0
   187
*/
sl@0
   188
	{
sl@0
   189
sl@0
   190
	if (iCount==0)
sl@0
   191
		return(0);
sl@0
   192
	Mem::Copy(aPtr,iTail,iSize);
sl@0
   193
	iTail+=iSize;
sl@0
   194
	if (iTail>=iPtrE)
sl@0
   195
		iTail=iPtr;
sl@0
   196
	iCount--;
sl@0
   197
	return(1);
sl@0
   198
	}
sl@0
   199
sl@0
   200
EXPORT_C TInt CCirBufBase::DoRemove(TUint8 *aPtr,TInt aCount)
sl@0
   201
/**
sl@0
   202
Implementation function for CCirBuf::Remove(T*,TInt)
sl@0
   203
sl@0
   204
Attempts to remove aCount objects from the circular buffer, but only if there
sl@0
   205
are objects in the buffer.
sl@0
   206
sl@0
   207
A binary copy of the objects is made to aPtr.
sl@0
   208
sl@0
   209
@param aPtr   A pointer to a location supplied by the caller capable of
sl@0
   210
              holding aCount objects.
sl@0
   211
sl@0
   212
@param aCount The number of objects to be removed from the circular buffer.
sl@0
   213
sl@0
   214
@return The number of objects successfully removed from the buffer. This value
sl@0
   215
        may be less than the number requested, and can range from 0 to aCount.
sl@0
   216
sl@0
   217
@panic E32USER-CBase 76, if aCount is not a positive value.
sl@0
   218
sl@0
   219
@see CCirBuf::Remove
sl@0
   220
*/
sl@0
   221
	{
sl@0
   222
sl@0
   223
	if (iCount==0)
sl@0
   224
		return(0);
sl@0
   225
	__ASSERT_ALWAYS(aCount>0,Panic(ECircRemoveCountNegative));
sl@0
   226
	aCount=Min(aCount,iCount);
sl@0
   227
	TInt rem=(iPtrE-iTail)/iSize;
sl@0
   228
	TInt len=rem*iSize;
sl@0
   229
	if (aCount<=rem)
sl@0
   230
		{
sl@0
   231
		Mem::Copy(aPtr,iTail,aCount*iSize);
sl@0
   232
		iTail+=aCount*iSize;
sl@0
   233
		}
sl@0
   234
	else
sl@0
   235
		{
sl@0
   236
		Mem::Copy(aPtr,iTail,len);
sl@0
   237
		rem=(aCount*iSize)-len;
sl@0
   238
		Mem::Copy(aPtr+len,iPtr,rem);
sl@0
   239
		iTail=iPtr+rem;
sl@0
   240
		}
sl@0
   241
	if (iTail>=iPtrE)
sl@0
   242
		iTail=iPtr;
sl@0
   243
	iCount-=aCount;
sl@0
   244
	return(aCount);
sl@0
   245
	}
sl@0
   246
sl@0
   247
EXPORT_C CCirBuffer::CCirBuffer()
sl@0
   248
	: CCirBuf<TUint8>()
sl@0
   249
/**
sl@0
   250
Default C++ constructor.
sl@0
   251
*/
sl@0
   252
	{}
sl@0
   253
sl@0
   254
EXPORT_C CCirBuffer::~CCirBuffer()
sl@0
   255
/**
sl@0
   256
Destructor
sl@0
   257
*/
sl@0
   258
	{
sl@0
   259
	}
sl@0
   260
sl@0
   261
EXPORT_C TInt CCirBuffer::Get()
sl@0
   262
/**
sl@0
   263
Removes an unsigned 8-bit integer value from the circular buffer and returns
sl@0
   264
its value. 
sl@0
   265
sl@0
   266
The returned TUint8 is promoted to a TInt to allow for negative error codes, 
sl@0
   267
e.g. KErrGeneral.
sl@0
   268
sl@0
   269
@return The unsigned 8-bit integer value removed from the circular buffer.
sl@0
   270
        KErrGeneral, if the circular buffer is empty.
sl@0
   271
*/
sl@0
   272
	{
sl@0
   273
sl@0
   274
	if (iCount==0)
sl@0
   275
		return(KErrGeneral);
sl@0
   276
	TUint8 *p=iTail++;
sl@0
   277
	if (iTail>=iPtrE)
sl@0
   278
		iTail=iPtr;
sl@0
   279
	iCount--;
sl@0
   280
	return(*p);
sl@0
   281
	}
sl@0
   282
sl@0
   283
EXPORT_C TInt CCirBuffer::Put(TInt aVal)
sl@0
   284
/**
sl@0
   285
Adds an unsigned 8-bit integer value in the range 0 to 255 to the circular buffer.
sl@0
   286
sl@0
   287
If the specified integer is outside the range 0 to 255,
sl@0
   288
this method discards all but the lowest 8 bits and treats those
sl@0
   289
as the unsigned integer to store.
sl@0
   290
For example, specifying -2 (or 510, or -258, etc) will result in 254 being stored,
sl@0
   291
and therefore in 254 being returned by the Get() method
sl@0
   292
(and not the number passed to Put()).
sl@0
   293
sl@0
   294
@param aVal The unsigned 8-bit integer value to be added.
sl@0
   295
@return KErrNone, if the unsigned integer is successfully added.
sl@0
   296
        KErrGeneral, if the unsigned integer cannnot be added because
sl@0
   297
        the circular buffer is full.
sl@0
   298
sl@0
   299
@see CCirBuffer::Get()
sl@0
   300
*/
sl@0
   301
	{
sl@0
   302
sl@0
   303
	__ASSERT_ALWAYS(iPtr!=NULL,Panic(ECircNoBufferAllocated));
sl@0
   304
	if (iCount>=iLength)
sl@0
   305
		return(KErrGeneral);
sl@0
   306
	*iHead++=(TUint8)aVal;
sl@0
   307
	if (iHead>=iPtrE)
sl@0
   308
		iHead=iPtr;
sl@0
   309
	iCount++;
sl@0
   310
	return(KErrNone);
sl@0
   311
	}
sl@0
   312