From 1ead9a09f39d417efa484475306baf13c1653142 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 31 Aug 2025 11:32:41 +0200 Subject: feat: `allocator_system` a better implementation and example added. Signed-off-by: Amlal El Mahrouss --- dev/examples/allocator_system/CMakeLists.txt | 16 +++++++++ dev/examples/allocator_system/allocator_system.cc | 43 +++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 dev/examples/allocator_system/CMakeLists.txt create mode 100644 dev/examples/allocator_system/allocator_system.cc (limited to 'dev/examples/allocator_system') diff --git a/dev/examples/allocator_system/CMakeLists.txt b/dev/examples/allocator_system/CMakeLists.txt new file mode 100644 index 0000000..ee19842 --- /dev/null +++ b/dev/examples/allocator_system/CMakeLists.txt @@ -0,0 +1,16 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Alloc + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(Alloc allocator_system.cc) + +target_link_libraries(Alloc PRIVATE Boost::container) + +set_property(TARGET Alloc PROPERTY CXX_STANDARD 20) +target_include_directories(Alloc PUBLIC ../../) diff --git a/dev/examples/allocator_system/allocator_system.cc b/dev/examples/allocator_system/allocator_system.cc new file mode 100644 index 0000000..90248cd --- /dev/null +++ b/dev/examples/allocator_system/allocator_system.cc @@ -0,0 +1,43 @@ +#include +#include + +struct MyClass +{ + int a; + std::string b; + + MyClass() : a(0), b("default") + { + std::cout << "MyClass() constructed\n"; + } + + MyClass(int x, std::string y) : a(x), b(std::move(y)) + { + std::cout << "MyClass(int, string) constructed\n"; + } + + ~MyClass() + { + std::cout << "~MyClass() destroyed\n"; + } +}; + +int main() +{ + using Alloc = ocl::standard_allocator_type; + Alloc allocator; + + // Test 1: claim() + unclaim() + std::cout << "=== Test 1: claim/unclaim ===\n"; + MyClass* raw = allocator.claim(); + std::cout << "raw->a = " << raw->a << ", raw->b = " << raw->b << "\n"; + allocator.unclaim(raw); // Manual delete + + // Test 2: construct() → shared_ptr + std::cout << "\n=== Test 2: construct (shared_ptr) ===\n"; + auto ptr = allocator.construct(42, "hello"); + std::cout << "ptr->a = " << ptr->a << ", ptr->b = " << ptr->b << "\n"; + + // `shared_ptr` will automatically delete the object + std::cout << "\n=== End of main ===\n"; +} -- cgit v1.2.3 From 2b0834fb9b4d07ed942ba0490706352caee1a282 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 31 Aug 2025 11:37:34 +0200 Subject: fix: add comment with BSL 1.0 license mention. Signed-off-by: Amlal El Mahrouss --- dev/examples/allocator_system/allocator_system.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'dev/examples/allocator_system') diff --git a/dev/examples/allocator_system/allocator_system.cc b/dev/examples/allocator_system/allocator_system.cc index 90248cd..c3cd689 100644 --- a/dev/examples/allocator_system/allocator_system.cc +++ b/dev/examples/allocator_system/allocator_system.cc @@ -1,3 +1,10 @@ +/* + * File: allocator_system.cc + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss. Licensed under the BSL 1.0 license + */ + #include #include -- cgit v1.2.3