Skip to content

Commit

Permalink
[Base] Add user-literals for several memory sizes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Wunkolo committed Dec 31, 2021
1 parent b64b4c6 commit 8ccb2d5
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/xenia/base/arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand Down
6 changes: 5 additions & 1 deletion src/xenia/base/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions src/xenia/base/literals.h
Original file line number Diff line number Diff line change
@@ -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_
6 changes: 4 additions & 2 deletions src/xenia/base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -59,6 +60,7 @@ DEFINE_int32(
"Logging");

namespace dp = disruptorplus;
using namespace xe::literals;

namespace xe {

Expand All @@ -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_) {
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/xenia/base/string_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/xenia/base/testing/threading_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/xenia/base/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
};
Expand Down
7 changes: 5 additions & 2 deletions src/xenia/cpu/backend/x64/x64_code_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "third_party/fmt/include/fmt/format.h"
#include "xenia/base/assert.h"
#include "xenia/base/clock.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
Expand All @@ -31,6 +32,8 @@ namespace cpu {
namespace backend {
namespace x64 {

using namespace xe::literals;

X64CodeCache::X64CodeCache() = default;

X64CodeCache::~X64CodeCache() {
Expand Down Expand Up @@ -227,7 +230,7 @@ void X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code,
old_commit_mark = generated_code_commit_mark_;
if (high_mark <= old_commit_mark) break;

new_commit_mark = old_commit_mark + 16 * 1024 * 1024;
new_commit_mark = old_commit_mark + 16_MiB;
if (generated_code_execute_base_ == generated_code_write_base_) {
xe::memory::AllocFixed(generated_code_execute_base_, new_commit_mark,
xe::memory::AllocationType::kCommit,
Expand Down Expand Up @@ -310,7 +313,7 @@ uint32_t X64CodeCache::PlaceData(const void* data, size_t length) {
old_commit_mark = generated_code_commit_mark_;
if (high_mark <= old_commit_mark) break;

new_commit_mark = old_commit_mark + 16 * 1024 * 1024;
new_commit_mark = old_commit_mark + 16_MiB;
if (generated_code_execute_base_ == generated_code_write_base_) {
xe::memory::AllocFixed(generated_code_execute_base_, new_commit_mark,
xe::memory::AllocationType::kCommit,
Expand Down
4 changes: 3 additions & 1 deletion src/xenia/cpu/backend/x64/x64_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "xenia/base/assert.h"
#include "xenia/base/atomic.h"
#include "xenia/base/debugging.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
Expand Down Expand Up @@ -50,8 +51,9 @@ namespace x64 {

using xe::cpu::hir::HIRBuilder;
using xe::cpu::hir::Instr;
using namespace xe::literals;

static const size_t kMaxCodeSize = 1 * 1024 * 1024;
static const size_t kMaxCodeSize = 1_MiB;

static const size_t kStashOffset = 32;
// static const size_t kStashOffsetHigh = 32 + 32;
Expand Down
4 changes: 3 additions & 1 deletion src/xenia/cpu/ppc/testing/ppc_testing_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "xenia/base/console_app_main.h"
#include "xenia/base/cvar.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/platform.h"
Expand All @@ -36,6 +37,7 @@ namespace cpu {
namespace test {

using xe::cpu::ppc::PPCContext;
using namespace xe::literals;

typedef std::vector<std::pair<std::string, std::string>> AnnotationList;

Expand Down Expand Up @@ -177,7 +179,7 @@ class TestSuite {

class TestRunner {
public:
TestRunner() : memory_size_(64 * 1024 * 1024) {
TestRunner() : memory_size_(64_MiB) {
memory_.reset(new Memory());
memory_->Initialize();
}
Expand Down
7 changes: 5 additions & 2 deletions src/xenia/cpu/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "xenia/base/cvar.h"
#include "xenia/base/debugging.h"
#include "xenia/base/exception_handler.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/memory.h"
#include "xenia/base/profiling.h"
Expand Down Expand Up @@ -57,6 +58,8 @@ namespace cpu {
using xe::cpu::ppc::PPCOpcode;
using xe::kernel::XThread;

using namespace xe::literals;

class BuiltinModule : public Module {
public:
explicit BuiltinModule(Processor* processor)
Expand Down Expand Up @@ -142,8 +145,8 @@ bool Processor::Setup(std::unique_ptr<backend::Backend> backend) {
// Open the trace data path, if requested.
functions_trace_path_ = cvars::trace_function_data_path;
if (!functions_trace_path_.empty()) {
functions_trace_file_ = ChunkedMappedMemoryWriter::Open(
functions_trace_path_, 32 * 1024 * 1024, true);
functions_trace_file_ =
ChunkedMappedMemoryWriter::Open(functions_trace_path_, 32_MiB, true);
}

return true;
Expand Down
6 changes: 4 additions & 2 deletions src/xenia/emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "xenia/base/cvar.h"
#include "xenia/base/debugging.h"
#include "xenia/base/exception_handler.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/mapped_memory.h"
#include "xenia/base/profiling.h"
Expand Down Expand Up @@ -60,6 +61,8 @@ DEFINE_string(

namespace xe {

using namespace xe::literals;

Emulator::Emulator(const std::filesystem::path& command_line,
const std::filesystem::path& storage_root,
const std::filesystem::path& content_root,
Expand Down Expand Up @@ -415,8 +418,7 @@ bool Emulator::SaveToFile(const std::filesystem::path& path) {
Pause();

filesystem::CreateFile(path);
auto map = MappedMemory::Open(path, MappedMemory::Mode::kReadWrite, 0,
1024ull * 1024ull * 1024ull * 2ull);
auto map = MappedMemory::Open(path, MappedMemory::Mode::kReadWrite, 0, 2_GiB);
if (!map) {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/xenia/gpu/d3d12/deferred_command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
#include <vector>

#include "xenia/base/assert.h"
#include "xenia/base/literals.h"
#include "xenia/base/math.h"
#include "xenia/ui/d3d12/d3d12_api.h"

namespace xe {
namespace gpu {
namespace d3d12 {

using namespace xe::literals;

class D3D12CommandProcessor;

class DeferredCommandList {
public:
DeferredCommandList(const D3D12CommandProcessor& command_processor,
size_t initial_size_bytes = 1024 * 1024);
size_t initial_size_bytes = 1_MiB);

void Reset();
void Execute(ID3D12GraphicsCommandList* command_list,
Expand Down
5 changes: 4 additions & 1 deletion src/xenia/gpu/vulkan/texture_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <algorithm>

#include "third_party/fmt/include/fmt/format.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
Expand All @@ -36,8 +37,10 @@ namespace vulkan {

using xe::ui::vulkan::CheckResult;

using namespace xe::literals;

constexpr uint32_t kMaxTextureSamplers = 32;
constexpr VkDeviceSize kStagingBufferSize = 64 * 1024 * 1024;
constexpr VkDeviceSize kStagingBufferSize = 64_MiB;

const char* get_dimension_name(xenos::DataDimension dimension) {
static const char* names[] = {
Expand Down
4 changes: 3 additions & 1 deletion src/xenia/gpu/vulkan/vulkan_command_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ namespace xe {
namespace gpu {
namespace vulkan {

using namespace xe::literals;
using namespace xe::gpu::xenos;

using xe::ui::vulkan::CheckResult;

constexpr size_t kDefaultBufferCacheCapacity = 256 * 1024 * 1024;
constexpr size_t kDefaultBufferCacheCapacity = 256_MiB;

VulkanCommandProcessor::VulkanCommandProcessor(
VulkanGraphicsSystem* graphics_system, kernel::KernelState* kernel_state)
Expand Down
7 changes: 5 additions & 2 deletions src/xenia/kernel/xthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "third_party/fmt/include/fmt/format.h"
#include "xenia/base/byte_stream.h"
#include "xenia/base/clock.h"
#include "xenia/base/literals.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/profiling.h"
Expand Down Expand Up @@ -41,6 +42,8 @@ const uint32_t XAPC::kDummyRundownRoutine;

using xe::cpu::ppc::PPCOpcode;

using namespace xe::literals;

uint32_t next_xthread_id_ = 0;

XThread::XThread(KernelState* kernel_state)
Expand Down Expand Up @@ -370,7 +373,7 @@ X_STATUS XThread::Create() {
RetainHandle();

xe::threading::Thread::CreationParameters params;
params.stack_size = 16 * 1024 * 1024; // Allocate a big host stack.
params.stack_size = 16_MiB; // Allocate a big host stack.
params.create_suspended = true;
thread_ = xe::threading::Thread::Create(params, [this]() {
// Set thread ID override. This is used by logging.
Expand Down Expand Up @@ -1018,7 +1021,7 @@ object_ref<XThread> XThread::Restore(KernelState* kernel_state,

xe::threading::Thread::CreationParameters params;
params.create_suspended = true; // Not done restoring yet.
params.stack_size = 16 * 1024 * 1024;
params.stack_size = 16_MiB;
thread->thread_ = xe::threading::Thread::Create(params, [thread, state]() {
// Set thread ID override. This is used by logging.
xe::threading::set_current_thread_id(thread->handle());
Expand Down
Loading

0 comments on commit 8ccb2d5

Please sign in to comment.