Skip to content

Commit

Permalink
C API doesn't new a smartnode instead it manually increments the node…
Browse files Browse the repository at this point in the history
… ref count
  • Loading branch information
Auburn committed Aug 31, 2024
1 parent 75eae50 commit 3651da8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/FastNoise/Generators/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ namespace FastNoise
constexpr HybridSourceT( float f = 0.0f ) : constant( f ) { }
};

namespace Internal
{
void BumpNodeRefences( const Generator*, bool );
}

class FASTNOISE_API Generator
{
public:
Expand Down Expand Up @@ -159,6 +164,7 @@ namespace FastNoise

template<typename>
friend class SmartNode;
friend void Internal::BumpNodeRefences( const Generator*, bool );
};

using GeneratorSource = GeneratorSourceT<Generator>;
Expand Down
23 changes: 18 additions & 5 deletions src/FastNoise/FastNoise_C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
#include <FastNoise/FastNoise.h>
#include <FastNoise/Metadata.h>

namespace FastNoise::Internal
{
void BumpNodeRefences( const Generator* ptr, bool up )
{
ptr->ReferencesFetchAdd( up ? 1 : -1 );
}
}

FastNoise::Generator* ToGen( void* p )
{
return static_cast<FastNoise::SmartNode<>*>( p )->get();
return static_cast<FastNoise::Generator*>( p );
}

const FastNoise::Generator* ToGen( const void* p )
{
return static_cast<const FastNoise::SmartNode<>*>( p )->get();
return static_cast<const FastNoise::Generator*>( p );
}

void StoreMinMax( float* floatArray2, FastNoise::OutputMinMax minMax )
Expand All @@ -25,14 +33,16 @@ void* fnNewFromEncodedNodeTree( const char* encodedString, unsigned simdLevel )
{
if( FastNoise::SmartNode<> node = FastNoise::NewFromEncodedNodeTree( encodedString, (FastSIMD::FeatureSet)simdLevel ) )
{
return new FastNoise::SmartNode<>( std::move( node ) );
FastNoise::Internal::BumpNodeRefences( node.get(), true );

return node.get();
}
return nullptr;
}

void fnDeleteNodeRef( void* node )
{
delete static_cast<FastNoise::SmartNode<>*>( node );
FastNoise::Internal::BumpNodeRefences( ToGen( node ), false );
}

unsigned fnGetSIMDLevel( const void* node )
Expand Down Expand Up @@ -113,7 +123,10 @@ void* fnNewFromMetadata( int id, unsigned simdLevel )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (FastNoise::Metadata::node_id)id ) )
{
return new FastNoise::SmartNode<>( metadata->CreateNode( (FastSIMD::FeatureSet)simdLevel ) );
FastNoise::SmartNode<> node = metadata->CreateNode( (FastSIMD::FeatureSet)simdLevel );
FastNoise::Internal::BumpNodeRefences( node.get(), true );

return node.get();
}
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/FastNoise/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ constexpr size_t gMetadataVectorSize<const Metadata*> = 45;
template<>
constexpr size_t gMetadataVectorSize<const char*> = 83;
template<>
constexpr size_t gMetadataVectorSize<Metadata::MemberVariable> = 75;
constexpr size_t gMetadataVectorSize<Metadata::MemberVariable> = 71;
template<>
constexpr size_t gMetadataVectorSize<Metadata::MemberNodeLookup> = 30;
template<>
constexpr size_t gMetadataVectorSize<Metadata::MemberHybrid> = 50;
constexpr size_t gMetadataVectorSize<Metadata::MemberHybrid> = 54;

template<typename T>
static std::vector<T>& GetVectorStorage()
Expand Down Expand Up @@ -105,7 +105,7 @@ static void AddToDataStream( std::vector<uint8_t>& dataStream, T value )
}
}

static void AddMemberLookupToDataStream( std::vector<uint8_t>& dataStream, uint8_t type, uint8_t index )
static void AddMemberLookupToDataStream( std::vector<uint8_t>& dataStream, uint8_t type, uint8_t index )
{
MemberLookup memberLookup;
memberLookup.member.type = type;
Expand Down

0 comments on commit 3651da8

Please sign in to comment.