diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-24 02:13:48 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-24 02:13:48 +0100 |
| commit | 65a8349aa5526d071b18cd4d42586c46faaa3823 (patch) | |
| tree | f6e2063319ceaaa02f523fb5c289e4f37411a2df /examples/allocator_system | |
| parent | df4ec096491ded6d58b9ee094d6942e3188c2d4a (diff) | |
feat! breaking changes for OCL v1.0.48.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'examples/allocator_system')
| -rw-r--r-- | examples/allocator_system/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | examples/allocator_system/allocator_system.cc | 53 |
2 files changed, 69 insertions, 0 deletions
diff --git a/examples/allocator_system/CMakeLists.txt b/examples/allocator_system/CMakeLists.txt new file mode 100644 index 0000000..a4bb69d --- /dev/null +++ b/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 ../../include/ocl) diff --git a/examples/allocator_system/allocator_system.cc b/examples/allocator_system/allocator_system.cc new file mode 100644 index 0000000..fd7ae56 --- /dev/null +++ b/examples/allocator_system/allocator_system.cc @@ -0,0 +1,53 @@ +/* + * File: allocator_system.cc + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. Licensed under the BSL 1.0 license + */ + +#include <memory/allocator_system.hpp> +#include <iostream> + +class MyClass final +{ +public: + 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() +{ + ocl::standard_allocator_type<MyClass> 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<int, std::string>(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"; +} |
