Replaced arrays with std::vector.
This commit is contained in:
		| @@ -44,12 +44,17 @@ namespace PolyVox | |||||||
| 		void decompress(CompressedBlock<VoxelType>* pSrcBlock, UncompressedBlock<VoxelType>* pDstBlock); | 		void decompress(CompressedBlock<VoxelType>* pSrcBlock, UncompressedBlock<VoxelType>* pDstBlock); | ||||||
|  |  | ||||||
| 	private: | 	private: | ||||||
|  | 		uint32_t getExpectedCompressedSize(uint32_t uUncompressedInputSize); | ||||||
| 		uint32_t getMaxCompressedSize(uint32_t uUncompressedInputSize); | 		uint32_t getMaxCompressedSize(uint32_t uUncompressedInputSize); | ||||||
| 		uint32_t compressWithMiniz(const void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); | 		uint32_t compressWithMiniz(const void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); | ||||||
| 		uint32_t decompressWithMiniz(const void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); | 		uint32_t decompressWithMiniz(const void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); | ||||||
|  |  | ||||||
| 		unsigned int m_uCompressionFlags; | 		unsigned int m_uCompressionFlags; | ||||||
|  |  | ||||||
|  | 		// Data gets compressed into this, then we check how big the result | ||||||
|  | 		// is and copy the required number of bytes to the destination block. | ||||||
|  | 		std::vector<uint8_t> m_vecTempBuffer; | ||||||
|  |  | ||||||
| 		// tdefl_compressor contains all the state needed by the low-level compressor so it's a pretty big struct (~300k). | 		// tdefl_compressor contains all the state needed by the low-level compressor so it's a pretty big struct (~300k). | ||||||
| 		tdefl_compressor* m_pDeflator; | 		tdefl_compressor* m_pDeflator; | ||||||
| 	}; | 	}; | ||||||
|   | |||||||
| @@ -62,9 +62,11 @@ namespace PolyVox | |||||||
| 		void* pSrcData = reinterpret_cast<void*>(pSrcBlock->getData()); | 		void* pSrcData = reinterpret_cast<void*>(pSrcBlock->getData()); | ||||||
| 		size_t uSrcLength = pSrcBlock->getDataSizeInBytes(); | 		size_t uSrcLength = pSrcBlock->getDataSizeInBytes(); | ||||||
|  |  | ||||||
| 		uint8_t tempBuffer[1000]; | 		// This compressor is expected to be used many times to compress a large number of blocks, but they are all | ||||||
| 		uint8_t* pDstData = reinterpret_cast<uint8_t*>( tempBuffer );				 | 		// expected to have the same size. Therefore the resize() function below will only perform allocation once. | ||||||
| 		size_t uDstLength = 1000; | 		m_vecTempBuffer.resize(getExpectedCompressedSize(uSrcLength)); | ||||||
|  | 		uint8_t* pDstData = &(m_vecTempBuffer[0]); | ||||||
|  | 		size_t uDstLength = m_vecTempBuffer.size(); | ||||||
|  |  | ||||||
| 		uint32_t uCompressedLength = 0; | 		uint32_t uCompressedLength = 0; | ||||||
|  |  | ||||||
| @@ -82,12 +84,13 @@ namespace PolyVox | |||||||
| 			// buffer is not big enough. So now we try again using a buffer that is definitely big enough. | 			// buffer is not big enough. So now we try again using a buffer that is definitely big enough. | ||||||
| 			// Note that ideally we will choose our earlier buffer size so that this almost never happens. | 			// Note that ideally we will choose our earlier buffer size so that this almost never happens. | ||||||
| 			logWarning() << "The compressor failed to compress the block, probabaly due to the buffer being too small."; | 			logWarning() << "The compressor failed to compress the block, probabaly due to the buffer being too small."; | ||||||
| 			logWarning() << "The compression will be tried again with a larger buffer"; | 			logWarning() << "The compression will be tried again with a larger buffer."; | ||||||
| 			uint32_t uMaxCompressedSize = getMaxCompressedSize(uSrcLength); |  | ||||||
| 			uint8_t* buffer = new uint8_t[ uMaxCompressedSize ]; |  | ||||||
|  |  | ||||||
| 			pDstData = reinterpret_cast<uint8_t*>( buffer );				 | 			std::vector<uint8_t> vecExtraBigBuffer; | ||||||
| 			uDstLength = uMaxCompressedSize; | 			vecExtraBigBuffer.resize(getMaxCompressedSize(uSrcLength)); | ||||||
|  |  | ||||||
|  | 			uint8_t* pDstData = &(vecExtraBigBuffer[0]); | ||||||
|  | 			size_t uDstLength = vecExtraBigBuffer.size(); | ||||||
|  |  | ||||||
| 			try | 			try | ||||||
| 			{		 | 			{		 | ||||||
| @@ -101,11 +104,8 @@ namespace PolyVox | |||||||
| 			{ | 			{ | ||||||
| 				// At this point it didn't work even with a bigger buffer. | 				// At this point it didn't work even with a bigger buffer. | ||||||
| 				// Not much more we can do so just rethrow the exception. | 				// Not much more we can do so just rethrow the exception. | ||||||
| 				delete[] buffer; |  | ||||||
| 				POLYVOX_THROW(std::runtime_error, "Failed to compress block data"); | 				POLYVOX_THROW(std::runtime_error, "Failed to compress block data"); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			delete[] buffer; |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -125,6 +125,15 @@ namespace PolyVox | |||||||
| 		POLYVOX_THROW_IF(uUncompressedLength != pDstBlock->getDataSizeInBytes(), std::runtime_error, "Decompressed data does not have the expected length"); | 		POLYVOX_THROW_IF(uUncompressedLength != pDstBlock->getDataSizeInBytes(), std::runtime_error, "Decompressed data does not have the expected length"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	template <typename VoxelType> | ||||||
|  | 	uint32_t MinizBlockCompressor<VoxelType>::getExpectedCompressedSize(uint32_t uUncompressedInputSize) | ||||||
|  | 	{ | ||||||
|  | 		// We expect this block compressor will be used for smoothly changing volume data such as density fields and so | ||||||
|  | 		// the compression rate might not be great. The value beloew is basically a guess based on previous experience. | ||||||
|  | 		uint32_t uExpectedCompressionRate = 4; | ||||||
|  | 		return uUncompressedInputSize / uExpectedCompressionRate; | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	template <typename VoxelType> | 	template <typename VoxelType> | ||||||
| 	uint32_t MinizBlockCompressor<VoxelType>::getMaxCompressedSize(uint32_t uUncompressedInputSize) | 	uint32_t MinizBlockCompressor<VoxelType>::getMaxCompressedSize(uint32_t uUncompressedInputSize) | ||||||
| 	{ | 	{ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user