From c3728c62357fea6c58198368e96bfcb4fee5a66d Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 28 May 2020 10:04:58 +0200 Subject: [PATCH] src: simplify MaybeStackBuffer::capacity() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33602 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Zeyu Yang Reviewed-By: Juan José Arboleda Reviewed-By: David Carlier Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- src/util.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/util.h b/src/util.h index 2f1fdada59ef4d..62229e3c448637 100644 --- a/src/util.h +++ b/src/util.h @@ -326,6 +326,11 @@ inline bool StringEqualNoCase(const char* a, const char* b); // strncasecmp() is locale-sensitive. Use StringEqualNoCaseN() instead. inline bool StringEqualNoCaseN(const char* a, const char* b, size_t length); +template +constexpr size_t arraysize(const T (&)[N]) { + return N; +} + // Allocates an array of member type T. For up to kStackStorageSize items, // the stack is used, otherwise malloc(). template @@ -365,8 +370,7 @@ class MaybeStackBuffer { // Current maximum capacity of the buffer with which SetLength() can be used // without first calling AllocateSufficientStorage(). size_t capacity() const { - return IsAllocated() ? capacity_ : - IsInvalidated() ? 0 : kStackStorageSize; + return capacity_; } // Make sure enough space for `storage` entries is available. @@ -408,6 +412,7 @@ class MaybeStackBuffer { // be used. void Invalidate() { CHECK(!IsAllocated()); + capacity_ = 0; length_ = 0; buf_ = nullptr; } @@ -428,10 +433,11 @@ class MaybeStackBuffer { CHECK(IsAllocated()); buf_ = buf_st_; length_ = 0; - capacity_ = 0; + capacity_ = arraysize(buf_st_); } - MaybeStackBuffer() : length_(0), capacity_(0), buf_(buf_st_) { + MaybeStackBuffer() + : length_(0), capacity_(arraysize(buf_st_)), buf_(buf_st_) { // Default to a zero-length, null-terminated buffer. buf_[0] = T(); } @@ -707,11 +713,6 @@ inline bool IsBigEndian() { return GetEndianness() == kBigEndian; } -template -constexpr size_t arraysize(const T (&)[N]) { - return N; -} - // Round up a to the next highest multiple of b. template constexpr T RoundUp(T a, T b) {