-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcore.c
More file actions
238 lines (190 loc) · 4.52 KB
/
core.c
File metadata and controls
238 lines (190 loc) · 4.52 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT
#include "core.h"
#include "build_hash.h"
#include "box2d/math_functions.h"
#if defined( B2_COMPILER_MSVC )
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>
#else
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef BOX2D_PROFILE
#include <tracy/TracyC.h>
#define b2TracyCAlloc( ptr, size ) TracyCAlloc( ptr, size )
#define b2TracyCFree( ptr ) TracyCFree( ptr )
#else
#define b2TracyCAlloc( ptr, size )
#define b2TracyCFree( ptr )
#endif
#include "atomic.h"
// This allows the user to change the length units at runtime
static float b2_lengthUnitsPerMeter = 1.0f;
void b2SetLengthUnitsPerMeter( float lengthUnits )
{
B2_ASSERT( b2IsValidFloat( lengthUnits ) && lengthUnits > 0.0f );
b2_lengthUnitsPerMeter = lengthUnits;
}
float b2GetLengthUnitsPerMeter( void )
{
return b2_lengthUnitsPerMeter;
}
static int b2DefaultAssertFcn( const char* condition, const char* fileName, int lineNumber )
{
fprintf( stderr, "BOX2D ASSERTION: %s, %s, line %d\n", condition, fileName, lineNumber );
fflush( stderr );
// return non-zero to break to debugger
return 1;
}
static b2AssertFcn* b2AssertHandler = b2DefaultAssertFcn;
void b2SetAssertFcn( b2AssertFcn* assertFcn )
{
B2_ASSERT( assertFcn != NULL );
b2AssertHandler = assertFcn;
}
#if !defined( NDEBUG ) || defined( B2_ENABLE_ASSERT )
int b2InternalAssert( const char* condition, const char* fileName, int lineNumber )
{
int result = b2AssertHandler( condition, fileName, lineNumber );
if ( result )
{
B2_BREAKPOINT;
}
return result;
}
#endif
static void b2DefaultLogFcn( const char* message )
{
printf( "Box2D: %s\n", message );
}
static b2LogFcn* b2LogHandler = b2DefaultLogFcn;
void b2SetLogFcn( b2LogFcn* logFcn )
{
B2_ASSERT( logFcn != NULL );
b2LogHandler = logFcn;
}
void b2Log( const char* format, ... )
{
va_list args;
va_start( args, format );
char buffer[512];
vsnprintf( buffer, sizeof( buffer ), format, args );
b2LogHandler( buffer );
va_end( args );
}
b2Version b2GetVersion( void )
{
return (b2Version){
.major = 3,
.minor = 2,
.revision = 0,
};
}
uint32_t b2GetBuildHash( void )
{
return B2_BUILD_HASH;
}
static b2AllocFcn* b2_allocFcn = NULL;
static b2FreeFcn* b2_freeFcn = NULL;
static b2AtomicInt b2_byteCount;
void b2SetAllocator( b2AllocFcn* allocFcn, b2FreeFcn* freeFcn )
{
b2_allocFcn = allocFcn;
b2_freeFcn = freeFcn;
}
// Use 32 byte alignment for everything. Works with 256bit SIMD.
#define B2_ALIGNMENT 32
void* b2Alloc( int size )
{
if ( size == 0 )
{
return NULL;
}
// This could cause some sharing issues, however Box2D rarely calls b2Alloc.
b2AtomicFetchAddInt( &b2_byteCount, size );
// Allocation must be a multiple of 32 or risk a seg fault
// https://en.cppreference.com/w/c/memory/aligned_alloc
int size32 = ( ( size - 1 ) | 0x1F ) + 1;
if ( b2_allocFcn != NULL )
{
void* ptr = b2_allocFcn( size32, B2_ALIGNMENT );
b2TracyCAlloc( ptr, size );
B2_ASSERT( ptr != NULL );
B2_ASSERT( ( (uintptr_t)ptr & 0x1F ) == 0 );
return ptr;
}
#ifdef B2_PLATFORM_WINDOWS
void* ptr = _aligned_malloc( size32, B2_ALIGNMENT );
#elif defined( B2_PLATFORM_ANDROID )
void* ptr = NULL;
if ( posix_memalign( &ptr, B2_ALIGNMENT, size32 ) != 0 )
{
// allocation failed, exit the application
exit( EXIT_FAILURE );
}
#else
void* ptr = aligned_alloc( B2_ALIGNMENT, size32 );
#endif
b2TracyCAlloc( ptr, size );
B2_ASSERT( ptr != NULL );
B2_ASSERT( ( (uintptr_t)ptr & 0x1F ) == 0 );
return ptr;
}
void* b2AllocZeroInit( int size )
{
void* memory = b2Alloc( size );
memset( memory, 0, size );
return memory;
}
void b2Free( void* mem, int size )
{
if ( mem == NULL )
{
return;
}
b2TracyCFree( mem );
if ( b2_freeFcn != NULL )
{
b2_freeFcn( mem, size );
}
else
{
#ifdef B2_PLATFORM_WINDOWS
_aligned_free( mem );
#else
free( mem );
#endif
}
b2AtomicFetchAddInt( &b2_byteCount, -size );
}
void* b2GrowAlloc( void* oldMem, int oldSize, int newSize )
{
B2_ASSERT( newSize > oldSize );
void* newMem = b2Alloc( newSize );
if ( oldSize > 0 )
{
memcpy( newMem, oldMem, oldSize );
b2Free( oldMem, oldSize );
}
return newMem;
}
void* b2GrowAllocZeroInit( void* oldMem, int oldSize, int newSize )
{
B2_ASSERT( newSize > oldSize );
void* newMem = b2Alloc( newSize );
if ( oldSize > 0 )
{
memcpy( newMem, oldMem, oldSize );
b2Free( oldMem, oldSize );
}
memset( (char*)newMem + oldSize, 0, newSize - oldSize );
return newMem;
}
int b2GetByteCount( void )
{
return b2AtomicLoadInt( &b2_byteCount );
}