Rename mj_stackAlloc to mj_stackAllocByte.

PiperOrigin-RevId: 566351365
Change-Id: I91541168e83b05a730113661cc9f07985ac9ff0c
This commit is contained in:
Yuval Tassa
2023-09-18 10:52:46 -07:00
committed by Copybara-Service
parent 812e617fe9
commit 9902b73502
18 changed files with 54 additions and 54 deletions
+4 -4
View File
@@ -1251,12 +1251,12 @@ mj_freeStack
Free the current :ref:`mjData` stack frame. All pointers returned by mj_stackAlloc since the last call Free the current :ref:`mjData` stack frame. All pointers returned by mj_stackAlloc since the last call
to mj_markStack must no longer be used afterwards. to mj_markStack must no longer be used afterwards.
.. _mj_stackAlloc: .. _mj_stackAllocByte:
mj_stackAlloc mj_stackAllocByte
~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
.. mujoco-include:: mj_stackAlloc .. mujoco-include:: mj_stackAllocByte
Allocate a number of bytes on :ref:`mjData` stack at a specific alignment. Allocate a number of bytes on :ref:`mjData` stack at a specific alignment.
Call mju_error on stack overflow. Call mju_error on stack overflow.
+3 -3
View File
@@ -43,10 +43,10 @@ General
:ref:`mj_freeStack`. These functions manage the :ref:`mjData stack<siStack>` in a fully encapsulated way (i.e., :ref:`mj_freeStack`. These functions manage the :ref:`mjData stack<siStack>` in a fully encapsulated way (i.e.,
without introducing a local variable at the call site). without introducing a local variable at the call site).
5. Changed the function :ref:`mj_stackAlloc` to allocate an arbitrary number of bytes, rather than in multiples of 5. Renamed ``mj_stackAlloc`` to :ref:`mj_stackAllocNum`. The new function :ref:`mj_stackAllocByte` allocates an
``sizeof(mjtNum)``, and added an additional argument for specifying the alignment of the returned pointer. arbitrary number of bytes and has an additional argument for specifying the alignment of the returned pointer.
**Migration:** The functionality for allocating ``mjtNum`` arrays is available via :ref:`mj_stackAllocNum`. **Migration:** The functionality for allocating ``mjtNum`` arrays is now available via :ref:`mj_stackAllocNum`.
6. Renamed the ``nstack`` field in :ref:`mjModel` and :ref:`mjData` to ``narena``. Changed ``narena``, ``pstack``, 6. Renamed the ``nstack`` field in :ref:`mjModel` and :ref:`mjData` to ``narena``. Changed ``narena``, ``pstack``,
and ``maxuse_stack`` to count number of bytes rather than number of :ref:`mjtNum` |-| s. and ``maxuse_stack`` to count number of bytes rather than number of :ref:`mjtNum` |-| s.
+1 -1
View File
@@ -2233,7 +2233,7 @@ void mj_resetDataDebug(const mjModel* m, mjData* d, unsigned char debug_value);
void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key); void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key);
void mj_markStack(mjData* d); void mj_markStack(mjData* d);
void mj_freeStack(mjData* d); void mj_freeStack(mjData* d);
void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment); void* mj_stackAllocByte(mjData* d, size_t bytes, size_t alignment);
mjtNum* mj_stackAllocNum(mjData* d, int size); mjtNum* mj_stackAllocNum(mjData* d, int size);
int* mj_stackAllocInt(mjData* d, int size); int* mj_stackAllocInt(mjData* d, int size);
void mj_deleteData(mjData* d); void mj_deleteData(mjData* d);
+1 -1
View File
@@ -721,7 +721,7 @@ The function :ref:`mj_stackAllocNum` checks if there is enough space, and if so
otherwise it triggers an error. It also keeps track of the maximum stack allocation; otherwise it triggers an error. It also keeps track of the maximum stack allocation;
see :ref:`diagnostics <siDiagnostics>` below. Note that :ref:`mj_stackAllocNum` is only used for allocating see :ref:`diagnostics <siDiagnostics>` below. Note that :ref:`mj_stackAllocNum` is only used for allocating
``mjtNum`` arrays, the most common type of array. :ref:`mj_stackAllocInt` is provided for integer array allocation, ``mjtNum`` arrays, the most common type of array. :ref:`mj_stackAllocInt` is provided for integer array allocation,
and :ref:`mj_stackAlloc` is provided for allocation of arbitrary number of bytes and alignment. and :ref:`mj_stackAllocByte` is provided for allocation of arbitrary number of bytes and alignment.
.. _siError: .. _siError:
+1 -1
View File
@@ -194,7 +194,7 @@ MJAPI void mj_freeStack(mjData* d);
// Allocate a number of bytes on mjData stack at a specific alignment. // Allocate a number of bytes on mjData stack at a specific alignment.
// Call mju_error on stack overflow. // Call mju_error on stack overflow.
MJAPI void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment); MJAPI void* mj_stackAllocByte(mjData* d, size_t bytes, size_t alignment);
// Allocate array of mjtNums on mjData stack. Call mju_error on stack overflow. // Allocate array of mjtNums on mjData stack. Call mju_error on stack overflow.
MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size); MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size);
+2 -2
View File
@@ -705,9 +705,9 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([
), ),
doc='Free the current mjData stack frame. All pointers returned by mj_stackAlloc since the last call to mj_markStack must no longer be used afterwards.', # pylint: disable=line-too-long doc='Free the current mjData stack frame. All pointers returned by mj_stackAlloc since the last call to mj_markStack must no longer be used afterwards.', # pylint: disable=line-too-long
)), )),
('mj_stackAlloc', ('mj_stackAllocByte',
FunctionDecl( FunctionDecl(
name='mj_stackAlloc', name='mj_stackAllocByte',
return_type=PointerType( return_type=PointerType(
inner_type=ValueType(name='void'), inner_type=ValueType(name='void'),
), ),
+8 -8
View File
@@ -126,7 +126,7 @@ PYBIND11_MODULE(_functions, pymodule) {
Def<traits::mj_resetData>(pymodule); Def<traits::mj_resetData>(pymodule);
Def<traits::mj_resetDataDebug>(pymodule); Def<traits::mj_resetDataDebug>(pymodule);
Def<traits::mj_resetDataKeyframe>(pymodule); Def<traits::mj_resetDataKeyframe>(pymodule);
// Skipped: mj_stackAlloc (doesn't make sense in Python) // Skipped: mj_stackAllocByte (doesn't make sense in Python)
// Skipped: mj_deleteData (have MjData.__del__) // Skipped: mj_deleteData (have MjData.__del__)
Def<traits::mj_resetCallbacks>(pymodule); Def<traits::mj_resetCallbacks>(pymodule);
Def<traits::mj_setConst>(pymodule); Def<traits::mj_setConst>(pymodule);
@@ -1356,7 +1356,7 @@ PYBIND11_MODULE(_functions, pymodule) {
data->ncon = ncon; data->ncon = ncon;
data->nefc = nefc; data->nefc = nefc;
data->contact = data->contact =
static_cast<raw::MjContact*>(InterceptMjErrors(::mj_arenaAlloc)( static_cast<raw::MjContact*>(InterceptMjErrors(::mj_arenaAllocByte)(
data, ncon * sizeof(raw::MjContact), alignof(raw::MjContact))); data, ncon * sizeof(raw::MjContact), alignof(raw::MjContact)));
if (!data->contact) { if (!data->contact) {
cleanup(data); cleanup(data);
@@ -1367,12 +1367,12 @@ PYBIND11_MODULE(_functions, pymodule) {
#define MJ_M(x) d.metadata().x #define MJ_M(x) d.metadata().x
#undef MJ_D #undef MJ_D
#define MJ_D(x) data->x #define MJ_D(x) data->x
#define X(type, name, nr, nc) \ #define X(type, name, nr, nc) \
data->name = static_cast<type*>(InterceptMjErrors(::mj_arenaAlloc)( \ data->name = static_cast<type*>(InterceptMjErrors(::mj_arenaAllocByte)( \
data, sizeof(type) * (nr) * (nc), alignof(type))); \ data, sizeof(type) * (nr) * (nc), alignof(type))); \
if (!data->name) { \ if (!data->name) { \
cleanup(data); \ cleanup(data); \
throw FatalError("insufficient arena memory available"); \ throw FatalError("insufficient arena memory available"); \
} }
MJDATA_ARENA_POINTERS_PRIMAL MJDATA_ARENA_POINTERS_PRIMAL
+1 -1
View File
@@ -22,7 +22,7 @@
// They should be regarded as part of MuJoCo's internal implementation detail. // They should be regarded as part of MuJoCo's internal implementation detail.
extern "C" { extern "C" {
MJAPI void _mjPRIVATE__set_tls_error_fn(void (*h)(const char*)); MJAPI void _mjPRIVATE__set_tls_error_fn(void (*h)(const char*));
MJAPI void* mj_arenaAlloc(mjData* d, int bytes, int alignment); MJAPI void* mj_arenaAllocByte(mjData* d, int bytes, int alignment);
} }
#endif // MUJOCO_PYTHON_PRIVATE_H_ #endif // MUJOCO_PYTHON_PRIVATE_H_
+5 -5
View File
@@ -820,11 +820,11 @@ MjDataWrapper MjDataWrapper::Deserialize(std::istream& input) {
#define MJ_M(x) m.x #define MJ_M(x) m.x
#undef MJ_D #undef MJ_D
#define MJ_D(x) d->x #define MJ_D(x) d->x
#define X(type, name, nr, nc) \ #define X(type, name, nr, nc) \
if ((nr) * (nc)) { \ if ((nr) * (nc)) { \
d->name = static_cast<decltype(d->name)>( \ d->name = static_cast<decltype(d->name)>( \
mj_arenaAlloc(d, sizeof(type) * (nr) * (nc), alignof(type))); \ mj_arenaAllocByte(d, sizeof(type) * (nr) * (nc), alignof(type))); \
ReadBytes(input, d->name, sizeof(type) * (nr) * (nc)); \ ReadBytes(input, d->name, sizeof(type) * (nr) * (nc)); \
} }
MJDATA_ARENA_POINTERS_CONTACT MJDATA_ARENA_POINTERS_CONTACT
+4 -4
View File
@@ -223,7 +223,7 @@ int mj_collideOBB(const mjtNum aabb1[6], const mjtNum aabb2[6],
} }
static mjCollisionTree* mj_stackAllocTree(mjData* d, int max_stack) { static mjCollisionTree* mj_stackAllocTree(mjData* d, int max_stack) {
return (mjCollisionTree*) mj_stackAlloc( return (mjCollisionTree*) mj_stackAllocByte(
d, max_stack * sizeof(mjCollisionTree), _Alignof(mjCollisionTree)); d, max_stack * sizeof(mjCollisionTree), _Alignof(mjCollisionTree));
} }
@@ -770,9 +770,9 @@ int mj_broadphase(const mjModel* m, mjData* d, int* pair, int maxpair) {
} }
// allocate sort buffer // allocate sort buffer
sortbuf = (mjtBroadphase*) mj_stackAlloc( sortbuf = (mjtBroadphase*) mj_stackAllocByte(
d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase)); d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase));
activebuf = (mjtBroadphase*) mj_stackAlloc( activebuf = (mjtBroadphase*) mj_stackAllocByte(
d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase)); d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase));
// init sortbuf with axis0 // init sortbuf with axis0
@@ -955,7 +955,7 @@ static void collideGeoms(const mjModel* m, mjData* d,
// allocate mjContact[mjMAXCONPAIR] on the arena // allocate mjContact[mjMAXCONPAIR] on the arena
mjContact* con = mjContact* con =
(mjContact*) mj_arenaAlloc(d, sizeof(mjContact) * mjMAXCONPAIR, _Alignof(mjContact)); (mjContact*) mj_arenaAllocByte(d, sizeof(mjContact) * mjMAXCONPAIR, _Alignof(mjContact));
if (!con) { if (!con) {
mj_warning(d, mjWARN_CONTACTFULL, d->ncon); mj_warning(d, mjWARN_CONTACTFULL, d->ncon);
return; return;
+1 -1
View File
@@ -459,7 +459,7 @@ static void collideBVH(const mjModel* m, mjData* d, int g,
int node; int node;
}; };
typedef struct CollideTreeArgs_ CollideTreeArgs; typedef struct CollideTreeArgs_ CollideTreeArgs;
CollideTreeArgs* stack = (CollideTreeArgs*) mj_stackAlloc( CollideTreeArgs* stack = (CollideTreeArgs*) mj_stackAllocByte(
d, max_stack * sizeof(CollideTreeArgs), _Alignof(CollideTreeArgs)); d, max_stack * sizeof(CollideTreeArgs), _Alignof(CollideTreeArgs));
int nstack = 0; int nstack = 0;
+8 -8
View File
@@ -61,13 +61,13 @@ static int arenaAllocEfc(const mjModel* m, mjData* d) {
(char*)d->arena + d->parena, d->narena - d->pstack - d->parena); (char*)d->arena + d->parena, d->narena - d->pstack - d->parena);
#endif #endif
#define X(type, name, nr, nc) \ #define X(type, name, nr, nc) \
d->name = mj_arenaAlloc(d, sizeof(type) * (nr) * (nc), _Alignof(type)); \ d->name = mj_arenaAllocByte(d, sizeof(type) * (nr) * (nc), _Alignof(type)); \
if (!d->name) { \ if (!d->name) { \
mj_warning(d, mjWARN_CNSTRFULL, d->narena); \ mj_warning(d, mjWARN_CNSTRFULL, d->narena); \
mj_clearEfc(d); \ mj_clearEfc(d); \
d->parena = d->ncon * sizeof(mjContact); \ d->parena = d->ncon * sizeof(mjContact); \
return 0; \ return 0; \
} }
MJDATA_ARENA_POINTERS_PRIMAL MJDATA_ARENA_POINTERS_PRIMAL
@@ -171,7 +171,7 @@ int mj_addContact(const mjModel* m, mjData* d, const mjContact* con) {
mj_clearEfc(d); mj_clearEfc(d);
// copy contact // copy contact
mjContact* dst = mj_arenaAlloc(d, sizeof(mjContact), _Alignof(mjContact)); mjContact* dst = mj_arenaAllocByte(d, sizeof(mjContact), _Alignof(mjContact));
if (!dst) { if (!dst) {
mj_warning(d, mjWARN_CONTACTFULL, d->ncon); mj_warning(d, mjWARN_CONTACTFULL, d->ncon);
return 1; return 1;
+2 -2
View File
@@ -1217,7 +1217,7 @@ mjData* mj_copyData(mjData* dest, const mjModel* m, const mjData* src) {
// allocate memory from the mjData arena // allocate memory from the mjData arena
void* mj_arenaAlloc(mjData* d, size_t bytes, size_t alignment) { void* mj_arenaAllocByte(mjData* d, size_t bytes, size_t alignment) {
size_t misalignment = fastmod(d->parena, alignment); size_t misalignment = fastmod(d->parena, alignment);
size_t padding = misalignment ? alignment - misalignment : 0; size_t padding = misalignment ? alignment - misalignment : 0;
@@ -1374,7 +1374,7 @@ void mj_freeStack(mjData* d) {
#endif #endif
} }
void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment) { void* mj_stackAllocByte(mjData* d, size_t bytes, size_t alignment) {
return stackalloc(d, bytes, alignment); return stackalloc(d, bytes, alignment);
} }
+2 -2
View File
@@ -104,7 +104,7 @@ MJAPI void mj_resetDataDebug(const mjModel* m, mjData* d, unsigned char debug_va
MJAPI void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key); MJAPI void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key);
// mjData arena allocate // mjData arena allocate
MJAPI void* mj_arenaAlloc(mjData* d, size_t bytes, size_t alignment); MJAPI void* mj_arenaAllocByte(mjData* d, size_t bytes, size_t alignment);
// mjData mark stack frame // mjData mark stack frame
MJAPI void mj_markStack(mjData* d); MJAPI void mj_markStack(mjData* d);
@@ -113,7 +113,7 @@ MJAPI void mj_markStack(mjData* d);
MJAPI void mj_freeStack(mjData* d); MJAPI void mj_freeStack(mjData* d);
// mjData stack allocate // mjData stack allocate
MJAPI void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment); MJAPI void* mj_stackAllocByte(mjData* d, size_t bytes, size_t alignment);
// mjData stack allocate for array of mjtNums // mjData stack allocate for array of mjtNums
MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size); MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size);
+6 -6
View File
@@ -113,12 +113,12 @@ static int arenaAllocIsland(const mjModel* m, mjData* d) {
size_t parena_old = d->parena; size_t parena_old = d->parena;
#define X(type, name, nr, nc) \ #define X(type, name, nr, nc) \
d->name = mj_arenaAlloc(d, sizeof(type) * (nr) * (nc), _Alignof(type)); \ d->name = mj_arenaAllocByte(d, sizeof(type) * (nr) * (nc), _Alignof(type)); \
if (!d->name) { \ if (!d->name) { \
mj_warning(d, mjWARN_CNSTRFULL, d->narena); \ mj_warning(d, mjWARN_CNSTRFULL, d->narena); \
clearIsland(d, parena_old); \ clearIsland(d, parena_old); \
return 0; \ return 0; \
} }
MJDATA_ARENA_POINTERS_ISLAND MJDATA_ARENA_POINTERS_ISLAND
+2 -2
View File
@@ -24,7 +24,7 @@
// stack allocate and initialize new mjArrayList // stack allocate and initialize new mjArrayList
mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_capacity) { mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_capacity) {
mjArrayList* array_list = (mjArrayList*) mj_stackAlloc( mjArrayList* array_list = (mjArrayList*) mj_stackAllocByte(
d, sizeof(mjArrayList), _Alignof(mjArrayList)); d, sizeof(mjArrayList), _Alignof(mjArrayList));
initial_capacity = mjMAX(1, initial_capacity); initial_capacity = mjMAX(1, initial_capacity);
array_list->d = d; array_list->d = d;
@@ -34,7 +34,7 @@ mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_
array_list->next_segment = NULL; array_list->next_segment = NULL;
// allocate array list buffer // allocate array list buffer
array_list->buffer = (void*) mj_stackAlloc( array_list->buffer = (void*) mj_stackAllocByte(
d, element_size * initial_capacity, _Alignof(mjtMaxAlign)); d, element_size * initial_capacity, _Alignof(mjtMaxAlign));
return array_list; return array_list;
} }
+2 -2
View File
@@ -830,8 +830,8 @@ TEST_F(EngineIoTest, RedZoneAlignmentTest) {
ASSERT_THAT(data, NotNull()); ASSERT_THAT(data, NotNull());
mj_markStack(data); mj_markStack(data);
mj_stackAlloc(data, 1, 1); mj_stackAllocByte(data, 1, 1);
mj_stackAlloc(data, 1, 1); mj_stackAllocByte(data, 1, 1);
mj_freeStack(data); mj_freeStack(data);
mj_deleteData(data); mj_deleteData(data);
+1 -1
View File
@@ -6110,7 +6110,7 @@ public static unsafe extern void mj_markStack(mjData_* d);
public static unsafe extern void mj_freeStack(mjData_* d); public static unsafe extern void mj_freeStack(mjData_* d);
[DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)] [DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void* mj_stackAlloc(mjData_* d, UIntPtr bytes, UIntPtr alignment); public static unsafe extern void* mj_stackAllocByte(mjData_* d, UIntPtr bytes, UIntPtr alignment);
[DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)] [DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern double* mj_stackAllocNum(mjData_* d, int size); public static unsafe extern double* mj_stackAllocNum(mjData_* d, int size);