pimappservices/calendar/shared/src/agmallocator.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-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 "agmallocator.h"
       
    17 #include "agmsimpleentry.h"
       
    18 #include "agmpanic.h"
       
    19 
       
    20 // ----------------------------------- CAgnSimpleEntryAllocator -------------------
       
    21 
       
    22 EXPORT_C CAgnSimpleEntry* CAgnSimpleEntryAllocator::CreateSimpleEntryL(CCalEntry::TType aType, const CAgnSimpleEntry* aEntry)
       
    23 /** Create CAgnSimpleEntry objects
       
    24 
       
    25 @internalComponent
       
    26 */
       
    27 	{
       
    28 	CAgnSimpleEntry* entry = new (ELeave, this) CAgnSimpleEntry(aType);
       
    29 	
       
    30 	if( aEntry )
       
    31 		{
       
    32 		TRAPD(err, entry->CopySimpleEntryL(*aEntry, CCalEntry::ECopyAll));
       
    33 		if (err != KErrNone)
       
    34 			{
       
    35 			Delete(entry);
       
    36 			User::Leave(err);
       
    37 			}
       
    38 		}
       
    39 	
       
    40 	return ( entry );
       
    41 	}
       
    42 
       
    43 EXPORT_C CAgnSimpleEntryAllocator::CAgnSimpleEntryAllocator()
       
    44 	{
       
    45 	}
       
    46 
       
    47 EXPORT_C CAgnSimpleEntryAllocator::~CAgnSimpleEntryAllocator()
       
    48 /** Destructor
       
    49 
       
    50 @internalComponent
       
    51 */
       
    52 	{
       
    53 	// Cleanup up any hanging around entries
       
    54 	
       
    55 	while ( iHeadPage )
       
    56 		{
       
    57 		CAgnMemPage* next = iHeadPage;
       
    58 		
       
    59 		iHeadPage = iHeadPage->Next();
       
    60 		
       
    61 		delete next;
       
    62 		}
       
    63 	}
       
    64 
       
    65 
       
    66 CAgnSimpleEntry* CAgnSimpleEntryAllocator::NewSimpleEntryL(TUint aSize)
       
    67 /** return pointer to a CAgnSimpleEntry
       
    68 
       
    69 @internalComponent
       
    70 */
       
    71 	{
       
    72 	CAgnMemPage* availPage = iHeadPage;
       
    73 	
       
    74 	while( availPage && (availPage->IsFull() || availPage->CellSize() != aSize) )
       
    75 		{
       
    76 		availPage = availPage->Next();			
       
    77 		}
       
    78 
       
    79 	if ( ! availPage )
       
    80 		{
       
    81 		availPage = CAgnMemPage::NewL(aSize);
       
    82 		availPage->SetNext(iHeadPage);
       
    83 		iHeadPage = availPage;
       
    84 		}
       
    85 		
       
    86 	++iCount;
       
    87 	
       
    88 	return ( static_cast<CAgnSimpleEntry*>(availPage->Alloc()) );
       
    89 	}
       
    90 
       
    91 
       
    92 EXPORT_C void CAgnSimpleEntryAllocator::Delete(CAgnSimpleEntry* aPtr)
       
    93 /** Put freed CAgnSimpleEntry on free list
       
    94 
       
    95 @internalComponent
       
    96 */
       
    97 	{
       
    98 	if ( aPtr )
       
    99 		{
       
   100 		CAgnMemPage* next = iHeadPage;
       
   101 		
       
   102 		while ( next && ! next->Contains(aPtr) )
       
   103 			{
       
   104 			next = next->Next();				
       
   105 			}
       
   106 			
       
   107 		if ( next )
       
   108 			{
       
   109 			// clean up any memory allocated by the object itself
       
   110 			// aPtr is only put in the free pool when Free is called
       
   111 			
       
   112 			aPtr->~CAgnSimpleEntry();
       
   113 			
       
   114 			next->Free();
       
   115 			
       
   116 			if ( next->IsEmpty() )
       
   117 				{
       
   118 				DeleteMemPage(next);					
       
   119 				}
       
   120 			}
       
   121 
       
   122 		--iCount;
       
   123 		
       
   124 		if ( iCount == 0 )		// no objects so delete everything
       
   125 			{
       
   126 			while ( iHeadPage )
       
   127 				{
       
   128 				CAgnMemPage* next = iHeadPage;
       
   129 				
       
   130 				iHeadPage = iHeadPage->Next();
       
   131 				
       
   132 				delete next;
       
   133 				}
       
   134 			}
       
   135 		}		
       
   136 	}
       
   137 
       
   138 
       
   139 void CAgnSimpleEntryAllocator::DeleteMemPage(CAgnMemPage* aPage)
       
   140 /** Delete a page of entries
       
   141 
       
   142 @internalComponent
       
   143 */
       
   144 	{
       
   145 	__ASSERT_DEBUG(iHeadPage, Panic(EAgmNoAllocatorMemoryPages));
       
   146 
       
   147 	CAgnMemPage* prev    = NULL;
       
   148 	CAgnMemPage* current = iHeadPage;
       
   149 	CAgnMemPage* next    = iHeadPage->Next();
       
   150 
       
   151 	while ( current != aPage && next )
       
   152 		{
       
   153 		prev = current;
       
   154 		current = next;
       
   155 		next = next->Next();
       
   156 		}
       
   157 		
       
   158 	__ASSERT_DEBUG(current, Panic(EAgmAllocatorMemoryPageNotFound));
       
   159 
       
   160 	if ( current == iHeadPage )
       
   161 		{
       
   162 		iHeadPage = next;			
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		prev->SetNext(next);
       
   167 		}
       
   168 	
       
   169 	delete aPage;
       
   170 	}
       
   171 
       
   172 
       
   173 // ----------------------------------- CAgnMemPage -----------------------------
       
   174 //
       
   175 // Manages allocation and freeing of memory cells
       
   176 //
       
   177 
       
   178 CAgnMemPage* CAgnMemPage::NewL(TUint aCellSize)
       
   179 //
       
   180 // 
       
   181 //
       
   182 	{
       
   183 	CAgnMemPage* self = new (ELeave) CAgnMemPage(aCellSize);
       
   184 	
       
   185     CleanupStack::PushL(self);
       
   186 	self->ConstructL();
       
   187 	CleanupStack::Pop();
       
   188 	
       
   189 	return ( self );
       
   190 	}
       
   191 
       
   192 
       
   193 void CAgnMemPage::ConstructL()
       
   194 /** Create array of KMemPageSize memory cells
       
   195 
       
   196 @internalComponent
       
   197 */
       
   198 	{
       
   199 	const TInt KSize = iCellSize * KMemPageSize;
       
   200 	
       
   201 	iEntries = (TInt8*) User::AllocL(KSize);
       
   202 	
       
   203 	Mem::FillZ(iEntries, KSize);
       
   204 	
       
   205 	iNextAvail = iEntries;
       
   206 	}
       
   207 
       
   208 
       
   209 CAgnMemPage::~CAgnMemPage()
       
   210 /** Destructor
       
   211 
       
   212 @internalComponent
       
   213 */
       
   214 	{	
       
   215 	User::Free(iEntries);
       
   216 	}
       
   217 
       
   218 
       
   219 TAny* CAgnMemPage::Alloc()
       
   220 /** Return pointer to available memory cell or Null if none available
       
   221 
       
   222 @internalComponent
       
   223 */
       
   224 	{
       
   225 	if ( IsFull() )
       
   226 		{
       
   227 		return NULL;			
       
   228 		}
       
   229 
       
   230 	TAny* alloc = iNextAvail;
       
   231 	iNextAvail += iCellSize;
       
   232 		
       
   233 	++iCount;
       
   234 	
       
   235 	return ( alloc );
       
   236 	}
       
   237 
       
   238 
       
   239 void CAgnMemPage::Free()
       
   240 /** Free memory cell
       
   241 
       
   242 @internalComponent
       
   243 */
       
   244 	{
       
   245 	--iCount;
       
   246 	}
       
   247