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