Change allocation of vectors of UniqueHandles to not rely on std::vector<>::data() returning a valid pointer after calling std::vector<>::reserve().

This commit is contained in:
asuessenbach
2020-03-31 09:26:02 +02:00
committed by Markus Tavenrath
parent a5e8a7ccb8
commit 407542705f
2 changed files with 92 additions and 105 deletions

View File

@@ -1977,25 +1977,25 @@ ${i} return ${returnName};
void VulkanHppGenerator::appendFunctionBodyEnhancedVectorOfUniqueHandles(std::string & str, std::string const& indentation, std::string const& name, CommandData const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, bool singular, bool withAllocator) const
{
std::string const stringTemplate =
R"(${i} static_assert( sizeof( ${type} ) <= sizeof( UniqueHandle<${type}, Dispatch> ), "${type} is greater than UniqueHandle<${type}, Dispatch>!" );
${i} std::vector<UniqueHandle<${type}, Dispatch>, Allocator> ${typeVariable}s${allocator};
${i} ${typeVariable}s.reserve( ${vectorSize} );
${i} ${type}* buffer = reinterpret_cast<${type}*>( reinterpret_cast<char*>( ${typeVariable}s.data() ) + ${vectorSize} * ( sizeof( UniqueHandle<${type}, Dispatch> ) - sizeof( ${type} ) ) );
${i} Result result = static_cast<Result>(d.vk${command}( m_device, ${arguments}, reinterpret_cast<Vk${type}*>( buffer ) ) );
R"(${i} std::vector<UniqueHandle<${type}, Dispatch>, Allocator> ${uniqueTypeVariable}s${allocator};
${i} std::vector<${type}> ${typeVariable}s( ${vectorSize} );
${i} Result result = static_cast<Result>( d.vk${command}( m_device, ${arguments}, reinterpret_cast<Vk${type}*>(${typeVariable}s.data()) ) );
${i} if ( ${successChecks} )
${i} {
${i} ${uniqueTypeVariable}s.reserve( ${vectorSize} );
${i} ${Deleter}<${DeleterTemplate},Dispatch> deleter( *this, ${deleterArg}, d );
${i} for ( size_t i=0 ; i<${vectorSize} ; i++ )
${i} {
${i} ${typeVariable}s.push_back( UniqueHandle<${type}, Dispatch>( buffer[i], deleter ) );
${i} ${uniqueTypeVariable}s.push_back( UniqueHandle<${type}, Dispatch>( ${typeVariable}s[i], deleter ) );
${i} }
${i} }
${i} return createResultValue( result, ${typeVariable}s, VULKAN_HPP_NAMESPACE_STRING "::${class}::${commandName}Unique"${successCodes} );
${i} return createResultValue( result, ${uniqueTypeVariable}s, VULKAN_HPP_NAMESPACE_STRING "::${class}::${commandName}Unique"${successCodes} );
)";
std::string type = (returnParamIndex != INVALID_INDEX) ? commandData.params[returnParamIndex].type.type : "";
std::string typeVariable = startLowerCase(stripPrefix(type, "Vk"));
std::string uniqueTypeVariable = "unique" + stripPrefix(type, "Vk");
std::string arguments;
appendArguments(arguments, commandData, returnParamIndex, templateParamIndex, vectorParamIndices, twoStep, true, singular, 1, commandData.params.size() - 1);
@@ -2037,6 +2037,7 @@ ${i} return createResultValue( result, ${typeVariable}s, VULKAN_HPP_NAMESPACE_S
{ "successCodes", successCodes },
{ "type", stripPrefix(type, "Vk") },
{ "typeVariable", typeVariable },
{ "uniqueTypeVariable", uniqueTypeVariable },
{ "vectorSize", isCreateFunction ? "createInfos.size()" : "allocateInfo." + typeVariable + "Count" }
});
}