messagingfw/msgsrvnstore/server/src/CMsvChangeBuffer.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2003-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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include "MSVPANIC.H"
       
    18 #include "CMsvChangeBuffer.h"
       
    19 
       
    20 
       
    21 //**********************************
       
    22 //CMsvChangeRecord
       
    23 //**********************************
       
    24 
       
    25 CMsvChangeRecord* CMsvChangeRecord::NewL(const TDesC8& aChange, TUint32 aNotifySequence)
       
    26 	{
       
    27 	CMsvChangeRecord* self = new(ELeave)CMsvChangeRecord(aNotifySequence);
       
    28 	CleanupStack::PushL(self);
       
    29 	self->ConstructL(aChange);
       
    30 	CleanupStack::Pop();
       
    31 	return self;
       
    32 	}
       
    33 
       
    34 void CMsvChangeRecord::ConstructL(const TDesC8& aChange)
       
    35 	{
       
    36 	iChange = HBufC8::NewL(aChange.Length());
       
    37 	iChange->Des() = aChange;
       
    38 	}
       
    39 
       
    40 CMsvChangeRecord::~CMsvChangeRecord()
       
    41 	{
       
    42 	delete iChange;
       
    43 	}
       
    44 
       
    45 CMsvChangeRecord::CMsvChangeRecord(TUint32 aNotifySequence)
       
    46 : iNotifySequence(aNotifySequence)
       
    47 	{
       
    48 	}
       
    49 
       
    50 //**********************************
       
    51 //CMsvChangeBuffer
       
    52 //**********************************
       
    53 
       
    54 CMsvChangeBuffer* CMsvChangeBuffer::NewL()
       
    55 	{
       
    56 	CMsvChangeBuffer* self = new(ELeave) CMsvChangeBuffer();
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL();
       
    59 	CleanupStack::Pop();
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 CMsvChangeBuffer::CMsvChangeBuffer()
       
    64 	{
       
    65 	__DECLARE_NAME(_S("CMsvChangeBuffer"));
       
    66 	}
       
    67 
       
    68 CMsvChangeBuffer::~CMsvChangeBuffer()
       
    69 	{
       
    70 	if (iArray)
       
    71 		{
       
    72 		iArray->ResetAndDestroy();
       
    73 		delete iArray;
       
    74 		}
       
    75 	}
       
    76 
       
    77 void CMsvChangeBuffer::ConstructL()
       
    78 	{
       
    79 	iArray = new (ELeave) CArrayPtrFlat<CMsvChangeRecord>(KMsvChangeBufferGranularity);
       
    80 	CMsvChangeRecord* ptr = NULL;
       
    81 	iArray->ResizeL(KMsvChangeBufferSize, ptr);
       
    82 	}
       
    83 
       
    84 void CMsvChangeBuffer::InL(const TDesC8& aChange)
       
    85 //
       
    86 // Inserts an entry into the FIFO buffer.
       
    87 // 
       
    88 	{
       
    89 	iNotifySequence++;
       
    90 	CMsvChangeRecord* record = CMsvChangeRecord::NewL(aChange, iNotifySequence);
       
    91 	iArray->At(iInPos++) = record;
       
    92 	
       
    93 	// Check if we've run past the end of the array.  If so, wrap around.
       
    94 	if (iInPos==iArray->Count())
       
    95 		iInPos=0;
       
    96 	
       
    97 	// Check if we've run out of space in the array
       
    98 	if (iInPos==iOutPos)
       
    99 		{
       
   100 		// Attempt to grow the array
       
   101 		TRAPD(ret, GrowArrayL());
       
   102 		if (ret != KErrNone)
       
   103 			// Error - possibly out of memory.
       
   104 			// Delete the last entry and rollback the iInPos variable to ensure
       
   105 			//  class is in a valid state.
       
   106 			{
       
   107 			delete record;
       
   108 			// If iInPos is not 0, subtract 1 from it,
       
   109 			//  else set it back to Count-1
       
   110 			iInPos = (iInPos ? iInPos-1 : iArray->Count()-1);
       
   111 			User::Leave(ret);
       
   112 			}
       
   113 		}
       
   114 	}
       
   115 
       
   116 const TDesC8& CMsvChangeBuffer::Next(TUint32& aNotifySequence)
       
   117 //
       
   118 // Returns the next entry from the FIFO buffer
       
   119 //
       
   120 	{
       
   121 	__ASSERT_ALWAYS(iOutPos!=iInPos,PanicServer(EMsvChangeBufferEmpty));
       
   122 	CMsvChangeRecord* record = iArray->At(iOutPos);
       
   123 	aNotifySequence = record->NotifySequence();
       
   124 	return record->Change();
       
   125 	}
       
   126 
       
   127 
       
   128 void CMsvChangeBuffer::Out()
       
   129 //
       
   130 // Removes the next entry from the queue
       
   131 //
       
   132 	{
       
   133 	__ASSERT_ALWAYS(iOutPos!=iInPos,PanicServer(EMsvChangeBufferEmpty));
       
   134 	delete iArray->At(iOutPos);
       
   135 	iArray->At(iOutPos++) = NULL;
       
   136 	if (iOutPos==iArray->Count())
       
   137 		{
       
   138 		iOutPos=0;
       
   139 		ShrinkArray();
       
   140 		}
       
   141 	}
       
   142 
       
   143 void CMsvChangeBuffer::ShrinkArray()
       
   144 //
       
   145 // Shrinks the array if possible
       
   146 //
       
   147 	{
       
   148 	while ((iArray->Count()-iInPos)>KMsvChangeBufferGranularity)
       
   149 		iArray->Delete(iInPos,KMsvChangeBufferGranularity);
       
   150 	}
       
   151 	
       
   152 void CMsvChangeBuffer::GrowArrayL()
       
   153 //
       
   154 // Grows the array
       
   155 // Can leave with OOM
       
   156 //
       
   157 	{
       
   158 	CMsvChangeRecord* ptr=NULL;
       
   159 	iArray->InsertL(iOutPos, ptr, KMsvChangeBufferGranularity);
       
   160 	iOutPos+=KMsvChangeBufferGranularity;
       
   161 	}
       
   162