Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
src: simplify MaybeStackBuffer::capacity()
  • Loading branch information
bnoordhuis committed May 28, 2020
commit f5ee884250bd77685e31889dcc49102712f61800
19 changes: 10 additions & 9 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -408,6 +412,7 @@ class MaybeStackBuffer {
// be used.
void Invalidate() {
CHECK(!IsAllocated());
capacity_ = 0;
length_ = 0;
buf_ = nullptr;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down