diff --git a/include/FastNoise/Generators/Generator.h b/include/FastNoise/Generators/Generator.h index 248452e..3139cad 100644 --- a/include/FastNoise/Generators/Generator.h +++ b/include/FastNoise/Generators/Generator.h @@ -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: @@ -159,6 +164,7 @@ namespace FastNoise template friend class SmartNode; + friend void Internal::BumpNodeRefences( const Generator*, bool ); }; using GeneratorSource = GeneratorSourceT; diff --git a/src/FastNoise/FastNoise_C.cpp b/src/FastNoise/FastNoise_C.cpp index 7c28f0e..527ea79 100644 --- a/src/FastNoise/FastNoise_C.cpp +++ b/src/FastNoise/FastNoise_C.cpp @@ -2,14 +2,22 @@ #include #include +namespace FastNoise::Internal +{ + void BumpNodeRefences( const Generator* ptr, bool up ) + { + ptr->ReferencesFetchAdd( up ? 1 : -1 ); + } +} + FastNoise::Generator* ToGen( void* p ) { - return static_cast*>( p )->get(); + return static_cast( p ); } const FastNoise::Generator* ToGen( const void* p ) { - return static_cast*>( p )->get(); + return static_cast( p ); } void StoreMinMax( float* floatArray2, FastNoise::OutputMinMax minMax ) @@ -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*>( node ); + FastNoise::Internal::BumpNodeRefences( ToGen( node ), false ); } unsigned fnGetSIMDLevel( const void* node ) @@ -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; } diff --git a/src/FastNoise/Metadata.cpp b/src/FastNoise/Metadata.cpp index 4119d04..8d52d94 100644 --- a/src/FastNoise/Metadata.cpp +++ b/src/FastNoise/Metadata.cpp @@ -25,11 +25,11 @@ constexpr size_t gMetadataVectorSize = 45; template<> constexpr size_t gMetadataVectorSize = 83; template<> -constexpr size_t gMetadataVectorSize = 75; +constexpr size_t gMetadataVectorSize = 71; template<> constexpr size_t gMetadataVectorSize = 30; template<> -constexpr size_t gMetadataVectorSize = 50; +constexpr size_t gMetadataVectorSize = 54; template static std::vector& GetVectorStorage() @@ -105,7 +105,7 @@ static void AddToDataStream( std::vector& dataStream, T value ) } } -static void AddMemberLookupToDataStream( std::vector& dataStream, uint8_t type, uint8_t index ) +static void AddMemberLookupToDataStream( std::vector& dataStream, uint8_t type, uint8_t index ) { MemberLookup memberLookup; memberLookup.member.type = type;