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(uint8_t) == 1);
29 DE_STATIC_ASSERT(sizeof(uint16_t) == 2);
30 DE_STATIC_ASSERT(sizeof(uint32_t) == 4);
31 DE_STATIC_ASSERT(sizeof(uint64_t) == 8);
32 DE_STATIC_ASSERT(sizeof(int8_t) == 1);
33 DE_STATIC_ASSERT(sizeof(int16_t) == 2);
34 DE_STATIC_ASSERT(sizeof(int32_t) == 4);
35 DE_STATIC_ASSERT(sizeof(int64_t) == 8);
36 DE_STATIC_ASSERT(sizeof(uintptr_t) == sizeof(void *));
37 DE_STATIC_ASSERT(sizeof(intptr_t) == 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_CPU == DE_CPU_RISCV_64) && \
42 (DE_PTR_SIZE == 8)) && \
43 !((DE_CPU == DE_CPU_X86 || DE_CPU == DE_CPU_ARM || DE_CPU == DE_CPU_MIPS || DE_CPU == DE_CPU_RISCV_32) && \
44 (DE_PTR_SIZE == 4))
45 #error "DE_CPU and DE_PTR_SIZE mismatch"
46 #endif
47
48 #if (DE_OS == DE_OS_UNIX || DE_OS == DE_OS_WIN32) && defined(NDEBUG)
49 /* We need __assert_fail declaration from assert.h */
50 #undef NDEBUG
51 #endif
52
53 #include <stdio.h>
54 #include <assert.h>
55 #include <string.h>
56
57 #if (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS) || defined(__FreeBSD__)
58 #include <signal.h>
59 #include <stdlib.h>
60 #endif
61
62 #if (DE_OS == DE_OS_ANDROID)
63 #include <android/log.h>
64 #endif
65
66 /*
67 #if (DE_OS == DE_OS_WIN32)
68 # define WIN32_LEAN_AND_MEAN
69 # include <windows.h>
70 #endif
71 */
72
73 DE_BEGIN_EXTERN_C
74
75 #if defined(DE_ASSERT_FAILURE_CALLBACK)
76 static deAssertFailureCallbackFunc g_assertFailureCallback = DE_NULL;
77 #endif
78
deSetAssertFailureCallback(deAssertFailureCallbackFunc callback)79 void deSetAssertFailureCallback(deAssertFailureCallbackFunc callback)
80 {
81 #if defined(DE_ASSERT_FAILURE_CALLBACK)
82 g_assertFailureCallback = callback;
83 #else
84 DE_UNREF(callback);
85 #endif
86 }
87
deAssertFail(const char * reason,const char * file,int line)88 void deAssertFail(const char *reason, const char *file, int line)
89 {
90 #if defined(DE_ASSERT_FAILURE_CALLBACK)
91 if (g_assertFailureCallback != DE_NULL)
92 {
93 /* Remove callback in case of the callback causes further asserts. */
94 deAssertFailureCallbackFunc callback = g_assertFailureCallback;
95 deSetAssertFailureCallback(DE_NULL);
96 callback(reason, file, line);
97 }
98 #endif
99
100 #if (((DE_OS == DE_OS_WIN32) || (DE_OS == DE_OS_WINCE)) && (DE_COMPILER == DE_COMPILER_MSC))
101 {
102 wchar_t wreason[1024];
103 wchar_t wfile[128];
104 int num;
105 int i;
106
107 /* MessageBox(reason, "Assertion failed", MB_OK); */
108
109 num = deMin32((int)strlen(reason), DE_LENGTH_OF_ARRAY(wreason) - 1);
110 for (i = 0; i < num; i++)
111 wreason[i] = reason[i];
112 wreason[i] = 0;
113
114 num = deMin32((int)strlen(file), DE_LENGTH_OF_ARRAY(wfile) - 1);
115 for (i = 0; i < num; i++)
116 wfile[i] = file[i];
117 wfile[i] = 0;
118
119 #if (DE_OS == DE_OS_WIN32)
120 _wassert(wreason, wfile, line);
121 #else /* WINCE */
122 assert(wreason);
123 #endif
124 }
125 #elif ((DE_OS == DE_OS_WIN32) && (DE_COMPILER == DE_COMPILER_CLANG || DE_COMPILER == DE_COMPILER_GCC))
126 _assert(reason, file, line);
127 #elif (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS) || defined(__FreeBSD__)
128 fprintf(stderr, "Assertion '%s' failed at %s:%d\n", reason, file, line);
129 raise(SIGTRAP);
130 abort();
131 #elif (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_FUCHSIA)
132 __assert_fail(reason, file, (unsigned int)line, "Unknown function");
133 #elif (DE_OS == DE_OS_QNX)
134 __assert(reason, file, (unsigned int)line, "Unknown function");
135 #elif (DE_OS == DE_OS_SYMBIAN)
136 __assert("Unknown function", file, line, reason);
137 #elif (DE_OS == DE_OS_ANDROID)
138 __android_log_print(ANDROID_LOG_ERROR, "delibs", "Assertion '%s' failed at %s:%d", reason, file, line);
139 __assert(file, line, reason);
140 #else
141 #error Implement assertion function on your platform.
142 #endif
143 }
144
145 DE_END_EXTERN_C
146