persistentstorage/store/UPAGE/UP_PAGE.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1998-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 "UP_STD.H"
       
    17 
       
    18 #define UNUSED_VAR(a) a = a
       
    19 
       
    20 const TInt KMemPoolGranularity=16;
       
    21 
       
    22 EXPORT_C void MPagePool::PushL()
       
    23 /** Pushes this object onto the cleanup stack. */
       
    24 	{
       
    25 	CleanupStack::PushL(TCleanupItem(REINTERPRET_CAST(TCleanupOperation,AcquireL()),this));
       
    26 		// platform dependency
       
    27 	}
       
    28 
       
    29 EXPORT_C void MPagePool::Delete(TPageRef aRef)
       
    30 /** Deletes a page, ignoring any errors.
       
    31 
       
    32 @param aRef Reference to the page to delete */
       
    33 	{
       
    34 	TRAPD(ignore,DeleteL(aRef));
       
    35     UNUSED_VAR(ignore);
       
    36 	}
       
    37 
       
    38 EXPORT_C void MPagePool::DeleteL(TPageRef aRef)
       
    39 /** Deletes a page, leaving if an error occurs.
       
    40 
       
    41 @param aRef Reference to the page to delete */
       
    42 	{
       
    43 	if (aRef!=KNullPageRef)
       
    44 		DoDeleteL(aRef);
       
    45 	}
       
    46 
       
    47 EXPORT_C CMemPagePool* CMemPagePool::NewL()
       
    48 /** Allocates and constructs a new CMemPagePool object.
       
    49 
       
    50 @return New CMemPagePool object */
       
    51 	{
       
    52 	CMemPagePool* pool=new(ELeave) CMemPagePool;
       
    53 	return pool;
       
    54 	}
       
    55 
       
    56 EXPORT_C CMemPagePool *CMemPagePool::NewLC()
       
    57 /** Allocates and constructs a new CMemPagePool object, and leaves it on the cleanup 
       
    58 stack.
       
    59 
       
    60 @return New CMemPagePool object */
       
    61 	{
       
    62 	CMemPagePool* pool=NewL();
       
    63 	CleanupStack::PushL(pool);
       
    64 	return pool;
       
    65 	}
       
    66 
       
    67 EXPORT_C CMemPagePool::CMemPagePool()
       
    68 	: iPages(KMemPoolGranularity)
       
    69 /** Default constructor. */
       
    70 	{}
       
    71 
       
    72 EXPORT_C CMemPagePool::~CMemPagePool()
       
    73 /** Destructor.
       
    74 
       
    75 On destruction, memory for all pages is freed. */
       
    76 	{
       
    77 	for (TInt ii=iPages.Count();--ii>=0;)
       
    78 		User::Free(iPages[ii]);
       
    79 	}
       
    80 
       
    81 EXPORT_C TPageAbandonFunction CMemPagePool::AcquireL()
       
    82 /** For memory-based pools, there is no need to abandon pages, so the function 
       
    83 returned does nothing. 
       
    84 
       
    85 @return Function that does nothing. */
       
    86 	{
       
    87 	return &DoAbandon;
       
    88 	}
       
    89 
       
    90 EXPORT_C TAny* CMemPagePool::AllocL()
       
    91 /** Allocates a new unassigned page. 
       
    92 
       
    93 @return Newly allocated page. */
       
    94 	{
       
    95 	TAny* page=User::AllocLC(KPoolPageSize);
       
    96 	iPages.AppendL(page);
       
    97 	CleanupStack::Pop();
       
    98 	return page;
       
    99 	}
       
   100 
       
   101 EXPORT_C TAny* CMemPagePool::LockL(TPageRef aRef)
       
   102 /** Returns a pointer to a specified page.
       
   103 
       
   104 @param aRef Reference to the page to get
       
   105 @return Page specified by aRef */
       
   106 	{
       
   107 	TAny* page=PageL(aRef);
       
   108 	if (page==NULL)
       
   109 		__LEAVE(KErrNotFound);
       
   110 //
       
   111 	return page;
       
   112 	}
       
   113 
       
   114 EXPORT_C TPageRef CMemPagePool::AssignL(const TAny* __DEBUG(aPage),TPageReclamation)
       
   115 //
       
   116 // Assign a reference to a newly allocated page. Supports only a single unassigned page at a time.
       
   117 //
       
   118 	{
       
   119 	__ASSERT_DEBUG(iPages[iPages.Count()-1]==aPage,User::Invariant());
       
   120 	return TPageRef(iPages.Count());
       
   121 	}
       
   122 
       
   123 EXPORT_C void CMemPagePool::UpdateL(const TAny*)
       
   124 //
       
   125 // Memory-based pages don't need to be updated.
       
   126 //
       
   127 	{}
       
   128 
       
   129 EXPORT_C void CMemPagePool::Unlock(const TAny*,TPageChange)
       
   130 //
       
   131 // Memory-based pages don't need to be unlocked.
       
   132 //
       
   133 	{}
       
   134 
       
   135 EXPORT_C void CMemPagePool::DoDeleteL(TPageRef aRef)
       
   136 //
       
   137 // Delete the page denoted by aRef.
       
   138 //
       
   139 	{
       
   140 	TAny*& page=PageL(aRef);
       
   141 	if (page==NULL)
       
   142 		__LEAVE(KErrNotFound);
       
   143 //
       
   144 	User::Free(page);
       
   145 	page=NULL;
       
   146 	}
       
   147 
       
   148 TAny*& CMemPagePool::PageL(TPageRef aRef)
       
   149 //
       
   150 // Return the page slot at aRef.
       
   151 //
       
   152 	{
       
   153 	TInt i=aRef.Value()-1;
       
   154 	if (i<0||i>=iPages.Count())
       
   155 		__LEAVE(KErrNotFound);
       
   156 //
       
   157 	return iPages[i];
       
   158 	}
       
   159 
       
   160 void CMemPagePool::DoAbandon(MPagePool&)
       
   161 //
       
   162 // Abandoning memory-based pages is a no-op.
       
   163 //
       
   164 	{}
       
   165