Rename mj_stackAllocByte to mj_stackAlloc and make it public.
PiperOrigin-RevId: 561133040 Change-Id: I4f5ffe51c1287aa0c82af68bfe17ed6dd2ad09da
This commit is contained in:
committed by
Copybara-Service
parent
a48e0aa355
commit
4c899b92cc
@@ -208,7 +208,8 @@ int mj_collideOBB(const mjtNum aabb1[6], const mjtNum aabb2[6],
|
||||
}
|
||||
|
||||
static mjCollisionTree* mj_stackAllocTree(mjData* d, int max_stack) {
|
||||
return (mjCollisionTree*)mj_stackAllocByte(d, max_stack * sizeof(mjCollisionTree*));
|
||||
return (mjCollisionTree*) mj_stackAlloc(
|
||||
d, max_stack * sizeof(mjCollisionTree), _Alignof(mjCollisionTree));
|
||||
}
|
||||
|
||||
// binary search between two body trees
|
||||
@@ -751,8 +752,10 @@ int mj_broadphase(const mjModel* m, mjData* d, int* pair, int maxpair) {
|
||||
}
|
||||
|
||||
// allocate sort buffer
|
||||
sortbuf = (mjtBroadphase*)mj_stackAllocByte(d, 2 * bufcnt * sizeof(mjtBroadphase));
|
||||
activebuf = (mjtBroadphase*)mj_stackAllocByte(d, 2 *bufcnt * sizeof(mjtBroadphase));
|
||||
sortbuf = (mjtBroadphase*) mj_stackAlloc(
|
||||
d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase));
|
||||
activebuf = (mjtBroadphase*) mj_stackAlloc(
|
||||
d, 2 * bufcnt * sizeof(mjtBroadphase), _Alignof(mjtBroadphase));
|
||||
|
||||
// init sortbuf with axis0
|
||||
int k = 0;
|
||||
|
||||
@@ -21,19 +21,10 @@
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#elif !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
|
||||
// No C11 support in Visual Studio 2019 update 7 and earlier.
|
||||
// However, MSVC allows C++ alignas to be used in C code, so
|
||||
// we can just skip the #include <stdalign.h>.
|
||||
#ifndef _MSC_VER
|
||||
#error "Compiler does not support C11."
|
||||
#endif
|
||||
#else
|
||||
#include <stdalign.h>
|
||||
#endif
|
||||
|
||||
struct mjCollisionTree_ {
|
||||
alignas(mjtNum) int node1;
|
||||
int node1;
|
||||
int node2;
|
||||
};
|
||||
typedef struct mjCollisionTree_ mjCollisionTree;
|
||||
|
||||
@@ -456,8 +456,8 @@ static void collideBVH(const mjModel* m, mjData* d, int g,
|
||||
int node;
|
||||
};
|
||||
typedef struct CollideTreeArgs_ CollideTreeArgs;
|
||||
CollideTreeArgs* stack = (CollideTreeArgs*) mj_stackAllocByte(
|
||||
d, max_stack * sizeof(CollideTreeArgs));
|
||||
CollideTreeArgs* stack = (CollideTreeArgs*) mj_stackAlloc(
|
||||
d, max_stack * sizeof(CollideTreeArgs), _Alignof(CollideTreeArgs));
|
||||
|
||||
int nstack = 0;
|
||||
stack[nstack].node = 0;
|
||||
|
||||
@@ -64,7 +64,9 @@
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__cplusplus)
|
||||
typedef long double max_align_t;
|
||||
typedef long double mjtMaxAlign;
|
||||
#else
|
||||
typedef max_align_t mjtMaxAlign;
|
||||
#endif
|
||||
|
||||
#endif // MUJOCO_SRC_ENGINE_ENGINE_CROSSPLATFORM_H_
|
||||
|
||||
+15
-11
@@ -27,7 +27,6 @@
|
||||
#include <mujoco/mjplugin.h>
|
||||
#include <mujoco/mjxmacro.h>
|
||||
#include "engine/engine_array_safety.h" // IWYU pragma: keep
|
||||
#include "engine/engine_crossplatform.h" // IWYU pragma: keep
|
||||
#include "engine/engine_resource.h"
|
||||
#include "engine/engine_macro.h"
|
||||
#include "engine/engine_plugin.h"
|
||||
@@ -1200,9 +1199,9 @@ mjData* mj_copyData(mjData* dest, const mjModel* m, const mjData* src) {
|
||||
|
||||
|
||||
// allocate memory from the mjData arena
|
||||
void* mj_arenaAlloc(mjData* d, int bytes, int alignment) {
|
||||
int misalignment = d->parena % alignment;
|
||||
int padding = misalignment ? alignment - misalignment : 0;
|
||||
void* mj_arenaAlloc(mjData* d, size_t bytes, size_t alignment) {
|
||||
size_t misalignment = d->parena % alignment;
|
||||
size_t padding = misalignment ? alignment - misalignment : 0;
|
||||
|
||||
// check size
|
||||
size_t bytes_available = d->narena - d->pstack;
|
||||
@@ -1228,8 +1227,9 @@ void* mj_arenaAlloc(mjData* d, int bytes, int alignment) {
|
||||
|
||||
|
||||
|
||||
// allocate size bytes on the mjData stack
|
||||
void* mj_stackAllocByte(mjData* d, size_t size) {
|
||||
// internal: allocate size bytes on the mjData stack
|
||||
// declared inline so that modular arithmetic with specific alignments can be optimized out
|
||||
static inline void* stackalloc(mjData* d, size_t size, size_t alignment) {
|
||||
// return NULL if empty
|
||||
if (!size) {
|
||||
return NULL;
|
||||
@@ -1254,8 +1254,8 @@ void* mj_stackAllocByte(mjData* d, size_t size) {
|
||||
// start of the memory to be allocated to the buffer
|
||||
uintptr_t start_ptr = end_ptr - (size + mjREDZONE);
|
||||
|
||||
// move start_ptr back to align to max_align_t
|
||||
start_ptr -= start_ptr % _Alignof(max_align_t); // NOLINT
|
||||
// align the pointer
|
||||
start_ptr -= start_ptr % alignment;
|
||||
|
||||
// new top of the stack
|
||||
uintptr_t new_pstack_ptr = start_ptr - mjREDZONE;
|
||||
@@ -1269,7 +1269,7 @@ void* mj_stackAllocByte(mjData* d, size_t size) {
|
||||
size_t stack_available_bytes = end_ptr - ((uintptr_t)d->arena + d->parena);
|
||||
size_t stack_required_bytes = end_ptr - new_pstack_ptr;
|
||||
if (stack_required_bytes > stack_available_bytes) {
|
||||
mjERROR("stack overflow: max = %zu, available = %zu, requested = %zu "
|
||||
mju_error("mjData stack overflow: max = %zu, available = %zu, requested = %zu "
|
||||
"(ne = %d, nf = %d, nefc = %d, ncon = %d)",
|
||||
stack_size_bytes, stack_available_bytes, stack_required_bytes,
|
||||
d->ne, d->nf, d->nefc, d->ncon);
|
||||
@@ -1303,12 +1303,16 @@ void* mj_stackAllocByte(mjData* d, size_t size) {
|
||||
return (void*)start_ptr;
|
||||
}
|
||||
|
||||
void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment) {
|
||||
return stackalloc(d, bytes, alignment);
|
||||
}
|
||||
|
||||
mjtNum* mj_stackAllocNum(mjData* d, int size) {
|
||||
return (mjtNum*)mj_stackAllocByte(d, size * sizeof(mjtNum));
|
||||
return (mjtNum*) stackalloc(d, size * sizeof(mjtNum), _Alignof(mjtNum));
|
||||
}
|
||||
|
||||
int* mj_stackAllocInt(mjData* d, int size) {
|
||||
return (int*)mj_stackAllocByte(d, size * sizeof(int));
|
||||
return (int*) stackalloc(d, size * sizeof(int), _Alignof(int));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,10 @@
|
||||
#include <mujoco/mjmodel.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstddef>
|
||||
extern "C" {
|
||||
#else
|
||||
#include <stddef.h>
|
||||
#endif
|
||||
|
||||
// internal hash map size factor (2 corresponds to a load factor of 0.5)
|
||||
@@ -100,7 +103,10 @@ MJAPI void mj_resetDataDebug(const mjModel* m, mjData* d, unsigned char debug_va
|
||||
MJAPI void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key);
|
||||
|
||||
// mjData arena allocate
|
||||
MJAPI void* mj_arenaAlloc(mjData* d, int bytes, int alignment);
|
||||
MJAPI void* mj_arenaAlloc(mjData* d, size_t bytes, size_t alignment);
|
||||
|
||||
// mjData stack allocate
|
||||
MJAPI void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment);
|
||||
|
||||
// mjData stack allocate for array of mjtNums
|
||||
MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size);
|
||||
@@ -108,9 +114,6 @@ MJAPI mjtNum* mj_stackAllocNum(mjData* d, int size);
|
||||
// mjData stack allocate for array of ints
|
||||
MJAPI int* mj_stackAllocInt(mjData* d, int size);
|
||||
|
||||
// mjData stack allocate for a specific size of bytes
|
||||
MJAPI void* mj_stackAllocByte(mjData* d, size_t size);
|
||||
|
||||
// de-allocate data
|
||||
MJAPI void mj_deleteData(mjData* d);
|
||||
|
||||
|
||||
@@ -13,16 +13,19 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "engine/engine_util_container.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "engine/engine_crossplatform.h"
|
||||
#include "engine/engine_io.h"
|
||||
|
||||
// stack allocate and initialize new mjArrayList
|
||||
mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_capacity) {
|
||||
mjArrayList* array_list = (mjArrayList*) mj_stackAllocByte(d, sizeof(mjArrayList));
|
||||
mjArrayList* array_list = (mjArrayList*) mj_stackAlloc(
|
||||
d, sizeof(mjArrayList), _Alignof(mjArrayList));
|
||||
initial_capacity = mjMAX(1, initial_capacity);
|
||||
array_list->d = d;
|
||||
array_list->element_size = element_size;
|
||||
@@ -31,7 +34,8 @@ mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_
|
||||
array_list->next_segment = NULL;
|
||||
|
||||
// allocate array list buffer
|
||||
array_list->buffer = (void*) mj_stackAllocByte(d, element_size * initial_capacity);
|
||||
array_list->buffer = (void*) mj_stackAlloc(
|
||||
d, element_size * initial_capacity, _Alignof(mjtMaxAlign));
|
||||
return array_list;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user