-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathengine_util_errmem.c
More file actions
277 lines (219 loc) · 6.56 KB
/
engine_util_errmem.c
File metadata and controls
277 lines (219 loc) · 6.56 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2021 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "engine/engine_util_errmem.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif
#include "engine/engine_array_safety.h"
#include "engine/engine_macro.h"
//------------------------- cross-platform aligned malloc/free -------------------------------------
static inline void* mju_alignedMalloc(size_t size, size_t align) {
#ifdef _WIN32
return _aligned_malloc(size, align);
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
return aligned_alloc(align, size);
#endif
}
static inline void mju_alignedFree(void* ptr) {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
//------------------------- UTF-8 file open --------------------------------------------------------
// open file with UTF-8 filename support
FILE* mju_fopen(const char* filename, const char* mode) {
#ifdef _WIN32
// convert UTF-8 filename to wide string
int wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
int wmlen = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
if (wlen == 0 || wmlen == 0) {
return NULL;
}
wchar_t* wfilename = (wchar_t*)malloc(wlen * sizeof(wchar_t));
wchar_t* wmode = (wchar_t*)malloc(wmlen * sizeof(wchar_t));
if (!wfilename || !wmode) {
free(wfilename);
free(wmode);
return NULL;
}
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, wlen);
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, wmlen);
FILE* fp = _wfopen(wfilename, wmode);
free(wfilename);
free(wmode);
return fp;
#else
return fopen(filename, mode);
#endif
}
//------------------------- default user handlers --------------------------------------------------
// define and clear handlers
void (*mju_user_error) (const char*) = 0;
void (*mju_user_warning) (const char*) = 0;
void* (*mju_user_malloc) (size_t) = 0;
void (*mju_user_free) (void*) = 0;
// restore default processing
void mju_clearHandlers(void) {
mju_user_error = 0;
mju_user_warning = 0;
mju_user_malloc = 0;
mju_user_free = 0;
}
//------------------------- internal-only handlers -------------------------------------------------
typedef void (*callback_fn)(const char*);
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_error_fn = NULL;
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_warning_fn = NULL;
callback_fn _mjPRIVATE__get_tls_error_fn(void) {
return _mjPRIVATE_tls_error_fn;
}
void _mjPRIVATE__set_tls_error_fn(callback_fn h) {
_mjPRIVATE_tls_error_fn = h;
}
callback_fn _mjPRIVATE__get_tls_warning_fn(void) {
return _mjPRIVATE_tls_warning_fn;
}
void _mjPRIVATE__set_tls_warning_fn(callback_fn h) {
_mjPRIVATE_tls_warning_fn = h;
}
//------------------------------ error hadling -----------------------------------------------------
// write datetime, type: message to MUJOCO_LOG.TXT
void mju_writeLog(const char* type, const char* msg) {
time_t rawtime;
struct tm timeinfo;
FILE* fp = mju_fopen("MUJOCO_LOG.TXT", "a+t");
if (fp) {
// get time
time(&rawtime);
#if defined(_POSIX_C_SOURCE) || defined(__APPLE__) || defined(__STDC_VERSION_TIME_H__) || defined(__EMSCRIPTEN__)
localtime_r(&rawtime, &timeinfo);
#elif defined(_WIN32)
localtime_s(&timeinfo, &rawtime);
#elif __STDC_LIB_EXT1__
localtime_s(&rawtime, &timeinfo);
#else
#error "Thread-safe version of `localtime` is not present in the standard C library"
#endif
// write to log file
fprintf(fp, "%s%s: %s\n\n", asctime(&timeinfo), type, msg);
fclose(fp);
}
}
void mju_error_raw(const char* msg) {
if (_mjPRIVATE_tls_error_fn) {
_mjPRIVATE_tls_error_fn(msg);
} else if (mju_user_error) {
mju_user_error(msg);
} else {
// write to log and console
mju_writeLog("ERROR", msg);
printf("ERROR: %s\n\n", msg);
// exit
exit(EXIT_FAILURE);
}
}
void mju_error_v(const char* msg, va_list args) {
// Format msg into errmsg
char errmsg[1024];
vsnprintf(errmsg, mjSIZEOFARRAY(errmsg), msg, args);
mju_error_raw(errmsg);
}
// write message to logfile and console, pause and exit
void mju_error(const char* msg, ...) {
va_list args;
va_start(args, msg);
mju_error_v(msg, args);
va_end(args);
}
// write message to logfile and console
void mju_warning(const char* msg, ...) {
char wrnmsg[1024];
// Format msg into wrnmsg
va_list args;
va_start(args, msg);
vsnprintf(wrnmsg, mjSIZEOFARRAY(wrnmsg), msg, args);
va_end(args);
if (_mjPRIVATE_tls_warning_fn) {
_mjPRIVATE_tls_warning_fn(wrnmsg);
} else if (mju_user_warning) {
mju_user_warning(wrnmsg);
} else {
// write to log file and console
mju_writeLog("WARNING", wrnmsg);
printf("WARNING: %s\n\n", wrnmsg);
}
}
// error with int argument
void mju_error_i(const char* msg, int i) {
mju_error(msg, i);
}
// warning with int argument
void mju_warning_i(const char* msg, int i) {
mju_warning(msg, i);
}
// error string argument
void mju_error_s(const char* msg, const char* text) {
mju_error(msg, text);
}
// warning string argument
void mju_warning_s(const char* msg, const char* text) {
mju_warning(msg, text);
}
//------------------------------ malloc and free ---------------------------------------------------
// allocate memory; byte-align on 64; pad size to multiple of 64
void* mju_malloc(size_t size) {
void* ptr = 0;
// user allocator
if (mju_user_malloc) {
ptr = mju_user_malloc(size);
}
// default allocator
else {
// pad size to multiple of 64
if (size > 0 && (size % 64)) {
size += 64 - (size % 64);
}
// allocate
if (size > 0) {
ptr = mju_alignedMalloc(size, 64);
}
}
// error if null pointer
if (!ptr && size > 0) {
mju_error("Could not allocate memory");
}
return ptr;
}
// free memory
void mju_free(void* ptr) {
// return if null
if (!ptr) {
return;
}
// free with user or built-in function
if (mju_user_free) {
mju_user_free(ptr);
} else {
mju_alignedFree(ptr);
}
}