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