os/ossrv/genericopenlibs/cppstdlib/src/stdcpp_support.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/src/stdcpp_support.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,213 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Name        : stdcpp_support.cpp
    1.18 +// Part of     : standard c++ library.
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <new>
    1.23 +#include <stdcpp_support.h>
    1.24 +#include <typeinfo>
    1.25 +#include <string>
    1.26 +#include <stdexcept>
    1.27 +
    1.28 +
    1.29 +EXPORT_C void TranslateSymErrorToCppException(TInt aError)
    1.30 +{
    1.31 +	switch(aError)
    1.32 +	{
    1.33 +	case KErrNoMemory:
    1.34 +		// This is about to use the emergency buffer!
    1.35 +		__THROW(std::bad_alloc());
    1.36 +	case KErrArgument:
    1.37 +		__THROW(std::invalid_argument("Invalid argument"));
    1.38 +	case KErrOverflow :
    1.39 +		__THROW(std::overflow_error("Overflow error"));
    1.40 +	case KErrUnderflow:
    1.41 +		__THROW(std::underflow_error("Underflow error"));
    1.42 +	default:
    1.43 +		__THROW(Symbian_error(KErrGeneral));
    1.44 +	}
    1.45 +}
    1.46 +
    1.47 +EXPORT_C TInt TranslateCppExceptionToSymError(const std::exception& aThrow)
    1.48 +{
    1.49 +#ifndef __GCCXML__
    1.50 +	const std::type_info& atype = typeid(aThrow);
    1.51 +
    1.52 +	if(atype == typeid (std::bad_alloc))
    1.53 +		return KErrNoMemory;
    1.54 +	else if(atype == typeid(std::invalid_argument))
    1.55 +		return KErrArgument;
    1.56 +	else if(atype == typeid(std::out_of_range))
    1.57 +		// std::out_of_range is of type logic_error which by definition means that it is
    1.58 +		// "presumably detectable before the program executes".
    1.59 +		// std::out_of_range is used to report an argument is not within the expected range.
    1.60 +		// The description of KErrArgument says an argument is out of range. Hence the mapping.
    1.61 +		return KErrArgument;
    1.62 +	else if(atype == typeid(std::overflow_error))
    1.63 +		return KErrOverflow;
    1.64 +	else if(atype == typeid(std::underflow_error))
    1.65 +		return KErrUnderflow;
    1.66 +	else
    1.67 +		return KErrGeneral;
    1.68 +#else
    1.69 +	return KErrGeneral;
    1.70 +#endif
    1.71 +}
    1.72 +
    1.73 +#ifdef __WINSCW__
    1.74 +/*
    1.75 + * The StdC++ global new and delete operators defined here. They are done only for the emulator
    1.76 + * as for EABI platforms, it is part of the runtime support(i.e., stdnew.dll).
    1.77 + */
    1.78 +#include <e32std.h>
    1.79 +
    1.80 +EXPORT_C const std::nothrow_t std::nothrow;
    1.81 +
    1.82 +EXPORT_C void* operator new(std::size_t a_size) __THROW(std::bad_alloc)
    1.83 +	{
    1.84 +	for (;;)
    1.85 +		{
    1.86 +		void* p = User::Alloc(a_size);
    1.87 +
    1.88 +		if (p)
    1.89 +			{
    1.90 +			return p;
    1.91 +			}
    1.92 +		std::new_handler nh_func ;
    1.93 +		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
    1.94 +		nh_func = (ptr)?(*ptr):NULL;
    1.95 +
    1.96 +		if (nh_func)
    1.97 +			{
    1.98 +			nh_func();
    1.99 +			}
   1.100 +		else
   1.101 +			{
   1.102 +			__THROW(std::bad_alloc());
   1.103 +			}
   1.104 +		}
   1.105 +	}
   1.106 +
   1.107 +EXPORT_C void* operator new[](std::size_t a_size) __THROW(std::bad_alloc)
   1.108 +	{
   1.109 +    return ::operator new(a_size);
   1.110 +	}
   1.111 +
   1.112 +EXPORT_C void* operator new(std::size_t a_size, const std::nothrow_t&) __NO_THROW
   1.113 +	{
   1.114 +	for (;;)
   1.115 +		{
   1.116 +		void* p = User::Alloc(a_size);
   1.117 +
   1.118 +		if (p)
   1.119 +			{
   1.120 +			return p;
   1.121 +			}
   1.122 +
   1.123 +		std::new_handler nh_func ;
   1.124 +		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
   1.125 +		nh_func = (ptr)?(*ptr):NULL;
   1.126 +
   1.127 +		if (nh_func)
   1.128 +			{
   1.129 +			try
   1.130 +				{
   1.131 +				nh_func();
   1.132 +				}
   1.133 +			catch (...)
   1.134 +				{
   1.135 +				return 0;
   1.136 +				}
   1.137 +			}
   1.138 +		else
   1.139 +			{
   1.140 +			return 0;
   1.141 +			}
   1.142 +		}
   1.143 +	}
   1.144 +
   1.145 +EXPORT_C void* operator new[](std::size_t a_size, const std::nothrow_t) __NO_THROW
   1.146 +	{
   1.147 +    return ::operator new(a_size, std::nothrow);
   1.148 +	}
   1.149 +
   1.150 +// Symbian-specific addition.
   1.151 +EXPORT_C void* operator new(std::size_t a_size, std::size_t a_extra_size) __NO_THROW
   1.152 +	{
   1.153 +    return ::operator new(a_size + a_extra_size, std::nothrow);
   1.154 +	}
   1.155 +
   1.156 +EXPORT_C void operator delete(void* p) __NO_THROW
   1.157 +	{
   1.158 +	User::Free(p);
   1.159 +	}
   1.160 +
   1.161 +EXPORT_C void operator delete(void* p, const std::nothrow_t&) __NO_THROW
   1.162 +	{
   1.163 +	User::Free(p);
   1.164 +	}
   1.165 +
   1.166 +EXPORT_C void operator delete[](void* p) __NO_THROW
   1.167 +	{
   1.168 +	User::Free(p);
   1.169 +	}
   1.170 +
   1.171 +EXPORT_C void operator delete[](void* p, const std::nothrow_t&) __NO_THROW
   1.172 +	{
   1.173 +	User::Free(p);
   1.174 +	}
   1.175 +
   1.176 +// Symbian-specific addition.
   1.177 +EXPORT_C void operator delete(void* p, std::size_t) __NO_THROW
   1.178 +	{
   1.179 +	User::Free(p);
   1.180 +	}
   1.181 +_LIT(K,"set_new_handler");
   1.182 +
   1.183 +
   1.184 +using std::new_handler;
   1.185 +
   1.186 +EXPORT_C new_handler std::set_new_handler(new_handler a_new_nh) __NO_THROW
   1.187 +	{
   1.188 +	new_handler current_nh;
   1.189 +	TAny* tls_word_p = Dll::Tls();
   1.190 +
   1.191 +	if (!tls_word_p)
   1.192 +		{
   1.193 +		// This is the first time we're called, so we need to set up the TLS.
   1.194 +
   1.195 +		tls_word_p = User::Alloc( sizeof(new_handler) );
   1.196 +		if ( !tls_word_p )
   1.197 +			{
   1.198 +			User::Panic(K, KErrNoMemory);
   1.199 +			}
   1.200 +
   1.201 +		Dll::SetTls(tls_word_p);
   1.202 +		current_nh = 0;
   1.203 +		}
   1.204 +	else
   1.205 +		{
   1.206 +		// Get the currently installed new_handler function.
   1.207 +		current_nh = *reinterpret_cast<new_handler*>(tls_word_p);
   1.208 +		}
   1.209 +	
   1.210 +	// Set the new new_handler.
   1.211 +	*reinterpret_cast<new_handler*>(tls_word_p) = a_new_nh;
   1.212 +
   1.213 +	return current_nh;
   1.214 +	}
   1.215 +
   1.216 +#endif //__WINSCW__