Skip to content

Commit

Permalink
Improve DynamicBVH code to make it clearer how the stack/heap works.
Browse files Browse the repository at this point in the history
Inspired by a Coverity issue about possible memcpy usage and overlapping memory.
  • Loading branch information
alesliehughes committed Dec 12, 2023
1 parent 41365c6 commit 4653196
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions core/math/dynamic_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ void DynamicBVH::aabb_query(const AABB &p_box, QueryResult &r_result) {
volume.min = p_box.position;
volume.max = p_box.position + p_box.size;

const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **alloca_stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **stack = alloca_stack;
stack[0] = bvh_root;
int32_t depth = 1;
int32_t threshold = ALLOCA_STACK_SIZE - 2;
Expand All @@ -343,7 +344,8 @@ void DynamicBVH::aabb_query(const AABB &p_box, QueryResult &r_result) {
if (depth > threshold) {
if (aux_stack.is_empty()) {
aux_stack.resize(ALLOCA_STACK_SIZE * 2);
memcpy(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
memcpy(aux_stack.ptr(), alloca_stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
alloca_stack = nullptr;
} else {
aux_stack.resize(aux_stack.size() * 2);
}
Expand Down Expand Up @@ -384,7 +386,8 @@ void DynamicBVH::convex_query(const Plane *p_planes, int p_plane_count, const Ve
}
}

const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **alloca_stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **stack = alloca_stack;
stack[0] = bvh_root;
int32_t depth = 1;
int32_t threshold = ALLOCA_STACK_SIZE - 2;
Expand All @@ -399,7 +402,8 @@ void DynamicBVH::convex_query(const Plane *p_planes, int p_plane_count, const Ve
if (depth > threshold) {
if (aux_stack.is_empty()) {
aux_stack.resize(ALLOCA_STACK_SIZE * 2);
memcpy(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
memcpy(aux_stack.ptr(), alloca_stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
alloca_stack = nullptr;
} else {
aux_stack.resize(aux_stack.size() * 2);
}
Expand Down Expand Up @@ -436,7 +440,8 @@ void DynamicBVH::ray_query(const Vector3 &p_from, const Vector3 &p_to, QueryResu

Vector3 bounds[2];

const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **alloca_stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *));
const Node **stack = alloca_stack;
stack[0] = bvh_root;
int32_t depth = 1;
int32_t threshold = ALLOCA_STACK_SIZE - 2;
Expand All @@ -456,7 +461,8 @@ void DynamicBVH::ray_query(const Vector3 &p_from, const Vector3 &p_to, QueryResu
if (depth > threshold) {
if (aux_stack.is_empty()) {
aux_stack.resize(ALLOCA_STACK_SIZE * 2);
memcpy(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
memcpy(aux_stack.ptr(), alloca_stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
alloca_stack = nullptr;
} else {
aux_stack.resize(aux_stack.size() * 2);
}
Expand Down

0 comments on commit 4653196

Please sign in to comment.