[Base] Add user-literals for several memory sizes
Rather than using `n * 1024 * 1024`, this adds a convenient `_MiB`/`_KiB` user-literal to the new `literals.h` header to concisely describe units of memory in a much more readable way. Any other useful literals can be added to this header. These literals exist in the `xe::literals` namespace so they are opt-in, similar to `std::chrono` literals, and require a `using namespace xe::literals` statement to utilize it within the current scope. I've done a pass through the codebase to replace trivial instances of `1024 * 1024 * ...` expressions being used but avoided anything that added additional casting complexity from `size_t` to `uint32_t` and such to keep this commit concise.
This commit is contained in:
@@ -13,12 +13,15 @@
|
||||
#include <cstdarg>
|
||||
|
||||
#include "xenia/base/assert.h"
|
||||
#include "xenia/base/literals.h"
|
||||
#include "xenia/base/math.h"
|
||||
|
||||
namespace xe {
|
||||
|
||||
using namespace xe::literals;
|
||||
|
||||
StringBuffer::StringBuffer(size_t initial_capacity) {
|
||||
buffer_capacity_ = std::max(initial_capacity, static_cast<size_t>(16 * 1024));
|
||||
buffer_capacity_ = std::max(initial_capacity, static_cast<size_t>(16_KiB));
|
||||
buffer_ = reinterpret_cast<char*>(std::malloc(buffer_capacity_));
|
||||
assert_not_null(buffer_);
|
||||
buffer_[0] = 0;
|
||||
@@ -40,7 +43,7 @@ void StringBuffer::Grow(size_t additional_length) {
|
||||
}
|
||||
size_t old_capacity = buffer_capacity_;
|
||||
size_t new_capacity =
|
||||
std::max(xe::round_up(buffer_offset_ + additional_length, 16 * 1024),
|
||||
std::max(xe::round_up(buffer_offset_ + additional_length, 16_KiB),
|
||||
old_capacity * 2);
|
||||
auto new_buffer = std::realloc(buffer_, new_capacity);
|
||||
assert_not_null(new_buffer);
|
||||
|
||||
Reference in New Issue
Block a user