mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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>
This commit is contained in:
1 parent
e3beb781e0
commit
eb8d6f5fd8
1 file changed
+10
-9
+10
-9
@@ -321,6 +321,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>
|
||||
@@ -360,8 +365,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.
|
||||
@@ -403,6 +407,7 @@ class MaybeStackBuffer {
|
||||
// be used.
|
||||
void Invalidate() {
|
||||
CHECK(!IsAllocated());
|
||||
capacity_ = 0;
|
||||
length_ = 0;
|
||||
buf_ = nullptr;
|
||||
}
|
||||
@@ -423,10 +428,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();
|
||||
}
|
||||
@@ -701,11 +707,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) {
|
||||
|
||||
Reference in new issue
Block a user