kernel/eka/compsupp/symaehabi/emergency_buffer.h
changeset 266 0008ccd16016
equal deleted inserted replaced
259:57b9594f5772 266:0008ccd16016
       
     1 // Copyright (c) 2009-2010 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 "ARM EABI LICENCE.txt"
       
     5 // which accompanies this distribution, and is available
       
     6 // in kernel/eka/compsupp.
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // 
       
    15 //
       
    16 
       
    17 template<typename T, unsigned N> class TEmergencyBuffer
       
    18 	{
       
    19 public:
       
    20 	void Init();
       
    21 public:
       
    22 	void* Alloc(unsigned);
       
    23 	void* Free(void*);
       
    24 private:
       
    25 	bool iIsOccupied[N];
       
    26 	T iSlots[N];
       
    27 private:
       
    28 	TEmergencyBuffer();
       
    29 	};
       
    30 
       
    31 template<typename T, unsigned N> inline void TEmergencyBuffer<T,N>::Init()
       
    32 	{
       
    33 	for (int i=0; i < N; i++)
       
    34 		{
       
    35 		iIsOccupied[i] = 0;
       
    36 		}
       
    37 	}
       
    38 
       
    39 template<typename T, unsigned N> void* TEmergencyBuffer<T,N>::Alloc(unsigned n)
       
    40 	{
       
    41 	if ( n <= sizeof(T) )
       
    42 		{
       
    43 		for (int i=0; i < N; i++)
       
    44 			{
       
    45 			bool& isOccupied = iIsOccupied[i];
       
    46 
       
    47 			if ( ! isOccupied )
       
    48 				{
       
    49 				isOccupied = 1;
       
    50 				return &iSlots[i];
       
    51 				}
       
    52 			}
       
    53 		}
       
    54 
       
    55 	return NULL;
       
    56 	}
       
    57 
       
    58 template<typename T, unsigned N> void* TEmergencyBuffer<T,N>::Free(void* p)
       
    59 	{
       
    60 	for (int i=0; i < N; i++)
       
    61 		{
       
    62 		const void* bp = &iSlots[i];
       
    63 		bool& isOccupied = iIsOccupied[i];
       
    64 
       
    65 		if ( bp == p && isOccupied )
       
    66 			{
       
    67 			isOccupied = 0;
       
    68 			return p;
       
    69 			}
       
    70 		}
       
    71 
       
    72 	return NULL;
       
    73 	}
       
    74 
       
    75