Remove engine_util_container and its test.
PiperOrigin-RevId: 945701044 Change-Id: I6ca3d4fc87cdcd9e3fc14ee49b3aa441a70cd8bd
This commit is contained in:
committed by
Copybara-Service
parent
0cb3fad654
commit
d6650f84e5
@@ -1,94 +0,0 @@
|
|||||||
// Copyright 2023 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_container.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <mujoco/mujoco.h>
|
|
||||||
#include "engine/engine_crossplatform.h"
|
|
||||||
#include "engine/engine_memory.h"
|
|
||||||
|
|
||||||
// stack allocate and initialize new mjArrayList
|
|
||||||
mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_capacity) {
|
|
||||||
mjArrayList* array_list = mjSTACKALLOC(d, 1, mjArrayList);
|
|
||||||
initial_capacity = mjMAX(1, initial_capacity);
|
|
||||||
array_list->d = d;
|
|
||||||
array_list->element_size = element_size;
|
|
||||||
array_list->capacity = initial_capacity;
|
|
||||||
array_list->size = 0;
|
|
||||||
array_list->next_segment = NULL;
|
|
||||||
|
|
||||||
// allocate array list buffer
|
|
||||||
array_list->buffer = (void*) mj_stackAllocByte(
|
|
||||||
d, element_size * initial_capacity, _Alignof(mjtMaxAlign));
|
|
||||||
return array_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// returns total number of elements in mjArrayList
|
|
||||||
size_t mju_arrayListSize(const mjArrayList* array_list) {
|
|
||||||
const mjArrayList* cursor = array_list;
|
|
||||||
size_t array_list_size = 0;
|
|
||||||
while (cursor) {
|
|
||||||
array_list_size += cursor->size;
|
|
||||||
cursor = cursor->next_segment;
|
|
||||||
}
|
|
||||||
return array_list_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// copies one element into an mjArrayList
|
|
||||||
void mju_arrayListAdd(mjArrayList* array_list, void* element) {
|
|
||||||
mjArrayList* cursor = array_list;
|
|
||||||
|
|
||||||
// find a non full segment or add a new segment
|
|
||||||
while (cursor->size == cursor->capacity) {
|
|
||||||
if (cursor->next_segment == NULL) {
|
|
||||||
// add a new segment with twice the capacity of the last segment
|
|
||||||
cursor->next_segment = mju_arrayListCreate(
|
|
||||||
cursor->d, cursor->element_size, 2 * cursor->capacity);
|
|
||||||
}
|
|
||||||
cursor = cursor->next_segment;
|
|
||||||
}
|
|
||||||
// copy element into segment
|
|
||||||
memcpy((mjtByte*)cursor->buffer + cursor->element_size * cursor->size,
|
|
||||||
element, cursor->element_size);
|
|
||||||
++cursor->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// returns pointer to element at index, NULL if out of bounds
|
|
||||||
void* mju_arrayListAt(const mjArrayList* array_list, size_t index) {
|
|
||||||
// if the index is larger than the current capacity, then it is in a later segment
|
|
||||||
const mjArrayList* cursor = array_list;
|
|
||||||
size_t total_capacity = 0;
|
|
||||||
while (cursor != NULL && index >= total_capacity + cursor->capacity) {
|
|
||||||
total_capacity += cursor->capacity;
|
|
||||||
cursor = cursor->next_segment;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cursor) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index - total_capacity >= cursor->size) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (mjtByte*)cursor->buffer + (cursor->element_size * (index - total_capacity));
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
// Copyright 2023 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.
|
|
||||||
|
|
||||||
#ifndef MUJOCO_SRC_ENGINE_ENGINE_UTIL_CONTAINER_H_
|
|
||||||
#define MUJOCO_SRC_ENGINE_ENGINE_UTIL_CONTAINER_H_
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include <mujoco/mjdata.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//---------------------------------- mjArrayList ---------------------------------------------------
|
|
||||||
struct mjArrayList_ {
|
|
||||||
// pointer to mjData to allow for growth of the list
|
|
||||||
mjData* d;
|
|
||||||
|
|
||||||
// size of element
|
|
||||||
size_t element_size;
|
|
||||||
|
|
||||||
// maximum number of elements
|
|
||||||
size_t capacity;
|
|
||||||
|
|
||||||
// number of elements in this list
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
// pointer to the next segment of the array list, NULL if last
|
|
||||||
struct mjArrayList_* next_segment;
|
|
||||||
|
|
||||||
// buffer for data in this segment stored in d->arena
|
|
||||||
void* buffer;
|
|
||||||
};
|
|
||||||
typedef struct mjArrayList_ mjArrayList;
|
|
||||||
|
|
||||||
// stack allocate and initialize new mjArrayList
|
|
||||||
mjArrayList* mju_arrayListCreate(mjData* d, size_t element_size, size_t initial_capacity);
|
|
||||||
|
|
||||||
// returns total number of elements in mjArrayList
|
|
||||||
size_t mju_arrayListSize(const mjArrayList* array_list);
|
|
||||||
|
|
||||||
// copies an element into an mjArrayList
|
|
||||||
void mju_arrayListAdd(mjArrayList* array_list, void* element);
|
|
||||||
|
|
||||||
// returns a pointer to the element at the specified location of the arraylist
|
|
||||||
// NULL returned if index is not in the mjArrayList
|
|
||||||
void* mju_arrayListAt(const mjArrayList* array_list, size_t index);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif // MUJOCO_SRC_ENGINE_ENGINE_UTIL_CONTAINER_H_
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
// Copyright 2023 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 "src/engine/engine_util_container.h"
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
#include <gmock/gmock.h>
|
|
||||||
#include <gtest/gtest.h>
|
|
||||||
#include <mujoco/mjdata.h>
|
|
||||||
#include <mujoco/mujoco.h>
|
|
||||||
#include "test/fixture.h"
|
|
||||||
|
|
||||||
namespace mujoco {
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using testing::NotNull;
|
|
||||||
|
|
||||||
template <int prev_size, typename T, int N, int capacity>
|
|
||||||
constexpr int GetExpectedStackUsageBytes() {
|
|
||||||
if constexpr (N <= 0) {
|
|
||||||
return prev_size;
|
|
||||||
} else {
|
|
||||||
constexpr auto RoundUpToAlignment = [](int x, int alignment) {
|
|
||||||
return alignment * (x / alignment + ((x % alignment) ? 1 : 0));
|
|
||||||
};
|
|
||||||
constexpr int size_with_arraylist = RoundUpToAlignment(
|
|
||||||
prev_size + sizeof(mjArrayList), alignof(mjArrayList));
|
|
||||||
constexpr int size_with_buffer = RoundUpToAlignment(
|
|
||||||
size_with_arraylist + capacity * sizeof(T), alignof(std::max_align_t));
|
|
||||||
return GetExpectedStackUsageBytes<size_with_buffer, T, N - capacity,
|
|
||||||
2 * capacity>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestMjArrayList : public MujocoTest {};
|
|
||||||
|
|
||||||
TEST_F(TestMjArrayList, TestMjArrayListSingleThreaded) {
|
|
||||||
std::array<char, 1024> error;
|
|
||||||
MjModelPtr m = LoadModelFromString("<mujoco/>", error.data(), error.size());
|
|
||||||
ASSERT_THAT(m.get(), NotNull()) << "Failed to load model: " << error.data();
|
|
||||||
MjDataPtr d = MakeData(m);
|
|
||||||
mj_markStack(d.get());
|
|
||||||
|
|
||||||
using DataType = int;
|
|
||||||
constexpr int kInitialCapacity = 10;
|
|
||||||
mjArrayList* array_list =
|
|
||||||
mju_arrayListCreate(d.get(), sizeof(DataType), kInitialCapacity);
|
|
||||||
|
|
||||||
constexpr int kNumElements = 35;
|
|
||||||
for (int i = 0; i < kNumElements; ++i) {
|
|
||||||
mju_arrayListAdd(array_list, &i);
|
|
||||||
}
|
|
||||||
EXPECT_EQ(mju_arrayListSize(array_list), kNumElements);
|
|
||||||
|
|
||||||
constexpr int kFrameMarkerSize = 2 * sizeof(size_t) + sizeof(void*);
|
|
||||||
constexpr int kExpectedMaxUseStack =
|
|
||||||
GetExpectedStackUsageBytes<kFrameMarkerSize, DataType, kNumElements,
|
|
||||||
kInitialCapacity>();
|
|
||||||
EXPECT_EQ(d->maxuse_stack, kExpectedMaxUseStack);
|
|
||||||
|
|
||||||
for (int i = 0; i < kNumElements; ++i) {
|
|
||||||
EXPECT_EQ(*static_cast<int*>(mju_arrayListAt(array_list, i)), i);
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_EQ(mju_arrayListAt(array_list, kNumElements), nullptr);
|
|
||||||
EXPECT_EQ(mju_arrayListAt(array_list, 100), nullptr);
|
|
||||||
|
|
||||||
mj_freeStack(d.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(TestMjArrayList, ZeroInitialCapacity) {
|
|
||||||
char error[1024];
|
|
||||||
MjModelPtr m = LoadModelFromString("<mujoco/>", error, sizeof(error));
|
|
||||||
ASSERT_THAT(m.get(), NotNull()) << "Failed to load model: " << error;
|
|
||||||
MjDataPtr d = MakeData(m);
|
|
||||||
mj_markStack(d.get());
|
|
||||||
mjArrayList* array_list =
|
|
||||||
mju_arrayListCreate(d.get(), sizeof(double), /*initial_capacity=*/0);
|
|
||||||
EXPECT_EQ(mju_arrayListSize(array_list), 0);
|
|
||||||
|
|
||||||
for (int i = 0; i < 35; ++i) {
|
|
||||||
double value = i;
|
|
||||||
mju_arrayListAdd(array_list, &value);
|
|
||||||
}
|
|
||||||
EXPECT_EQ(mju_arrayListSize(array_list), 35);
|
|
||||||
|
|
||||||
for (int i = 0; i < 35; ++i) {
|
|
||||||
EXPECT_EQ(*static_cast<double*>(mju_arrayListAt(array_list, i)), i);
|
|
||||||
}
|
|
||||||
EXPECT_EQ(mju_arrayListAt(array_list, 35), nullptr);
|
|
||||||
|
|
||||||
mj_freeStack(d.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
} // namespace mujoco
|
|
||||||
Reference in New Issue
Block a user