summaryrefslogtreecommitdiffstats
path: root/external/optick/optick_common.h
blob: 4468911e967c4e5f06cce0a2040d53228bddcf4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once

#include "optick.config.h"

#if USE_OPTICK

#include "optick.h"

#include <cstdio>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#if defined(OPTICK_MSVC)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define NOMINMAX
#include <windows.h>
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef signed char int8;
typedef unsigned char uint8;
typedef unsigned char byte;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
#if defined(OPTICK_MSVC)
typedef __int64 int64;
typedef unsigned __int64 uint64;
#elif defined(OPTICK_GCC)
typedef int64_t int64;
typedef uint64_t uint64;
#else
#error Compiler is not supported
#endif
static_assert(sizeof(int8) == 1, "Invalid type size, int8");
static_assert(sizeof(uint8) == 1, "Invalid type size, uint8");
static_assert(sizeof(byte) == 1, "Invalid type size, byte");
static_assert(sizeof(int16) == 2, "Invalid type size, int16");
static_assert(sizeof(uint16) == 2, "Invalid type size, uint16");
static_assert(sizeof(int32) == 4, "Invalid type size, int32");
static_assert(sizeof(uint32) == 4, "Invalid type size, uint32");
static_assert(sizeof(int64) == 8, "Invalid type size, int64");
static_assert(sizeof(uint64) == 8, "Invalid type size, uint64");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef uint64 ThreadID;
static const ThreadID INVALID_THREAD_ID = (ThreadID)-1;
typedef uint32 ProcessID;
static const ProcessID INVALID_PROCESS_ID = (ProcessID)-1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Memory
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_MSVC)
#define OPTICK_ALIGN(N) __declspec( align( N ) )
#elif defined(OPTICK_GCC)
#define OPTICK_ALIGN(N) __attribute__((aligned(N)))
#else
#error Can not define OPTICK_ALIGN. Unknown platform.
#endif
#define OPTICK_CACHE_LINE_SIZE 64
#define OPTICK_ALIGN_CACHE OPTICK_ALIGN(OPTICK_CACHE_LINE_SIZE)
#define OPTICK_ARRAY_SIZE(ARR) (sizeof(ARR)/sizeof((ARR)[0]))
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_MSVC)
#define OPTICK_NOINLINE __declspec(noinline)
#elif defined(OPTICK_GCC)
#define OPTICK_NOINLINE __attribute__((__noinline__))
#else
#error Compiler is not supported
#endif
////////////////////////////////////////////////////////////////////////
// OPTICK_THREAD_LOCAL
////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_MSVC)
#define OPTICK_THREAD_LOCAL __declspec(thread)
#elif defined(OPTICK_GCC)
#define OPTICK_THREAD_LOCAL __thread
#else
#error Can not define OPTICK_THREAD_LOCAL. Unknown platform.
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Asserts
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_MSVC)
#define OPTICK_DEBUG_BREAK __debugbreak()
#elif defined(OPTICK_GCC)
#define OPTICK_DEBUG_BREAK __builtin_trap()
#else
	#error Can not define OPTICK_DEBUG_BREAK. Unknown platform.
#endif
#define OPTICK_UNUSED(x) (void)(x)
#ifdef _DEBUG
	#define OPTICK_ASSERT(arg, description) if (!(arg)) { OPTICK_DEBUG_BREAK; }
	#define OPTICK_FAILED(description) { OPTICK_DEBUG_BREAK; }
#else
	#define OPTICK_ASSERT(arg, description)
	#define OPTICK_FAILED(description)
#endif
#define OPTICK_VERIFY(arg, description, operation) if (!(arg)) { OPTICK_DEBUG_BREAK; operation; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Safe functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_LINUX) || defined(OPTICK_OSX)
template<size_t sizeOfBuffer>
inline int sprintf_s(char(&buffer)[sizeOfBuffer], const char* format, ...)
{
	va_list ap;
	va_start(ap, format);
	int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
	va_end(ap);
	return result;
}
#endif

#if defined(OPTICK_GCC)
template<size_t sizeOfBuffer>
inline int wcstombs_s(char(&buffer)[sizeOfBuffer], const wchar_t* src, size_t maxCount)
{
	return wcstombs(buffer, src, maxCount);
}
#endif

#if defined(OPTICK_MSVC)
template<size_t sizeOfBuffer>
inline int wcstombs_s(char(&buffer)[sizeOfBuffer], const wchar_t* src, size_t maxCount)
{
	size_t converted = 0;
	return wcstombs_s(&converted, buffer, src, maxCount);
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif //USE_OPTICK