From eb4df5d8e885c7597e92d7579af14145a66ff200 Mon Sep 17 00:00:00 2001 From: mnerv <24420859+mnerv@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:56:10 +0200 Subject: [PATCH] Initial commit --- .editorconfig | 21 +++++++++++++++ .gitignore | 40 ++++++++++++++++++++++++++++ CMakeLists.txt | 39 +++++++++++++++++++++++++++ helloalloc.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 helloalloc.cpp diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8dbbd2a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +[*] +end_of_line = LF +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true + +[*.{ts,js,tsx,jsx,lua,yml,json}] +charset = utf-8 +indent_style = space +indent_size = 2 + +[*.go] +indent_style = tab + +[Makefile] +indent_style = tab + +[meson.build] +indent_size = 2 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..addec9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Binaries +bin +obj + +# CMake +build +cmake-build-* +meson-build-* + +# Xcode +*.xcworkspace +*.xcodeproj + +# Visual Studio +*.sln +*.vcxproj +*.vcxproj.filters +*.vcxproj.user +.vs + +# Makefile +Makefile +*.make + +# nvim +.ccls +.ccls-cache +compile_commands.json + +# Visual Studio Code +.vscode + +# IntelliJ +.idea + +# macOS +.DS_Store +.AppleDouble +.LSOverride + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c7a87b9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.12) +project(helloalloc VERSION 0.0.0) +set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Group CMake targets inside a folder +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Generate compile_commands.json for language servers + +find_package(fmt CONFIG REQUIRED) + +if (NOT MSVC) + set(TARGET_OPTIONS + "-Wall" + "-Wextra" + "-Wconversion" + "-Wpedantic" + "-Wshadow" + "-Werror" + ) +else() + string(JOIN " " TARGET_DEFINTIONS + ${TARGET_DEFINTIONS} + "_WIN32_WINNT=0x0A00" + ) + set(TARGET_OPTIONS + "/W4" + "/WX" + ) +endif() + +# source files and includes +set(TARGET_NAME helloalloc) +set(TARGET_SOURCE_FILES + "helloalloc.cpp" +) +add_executable(${TARGET_NAME} ${TARGET_SOURCE_FILES}) +target_include_directories(${TARGET_NAME} PRIVATE "${PROJECT_SOURCE_DIR}") +# target_compile_definitions(${TARGET_NAME} PRIVATE ${TARGET_DEFINTIONS}) +target_compile_features(${TARGET_NAME} PRIVATE cxx_std_20) +target_link_libraries(${TARGET_NAME} PUBLIC fmt::fmt-header-only) +source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${TARGET_SOURCE_FILES}) + diff --git a/helloalloc.cpp b/helloalloc.cpp new file mode 100644 index 0000000..964b6b4 --- /dev/null +++ b/helloalloc.cpp @@ -0,0 +1,71 @@ +#include +#include "fmt/format.h" + +namespace sky { +constexpr std::size_t address_size = 3; +constexpr std::size_t payload_size = 15; +using address_t = uint8_t[address_size]; +using payload_t = uint8_t[payload_size]; + +auto mcp_u32_to_address(address_t& dest, std::uint32_t const& addr) -> void { + dest[0] = static_cast((addr & 0x000000FF) >> 0); + dest[1] = static_cast((addr & 0x0000FF00) >> 8); + dest[2] = static_cast((addr & 0x00FF0000) >> 16); +} +} + +template +class FixedSizeAllocator { +public: + using value_type = T; + using pointer_type = T*; + using size_type = std::size_t; + + FixedSizeAllocator() : m_used_size(0) {} + + auto operator()(const T& lhs, const T& rhs) const -> bool { + return lhs < rhs; + } + + auto allocate(size_type n) -> pointer_type { + if (m_used_size + n > N) throw std::bad_alloc(); + + T* result = std::reinterpret_pointer_cast(&m_pool[m_used_size]); + m_used_size += n; + return result; + } + + auto deallocate(pointer_type ptr, size_type n) -> void { + if (ptr != &m_pool[m_used_size - n]) + throw std::logic_error("Deallocating out of order"); + m_used_size -= n; + } + +private: + std::uint8_t m_pool[N]{}; + std::size_t m_used_size; +}; + +auto main() -> int { + std::set> hello{}; + + std::uint32_t addr1_u32 = 32; + std::uint32_t addr2_u32 = 64; + std::uint32_t addr3_u32 = 128; + std::uint32_t addr4_u32 = 256; + + hello.insert(addr1_u32); + hello.insert(addr2_u32); + hello.insert(addr3_u32); + hello.insert(addr4_u32); + hello.insert(addr1_u32); + hello.insert(addr1_u32); + + for (auto const& addr : hello) { + sky::address_t buffer; + sky::mcp_u32_to_address(buffer, addr); + fmt::print("{:#04x}:{:#04x}:{:#04x}\n", buffer[2], buffer[1], buffer[0]); + } + + return 0; +}