Skip to content

Commit 2935f72

Browse files
bnoordhuisjasnell
authored andcommitted
src: simplify MaybeStackBuffer::capacity()
PR-URL: #33602 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 830ef81 commit 2935f72

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/util.h

+10-9
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ inline bool StringEqualNoCase(const char* a, const char* b);
326326
// strncasecmp() is locale-sensitive. Use StringEqualNoCaseN() instead.
327327
inline bool StringEqualNoCaseN(const char* a, const char* b, size_t length);
328328

329+
template <typename T, size_t N>
330+
constexpr size_t arraysize(const T (&)[N]) {
331+
return N;
332+
}
333+
329334
// Allocates an array of member type T. For up to kStackStorageSize items,
330335
// the stack is used, otherwise malloc().
331336
template <typename T, size_t kStackStorageSize = 1024>
@@ -365,8 +370,7 @@ class MaybeStackBuffer {
365370
// Current maximum capacity of the buffer with which SetLength() can be used
366371
// without first calling AllocateSufficientStorage().
367372
size_t capacity() const {
368-
return IsAllocated() ? capacity_ :
369-
IsInvalidated() ? 0 : kStackStorageSize;
373+
return capacity_;
370374
}
371375

372376
// Make sure enough space for `storage` entries is available.
@@ -408,6 +412,7 @@ class MaybeStackBuffer {
408412
// be used.
409413
void Invalidate() {
410414
CHECK(!IsAllocated());
415+
capacity_ = 0;
411416
length_ = 0;
412417
buf_ = nullptr;
413418
}
@@ -428,10 +433,11 @@ class MaybeStackBuffer {
428433
CHECK(IsAllocated());
429434
buf_ = buf_st_;
430435
length_ = 0;
431-
capacity_ = 0;
436+
capacity_ = arraysize(buf_st_);
432437
}
433438

434-
MaybeStackBuffer() : length_(0), capacity_(0), buf_(buf_st_) {
439+
MaybeStackBuffer()
440+
: length_(0), capacity_(arraysize(buf_st_)), buf_(buf_st_) {
435441
// Default to a zero-length, null-terminated buffer.
436442
buf_[0] = T();
437443
}
@@ -707,11 +713,6 @@ inline bool IsBigEndian() {
707713
return GetEndianness() == kBigEndian;
708714
}
709715

710-
template <typename T, size_t N>
711-
constexpr size_t arraysize(const T (&)[N]) {
712-
return N;
713-
}
714-
715716
// Round up a to the next highest multiple of b.
716717
template <typename T>
717718
constexpr T RoundUp(T a, T b) {

0 commit comments

Comments
 (0)