[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:
Wunkolo
2021-12-30 17:25:30 -08:00
committed by Rick Gibbed
parent b64b4c6761
commit 1a8068b151
19 changed files with 109 additions and 25 deletions

View File

@@ -60,7 +60,7 @@ void* Arena::Alloc(size_t size, size_t align) {
if (active_chunk_) {
if (active_chunk_->capacity - active_chunk_->offset <
size + get_padding() + 4096) {
size + get_padding() + 4_KiB) {
Chunk* next = active_chunk_->next;
if (!next) {
assert_true(size + get_padding() < chunk_size_,

View File

@@ -14,11 +14,15 @@
#include <cstdint>
#include <vector>
#include "xenia/base/literals.h"
namespace xe {
using namespace xe::literals;
class Arena {
public:
explicit Arena(size_t chunk_size = 4 * 1024 * 1024);
explicit Arena(size_t chunk_size = 4_MiB);
~Arena();
void Reset();

39
src/xenia/base/literals.h Normal file
View File

@@ -0,0 +1,39 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_LITERALS_H_
#define XENIA_BASE_LITERALS_H_
#include <cstdint>
namespace xe::literals {
constexpr size_t operator""_KiB(unsigned long long int x) {
return 1024ULL * x;
}
constexpr size_t operator""_MiB(unsigned long long int x) {
return 1024_KiB * x;
}
constexpr size_t operator""_GiB(unsigned long long int x) {
return 1024_MiB * x;
}
constexpr size_t operator""_TiB(unsigned long long int x) {
return 1024_GiB * x;
}
constexpr size_t operator""_PiB(unsigned long long int x) {
return 1024_TiB * x;
}
} // namespace xe::literals
#endif // XENIA_BASE_LITERALS_H_

View File

@@ -25,6 +25,7 @@
#include "xenia/base/cvar.h"
#include "xenia/base/debugging.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/literals.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
#include "xenia/base/platform.h"
@@ -59,6 +60,7 @@ DEFINE_int32(
"Logging");
namespace dp = disruptorplus;
using namespace xe::literals;
namespace xe {
@@ -74,7 +76,7 @@ struct LogLine {
char prefix_char;
};
thread_local char thread_log_buffer_[64 * 1024];
thread_local char thread_log_buffer_[64_KiB];
FileLogSink::~FileLogSink() {
if (file_) {
@@ -234,7 +236,7 @@ class Logger {
}
private:
static const size_t kBufferSize = 8 * 1024 * 1024;
static const size_t kBufferSize = 8_MiB;
uint8_t buffer_[kBufferSize];
static const size_t kBlockSize = 256;

View File

@@ -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);

View File

@@ -824,7 +824,7 @@ TEST_CASE("Create and Run Thread", "[thread]") {
}
SECTION("16kb stack size") {
params.stack_size = 16 * 1024 * 1024;
params.stack_size = 16_MiB;
thread = Thread::Create(params, [] {
Thread::Exit(-1);
FAIL("Function must not return");

View File

@@ -25,11 +25,14 @@
#include <vector>
#include "xenia/base/assert.h"
#include "xenia/base/literals.h"
#include "xenia/base/platform.h"
namespace xe {
namespace threading {
using namespace xe::literals;
#if XE_PLATFORM_ANDROID
void AndroidInitialize();
void AndroidShutdown();
@@ -368,7 +371,7 @@ struct ThreadPriority {
class Thread : public WaitHandle {
public:
struct CreationParameters {
size_t stack_size = 4 * 1024 * 1024;
size_t stack_size = 4_MiB;
bool create_suspended = false;
int32_t initial_priority = 0;
};