Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/cpp-sanitizers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: C++ sanitizers

on:
workflow_call:

permissions:
contents: read

jobs:
sanitizers:
runs-on: ubuntu-latest
timeout-minutes: 15
defaults:
run:
working-directory: cpp
env:
CC: clang
CXX: clang++
steps:
- uses: actions/checkout@v4
- name: Install C++ dependencies
run: |
pipx install conan
conan profile detect
sed -i 's/^compiler.cppstd=.*/compiler.cppstd=20/' "$(conan profile path default)"
bash ./install-local-conan-dependencies.sh
- name: Build tests with ASan and UBSan
run: |
cmake -S . -B build-sanitizers -DCMAKE_TOOLCHAIN_FILE="$PWD/conan_toolchain.cmake" \
-DCMAKE_BUILD_TYPE=Release -DLINKS_PLATFORM_TESTS=ON \
-DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer" \
-DCMAKE_CXX_FLAGS_RELEASE="-O0 -gline-tables-only"
cmake --build build-sanitizers --target Platform.Memory.Tests --parallel 1
- name: Run sanitizer tests
env:
ASAN_OPTIONS: alloc_dealloc_mismatch=1:detect_leaks=1
UBSAN_OPTIONS: halt_on_error=1:print_stacktrace=1
run: ctest --test-dir build-sanitizers --output-on-failure --no-tests=error
28 changes: 13 additions & 15 deletions .github/workflows/cpp-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ name: Test cpp

on:
push:
branches:
- 'main'
branches: [main]
paths:
- 'cpp/**.h'
- 'cpp/**.cpp'
- 'cpp/CMakeLists.txt'
- 'cpp/conanfile.txt'
- 'cpp/**'
- '.github/workflows/cpp-test.yml'
- '.github/workflows/cpp-sanitizers.yml'
pull_request:
types:
- edited
branches:
- 'main'
types: [opened, edited, synchronize, reopened]
branches: [main]
paths:
- 'cpp/**.h'
- 'cpp/**.cpp'
- 'cpp/CMakeLists.txt'
- 'cpp/conanfile.txt'
- 'cpp/**'
- '.github/workflows/cpp-test.yml'
- '.github/workflows/cpp-sanitizers.yml'
- '.github/workflows/deploy-cpp.yml'

jobs:
main:
uses: linksplatform/Workflows/.github/workflows/cpp-test.yml@main
sanitizers:
uses: ./.github/workflows/cpp-sanitizers.yml
7 changes: 6 additions & 1 deletion .github/workflows/deploy-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ on:
workflow_dispatch:

jobs:
sanitizers:
uses: ./.github/workflows/cpp-sanitizers.yml
main:
needs: sanitizers
permissions:
contents: write
uses: linksplatform/Workflows/.github/workflows/deploy-cpp.yml@main
with:
recipe_folder: "platform.delegates"
recipe_folder: "platform.memory"
secrets:
NUGET_TOKEN: ${{ secrets.NUGET_TOKEN }}
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ target_link_libraries(${PROJECT_NAME}.Library INTERFACE Platform.Collections::Pl
target_link_libraries(${PROJECT_NAME}.Library INTERFACE mio::mio)

if(${LINKS_PLATFORM_TESTS})
enable_testing()
add_executable(${PROJECT_NAME}.Tests ${PROJECT_NAME}.Tests/AllTests.cpp)
set_target_properties(${PROJECT_NAME}.Tests PROPERTIES CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE GTest::gtest)
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE GTest::gtest_main)
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE ${PROJECT_NAME}.Library)
add_test(NAME ${PROJECT_NAME}.Tests COMMAND ${PROJECT_NAME}.Tests)

add_executable(MappedFileFixture ../examples/mapped-file-interop/fixture.cpp)
set_target_properties(MappedFileFixture PROPERTIES CXX_STANDARD 20)
Expand Down
9 changes: 9 additions & 0 deletions cpp/Platform.Memory.Tests/HeapResizableDirectMemoryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
return *((std::byte*)pointer1 + heapMemory.ReservedCapacity() - 1);
}

TEST(HeapResizableDirectMemory, DestroysAllocatedMemory)
{
HeapResizableDirectMemory heapMemory{4096};
heapMemory.UsedCapacity(1);
*static_cast<std::byte*>(heapMemory.Pointer()) = std::byte{42};
}

TEST(HeapResizableDirectMemory, CorrectMemoryReallocation)
{
auto heapMemory = HeapResizableDirectMemory();
ASSERT_GT(heapMemory.ReservedCapacity(), 0u);
ASSERT_NE(heapMemory.Pointer(), nullptr);
auto value1 = GetLastByte(heapMemory);
heapMemory.ReservedCapacity(heapMemory.ReservedCapacity() * 2);
auto value2 = GetLastByteByCopy(heapMemory);
Expand Down
4 changes: 2 additions & 2 deletions cpp/Platform.Memory/HeapResizableDirectMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

public: ~HeapResizableDirectMemory() final
{
delete static_cast<std::byte*>(Pointer());
std::free(Pointer());
}
};
}
}
4 changes: 2 additions & 2 deletions cpp/Platform.Memory/Platform.Memory.TemplateLibrary.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<title>LinksPlatform's Platform.Memory Template Library</title>
<summary>LinksPlatform's Platform.Memory is a Template Library what contains crossplatform class API for memory mapping.</summary>
<description>LinksPlatform's Platform.Memory is a Template Library what contains crossplatform class API for memory mapping. Use Platform.Memory.h file to include the library.</description>
<releaseNotes>Initial realise</releaseNotes>
<version>0.1.0</version>
<releaseNotes>Free heap memory with the allocator that reserved it.</releaseNotes>
<version>0.1.1</version>
<authors>konard</authors>
<owners>konard</owners>
<copyright>konard</copyright>
Expand Down
1 change: 1 addition & 0 deletions cpp/Platform.Memory/Platform.Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <windows.h>
#include <sysinfoapi.h>
#endif
#include <cstdlib>
#include <execution>
#include <fstream>
#include <filesystem>
Expand Down
6 changes: 2 additions & 4 deletions cpp/Platform.Memory/ResizableDirectMemoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
namespace Internal
{
std::size_t page_size() noexcept
inline std::size_t page_size() noexcept
{
std::size_t page_size;
#if defined(WIN32)
Expand All @@ -16,16 +16,14 @@
#endif
return page_size;
}

auto PageSize = page_size();
}

class ResizableDirectMemoryBase : public IResizableDirectMemory
{
protected: using IResizableDirectMemory::pointer_t;
protected: using IResizableDirectMemory::capacity_t;

public: inline static const std::size_t MinimumCapacity = Internal::PageSize;
public: inline static const std::size_t MinimumCapacity = Internal::page_size();

private: pointer_t _pointer = nullptr;
private: capacity_t _reservedCapacity = 0;
Expand Down
Loading