Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2020-05-28 11:04:58 +0300
committerJames M Snell <jasnell@gmail.com>2020-05-31 02:29:33 +0300
commit2935f72ae1b991c64118031dec12e7d7db7714a5 (patch)
tree74f984634dc9dc53df124824eb8a40e90257275e /src/util.h
parent830ef8134160fdcbb96289af4a136b70d994f6d6 (diff)
src: simplify MaybeStackBuffer::capacity()
PR-URL: https://github.com/nodejs/node/pull/33602 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/util.h b/src/util.h
index 2f1fdada59e..62229e3c448 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 <typename T, size_t N>
+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 <typename T, size_t kStackStorageSize = 1024>
@@ -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 <typename T, size_t N>
-constexpr size_t arraysize(const T (&)[N]) {
- return N;
-}
-
// Round up a to the next highest multiple of b.
template <typename T>
constexpr T RoundUp(T a, T b) {