Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change GLOBAL_ALIGNMENT to 8 bytes #316

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/reactphysics3d/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ constexpr uint16 NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD = 256;
constexpr decimal SAME_CONTACT_POINT_DISTANCE_THRESHOLD = decimal(0.01);

/// Global alignment (in bytes) that all allocators must enforce
constexpr uint8 GLOBAL_ALIGNMENT = 16;
constexpr uint8 GLOBAL_ALIGNMENT = 8;

/// Current version of ReactPhysics3D
const std::string RP3D_VERSION = std::string("0.9.0");
Expand Down
6 changes: 3 additions & 3 deletions include/reactphysics3d/memory/DefaultAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace reactphysics3d {
// Class DefaultAllocator
/**
* This class represents a default memory allocator that uses standard C++ functions
* to allocated 16-bytes aligned memory.
* to allocated 8-bytes aligned memory.
*
*/
class DefaultAllocator : public MemoryAllocator {
Expand All @@ -53,7 +53,7 @@ class DefaultAllocator : public MemoryAllocator {
DefaultAllocator& operator=(DefaultAllocator& allocator) = default;

/// Allocate memory of a given size (in bytes) and return a pointer to the
/// allocated memory. The returned allocated memory must be 16 bytes aligned.
/// allocated memory. The returned allocated memory must be 8 bytes aligned.
virtual void* allocate(size_t size) override {

// If compiler is Visual Studio
Expand All @@ -63,7 +63,7 @@ class DefaultAllocator : public MemoryAllocator {
return _aligned_malloc(size, GLOBAL_ALIGNMENT);
#else

// Return 16-bytes aligned memory
// Return 8-bytes aligned memory
return std::aligned_alloc(GLOBAL_ALIGNMENT, size);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion include/reactphysics3d/memory/MemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MemoryAllocator {
MemoryAllocator& operator=(MemoryAllocator& allocator) = default;

/// Allocate memory of a given size (in bytes) and return a pointer to the
/// allocated memory. The return allocated memory must be 16 bytes aligned.
/// allocated memory. The return allocated memory must be 8 bytes aligned.
virtual void* allocate(size_t size)=0;

/// Release previously allocated memory.
Expand Down
2 changes: 1 addition & 1 deletion include/reactphysics3d/memory/MemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ RP3D_FORCE_INLINE void* MemoryManager::allocate(AllocationType allocationType, s
case AllocationType::Frame: allocatedMemory = mSingleFrameAllocator.allocate(size); break;
}

assert(allocatedMemory == nullptr || reinterpret_cast<uintptr_t>(allocatedMemory) % 16 == 0);
assert(allocatedMemory == nullptr || reinterpret_cast<uintptr_t>(allocatedMemory) % 8 == 0);

return allocatedMemory;
}
Expand Down
4 changes: 2 additions & 2 deletions include/reactphysics3d/memory/PoolAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class PoolAllocator : public MemoryAllocator {
static const int NB_HEAPS = 128;

/// Minimum unit size
static const size_t MIN_UNIT_SIZE = 16;
static const size_t MIN_UNIT_SIZE = 8;

/// Maximum memory unit size. An allocation request of a size smaller or equal to
/// this size will be handled using the small block allocator. However, for an
Expand All @@ -91,7 +91,7 @@ class PoolAllocator : public MemoryAllocator {
static const size_t MAX_UNIT_SIZE = NB_HEAPS * MIN_UNIT_SIZE;

/// Size of a memory chunk
static const size_t BLOCK_SIZE = 16 * MAX_UNIT_SIZE;
static const size_t BLOCK_SIZE = 8 * MAX_UNIT_SIZE;

// -------------------- Attributes -------------------- //

Expand Down
4 changes: 2 additions & 2 deletions src/memory/HeapAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void* HeapAllocator::allocate(size_t size) {
// Offset the allocated address such that it is properly aligned
allocatedMemory = computeAlignedAddress(allocatedMemory);

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(allocatedMemory) % GLOBAL_ALIGNMENT == 0);

return allocatedMemory;
Expand Down Expand Up @@ -324,7 +324,7 @@ void HeapAllocator::reserve(size_t sizeToAllocate) {
void* memory = mBaseAllocator.allocate(sizeToAllocate + sizeof(MemoryUnitHeader));
assert(memory != nullptr);

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(memory) % GLOBAL_ALIGNMENT == 0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the assertion that was failing. The memory address was aligned to 8 not 16 (when GLOBAL_ALIGNMENT was 16)

Copy link
Owner

@DanielChappuis DanielChappuis Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide a document/link or something saying that 8 bytes memory alignment is required on Android? I want to make sure this is not a bug or something else that is wrong. I don't want to change this value just because it solves an issue on your side.

Can you please tell me what asserts where failing exactly with 16 bytes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any documents or links that says 8 bytes memory alignment is required on Android. However I just know from experience getting my Android NDK app up and running that 8 bytes is optimal.
The assertion that was failing was HeapAllocator.cpp:328

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this assert was failing when GLOBAL_ALIGMNENT =16, this could be a bug. It means that the previous call:

void* memory = mBaseAllocator.allocate(sizeToAllocate + sizeof(MemoryUnitHeader));

failed to allocated 16 bytes aligned memory. That's strange because this statement should call the following line:

return std::aligned_alloc(GLOBAL_ALIGNMENT, size);

Can you check if this line with the call to std::aligned_alloc() really returns memory that is not 16 bytes aligned when GLOBAL_ALIGNMENT=16? This mean that there is something wrong with this standard library method on Android or maybe there is another method that should be used on Android to get aligned memory but the solution is probably not to change GLOBAL_ALIGNMENT to 8 bytes.

Copy link
Author

@ZeunO8 ZeunO8 Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have another debugging session later on today and get back to you (:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello. I know when spoke about this last year but do you have any news regarding this issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't sadly. I have gone through various Physics Engines in the last year, currently settling on Bullet.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks for your feedback.


// Create a new memory unit for the allocated memory
Expand Down
6 changes: 3 additions & 3 deletions src/memory/PoolAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void* PoolAllocator::allocate(size_t size) {
// Allocate memory using default allocation
void* allocatedMemory = mBaseAllocator.allocate(size);

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(allocatedMemory) % GLOBAL_ALIGNMENT == 0);

return allocatedMemory;
Expand All @@ -137,7 +137,7 @@ void* PoolAllocator::allocate(size_t size) {

void* allocatedMemory = static_cast<void*>(unit);

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(allocatedMemory) % GLOBAL_ALIGNMENT == 0);

return allocatedMemory;
Expand Down Expand Up @@ -183,7 +183,7 @@ void* PoolAllocator::allocate(size_t size) {

void* allocatedMemory = newBlock->memoryUnits;

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(allocatedMemory) % GLOBAL_ALIGNMENT == 0);

// Return the pointer to the first memory unit of the new allocated block
Expand Down
6 changes: 3 additions & 3 deletions src/memory/SingleFrameAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SingleFrameAllocator::SingleFrameAllocator(MemoryAllocator& baseAllocator) : mBa

assert(allocatedMemory != nullptr);

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(allocatedMemory) % GLOBAL_ALIGNMENT == 0);

mMemoryBufferStart = static_cast<char*>(allocatedMemory);
Expand All @@ -55,7 +55,7 @@ SingleFrameAllocator::~SingleFrameAllocator() {
}

// Allocate memory of a given size (in bytes) and return a pointer to the
// allocated memory. Allocated memory must be 16-bytes aligned.
// allocated memory. Allocated memory must be 8-bytes aligned.
void* SingleFrameAllocator::allocate(size_t size) {

// Lock the method with a mutex
Expand Down Expand Up @@ -83,7 +83,7 @@ void* SingleFrameAllocator::allocate(size_t size) {
// Increment the offset
mCurrentOffset += totalSize;

// Check that allocated memory is 16-bytes aligned
// Check that allocated memory is 8-bytes aligned
assert(reinterpret_cast<uintptr_t>(nextAvailableMemory) % GLOBAL_ALIGNMENT == 0);

// Return the next available memory location
Expand Down