persistentstorage/sql/SRC/Common/SqlBufFlat.cpp
branchRCL_3
changeset 24 cc28652e0254
parent 23 26645d81f48d
equal deleted inserted replaced
23:26645d81f48d 24:cc28652e0254
     1 // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
    15 
    16 #include "SqlUtil.h"
    16 #include "SqlUtil.h"
    17 #include "SqlBufFlat.h"
    17 #include "SqlBufFlat.h"
    18 #include "OstTraceDefinitions.h"
       
    19 
    18 
    20 /**
    19 /**
    21 Sets the flat buffer pointer to NULL
    20 Sets the flat buffer pointer to NULL
    22 */
    21 */
    23 RSqlBufFlat::RSqlBufFlat() :
    22 RSqlBufFlat::RSqlBufFlat() :
    44 @return KErrNone, The operation has completed successfully;
    43 @return KErrNone, The operation has completed successfully;
    45 		KErrNoMemory, Out of memory condition has occured.
    44 		KErrNoMemory, Out of memory condition has occured.
    46 */
    45 */
    47 TInt RSqlBufFlat::SetCount(TInt aCount)
    46 TInt RSqlBufFlat::SetCount(TInt aCount)
    48 	{
    47 	{
    49 	__ASSERT_DEBUG(aCount >= 0, __SQLPANIC(ESqlPanicBadArgument));
    48 	__SQLASSERT(aCount >= 0, ESqlPanicBadArgument);
    50 	TInt headerSize = sizeof(RSqlBufFlat::TCell) * aCount;
    49 	TInt headerSize = sizeof(RSqlBufFlat::TCell) * aCount;
    51 	TInt newSize = headerSize + sizeof(RSqlBufFlat::TBufFlat);
    50 	TInt newSize = headerSize + sizeof(RSqlBufFlat::TBufFlat);
    52 	if(DoReAlloc(newSize) != KErrNone)
    51 	if(DoReAlloc(newSize) != KErrNone)
    53 		{
    52 		{
    54 		return KErrNoMemory;	
    53 		return KErrNoMemory;	
   109 	if(iMaxSize > KBufLimit)
   108 	if(iMaxSize > KBufLimit)
   110 		{
   109 		{
   111 		iMaxSize = KBufLimit - 1;  //to force the reallocation
   110 		iMaxSize = KBufLimit - 1;  //to force the reallocation
   112 		(void)DoReAlloc(KBufLimit);//User::ReAlloc() does not fail if the new requested size is less than the current block size
   111 		(void)DoReAlloc(KBufLimit);//User::ReAlloc() does not fail if the new requested size is less than the current block size
   113 		}
   112 		}
   114 	__ASSERT_DEBUG(oldBuf == iBuf, __SQLPANIC(ESqlPanicInternalError));
   113 	__SQLASSERT(oldBuf == iBuf, ESqlPanicInternalError);
   115 	SQLFLATBUF_INVARIANT();
   114 	SQLFLATBUF_INVARIANT();
   116 	}
   115 	}
   117 
   116 
   118 /**
   117 /**
   119 Cleans up the flat buffer content but does not free the occupied memory.
   118 Cleans up the flat buffer content but does not free the occupied memory.
   155 		KErrNoMemory, Out of memory condition has occured.
   154 		KErrNoMemory, Out of memory condition has occured.
   156 */
   155 */
   157 TInt RSqlBufFlat::SetField(TInt aIndex, TInt aType, const void* aData, TInt aDataLength)
   156 TInt RSqlBufFlat::SetField(TInt aIndex, TInt aType, const void* aData, TInt aDataLength)
   158 	{
   157 	{
   159 	SQLFLATBUF_INVARIANT();
   158 	SQLFLATBUF_INVARIANT();
   160 	__ASSERT_DEBUG((TUint)aIndex < iBuf->iCount, __SQLPANIC(ESqlPanicBadArgument));
   159 	__SQLASSERT((TUint)aIndex < iBuf->iCount, ESqlPanicBadArgument);
   161 	__ASSERT_DEBUG((TUint)aType < RSqlBufFlat::EMaxType, __SQLPANIC(ESqlPanicBadArgument));
   160 	__SQLASSERT((TUint)aType < RSqlBufFlat::EMaxType, ESqlPanicBadArgument);
   162 	__ASSERT_DEBUG((TUint)aDataLength < RSqlBufFlat::EMaxLength, __SQLPANIC(ESqlPanicBadArgument));
   161 	__SQLASSERT((TUint)aDataLength < RSqlBufFlat::EMaxLength, ESqlPanicBadArgument);
   163 	if(aData)						//field value "present"
   162 	if(aData)						//field value "present"
   164 		{
   163 		{
   165 		__ASSERT_DEBUG(aDataLength >= 0, __SQLPANIC(ESqlPanicBadArgument));
   164 		__SQLASSERT(aDataLength >= 0, ESqlPanicBadArgument);
   166 		if(aDataLength > 0)
   165 		if(aDataLength > 0)
   167 			{
   166 			{
   168 			if(Reserve(aDataLength) != KErrNone)
   167 			if(Reserve(aDataLength) != KErrNone)
   169 				{
   168 				{
   170 				return KErrNoMemory;	
   169 				return KErrNoMemory;	
   194 - "Not present";
   193 - "Not present";
   195 */	
   194 */	
   196 void RSqlBufFlat::DoInit()
   195 void RSqlBufFlat::DoInit()
   197 	{
   196 	{
   198 	TBufFlat& buf = *iBuf;
   197 	TBufFlat& buf = *iBuf;
   199 	__ASSERT_DEBUG(buf.iCount >= 0, __SQLPANIC(ESqlPanicInternalError));
   198 	__SQLASSERT(buf.iCount >= 0, ESqlPanicInternalError);
   200 	__ASSERT_DEBUG(buf.iSize <= iMaxSize, __SQLPANIC(ESqlPanicInternalError));
   199 	__SQLASSERT(buf.iSize <= iMaxSize, ESqlPanicInternalError);
   201 	__ASSERT_DEBUG(buf.iHeaderSize == sizeof(RSqlBufFlat::TCell) * buf.iCount, __SQLPANIC(ESqlPanicInternalError));
   200 	__SQLASSERT(buf.iHeaderSize == sizeof(RSqlBufFlat::TCell) * buf.iCount, ESqlPanicInternalError);
   202 	if(buf.iHeaderSize > 0)
   201 	if(buf.iHeaderSize > 0)
   203 		{
   202 		{
   204 		Mem::FillZ(Header(), buf.iHeaderSize);
   203 		Mem::FillZ(Header(), buf.iHeaderSize);
   205 		}
   204 		}
   206 	}
   205 	}
   243 @param aDataLength Field data length, may be 0
   242 @param aDataLength Field data length, may be 0
   244 */
   243 */
   245 void RSqlBufFlat::DoSet(TInt aIndex, TInt aType, const void* aData, TInt aDataLength)
   244 void RSqlBufFlat::DoSet(TInt aIndex, TInt aType, const void* aData, TInt aDataLength)
   246 	{
   245 	{
   247 	TBufFlat& buf = *iBuf;
   246 	TBufFlat& buf = *iBuf;
   248 	__ASSERT_DEBUG((TUint)aDataLength < RSqlBufFlat::EMaxLength, __SQLPANIC(ESqlPanicBadArgument));
   247 	__SQLASSERT((TUint)aDataLength < RSqlBufFlat::EMaxLength, ESqlPanicBadArgument);
   249 	__ASSERT_DEBUG(aDataLength > 0 ? aData != NULL : ETrue, __SQLPANIC(ESqlPanicBadArgument));
   248 	__SQLASSERT(aDataLength > 0 ? aData != NULL : ETrue, ESqlPanicBadArgument);
   250 	__ASSERT_DEBUG(aDataLength <= (iMaxSize - buf.iSize), __SQLPANIC(ESqlPanicInternalError));
   249 	__SQLASSERT(aDataLength <= (iMaxSize - buf.iSize), ESqlPanicInternalError);
   251 	__ASSERT_DEBUG(::IsAligned8(buf.iSize), __SQLPANIC(ESqlPanicInternalError));
   250 	__SQLASSERT(::IsAligned8(buf.iSize), ESqlPanicInternalError);
   252 	RSqlBufFlat::TCell& cell = *(Header() + aIndex);
   251 	RSqlBufFlat::TCell& cell = *(Header() + aIndex);
   253 	cell.iBits = (TUint)(((TUint)aType << RSqlBufFlat::EWidthLen) | (TUint)aDataLength);
   252 	cell.iBits = (TUint)(((TUint)aType << RSqlBufFlat::EWidthLen) | (TUint)aDataLength);
   254 	cell.iPos = 1;	//not 0, because 0 means "not present"
   253 	cell.iPos = 1;	//not 0, because 0 means "not present"
   255 	if(aDataLength > 0)	//for fields with length > 0 set the data and reinitalize cell.iPos
   254 	if(aDataLength > 0)	//for fields with length > 0 set the data and reinitalize cell.iPos
   256 		{
   255 		{
   290 /**
   289 /**
   291 Panics in _DEBUG mode if the flat buffer content is inconsistent.
   290 Panics in _DEBUG mode if the flat buffer content is inconsistent.
   292 */
   291 */
   293 void RSqlBufFlat::Invariant() const
   292 void RSqlBufFlat::Invariant() const
   294 	{
   293 	{
   295 	__ASSERT_DEBUG(iBuf != NULL, __SQLPANIC(ESqlPanicInternalError));
   294 	__SQLASSERT(iBuf != NULL, ESqlPanicInternalError);
   296 	const TBufFlat& buf = *iBuf;
   295 	const TBufFlat& buf = *iBuf;
   297 	__ASSERT_DEBUG(buf.iCount >= 0, __SQLPANIC(ESqlPanicInternalError));
   296 	__SQLASSERT(buf.iCount >= 0, ESqlPanicInternalError);
   298 	__ASSERT_DEBUG(buf.iHeaderSize == sizeof(RSqlBufFlat::TCell) * buf.iCount, __SQLPANIC(ESqlPanicInternalError));
   297 	__SQLASSERT(buf.iHeaderSize == sizeof(RSqlBufFlat::TCell) * buf.iCount, ESqlPanicInternalError);
   299 	__ASSERT_DEBUG(::IsAligned8(buf.iSize), __SQLPANIC(ESqlPanicInternalError));
   298 	__SQLASSERT(::IsAligned8(buf.iSize), ESqlPanicInternalError);
   300 	__ASSERT_DEBUG(buf.iSize >= buf.iHeaderSize + sizeof(RSqlBufFlat::TBufFlat), __SQLPANIC(ESqlPanicInternalError));
   299 	__SQLASSERT(buf.iSize >= buf.iHeaderSize + sizeof(RSqlBufFlat::TBufFlat), ESqlPanicInternalError);
   301 	__ASSERT_DEBUG(buf.iSize <= iMaxSize, __SQLPANIC(ESqlPanicInternalError));
   300 	__SQLASSERT(buf.iSize <= iMaxSize, ESqlPanicInternalError);
   302 	__ASSERT_DEBUG(buf.iSize <= User::AllocLen(iBuf), __SQLPANIC(ESqlPanicInternalError));	
   301  	__SQLASSERT(buf.iSize <= User::AllocLen(iBuf), ESqlPanicInternalError);	
   303 	for(TInt i=0;i<(TInt)buf.iCount;++i)
   302 	for(TInt i=0;i<(TInt)buf.iCount;++i)
   304 		{
   303 		{
   305 		const RSqlBufFlat::TCell& cell = *((reinterpret_cast <const RSqlBufFlat::TCell*> (iBuf + 1)) + i);
   304 		const RSqlBufFlat::TCell& cell = *((reinterpret_cast <const RSqlBufFlat::TCell*> (iBuf + 1)) + i);
   306 		__ASSERT_DEBUG(cell.Type() < RSqlBufFlat::EMaxType, __SQLPANIC(ESqlPanicInternalError));
   305 		__SQLASSERT(cell.Type() < RSqlBufFlat::EMaxType, ESqlPanicInternalError);
   307 		if(cell.Size() > 0 && cell.iPos >= buf.iHeaderSize) //only for present fields with length > 0
   306 		if(cell.Size() > 0 && cell.iPos >= buf.iHeaderSize) //only for present fields with length > 0
   308 			{
   307 			{
   309 			__ASSERT_DEBUG((TUint)cell.Size() <= buf.iSize, __SQLPANIC(ESqlPanicInternalError));
   308 			__SQLASSERT((TUint)cell.Size() <= buf.iSize, ESqlPanicInternalError);
   310 			__ASSERT_DEBUG(cell.iPos < (buf.iSize - sizeof(RSqlBufFlat::TBufFlat)), __SQLPANIC(ESqlPanicInternalError));
   309 			__SQLASSERT(cell.iPos < (buf.iSize - sizeof(RSqlBufFlat::TBufFlat)), ESqlPanicInternalError);
   311 			TUint64 val = *(TUint64*)(reinterpret_cast <TUint8*> (iBuf) + cell.iPos + sizeof(RSqlBufFlat::TBufFlat) - sizeof(KSqlBufFlatMagicValue));
   310 			TUint64 val = *(TUint64*)(reinterpret_cast <TUint8*> (iBuf) + cell.iPos + sizeof(RSqlBufFlat::TBufFlat) - sizeof(KSqlBufFlatMagicValue));
   312 			__ASSERT_DEBUG(val == KSqlBufFlatMagicValue, __SQLPANIC(ESqlPanicInternalError));
   311 			__SQLASSERT(val == KSqlBufFlatMagicValue, ESqlPanicInternalError);
   313 			}
   312 			}
   314 		}
   313 		}
   315 	}
   314 	}
   316 #endif//_DEBUG
   315 #endif//_DEBUG