forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
miniutl.h
220 lines (189 loc) · 5.49 KB
/
miniutl.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright (C) 2019 Flying With Gauss */
/* Because I removed all tier0 stuff, we need to compensate it with big universal header */
#pragma once
#ifndef MINIUTL_H
#define MINIUTL_H
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#if defined(_MSC_VER)
#define FMTFUNCTION( x, y )
#else
#if defined(__GNUC__)
#ifdef __MINGW__
#define FMTFUNCTION( fmtargnumber, firstvarargnumber ) __attribute__ (( format( __MINGW_PRINTF_FORMAT, fmtargnumber, firstvarargnumber )))
#else
#define FMTFUNCTION( fmtargnumber, firstvarargnumber ) __attribute__ (( format( __printf__, fmtargnumber, firstvarargnumber )))
#endif
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#else
#define PRINTF_FORMAT_STRING
#define FMTFUNCTION( x, y )
#endif
#define _vsnprintf vsnprintf
#ifndef __cdecl
#define __cdecl
#endif
#endif
#if _MSC_VER >= 1600 && defined(_PREFAST_)// VS 2010 and above.
// Tag all printf-style format strings with this (consumed by MSVC).
#define PRINTF_FORMAT_STRING _Printf_format_string_
#define SCANF_FORMAT_STRING _Scanf_format_string_impl_
// Various macros for specifying the capacity of the buffer pointed
// to by a function parameter. Variations include in/out/inout,
// CAP (elements) versus BYTECAP (bytes), and null termination or
// not (_Z).
#define IN_Z _In_z_
#define IN_CAP(x) _In_count_(x)
#define IN_BYTECAP(x) _In_bytecount_(x)
#define OUT_Z_CAP(x) _Out_z_cap_(x)
#define OUT_CAP(x) _Out_cap_(x)
#define OUT_BYTECAP(x) _Out_bytecap_(x)
#define OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
#define INOUT_BYTECAP(x) _Inout_bytecap_(x)
#define INOUT_Z_CAP(x) _Inout_z_cap_(x)
#define INOUT_Z_BYTECAP(x) _Inout_z_bytecap_(x)
// These macros are use for annotating array reference parameters, typically used in functions
// such as V_strcpy_safe. Because they are array references the capacity is already known.
#if _MSC_VER >= 1700
#define IN_Z_ARRAY _Pre_z_
#define OUT_Z_ARRAY _Post_z_
#define INOUT_Z_ARRAY _Prepost_z_
#else
#define IN_Z_ARRAY _Deref_pre_z_
#define OUT_Z_ARRAY _Deref_post_z_
#define INOUT_Z_ARRAY _Deref_prepost_z_
#endif // _MSC_VER >= 1700
#else
#define PRINTF_FORMAT_STRING
#define SCANF_FORMAT_STRING
#define IN_Z
#define IN_CAP(x)
#define IN_BYTECAP(x)
#define OUT_Z_CAP(x)
#define OUT_CAP(x)
#define OUT_BYTECAP(x)
#define OUT_Z_BYTECAP(x)
#define INOUT_BYTECAP(x)
#define INOUT_Z_CAP(x)
#define INOUT_Z_BYTECAP(x)
#define OUT_Z_ARRAY
#define INOUT_Z_ARRAY
#endif
#include "strtools.h"
#ifdef MY_COMPILER_SUCKS
#define COMPILE_TIME_ASSERT( pred ) typedef int UNIQUE_ID[ (pred) ? 1 : -1]
#else
#define COMPILE_TIME_ASSERT( pred ) static_assert( pred, "Compile time assert constraint is not true: " #pred )
#endif
#define Assert( x ) assert( x )
#define DbgAssert( x ) assert( x ) // a1ba: this should raise under debugger only?
#ifndef NDEBUG
inline void AssertMsg( int pred, const char *fmt, ... )
{
char buf[1024];
va_list va;
va_start( va, fmt );
_vsnprintf( buf, sizeof( buf ), fmt, va );
va_end( va );
assert( pred && fmt );
}
#define AssertMsg1( x, msg, msg1 ) AssertMsg( x, msg, msg1 )
#define DbgAssertMsg1( x, msg, msg1 ) AssertMsg( x, msg, msg1 )
#define AssertMsg2( x, msg, msg1, msg2 ) AssertMsg( x, msg, msg1, msg2 )
#define AssertEquals( _exp, _expectedValue ) AssertMsg2( (_exp) == (_expectedValue), "Expected %d but got %d!", (_expectedValue), (_exp) )
#else
#define AssertMsg( x, msg ) ( x )
#define AssertMsg1( x, msg, msg1 ) ( x )
#define DbgAssertMsg1( x, msg, msg1 ) ( x )
#define AssertMsg2( x, msg, msg1, msg2 ) ( x )
#define AssertEquals( x, y ) ( x )
#endif
#define VerifyEquals( x, y ) AssertEquals( x, y )
#define PvAlloc malloc
#define PvRealloc realloc
#define FreePv free
#ifndef _WIN32
#define PlatformSecureZeroMemory( ptr, len ) memset( ptr, 0, len )
#else
#define PlatformSecureZeroMemory( ptr, len ) SecureZeroMemory( ptr, len )
#endif
#define Msg printf
inline void Error( const char *msg )
{
puts( msg );
abort();
}
#define MEM_ALLOC_CREDIT_CLASS()
// This is the preferred Min operator. Using the MIN macro can lead to unexpected
// side-effects or more expensive code.
template< class T >
static inline T const & Min( T const &val1, T const &val2 )
{
return val1 < val2 ? val1 : val2;
}
// This is the preferred Max operator. Using the MAX macro can lead to unexpected
// side-effects or more expensive code.
template< class T >
static inline T const & Max( T const &val1, T const &val2 )
{
return val1 > val2 ? val1 : val2;
}
#define V_ARRAYSIZE( arr ) ( sizeof((arr)) / sizeof((arr)[0]) )
typedef unsigned int uint;
#ifdef _MSC_VER
#include <new.h>
#else
#include <new>
#endif
template <class T>
inline void Construct( T* pMemory )
{
::new( pMemory ) T;
}
template <class T>
inline void CopyConstruct( T* pMemory, T const& src )
{
::new( pMemory ) T(src);
}
template <class T>
inline void Destruct( T* pMemory )
{
pMemory->~T();
}
namespace basetypes
{
template <class T>
inline bool IsPowerOf2( T n )
{
return n > 0 && (n & (n - 1)) == 0;
}
template <class T1, class T2>
inline T2 ModPowerOf2( T1 a, T2 b )
{
return T2( a ) & (b - 1);
}
template <class T>
inline T RoundDownToMultipleOf( T n, T m )
{
return n - (IsPowerOf2( m ) ? ModPowerOf2( n, m ) : (n%m));
}
template <class T>
inline T RoundUpToMultipleOf( T n, T m )
{
if ( !n )
{
return m;
}
else
{
return RoundDownToMultipleOf( n + m - 1, m );
}
}
}
#endif // MINIUTL_H