IXWebSocketPerMessageDeflateCodec: use std::array instead of std::unique_ptr for a fixed size array

This commit is contained in:
Benjamin Sergeant 2020-08-17 16:36:24 -07:00
parent 42f71364ca
commit 72472f2899
2 changed files with 11 additions and 21 deletions

View File

@ -16,8 +16,6 @@ namespace
// is treated as a char* and the null termination (\x00) makes it // is treated as a char* and the null termination (\x00) makes it
// look like an empty string. // look like an empty string.
const std::string kEmptyUncompressedBlock = std::string("\x00\x00\xff\xff", 4); const std::string kEmptyUncompressedBlock = std::string("\x00\x00\xff\xff", 4);
const int kBufferSize = 1 << 14;
} // namespace } // namespace
namespace ix namespace ix
@ -26,7 +24,6 @@ namespace ix
// Compressor // Compressor
// //
WebSocketPerMessageDeflateCompressor::WebSocketPerMessageDeflateCompressor() WebSocketPerMessageDeflateCompressor::WebSocketPerMessageDeflateCompressor()
: _compressBufferSize(kBufferSize)
{ {
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
memset(&_deflateState, 0, sizeof(_deflateState)); memset(&_deflateState, 0, sizeof(_deflateState));
@ -57,8 +54,6 @@ namespace ix
if (ret != Z_OK) return false; if (ret != Z_OK) return false;
_compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
_flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH; _flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH;
return true; return true;
@ -145,14 +140,14 @@ namespace ix
do do
{ {
// Output to local buffer // Output to local buffer
_deflateState.avail_out = (uInt) _compressBufferSize; _deflateState.avail_out = (uInt) _compressBuffer.size();
_deflateState.next_out = _compressBuffer.get(); _deflateState.next_out = &_compressBuffer.front();
deflate(&_deflateState, _flush); deflate(&_deflateState, _flush);
output = _compressBufferSize - _deflateState.avail_out; output = _compressBuffer.size() - _deflateState.avail_out;
out.insert(out.end(), _compressBuffer.get(), _compressBuffer.get() + output); out.insert(out.end(), _compressBuffer.begin(), _compressBuffer.begin() + output);
} while (_deflateState.avail_out == 0); } while (_deflateState.avail_out == 0);
if (endsWithEmptyUnCompressedBlock(out)) if (endsWithEmptyUnCompressedBlock(out))
@ -170,7 +165,6 @@ namespace ix
// Decompressor // Decompressor
// //
WebSocketPerMessageDeflateDecompressor::WebSocketPerMessageDeflateDecompressor() WebSocketPerMessageDeflateDecompressor::WebSocketPerMessageDeflateDecompressor()
: _compressBufferSize(kBufferSize)
{ {
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
memset(&_inflateState, 0, sizeof(_inflateState)); memset(&_inflateState, 0, sizeof(_inflateState));
@ -198,8 +192,6 @@ namespace ix
if (ret != Z_OK) return false; if (ret != Z_OK) return false;
_compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
_flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH; _flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH;
return true; return true;
@ -232,8 +224,8 @@ namespace ix
do do
{ {
_inflateState.avail_out = (uInt) _compressBufferSize; _inflateState.avail_out = (uInt) _compressBuffer.size();
_inflateState.next_out = _compressBuffer.get(); _inflateState.next_out = &_compressBuffer.front();
int ret = inflate(&_inflateState, Z_SYNC_FLUSH); int ret = inflate(&_inflateState, Z_SYNC_FLUSH);
@ -242,8 +234,8 @@ namespace ix
return false; // zlib error return false; // zlib error
} }
out.append(reinterpret_cast<char*>(_compressBuffer.get()), out.append(reinterpret_cast<char*>(&_compressBuffer.front()),
_compressBufferSize - _inflateState.avail_out); _compressBuffer.size() - _inflateState.avail_out);
} while (_inflateState.avail_out == 0); } while (_inflateState.avail_out == 0);
return true; return true;

View File

@ -9,7 +9,7 @@
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
#include "zlib.h" #include "zlib.h"
#endif #endif
#include <memory> #include <array>
#include <string> #include <string>
#include <vector> #include <vector>
@ -34,8 +34,7 @@ namespace ix
bool endsWithEmptyUnCompressedBlock(const T& value); bool endsWithEmptyUnCompressedBlock(const T& value);
int _flush; int _flush;
size_t _compressBufferSize; std::array<unsigned char, 1 << 14> _compressBuffer;
std::unique_ptr<unsigned char[]> _compressBuffer;
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
z_stream _deflateState; z_stream _deflateState;
@ -53,8 +52,7 @@ namespace ix
private: private:
int _flush; int _flush;
size_t _compressBufferSize; std::array<unsigned char, 1 << 14> _compressBuffer;
std::unique_ptr<unsigned char[]> _compressBuffer;
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
z_stream _inflateState; z_stream _inflateState;