From 583e0688e33c4f3d7e3f1e2f30dd78690eb58fd4 Mon Sep 17 00:00:00 2001 From: Congcong Kuo Date: Tue, 27 May 2025 01:49:56 +0800 Subject: [PATCH] =?UTF-8?q?daemon:=20Remove=20=E2=80=98AutoDeleteArray?= =?UTF-8?q?=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * libutil/util.hh (AutoDeleteArray): Remove. * libutil/util.cc (readString, readStrings): Use ‘std::vector’ instead of ‘AutoDeleteArray’. * libutil/serialise.cc (readFile): Likewise. Change-Id: I45362998dbb8226874f66b77cd19f071f7bb2ab3 Signed-off-by: Ludovic Courtès --- nix/libutil/serialise.cc | 19 +++++++++---------- nix/libutil/util.cc | 7 +++---- nix/libutil/util.hh | 12 ------------ 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/nix/libutil/serialise.cc b/nix/libutil/serialise.cc index 92417507508..6f04ab15918 100644 --- a/nix/libutil/serialise.cc +++ b/nix/libutil/serialise.cc @@ -16,11 +16,11 @@ BufferedSink::~BufferedSink() delete[] buffer; } - + void BufferedSink::operator () (const unsigned char * data, size_t len) { if (!buffer) buffer = new unsigned char[bufSize]; - + while (len) { /* Optimisation: bypass the buffer if the data exceeds the buffer size. */ @@ -96,7 +96,7 @@ size_t BufferedSource::read(unsigned char * data, size_t len) if (!buffer) buffer = new unsigned char[bufSize]; if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize); - + /* Copy out the data in the buffer. */ size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len; memcpy(data, buffer + bufPosOut, n); @@ -247,18 +247,17 @@ size_t readString(unsigned char * buf, size_t max, Source & source) return len; } - + string readString(Source & source) { size_t len = readInt(source); - unsigned char * buf = new unsigned char[len]; - AutoDeleteArray d(buf); - source(buf, len); - readPadding(len, source); - return string((char *) buf, len); + std::vector buf(len); + source(buf.data(), buf.size()); + readPadding(buf.size(), source); + return string((char *) buf.data(), buf.size()); } - + template T readStrings(Source & source) { unsigned int count = readInt(source); diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc index 56f116046c4..398f61841f1 100644 --- a/nix/libutil/util.cc +++ b/nix/libutil/util.cc @@ -271,11 +271,10 @@ string readFile(int fd) if (fstat(fd, &st) == -1) throw SysError("statting file"); - unsigned char * buf = new unsigned char[st.st_size]; - AutoDeleteArray d(buf); - readFull(fd, buf, st.st_size); + std::vector buf(st.st_size); + readFull(fd, buf.data(), buf.size()); - return string((char *) buf, st.st_size); + return string((char *) buf.data(), buf.size()); } diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh index 377aac06847..03234e3a5d2 100644 --- a/nix/libutil/util.hh +++ b/nix/libutil/util.hh @@ -194,18 +194,6 @@ string drainFD(int fd); /* Automatic cleanup of resources. */ -template -struct AutoDeleteArray -{ - T * p; - AutoDeleteArray(T * p) : p(p) { } - ~AutoDeleteArray() - { - delete [] p; - } -}; - - class AutoDelete { Path path;