Skip to content

Commit eb8d6f5

Browse files
bnoordhuiscodebytere
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 e3beb78 commit eb8d6f5

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
@@ -321,6 +321,11 @@ inline bool StringEqualNoCase(const char* a, const char* b);
321321
// strncasecmp() is locale-sensitive. Use StringEqualNoCaseN() instead.
322322
inline bool StringEqualNoCaseN(const char* a, const char* b, size_t length);
323323

324+
template <typename T, size_t N>
325+
constexpr size_t arraysize(const T (&)[N]) {
326+
return N;
327+
}
328+
324329
// Allocates an array of member type T. For up to kStackStorageSize items,
325330
// the stack is used, otherwise malloc().
326331
template <typename T, size_t kStackStorageSize = 1024>
@@ -360,8 +365,7 @@ class MaybeStackBuffer {
360365
// Current maximum capacity of the buffer with which SetLength() can be used
361366
// without first calling AllocateSufficientStorage().
362367
size_t capacity() const {
363-
return IsAllocated() ? capacity_ :
364-
IsInvalidated() ? 0 : kStackStorageSize;
368+
return capacity_;
365369
}
366370

367371
// Make sure enough space for `storage` entries is available.
@@ -403,6 +407,7 @@ class MaybeStackBuffer {
403407
// be used.
404408
void Invalidate() {
405409
CHECK(!IsAllocated());
410+
capacity_ = 0;
406411
length_ = 0;
407412
buf_ = nullptr;
408413
}
@@ -423,10 +428,11 @@ class MaybeStackBuffer {
423428
CHECK(IsAllocated());
424429
buf_ = buf_st_;
425430
length_ = 0;
426-
capacity_ = 0;
431+
capacity_ = arraysize(buf_st_);
427432
}
428433

429-
MaybeStackBuffer() : length_(0), capacity_(0), buf_(buf_st_) {
434+
MaybeStackBuffer()
435+
: length_(0), capacity_(arraysize(buf_st_)), buf_(buf_st_) {
430436
// Default to a zero-length, null-terminated buffer.
431437
buf_[0] = T();
432438
}
@@ -701,11 +707,6 @@ inline bool IsBigEndian() {
701707
return GetEndianness() == kBigEndian;
702708
}
703709

704-
template <typename T, size_t N>
705-
constexpr size_t arraysize(const T (&)[N]) {
706-
return N;
707-
}
708-
709710
// Round up a to the next highest multiple of b.
710711
template <typename T>
711712
constexpr T RoundUp(T a, T b) {

0 commit comments

Comments
 (0)