Implementation:Google deepmind Mujoco Engine Util Errmem
| Knowledge Sources | |
|---|---|
| Domains | Physics Simulation, Error Handling, Memory Management |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
Implements error handling, warning reporting, and memory allocation/deallocation utilities for the MuJoCo engine, with support for user-defined handlers and thread-local callbacks.
Description
This file provides MuJoCo's error and memory management infrastructure. The error system supports three levels of handler priority: thread-local handlers (set via _mjPRIVATE__set_tls_error_fn), user-installed global handlers (mju_user_error, mju_user_warning), and default handlers that write to MUJOCO_LOG.TXT and the console. mju_error formats a message and either calls the appropriate handler or exits the process. mju_warning similarly dispatches warnings without exiting. Convenience variants mju_error_i, mju_error_s, mju_warning_i, mju_warning_s handle typed arguments. The memory system provides mju_malloc (64-byte-aligned allocation using platform-specific aligned_alloc or _aligned_malloc, with size padding to 64-byte multiples) and mju_free, both supporting user-defined allocator overrides. mju_clearHandlers resets all user callbacks to NULL.
Usage
Called throughout the engine for error reporting, warning messages, and dynamic memory allocation. User applications can install custom handlers to integrate MuJoCo error reporting with their own logging systems.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: src/engine/engine_util_errmem.c
- Lines: 1-240
Key Functions
// User-installable handlers
void (*mju_user_error)(const char*);
void (*mju_user_warning)(const char*);
void* (*mju_user_malloc)(size_t);
void (*mju_user_free)(void*);
// Handler management
void mju_clearHandlers(void);
// Thread-local error/warning handlers (internal)
callback_fn _mjPRIVATE__get_tls_error_fn(void);
void _mjPRIVATE__set_tls_error_fn(callback_fn h);
callback_fn _mjPRIVATE__get_tls_warning_fn(void);
void _mjPRIVATE__set_tls_warning_fn(callback_fn h);
// Error and warning reporting
void mju_writeLog(const char* type, const char* msg);
void mju_error(const char* msg, ...);
void mju_warning(const char* msg, ...);
void mju_error_i(const char* msg, int i);
void mju_error_s(const char* msg, const char* text);
// Memory management
void* mju_malloc(size_t size);
void mju_free(void* ptr);
Import
#include "engine/engine_util_errmem.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| msg | const char* | Yes | Format string for error or warning message |
| size | size_t | Yes | Number of bytes to allocate |
| ptr | void* | Yes | Pointer to free (NULL-safe) |
| h | callback_fn | No | Handler function to install for thread-local error/warning |
Outputs
| Name | Type | Description |
|---|---|---|
| return value (mju_malloc) | void* | 64-byte-aligned allocated memory, or calls mju_error on failure |
| side effect (mju_error) | - | Calls handler, writes to log, and exits process if no handler installed |
| side effect (mju_warning) | - | Calls handler or writes to log and console |