genericopenlibs/cppstdlib/src/operator_new.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2006-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 // Name        : operator_new.cpp
       
    15 // Part of     : libstdcpp
       
    16 // Adaptation layer to get locale functionality.
       
    17 // Version     : 
       
    18 // This material, including documentation and any related 
       
    19 // computer programs, is protected by copyright controlled by 
       
    20 // Nokia Corporation. All rights are reserved. Copying, 
       
    21 // including reproducing, storing, adapting or translating, any 
       
    22 // or all of this material requires the prior written consent of 
       
    23 // Nokia Corporation. This material also contains confidential 
       
    24 // information which may not be disclosed to others without the 
       
    25 // prior written consent of Nokia Corporation.
       
    26 //
       
    27 
       
    28 
       
    29 #include "new"
       
    30 #include <e32std.h>
       
    31 
       
    32 
       
    33 #ifdef __SYMBIAN_STDCPP_SUPPORT__ 
       
    34 
       
    35 EXPORT_C void* operator new(std::size_t a_size) __THROW(std::bad_alloc)
       
    36 	{
       
    37 	for (;;)
       
    38 		{
       
    39 		void* p = User::Alloc(a_size);
       
    40 
       
    41 		if (p)
       
    42 			{
       
    43 			return p; 
       
    44 			}
       
    45 
       
    46 		std::new_handler nh_func ;
       
    47 		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
       
    48 		nh_func = (ptr)?(*ptr):NULL;
       
    49 
       
    50 		if (nh_func)
       
    51 			{
       
    52 			nh_func();
       
    53 			}
       
    54 		else
       
    55 			{
       
    56 			__THROW(std::bad_alloc());
       
    57 			}
       
    58 		}
       
    59 	}
       
    60 
       
    61 EXPORT_C void* operator new[](std::size_t a_size) __THROW(std::bad_alloc)
       
    62 	{
       
    63 	return ::operator new(a_size);
       
    64 	}
       
    65 
       
    66 EXPORT_C void* operator new(std::size_t a_size, const std::nothrow_t&) __NO_THROW
       
    67 	{
       
    68 	for (;;)
       
    69 		{
       
    70 		void* p = User::Alloc(a_size);
       
    71 
       
    72 		if (p)
       
    73 			{
       
    74 			return p;
       
    75 			}
       
    76 
       
    77 		std::new_handler nh_func ;
       
    78 		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
       
    79 		nh_func = (ptr)?(*ptr):NULL;
       
    80 
       
    81 		if (nh_func)
       
    82 			{
       
    83 			try
       
    84 				{
       
    85 				nh_func();
       
    86 				}
       
    87 			catch (...)
       
    88 				{
       
    89 				return 0;
       
    90 				}
       
    91 			}
       
    92 		else
       
    93 			{
       
    94 			return 0;
       
    95 			}
       
    96 		}
       
    97 	}
       
    98 
       
    99 EXPORT_C void* operator new[](std::size_t a_size, const std::nothrow_t&) __NO_THROW
       
   100 	{
       
   101 	return ::operator new(a_size, std::nothrow);
       
   102 	}
       
   103 
       
   104 // Symbian-specific addition.
       
   105 EXPORT_C void* operator new(std::size_t a_size, std::size_t a_extra_size) __NO_THROW
       
   106 	{
       
   107 	return User::Alloc(a_size + a_extra_size);
       
   108 	}
       
   109 
       
   110 #ifdef __EABI__
       
   111 //TODO: MOVE to another file
       
   112 EXPORT_C std::bad_alloc::bad_alloc() __NO_THROW 
       
   113 	{}
       
   114 EXPORT_C std::bad_alloc::bad_alloc(const std::bad_alloc&) __NO_THROW
       
   115 	{}
       
   116 EXPORT_C std::bad_alloc& std::bad_alloc::operator=(const std::bad_alloc&) __NO_THROW
       
   117 	{ return *this;}
       
   118 EXPORT_C std::bad_alloc::~bad_alloc() __NO_THROW
       
   119 	{}
       
   120 EXPORT_C const char* std::bad_alloc::what() const __NO_THROW
       
   121     { return "bad_alloc";}
       
   122 #endif  // __EABI__
       
   123 
       
   124 #endif // __SYMBIAN_STDCPP_SUPPORT__