Skip to content

Commit

Permalink
COMMON: Replace ScopedPtr/ScopedArray with std::unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Aug 11, 2020
1 parent 1c64505 commit 34f182e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 36 deletions.
7 changes: 4 additions & 3 deletions src/common/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

#include <cassert>

#include <memory>

#include "src/common/base64.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/error.h"
#include "src/common/readstream.h"
Expand Down Expand Up @@ -209,7 +210,7 @@ void encodeBase64(ReadStream &data, std::list<UString> &base64, size_t lineLengt

SeekableReadStream *decodeBase64(const UString &base64) {
const size_t dataLength = (countLength(base64) / 4) * 3;
ScopedArray<byte> data(new byte[dataLength]);
std::unique_ptr<byte[]> data = std::make_unique<byte[]>(dataLength);

MemoryWriteStream output(data.get(), dataLength);

Expand All @@ -221,7 +222,7 @@ SeekableReadStream *decodeBase64(const UString &base64) {

SeekableReadStream *decodeBase64(const std::list<UString> &base64) {
const size_t dataLength = (countLength(base64) / 4) * 3;
ScopedArray<byte> data(new byte[dataLength]);
std::unique_ptr<byte[]> data = std::make_unique<byte[]>(dataLength);

MemoryWriteStream output(data.get(), dataLength);

Expand Down
5 changes: 3 additions & 2 deletions src/common/blowfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

#include <cassert>

#include <memory>

#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/memreadstream.h"
#include "src/common/blowfish.h"

Expand Down Expand Up @@ -345,7 +346,7 @@ MemoryReadStream *blowfishEBC(SeekableReadStream &input, const std::vector<byte>
// Round up to the next multiple of the block size
const size_t outputSize = ((inputSize + kBlockSize - 1) / kBlockSize) * kBlockSize;

ScopedArray<byte> output(new byte[outputSize]);
std::unique_ptr<byte[]> output = std::make_unique<byte[]>(outputSize);

byte buffer[kBlockSize];
byte *data = output.get();
Expand Down
17 changes: 9 additions & 8 deletions src/common/deflate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
* Compress (deflate) and decompress (inflate) using zlib's DEFLATE algorithm.
*/

#include <memory>

#include <zlib.h>

#include <boost/scope_exit.hpp>

#include "src/common/deflate.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/ptrvector.h"
#include "src/common/memreadstream.h"

Expand Down Expand Up @@ -85,7 +86,7 @@ static void initDeflateZStream(z_stream &strm, int windowBits, size_t size, cons
byte *decompressDeflate(const byte *data, size_t inputSize,
size_t outputSize, int windowBits) {

ScopedArray<byte> decompressedData(new byte[outputSize]);
std::unique_ptr<byte[]> decompressedData = std::make_unique<byte[]>(outputSize);

z_stream strm;
BOOST_SCOPE_EXIT( (&strm) ) {
Expand Down Expand Up @@ -143,7 +144,7 @@ byte *decompressDeflateWithoutOutputSize(const byte *data, size_t inputSize, siz
if (zResult != Z_STREAM_END)
throw Exception("Failed to inflate: %s (%d)", zError(zResult), zResult);

ScopedArray<byte> decompressedData(new byte[strm.total_out]);
std::unique_ptr<byte[]> decompressedData = std::make_unique<byte[]>(strm.total_out);
for (size_t i = 0; i < buffers.size(); ++i) {
if (i == buffers.size() - 1)
std::memcpy(decompressedData.get() + i * frameSize, buffers[i], strm.total_out % frameSize);
Expand All @@ -158,7 +159,7 @@ byte *decompressDeflateWithoutOutputSize(const byte *data, size_t inputSize, siz
SeekableReadStream *decompressDeflate(ReadStream &input, size_t inputSize,
size_t outputSize, int windowBits) {

ScopedArray<byte> compressedData(new byte[inputSize]);
std::unique_ptr<byte[]> compressedData = std::make_unique<byte[]>(inputSize);
if (input.read(compressedData.get(), inputSize) != inputSize)
throw Exception(kReadError);

Expand All @@ -169,7 +170,7 @@ SeekableReadStream *decompressDeflate(ReadStream &input, size_t inputSize,

SeekableReadStream *decompressDeflateWithoutOutputSize(ReadStream &input, size_t inputSize,
int windowBits, unsigned int frameSize) {
ScopedArray<byte> compressedData(new byte[inputSize]);
std::unique_ptr<byte[]> compressedData = std::make_unique<byte[]>(inputSize);
if (input.read(compressedData.get(), inputSize) != inputSize)
throw Exception(kReadError);

Expand All @@ -192,7 +193,7 @@ size_t decompressDeflateChunk(SeekableReadStream &input, int windowBits,
strm.avail_out = outputSize;
strm.next_out = output;

ScopedArray<byte> inputData(new byte[frameSize]);
std::unique_ptr<byte[]> inputData = std::make_unique<byte[]>(frameSize);

/* As long as the zlib stream has not ended, the chunk end was not reached.
* Read a frame from the input buffer and decompress. */
Expand Down Expand Up @@ -249,7 +250,7 @@ byte *compressDeflate(const byte *data, size_t inputSize, size_t &outputSize, in
throw Exception("Failed to deflate: %s (%d)", zError(zResult), zResult);
} while (strm.avail_in != 0);

ScopedArray<byte> compressedData(new byte[strm.total_out]);
std::unique_ptr<byte[]> compressedData = std::make_unique<byte[]>(strm.total_out);
for (size_t i = 0; i < buffers.size(); ++i) {
if (i == buffers.size() - 1)
std::memcpy(compressedData.get() + i * frameSize, buffers[i], strm.total_out % frameSize);
Expand All @@ -263,7 +264,7 @@ byte *compressDeflate(const byte *data, size_t inputSize, size_t &outputSize, in
}

SeekableReadStream *compressDeflate(ReadStream &input, size_t inputSize, int windowBits, unsigned int frameSize) {
ScopedArray<byte> uncompressedData(new byte[inputSize]);
std::unique_ptr<byte[]> uncompressedData = std::make_unique<byte[]>(inputSize);
if (input.read(uncompressedData.get(), inputSize) != inputSize)
throw Exception(kReadError);

Expand Down
14 changes: 7 additions & 7 deletions src/common/encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include <iconv.h>

#include <vector>
#include <memory>

#include "src/common/encoding.h"
#include "src/common/encoding_strings.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/singleton.h"
#include "src/common/ustring.h"
#include "src/common/memreadstream.h"
Expand Down Expand Up @@ -124,7 +124,7 @@ class ConversionManager : public Singleton<ConversionManager> {
size_t inBytes = nIn;
size_t outBytes = nOut;

ScopedArray<byte> convData(new byte[outBytes]);
std::unique_ptr<byte[]> convData = std::make_unique<byte[]>(outBytes);

byte *outBuf = convData.get();

Expand All @@ -149,7 +149,7 @@ class ConversionManager : public Singleton<ConversionManager> {
return "[!!!]";

size_t size;
ScopedArray<byte> dataOut(doConvert(ctx, data, n, n * growth + termSize, size));
std::unique_ptr<byte[]> dataOut(doConvert(ctx, data, n, n * growth + termSize, size));
if (!dataOut)
return "[!?!]";

Expand All @@ -168,7 +168,7 @@ class ConversionManager : public Singleton<ConversionManager> {
size_t nOut = nIn * growth + termSize;

size_t size;
ScopedArray<byte> dataOut(doConvert(ctx, dataIn, nIn, nOut, size));
std::unique_ptr<byte[]> dataOut(doConvert(ctx, dataIn, nIn, nOut, size));
if (!dataOut)
return 0;

Expand All @@ -179,7 +179,7 @@ class ConversionManager : public Singleton<ConversionManager> {
}

MemoryReadStream *clean7bitASCII(const UString &str, bool terminate) {
ScopedArray<byte> dataOut(new byte[str.size() + (terminate ? 1 : 0)]);
std::unique_ptr<byte[]> dataOut = std::make_unique<byte[]>(str.size() + (terminate ? 1 : 0));

size_t size = 0;
for (UString::iterator c = str.begin(); c != str.end(); ++c)
Expand Down Expand Up @@ -355,7 +355,7 @@ UString readString(const byte *data, size_t size, Encoding encoding) {
}

size_t writeString(WriteStream &stream, const UString &str, Encoding encoding, bool terminate) {
ScopedPtr<MemoryReadStream> data(convertString(str, encoding, terminate));
std::unique_ptr<MemoryReadStream> data(convertString(str, encoding, terminate));

const size_t n = stream.writeStream(*data);

Expand All @@ -366,7 +366,7 @@ void writeStringFixed(WriteStream &stream, const UString &str, Encoding encoding
if (length == 0)
return;

ScopedPtr<MemoryReadStream> data(convertString(str, encoding, false));
std::unique_ptr<MemoryReadStream> data(convertString(str, encoding, false));

size_t n = stream.writeStream(*data, length);
while (n++ < length)
Expand Down
11 changes: 6 additions & 5 deletions src/common/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#ifndef COMMON_HASH_H
#define COMMON_HASH_H

#include <memory>

#include "src/common/types.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/encoding.h"
#include "src/common/memreadstream.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ static inline uint32 hashStringDJB2(const UString &string) {
static inline uint32 hashStringDJB2(const UString &string, Encoding encoding) {
uint32 hash = 5381;

ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
std::unique_ptr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

Expand Down Expand Up @@ -89,7 +90,7 @@ static inline uint32 hashStringFNV32(const UString &string) {
static inline uint32 hashStringFNV32(const UString &string, Encoding encoding) {
uint32 hash = 0x811C9DC5;

ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
std::unique_ptr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

Expand Down Expand Up @@ -118,7 +119,7 @@ static inline uint64 hashStringFNV64(const UString &string) {
static inline uint64 hashStringFNV64(const UString &string, Encoding encoding) {
uint64 hash = 0xCBF29CE484222325LL;

ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
std::unique_ptr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

Expand Down Expand Up @@ -202,7 +203,7 @@ static inline uint32 hashStringCRC32(const UString &string) {
static inline uint32 hashStringCRC32(const UString &string, Encoding encoding) {
uint32 hash = 0xFFFFFFFF;

ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
std::unique_ptr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

Expand Down
7 changes: 4 additions & 3 deletions src/common/lzma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
#include "src/common/types.h"
#include <lzma.h>

#include <memory>

#include <boost/scope_exit.hpp>

#include "src/common/lzma.h"
#include "src/common/scopedptr.h"
#include "src/common/error.h"
#include "src/common/memreadstream.h"
#include "src/common/memwritestream.h"
Expand Down Expand Up @@ -96,7 +97,7 @@ byte *decompressLZMA1(const byte *data, size_t inputSize, size_t outputSize, boo
if ((lzmaRet = lzma_raw_decoder(&strm, filters)) != LZMA_OK)
throw Exception("Failed to create raw LZMA1 decoder: %d", (int) lzmaRet);

ScopedArray<byte> outputData(new byte[outputSize]);
std::unique_ptr<byte[]> outputData = std::make_unique<byte[]>(outputSize);

strm.next_in = data;
strm.avail_in = inputSize;
Expand All @@ -122,7 +123,7 @@ byte *decompressLZMA1(const byte *data, size_t inputSize, size_t outputSize, boo
}

SeekableReadStream *decompressLZMA1(ReadStream &input, size_t inputSize, size_t outputSize, bool noEndMarker) {
ScopedArray<byte> inputData(new byte[inputSize]);
std::unique_ptr<byte[]> inputData = std::make_unique<byte[]>(inputSize);
if (input.read(inputData.get(), inputSize) != inputSize)
throw Exception(kReadError);

Expand Down
5 changes: 3 additions & 2 deletions src/common/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
#include <cassert>
#include <cstdlib>

#include <memory>

#include <boost/locale.hpp>
#include <boost/filesystem/path.hpp>

#include "src/common/platform.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/encoding.h"
#include "src/common/filepath.h"

Expand Down Expand Up @@ -164,7 +165,7 @@ static inline UString getWindowsVariable(const wchar_t *variable) {
return "";

const size_t size = length * sizeof(wchar_t);
ScopedArray<byte> data(new byte[size]);
std::unique_ptr<byte[]> data = std::make_unique<byte[]>(size);

DWORD newLength = GetEnvironmentVariableW(variable, reinterpret_cast<wchar_t *>(data.get()), length);
if (!newLength || (newLength > length))
Expand Down
5 changes: 3 additions & 2 deletions src/common/readstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@

#include <cassert>

#include <memory>

#include "src/common/readstream.h"
#include "src/common/memreadstream.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"

namespace Common {

Expand All @@ -65,7 +66,7 @@ ReadStream::~ReadStream() {
}

MemoryReadStream *ReadStream::readStream(size_t dataSize) {
ScopedArray<byte> buf(new byte[dataSize]);
std::unique_ptr<byte[]> buf = std::make_unique<byte[]>(dataSize);

if (read(buf.get(), dataSize) != dataSize)
throw Exception(kReadError);
Expand Down
5 changes: 3 additions & 2 deletions src/common/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
#include <cstring>
#include <cassert>

#include <memory>

#include "src/common/system.h"
#include "src/common/strutil.h"
#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/ustring.h"
#include "src/common/scopedptr.h"
#include "src/common/memreadstream.h"
#include "src/common/memwritestream.h"

Expand Down Expand Up @@ -381,7 +382,7 @@ size_t searchBackwards(SeekableReadStream &haystack, const byte *needle, size_t
const size_t sizeFile = haystack.size();
const size_t maxBack = MIN<size_t>(maxReadBack, sizeFile);

ScopedArray<byte> buf(new byte[kReadBufferSize + needleSize]);
std::unique_ptr<byte[]> buf = std::make_unique<byte[]>(kReadBufferSize + needleSize);

size_t backRead = needleSize;
while (backRead < maxBack) {
Expand Down
4 changes: 2 additions & 2 deletions src/common/zipfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

#include <list>
#include <vector>
#include <memory>

#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"

namespace Common {
Expand Down Expand Up @@ -70,7 +70,7 @@ class ZipFile : boost::noncopyable {

typedef std::vector<IFile> IFileList;

ScopedPtr<SeekableReadStream> _zip;
std::unique_ptr<SeekableReadStream> _zip;

/** External list of file names and types. */
FileList _files;
Expand Down

0 comments on commit 34f182e

Please sign in to comment.