Format memory warning in human-friendly way.

PiperOrigin-RevId: 486610408
Change-Id: I6beecb12059e69f4407e921d20efc54ce6b3f491
This commit is contained in:
Alessio Quaglino
2022-11-07 02:55:37 -08:00
committed by Copybara-Service
parent fc24097b92
commit fd4d47e4a5
11 changed files with 107 additions and 29 deletions
+7
View File
@@ -33,6 +33,13 @@
#define mjMAX(a, b) (((a) > (b)) ? (a) : (b))
#define mjMIN(a, b) (((a) < (b)) ? (a) : (b))
// thread local macro
#ifdef _MSC_VER
#define mjTHREADLOCAL __declspec(thread)
#else
#define mjTHREADLOCAL _Thread_local
#endif
//-------------------------- timer macros ----------------------------------------------------------
+1 -5
View File
@@ -24,6 +24,7 @@
#include <unistd.h>
#endif
#include "engine/engine_macro.h"
//------------------------- cross-platform aligned malloc/free -------------------------------------
static inline void* mju_alignedMalloc(size_t size, size_t align) {
@@ -73,11 +74,6 @@ void mju_clearHandlers(void) {
typedef void (*callback_fn)(const char*);
#ifdef _MSC_VER
#define mjTHREADLOCAL __declspec(thread)
#else
#define mjTHREADLOCAL _Thread_local
#endif
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_error_fn = NULL;
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_warning_fn = NULL;
+23 -2
View File
@@ -946,9 +946,30 @@ int mju_str2Type(const char* str) {
// return human readable number of bytes using standard letter suffix
const char* mju_writeNumBytes(const size_t nbytes) {
int i;
static mjTHREADLOCAL char message[20];
static const char suffix[] = " KMGTPE";
for (i=0; i<6; i++) {
const size_t bits = (size_t)(1) << (10*(6-i));
if (nbytes >= bits && !(nbytes & (bits - 1))) {
break;
}
}
if (i<6) {
mjSNPRINTF(message, "%zu%c", nbytes >> (10*(6-i)), suffix[6-i]);
} else {
mjSNPRINTF(message, "%zu", nbytes >> (10*(6-i)));
}
return message;
}
// warning text
const char* mju_warningText(int warning, size_t info) {
static char str[1000];
static mjTHREADLOCAL char str[1000];
switch (warning) {
case mjWARN_INERTIA:
@@ -965,7 +986,7 @@ const char* mju_warningText(int warning, size_t info) {
case mjWARN_CNSTRFULL:
mjSNPRINTF(str,
"Insufficient arena memory for the number of constraints generated. "
"Increase arena memory allocation above %zu bytes.", info);
"Increase arena memory allocation above %s bytes.", mju_writeNumBytes(info));
break;
case mjWARN_VGEOMFULL:
+3
View File
@@ -87,6 +87,9 @@ MJAPI const char* mju_type2Str(int type);
// convert type name to type id (mjtObj)
MJAPI int mju_str2Type(const char* str);
// return human readable number of bytes using standard letter suffix
MJAPI const char* mju_writeNumBytes(const size_t nbytes);
// warning text
MJAPI const char* mju_warningText(int warning, size_t info);