Skip to content

Commit

Permalink
removed stretchy buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Sep 21, 2024
1 parent f4a1624 commit 3d33eec
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 99 deletions.
59 changes: 1 addition & 58 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,6 @@

#include "array.h"

#include "core.h"

#include <string.h>

void* b2CreateArray( int elementSize, int capacity )
{
void* result = (b2ArrayHeader*)b2Alloc( sizeof( b2ArrayHeader ) + elementSize * capacity ) + 1;
b2Array( result ).count = 0;
b2Array( result ).capacity = capacity;
return result;
}

void b2DestroyArray( void* a, int elementSize )
{
int capacity = b2Array( a ).capacity;
int size = sizeof( b2ArrayHeader ) + elementSize * capacity;
b2Free( ( (b2ArrayHeader*)a ) - 1, size );
}

void b2Array_Grow( void** a, int elementSize )
{
int capacity = b2Array( *a ).capacity;
B2_ASSERT( capacity == b2Array( *a ).count );

// grow by 50%
int newCapacity = capacity + ( capacity >> 1 );
newCapacity = newCapacity >= 2 ? newCapacity : 2;
void* tmp = *a;
*a = (b2ArrayHeader*)b2Alloc( sizeof( b2ArrayHeader ) + elementSize * newCapacity ) + 1;
b2Array( *a ).capacity = newCapacity;
b2Array( *a ).count = capacity;
memcpy( *a, tmp, capacity * elementSize );
b2DestroyArray( tmp, elementSize );
}

void b2Array_Resize( void** a, int elementSize, int count )
{
int capacity = b2Array( *a ).capacity;
if ( capacity >= count )
{
b2Array( *a ).count = count;
return;
}

int originalCount = b2Array( *a ).count;

// grow by 50%
int newCapacity = count + ( count >> 1 );
newCapacity = newCapacity >= 2 ? newCapacity : 2;
void* tmp = *a;
*a = (b2ArrayHeader*)b2Alloc( sizeof( b2ArrayHeader ) + elementSize * newCapacity ) + 1;
b2Array( *a ).capacity = newCapacity;
b2Array( *a ).count = count;

// copy existing elements
memcpy( *a, tmp, originalCount * elementSize );
b2DestroyArray( tmp, elementSize );
}
#include <stddef.h>

B2_ARRAY_SOURCE( int, b2Int );
41 changes: 0 additions & 41 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,6 @@

#include "core.h"

// todo compare with https://github.com/skeeto/growable-buf

typedef struct b2ArrayHeader
{
int count;
int capacity;
} b2ArrayHeader;

#define b2Array( a ) ( (b2ArrayHeader*)( a ) )[-1]

void* b2CreateArray( int elementSize, int capacity );
void b2DestroyArray( void* a, int elementSize );
void b2Array_Grow( void** a, int elementSize );
void b2Array_Resize( void** a, int elementSize, int count );

#define b2CheckIndex( a, index ) B2_ASSERT( 0 <= index && index < b2Array( a ).count )
#define b2CheckId( ARRAY, ID ) B2_ASSERT( 0 <= ID && ID < b2Array( ARRAY ).count && ARRAY[ID].id == ID )
#define b2CheckIdAndRevision( ARRAY, ID, REV ) \
B2_ASSERT( 0 <= ID && ID < b2Array( ARRAY ).count && ARRAY[ID].id == ID && ARRAY[ID].revision == REV )

#define b2Array_Clear( a ) b2Array( a ).count = 0

#define b2Array_Push( a, element ) \
if ( b2Array( a ).count == b2Array( a ).capacity ) \
b2Array_Grow( (void**)&a, sizeof( element ) ); \
B2_ASSERT( b2Array( a ).count < b2Array( a ).capacity ); \
a[b2Array( a ).count++] = element

#define b2Array_RemoveSwap( a, index ) \
B2_ASSERT( 0 <= index && index < b2Array( a ).count ); \
a[index] = a[b2Array( a ).count - 1]; \
b2Array( a ).count--

#define b2Array_Last( a ) ( a )[b2Array( a ).count - 1];

#define b2Array_Pop( a ) \
B2_ASSERT( 0 < b2Array( a ).count ); \
b2Array( a ).count--

#define b2GetArrayBytes( a, elementSize ) ( (int)elementSize * b2Array( a ).capacity )

// Macro based dynamic arrays
// Pros
// - type safe
Expand Down
1 change: 1 addition & 0 deletions src/dynamic_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,7 @@ static int32_t b2BuildTree( b2DynamicTree* tree, int32_t leafCount )
int32_t* binIndices = tree->binIndices;
#endif

// todo large stack item
struct b2RebuildItem stack[b2_treeStackSize];
int32_t top = 0;

Expand Down

0 comments on commit 3d33eec

Please sign in to comment.