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
33 changes: 9 additions & 24 deletions cpp/Platform.Memory/DirectMemoryAsArrayMemoryAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,22 @@
// get => std::string("Array as memory block at '").append(Platform::Converters::To<std::string>(Pointer)).append("' address.");
//}

std::size_t current_index = 0;

[[no_unique_address]] struct : PropertySetup<Self> {
using PropertySetup<Self>::self;

operator TElement&() { return *(reinterpret_cast<TElement*>(self().Pointer()) + self().current_index); }

auto& operator=(TElement value)
{
TElement& ref = *this;
ref = value;
return *this;
}
} _Index;
public: TElement& operator[](std::size_t index) override
{
return reinterpret_cast<TElement*>(_memory.Pointer())[index];
}

public: auto&& operator[](std::size_t index)
public: const TElement& operator[](std::size_t index) const override
{
current_index = index;
return _Index;
return reinterpret_cast<const TElement*>(_memory.Pointer())[index];
}

public: DirectMemoryAsArrayMemoryAdapter(IDirectMemory &memory)
:_memory(memory)
{
using namespace Platform::Exceptions::Ensure;
Always::ArgumentMeetsCriteria(
memory,
[](auto& m) { return (m.Size() % sizeof(TElement)) == 0; },
"memory",
"Memory is not aligned to element size."
);
if ((memory.Size() % sizeof(TElement)) != 0) {
throw std::invalid_argument("Memory is not aligned to element size.");
}
}
};
}
2 changes: 1 addition & 1 deletion cpp/Platform.Memory/HeapResizableDirectMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

public: ~HeapResizableDirectMemory() final
{
delete static_cast<std::byte*>(Pointer());
std::free(Pointer());
}
};
}
6 changes: 2 additions & 4 deletions cpp/Platform.Memory/IArrayMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
template <typename TElement> class IArrayMemory<TElement> : public IMemory
{
public:
//virtual TElement& operator[](std::size_t index) {}

// TODO: impl const
//virtual const TElement& operator[](std::size_t index) const {}
virtual TElement& operator[](std::size_t index) = 0;
virtual const TElement& operator[](std::size_t index) const = 0;
};
}
47 changes: 36 additions & 11 deletions cpp/Platform.Memory/Platform.Memory.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
#pragma once

#include <Platform.Collections.h>
#include <cstddef>
#include <cstdlib>
#include <vector>
#include <ranges>
#include <algorithm>
#include <execution>
#include <limits>
#include <memory>
#include <stdexcept>

#ifdef WIN32
#include <windows.h>
#include <sysinfoapi.h>
#else
#include <unistd.h>
#endif
#include <execution>
#include <fstream>
#include <filesystem>
#include <gsl/gsl>
#include <mio/mmap.hpp>

#include "memory_mapped_file.hpp"
#include "memory_mapped_file.cpp"
// Minimal Range and Exception handling for compilation
namespace Platform::Ranges {
template<typename T>
struct Range {
T min, max;
Range(T min_val, T max_val) : min(min_val), max(max_val) {}
};

namespace Ensure::Always {
template<typename T>
void ArgumentInRange(T value, const Range<T>& range) {
if (value < range.min || value > range.max) {
throw std::out_of_range("Argument out of range");
}
}
}
}

// Include core memory interfaces and implementations
#include "IMemory.h"
#include "IDirectMemory.h"
#include "IArrayMemory.h"
#include "ArrayMemory.h"
#include "FileArrayMemory.h"

#include "IResizableDirectMemory.h"
#include "ResizableDirectMemoryBase.h"
#include "HeapResizableDirectMemory.h"

#include "FileMappedResizableDirectMemory.h"
#include "TemporaryFileMappedResizableDirectMemory.h"
#include "DirectMemoryAsArrayMemoryAdapter.h"

// File-based implementations require external dependencies - included separately
// #include "memory_mapped_file.hpp"
// #include "memory_mapped_file.cpp"
// #include "FileArrayMemory.h"
// #include "FileMappedResizableDirectMemory.h"
// #include "TemporaryFileMappedResizableDirectMemory.h"
6 changes: 2 additions & 4 deletions cpp/Platform.Memory/ResizableDirectMemoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@
public: void ReservedCapacity(capacity_t value)
{
using namespace Platform::Ranges;
using namespace Platform::Exceptions;

if (value != _reservedCapacity)
{
Ranges::Ensure::Always::ArgumentInRange(value, Range{_usedCapacity, std::numeric_limits<capacity_t>::max()});
Ensure::Always::ArgumentInRange(value, Range{_usedCapacity, std::numeric_limits<capacity_t>::max()});
OnReservedCapacityChanged(_reservedCapacity, value);
_reservedCapacity = value;
}
Expand All @@ -75,8 +74,7 @@

if (value != _usedCapacity)
{
// TODO: Use modernize Ranges version
Ensure::Always::ArgumentInRange(value, Range(0, _reservedCapacity));
Ensure::Always::ArgumentInRange(value, Range{capacity_t(0), _reservedCapacity});
_usedCapacity = value;
}
}
Expand Down
51 changes: 51 additions & 0 deletions cpp/simple_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Platform.Memory/Platform.Memory.h"
#include <iostream>
#include <cassert>

using namespace Platform::Memory;

int main() {
std::cout << "Testing Platform.Memory C++ implementation...\n";

try {
// Test HeapResizableDirectMemory
HeapResizableDirectMemory heapMemory;
std::cout << "Created HeapResizableDirectMemory with size: " << heapMemory.Size() << "\n";
std::cout << "Reserved capacity: " << heapMemory.ReservedCapacity() << "\n";
std::cout << "Used capacity: " << heapMemory.UsedCapacity() << "\n";

// Set used capacity
heapMemory.UsedCapacity(1024);
std::cout << "After setting used capacity to 1024: " << heapMemory.UsedCapacity() << "\n";

// Test ArrayMemory
ArrayMemory<int> arrayMemory(10);
std::cout << "Created ArrayMemory<int> with size: " << arrayMemory.Size() << "\n";

// Test array access
arrayMemory[0] = 42;
arrayMemory[1] = 100;
std::cout << "Set arrayMemory[0] = 42, arrayMemory[1] = 100\n";
std::cout << "arrayMemory[0] = " << arrayMemory[0] << "\n";
std::cout << "arrayMemory[1] = " << arrayMemory[1] << "\n";

// Test DirectMemoryAsArrayMemoryAdapter
DirectMemoryAsArrayMemoryAdapter<std::byte> adapter(heapMemory);
std::cout << "Created DirectMemoryAsArrayMemoryAdapter<std::byte>\n";
std::cout << "Adapter size: " << adapter.Size() << "\n";

// Set some bytes
adapter[0] = std::byte{0xFF};
adapter[1] = std::byte{0xAB};
std::cout << "Set adapter[0] = 0xFF, adapter[1] = 0xAB\n";
std::cout << "adapter[0] = " << static_cast<int>(adapter[0]) << "\n";
std::cout << "adapter[1] = " << static_cast<int>(adapter[1]) << "\n";

std::cout << "All tests passed!\n";
return 0;
}
catch (const std::exception& e) {
std::cerr << "Test failed with exception: " << e.what() << "\n";
return 1;
}
}
Loading