[tint][utils] Minor tweaks to BumpAllocator
Based on post merge feedback of 155823
Bug: chromium:1491912
Change-Id: I6dfa04821195b32243f736ae0b28d2ae25769faf
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/155900
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/utils/memory/bump_allocator.h b/src/tint/utils/memory/bump_allocator.h
index c1df71a..6775d05 100644
--- a/src/tint/utils/memory/bump_allocator.h
+++ b/src/tint/utils/memory/bump_allocator.h
@@ -17,6 +17,7 @@
#include <algorithm>
#include <array>
+#include <cstddef>
#include <cstring>
#include <utility>
@@ -65,20 +66,21 @@
/// current block is full.
/// @param size_in_bytes the number of bytes to allocate
/// @returns the pointer to the allocated memory or `nullptr` if the memory can not be allocated
- uint8_t* Allocate(size_t size_in_bytes) {
+ std::byte* Allocate(size_t size_in_bytes) {
if (TINT_UNLIKELY(data.current_offset + size_in_bytes < size_in_bytes)) {
return nullptr; // integer overflow
}
if (data.current_offset + size_in_bytes > data.current_data_size) {
// Allocate a new block from the heap
auto* prev_block = data.current;
- data.current_data_size = std::max(size_in_bytes, kDefaultBlockDataSize);
- data.current =
- Bitcast<BlockHeader*>(new uint8_t[sizeof(BlockHeader) + data.current_data_size]);
- if (!data.current) {
+ size_t data_size = std::max(size_in_bytes, kDefaultBlockDataSize);
+ data.current = Bitcast<BlockHeader*>(new (std::nothrow)
+ std::byte[sizeof(BlockHeader) + data_size]);
+ if (TINT_UNLIKELY(!data.current)) {
return nullptr; // out of memory
}
data.current->next = nullptr;
+ data.current_data_size = data_size;
data.current_offset = 0;
if (prev_block) {
prev_block->next = data.current;
@@ -87,7 +89,7 @@
}
}
- auto* base = Bitcast<uint8_t*>(data.current) + sizeof(BlockHeader);
+ auto* base = Bitcast<std::byte*>(data.current) + sizeof(BlockHeader);
auto* ptr = base + data.current_offset;
data.current_offset += size_in_bytes;
data.count++;
@@ -99,7 +101,7 @@
auto* block = data.root;
while (block != nullptr) {
auto* next = block->next;
- delete[] Bitcast<uint8_t*>(block);
+ delete[] Bitcast<std::byte*>(block);
block = next;
}
data = {};
diff --git a/src/tint/utils/memory/bump_allocator_test.cc b/src/tint/utils/memory/bump_allocator_test.cc
index 31f3809..c1cc22a 100644
--- a/src/tint/utils/memory/bump_allocator_test.cc
+++ b/src/tint/utils/memory/bump_allocator_test.cc
@@ -34,12 +34,14 @@
for (size_t n : {
BumpAllocator::kDefaultBlockDataSize - sizeof(void*),
BumpAllocator::kDefaultBlockDataSize - 4,
+ BumpAllocator::kDefaultBlockDataSize - 1,
BumpAllocator::kDefaultBlockDataSize,
+ BumpAllocator::kDefaultBlockDataSize + 1,
BumpAllocator::kDefaultBlockDataSize + 4,
BumpAllocator::kDefaultBlockDataSize + sizeof(void*),
}) {
BumpAllocator allocator;
- auto ptr = allocator.Allocate(n);
+ auto* ptr = allocator.Allocate(n);
memset(ptr, 0x42, n);
}
}
diff --git a/src/tint/utils/symbol/symbol_table.cc b/src/tint/utils/symbol/symbol_table.cc
index 2830a41..c56c5c0 100644
--- a/src/tint/utils/symbol/symbol_table.cc
+++ b/src/tint/utils/symbol/symbol_table.cc
@@ -39,6 +39,7 @@
Symbol SymbolTable::RegisterInternal(std::string_view name) {
char* name_mem = Bitcast<char*>(name_allocator_.Allocate(name.length() + 1));
if (name_mem == nullptr) {
+ TINT_ICE() << "failed to allocate memory for symbol's string";
return Symbol();
}