eapol/eapol_framework/eapol_common/am/common/eap_am_memory.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 9 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 
       
    29 #include "eap_variable_data.h"
       
    30 #include "eap_am_export.h"
       
    31 #include "abs_eap_am_tools.h"
       
    32 #include "eap_am_memory.h"
       
    33 
       
    34 #if defined(DMALLOC)
       
    35 	//#pragma message("Uses dmalloc.") 
       
    36 	#include <dmalloc.h>
       
    37 #endif // #if defined(DMALLOC)
       
    38 
       
    39 #if defined(DMALLOC) || defined(USE_EAP_MEMORY_FUNCTIONS)
       
    40 
       
    41 #if (defined(_WIN32) || defined(__GNUC__)) && !defined(__SYMBIAN32__)
       
    42 
       
    43 	static jph_new_handler g_jph_new_handler = NULL;
       
    44 
       
    45 	#if defined(USE_EAP_MEMORY_FUNCTIONS_FAILURES)
       
    46 
       
    47 		#include <time.h>
       
    48 		bool g_eap_alloc_failures_enabled_flag = false;
       
    49 		u32_t g_eap_alloc_failures_probability = 0ul;
       
    50 		u32_t g_eap_alloc_failures_skip_count = 0ul;
       
    51 		u32_t g_eap_alloc_failures_skip_counter_value = 0ul;
       
    52 
       
    53 		const u32_t g_eap_alloc_max_probapility = 4294967295ul;
       
    54 
       
    55 		void g_eap_set_memory_parameters(
       
    56 			const u32_t alloc_failures_probability,
       
    57 			const u32_t alloc_failures_skip_count)
       
    58 		{
       
    59 			srand(time(0));
       
    60 
       
    61 			g_eap_alloc_failures_enabled_flag = true;
       
    62 
       
    63 			g_eap_alloc_failures_probability = alloc_failures_probability;
       
    64 
       
    65 			g_eap_alloc_failures_skip_count = alloc_failures_skip_count;
       
    66 
       
    67 			g_eap_alloc_failures_skip_counter_value = 0ul;
       
    68 		}
       
    69 
       
    70 		void g_eap_alloc_failures_enabled()
       
    71 		{
       
    72 			if (g_eap_alloc_failures_probability != 0)
       
    73 			{
       
    74 				// Parameters must be set before failures can be enabled.
       
    75 				g_eap_alloc_failures_enabled_flag = true;
       
    76 			}
       
    77 		}
       
    78 
       
    79 		void g_eap_alloc_failures_disabled()
       
    80 		{
       
    81 			g_eap_alloc_failures_enabled_flag = false;
       
    82 		}
       
    83 
       
    84 		bool g_eap_alloc_failures_active()
       
    85 		{
       
    86 			return (g_eap_alloc_failures_skip_counter_value >= g_eap_alloc_failures_skip_count
       
    87 				&& g_eap_alloc_failures_probability == g_eap_alloc_max_probapility);
       
    88 		}
       
    89 
       
    90 		static bool g_eap_memory_randomize_error()
       
    91 		{
       
    92 			// We cannot use abs_eap_am_tools_c here.
       
    93 
       
    94 			if (g_eap_alloc_failures_enabled_flag == false)
       
    95 			{
       
    96 				return false;
       
    97 			}
       
    98 
       
    99 			if (g_eap_alloc_failures_skip_counter_value < g_eap_alloc_failures_skip_count)
       
   100 			{
       
   101 				++g_eap_alloc_failures_skip_counter_value;
       
   102 				return false;
       
   103 			}
       
   104 
       
   105 			u32_t random_guard = static_cast<u32_t>(rand());
       
   106 			if (random_guard < g_eap_alloc_failures_probability)
       
   107 			{
       
   108 				return true;
       
   109 			}
       
   110 			else
       
   111 			{
       
   112 				return false;
       
   113 			}
       
   114 		}
       
   115 	#endif //#if defined(USE_EAP_MEMORY_FUNCTIONS_FAILURES)
       
   116 
       
   117 #else
       
   118 	// Note Symbian does not support global writable data in DLL.
       
   119 #endif //#if defined(_WIN32) || defined(__GNUC__)
       
   120 
       
   121 
       
   122 #if defined(_WIN32) || defined(__GNUC__)
       
   123 /*
       
   124  * An overload function the malloc.
       
   125  */
       
   126 EAP_C_FUNC_EXPORT void *jph_malloc(size_t n)
       
   127 {
       
   128 
       
   129 #if defined(USE_EAP_MEMORY_FUNCTIONS_FAILURES)
       
   130 	if (g_eap_memory_randomize_error() == true)
       
   131 	{
       
   132 		// This is failed allocation.
       
   133 		return 0;
       
   134 	}
       
   135 #endif //#if defined(USE_EAP_MEMORY_FUNCTIONS_FAILURES)
       
   136 
       
   137 	#if defined(__SYMBIAN32__)
       
   138 		return User::Alloc(n);
       
   139 	#else
       
   140 		return malloc(n);
       
   141 	#endif
       
   142 }
       
   143 
       
   144 /*
       
   145  * An overload function the malloc.
       
   146  */
       
   147 EAP_C_FUNC_EXPORT void *jph_malloc_ex(size_t n, const char *file, int line)
       
   148 {
       
   149 	return jph_malloc(n);
       
   150 }
       
   151 #endif //#if defined(_WIN32) || defined(__GNUC__)
       
   152 
       
   153 
       
   154 #if defined(_WIN32) || defined(__GNUC__)
       
   155 	#if defined(USE_JPH_REALLOC)
       
   156 	/*
       
   157 	 * An overload function the realloc.
       
   158 	 */
       
   159 	EAP_C_FUNC_EXPORT void *jph_realloc(void *oldbuf, size_t n)
       
   160 	{
       
   161 		return realloc(oldbuf,n);
       
   162 	}
       
   163 
       
   164 	/*
       
   165 	 * An overload function the realloc.
       
   166 	 */
       
   167 	EAP_C_FUNC_EXPORT void *jph_realloc_ex(void *oldbuf, size_t n, const char *file, int line)
       
   168 	{
       
   169 		return jph_realloc(oldbuf,n);
       
   170 	}
       
   171 	#endif //#if defined(USE_JPH_REALLOC)
       
   172 
       
   173 
       
   174 	#if defined(USE_JPH_CALLOC)
       
   175 	/*
       
   176 	 * An overload function the jph_calloc.
       
   177 	 */
       
   178 	EAP_C_FUNC_EXPORT void *jph_calloc(size_t count, size_t size)
       
   179 	{
       
   180 		return calloc(count,size);
       
   181 	}
       
   182 	#endif //#if defined(USE_JPH_CALLOC)
       
   183 
       
   184 
       
   185 	/*
       
   186 	 * An overload function for the free.
       
   187 	 */
       
   188 	EAP_C_FUNC_EXPORT void jph_free(void *cp)
       
   189 	{
       
   190 		#if defined(__SYMBIAN32__)
       
   191 			User::Free(cp);
       
   192 		#else
       
   193 			free( cp );
       
   194 		#endif
       
   195 	}
       
   196 
       
   197 
       
   198 	EAP_C_FUNC_EXPORT void *jph_new(size_t n)
       
   199 	{
       
   200 		void *mem = jph_malloc(n);
       
   201 
       
   202 	#if (defined(_WIN32) || defined(__GNUC__)) && !defined(__SYMBIAN32__)
       
   203 		if (!mem && g_jph_new_handler)
       
   204 		{
       
   205 			if (g_jph_new_handler(n))
       
   206 			{
       
   207 				mem = jph_malloc(n);
       
   208 			}
       
   209 		}
       
   210 	#endif //#if (defined(_WIN32) || defined(__GNUC__)) && !defined(__SYMBIAN32__)
       
   211 
       
   212 		return mem;
       
   213 	}
       
   214 
       
   215 	EAP_C_FUNC_EXPORT void jph_delete(void *cp)
       
   216 	{
       
   217 		jph_free( cp );
       
   218 	}
       
   219 
       
   220 #endif /* #if defined(_WIN32) */
       
   221 
       
   222 
       
   223 #if defined(_WIN32) && defined(__GNUC__)
       
   224 
       
   225 
       
   226 /* This will be __builtin_new. */
       
   227 EAP_C_FUNC_EXPORT void *operator new(size_t n)
       
   228 {
       
   229 	return jph_new(n);
       
   230 }
       
   231 
       
   232 /* This will be __builtin_vec_new. */
       
   233 EAP_C_FUNC_EXPORT void *operator new[](size_t n)
       
   234 {
       
   235 	return jph_new(n);
       
   236 }
       
   237 
       
   238 /* This will be __builtin_delete. */
       
   239 EAP_C_FUNC_EXPORT void operator delete(void *cp)
       
   240 {
       
   241 	if (cp)
       
   242 	{
       
   243 		jph_delete(cp);
       
   244 	}
       
   245 }
       
   246 
       
   247 /* This will be __builtin_vec_delete. */
       
   248 EAP_C_FUNC_EXPORT void operator delete[](void *cp)
       
   249 {
       
   250 	if (cp)
       
   251 	{
       
   252 		jph_delete(cp);
       
   253 	}
       
   254 }
       
   255 
       
   256 #endif /* #if defined(_WIN32) */
       
   257 
       
   258 
       
   259 #if !defined(_WIN32) && defined(__GNUC__)
       
   260 
       
   261 EAP_C_FUNC_EXPORT void *operator new(std::size_t n) throw (std::bad_alloc)
       
   262 {
       
   263 	return jph_new(n);
       
   264 }
       
   265 
       
   266 EAP_C_FUNC_EXPORT void *operator new[](std::size_t n) throw (std::bad_alloc)
       
   267 {
       
   268 	return jph_new(n);
       
   269 }
       
   270 
       
   271 EAP_C_FUNC_EXPORT void operator delete(void *cp) throw()
       
   272 {
       
   273 	if (cp)
       
   274 	{
       
   275 		jph_delete(cp);
       
   276 	}
       
   277 }
       
   278 
       
   279 EAP_C_FUNC_EXPORT void operator delete[](void *cp) throw()
       
   280 {
       
   281 	if (cp)
       
   282 	{
       
   283 		jph_delete(cp);
       
   284 	}
       
   285 }
       
   286 
       
   287 #endif
       
   288 
       
   289 
       
   290 #if (defined(_WIN32) || defined(__GNUC__)) && !defined(__SYMBIAN32__)
       
   291 EAP_C_FUNC_EXPORT jph_new_handler jph_set_new_handler(jph_new_handler handler)
       
   292 {
       
   293 	jph_new_handler oldhandler = g_jph_new_handler;
       
   294 	g_jph_new_handler = handler;
       
   295 	return oldhandler;
       
   296 }
       
   297 #endif //#if defined(_WIN32) || defined(__GNUC__)
       
   298 
       
   299 #endif //#if defined(DMALLOC) || defined(USE_EAP_MEMORY_FUNCTIONS)
       
   300 
       
   301 
       
   302 
       
   303 
       
   304 // End.