box3d/test/test_allocator.c
Erin Catto c37cfe4cd7
SIMD hull collision (#93)
SIMD based hull collision inspired by example code from @cairnc in #54 

<img width="1676" height="1332" alt="image"
src="https://github.com/user-attachments/assets/58b137bd-bf7b-4c99-863c-64341b973a3d"
/>

- Fixed #78
- Fixed #85 and fixed CI to error on UB.
- Updated benchmarks
- Added AMD scalar benchmarks
- Added Neon benchmarks
- More determinism unit tests
- Sample leak detection
- Added b3Body_AllowFastRotation
- Added b3Body_IsFastRotationAllowed
- Bumped b3HullData version
- Bumped b3CompoundData version
2026-07-18 22:43:03 -07:00

99 lines
2.3 KiB
C

// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "block_allocator.h"
#include "core.h"
#include "test_macros.h"
#include "box3d/types.h"
typedef struct
{
int value1;
float value2;
} Foo;
_Static_assert( sizeof( Foo ) >= sizeof( void* ), "too small" );
static int TestBlockAllocate( void )
{
b3BlockAllocator allocator = b3CreateBlockAllocator( (int)sizeof( Foo ), 2 );
Foo* item1 = b3AllocateElement( &allocator );
ENSURE( item1 != NULL );
Foo* item2 = b3AllocateElement( &allocator );
ENSURE( item2 != NULL );
b3DestroyBlockAllocator( &allocator );
return 0;
}
static int TestBlockClear( void )
{
b3BlockAllocator allocator = b3CreateBlockAllocator( (int)sizeof( Foo ), 0 );
Foo* item1 = b3AllocateElement( &allocator );
Foo* item2 = b3AllocateElement( &allocator );
b3FreeElement( &allocator, item1 );
b3FreeElement( &allocator, item2 );
b3DestroyBlockAllocator( &allocator );
return 0;
}
// The free list link is a pointer stored in the element, so a stride that is not a multiple
// of the alignment leaves odd elements on a bad boundary. See issue #78.
static int TestBlockAlignment( void )
{
// The manifold sizes are the reported case
int elementSizes[] = { 12, (int)sizeof( b3Manifold ), 3 * (int)sizeof( b3Manifold ) };
for ( int i = 0; i < ARRAY_COUNT( elementSizes ); ++i )
{
b3BlockAllocator allocator = b3CreateBlockAllocator( elementSizes[i], 0 );
// Hold the elements so the indices advance and a block boundary is crossed
void* elements[B3_BLOCK_SIZE + 8];
int count = ARRAY_COUNT( elements );
for ( int j = 0; j < count; ++j )
{
elements[j] = b3AllocateElement( &allocator );
ENSURE( elements[j] != NULL );
ENSURE( ( (uintptr_t)elements[j] & ( B3_ALIGNMENT - 1 ) ) == 0 );
}
for ( int j = 0; j < count; ++j )
{
b3FreeElement( &allocator, elements[j] );
}
// Now drain the free list
for ( int j = 0; j < count; ++j )
{
elements[j] = b3AllocateElement( &allocator );
ENSURE( elements[j] != NULL );
ENSURE( ( (uintptr_t)elements[j] & ( B3_ALIGNMENT - 1 ) ) == 0 );
}
for ( int j = 0; j < count; ++j )
{
b3FreeElement( &allocator, elements[j] );
}
b3DestroyBlockAllocator( &allocator );
}
return 0;
}
int AllocatorTest( void )
{
RUN_SUBTEST( TestBlockAllocate );
RUN_SUBTEST( TestBlockClear );
RUN_SUBTEST( TestBlockAlignment );
return 0;
}