kernel/eka/euser/us_ref.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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 the License "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 // e32\euser\us_ref.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "us_std.h"
       
    19 
       
    20 
       
    21 
       
    22 /**
       
    23 Frees the memory holding the contained object.
       
    24 */
       
    25 EXPORT_C void RRefBase::Free()
       
    26 	{
       
    27 
       
    28 	if (iPtr)
       
    29 		{
       
    30 		if (--iPtr[-1]==0)
       
    31 			User::Free(iPtr-1);
       
    32 		iPtr=NULL;
       
    33 		}
       
    34 	}
       
    35 
       
    36 
       
    37 
       
    38 
       
    39 /**
       
    40 Creates a copy of an object, which is to be contained by
       
    41 this reference object.
       
    42 
       
    43 @param aPtr  A pointer to memory holding a copy of the object.
       
    44 @param aSize The size of the memory required to hold the object.
       
    45 */
       
    46 EXPORT_C void RRefBase::DoAlloc(const TAny *aPtr,TInt aSize)
       
    47 	{
       
    48 
       
    49 	__ASSERT_ALWAYS(aSize>=0,Panic(ERefAllocSizeNegative));
       
    50 	Free();
       
    51 	iPtr=(TInt *)User::Alloc(aSize+sizeof(TInt));
       
    52 	if (iPtr!=NULL)
       
    53 		{
       
    54 		iPtr++;
       
    55 		iPtr[-1]=1;
       
    56 		Mem::Copy(iPtr,aPtr,aSize);
       
    57 		}
       
    58 	}
       
    59 
       
    60 
       
    61 
       
    62 
       
    63 /**
       
    64 Creates a copy of an object, which is to be contained by
       
    65 this reference object, and leaves on error.
       
    66 
       
    67 @param aPtr  A pointer to memory holding a copy of the object.
       
    68 @param aSize The size of the memory required to hold the object.
       
    69 */
       
    70 EXPORT_C void RRefBase::DoAllocL(const TAny *aPtr,TInt aSize)
       
    71 	{
       
    72 
       
    73 	DoAlloc(aPtr,aSize);
       
    74 	User::LeaveIfNull(iPtr);
       
    75 	}
       
    76 
       
    77 
       
    78 
       
    79 
       
    80 /**
       
    81 Provides an implementation for the RRef class copy
       
    82 constructor and assignment operator.
       
    83 
       
    84 @param aRef The reference object to be copied.
       
    85 
       
    86 @see RRef
       
    87 */
       
    88 EXPORT_C void RRefBase::Copy(const RRefBase &aRef)
       
    89 	{
       
    90 
       
    91 	if (this!=(&aRef))
       
    92 		{
       
    93 		Free();
       
    94 		iPtr=aRef.iPtr;
       
    95 		iPtr[-1]++;
       
    96 		}
       
    97 	}
       
    98