1 /*-------------------------------------------------------------------------
2  * drawElements Base Portability Library
3  * -------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Basic portability.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deDefs.h"
25 #include "deInt32.h"
26 
27 /* Assert base type sizes. */
28 DE_STATIC_ASSERT(sizeof(deUint8)	== 1);
29 DE_STATIC_ASSERT(sizeof(deUint16)	== 2);
30 DE_STATIC_ASSERT(sizeof(deUint32)	== 4);
31 DE_STATIC_ASSERT(sizeof(deUint64)	== 8);
32 DE_STATIC_ASSERT(sizeof(deInt8)		== 1);
33 DE_STATIC_ASSERT(sizeof(deInt16)	== 2);
34 DE_STATIC_ASSERT(sizeof(deInt32)	== 4);
35 DE_STATIC_ASSERT(sizeof(deInt64)	== 8);
36 DE_STATIC_ASSERT(sizeof(deUintptr)	== sizeof(void*));
37 DE_STATIC_ASSERT(sizeof(deIntptr)	== sizeof(void*));
38 DE_STATIC_ASSERT(DE_PTR_SIZE		== sizeof(void*));
39 
40 /* Sanity checks for DE_PTR_SIZE & DE_CPU */
41 #if !((DE_CPU == DE_CPU_X86_64 || DE_CPU == DE_CPU_ARM_64 || DE_CPU == DE_CPU_MIPS_64) && (DE_PTR_SIZE == 8)) && \
42 	!((DE_CPU == DE_CPU_X86    || DE_CPU == DE_CPU_ARM    || DE_CPU == DE_CPU_MIPS)    && (DE_PTR_SIZE == 4))
43 #	error "DE_CPU and DE_PTR_SIZE mismatch"
44 #endif
45 
46 #if (DE_OS == DE_OS_UNIX || DE_OS == DE_OS_WIN32) && defined(NDEBUG)
47 	/* We need __assert_fail declaration from assert.h */
48 #	undef NDEBUG
49 #endif
50 
51 #include <stdio.h>
52 #include <assert.h>
53 #include <string.h>
54 
55 #if (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS)
56 #	include <signal.h>
57 #	include <stdlib.h>
58 #endif
59 
60 #if (DE_OS == DE_OS_ANDROID)
61 #	include <android/log.h>
62 #endif
63 
64 /*
65 #if (DE_OS == DE_OS_WIN32)
66 #	define WIN32_LEAN_AND_MEAN
67 #	include <windows.h>
68 #endif
69 */
70 
71 DE_BEGIN_EXTERN_C
72 
73 #if defined(DE_ASSERT_FAILURE_CALLBACK)
74 static deAssertFailureCallbackFunc g_assertFailureCallback = DE_NULL;
75 #endif
76 
deSetAssertFailureCallback(deAssertFailureCallbackFunc callback)77 void deSetAssertFailureCallback (deAssertFailureCallbackFunc callback)
78 {
79 #if defined(DE_ASSERT_FAILURE_CALLBACK)
80 	g_assertFailureCallback = callback;
81 #else
82 	DE_UNREF(callback);
83 #endif
84 }
85 
deAssertFail(const char * reason,const char * file,int line)86 void deAssertFail (const char* reason, const char* file, int line)
87 {
88 #if defined(DE_ASSERT_FAILURE_CALLBACK)
89 	if (g_assertFailureCallback != DE_NULL)
90 	{
91 		/* Remove callback in case of the callback causes further asserts. */
92 		deAssertFailureCallbackFunc callback = g_assertFailureCallback;
93 		deSetAssertFailureCallback(DE_NULL);
94 		callback(reason, file, line);
95 	}
96 #endif
97 
98 #if (((DE_OS == DE_OS_WIN32) || (DE_OS == DE_OS_WINCE)) && (DE_COMPILER == DE_COMPILER_MSC))
99 	{
100 		wchar_t	wreason[1024];
101 		wchar_t	wfile[128];
102 		int		num;
103 		int		i;
104 
105 	/*	MessageBox(reason, "Assertion failed", MB_OK); */
106 
107 		num = deMin32((int)strlen(reason), DE_LENGTH_OF_ARRAY(wreason)-1);
108 		for (i = 0; i < num; i++)
109 			wreason[i] = reason[i];
110 		wreason[i] = 0;
111 
112 		num = deMin32((int)strlen(file), DE_LENGTH_OF_ARRAY(wfile)-1);
113 		for (i = 0; i < num; i++)
114 			wfile[i] = file[i];
115 		wfile[i] = 0;
116 
117 #	if (DE_OS == DE_OS_WIN32)
118 		_wassert(wreason, wfile, line);
119 #	else /* WINCE */
120 		assert(wreason);
121 #	endif
122 	}
123 #elif ((DE_OS == DE_OS_WIN32) && (DE_COMPILER == DE_COMPILER_CLANG))
124 	_assert(reason, file, line);
125 #elif (DE_OS == DE_OS_UNIX)
126 	__assert_fail(reason, file, (unsigned int)line, "Unknown function");
127 #elif (DE_OS == DE_OS_SYMBIAN)
128 	__assert("Unknown function", file, line, reason);
129 #elif (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS)
130 	fprintf(stderr, "Assertion '%s' failed at %s:%d\n", reason, file, line);
131 	raise(SIGTRAP);
132 	abort();
133 #elif (DE_OS == DE_OS_ANDROID)
134 	__android_log_print(ANDROID_LOG_ERROR, "delibs", "Assertion '%s' failed at %s:%d", reason, file, line);
135 	__assert(file, line, reason);
136 #else
137 #	error Implement assertion function on your platform.
138 #endif
139 }
140 
141 DE_END_EXTERN_C
142