From 79f9ae989ecd62a875e2282b010a73abef1d1cf1 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 31 Aug 2025 11:10:20 +0200 Subject: feat: `allocator_system` container for custom allocator needs. Signed-off-by: Amlal El Mahrouss --- dev/lib/core/allocator_system.hpp | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 dev/lib/core/allocator_system.hpp diff --git a/dev/lib/core/allocator_system.hpp b/dev/lib/core/allocator_system.hpp new file mode 100644 index 0000000..c75971b --- /dev/null +++ b/dev/lib/core/allocator_system.hpp @@ -0,0 +1,75 @@ +/* + * File: core/allocator_system.hpp + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss. Licensed under the BSL 1.0 license + */ + +#ifndef _OCL_ALLOCATOR_SYSTEM_HPP +#define _OCL_ALLOCATOR_SYSTEM_HPP + +#include +#include +#include + +namespace ocl +{ + struct new_op final + { + template + inline auto operator()() -> type* + { + return new type; + } + + template + inline auto operator()(var_type&&... args) -> type* + { + return new type{std::forward(args...)}; + } + }; + + struct delete_op final + { + template + inline auto operator()(type* t) -> void + { + delete t; + } + }; + + template + class allocator_system + { + allocator_delete del_; + + public: + allocator_system() = default; + ~allocator_system() = default; + + allocator_system& operator=(const allocator_system&) = delete; + allocator_system(const allocator_system&) = delete; + + template + ret_type* claim() noexcept + { + return allocator_new(sizeof(ret_type)); + } + + template + auto construct(var_type&&... args) -> std::shared_ptr + { + return std::shared_ptr(allocator_new(ret_type{std::forward(args...)}), del_()); + } + + template + void unclaim(ret_type* ptr) + { + del_(ptr); + } + }; + + using standard_allocator_type = allocator_system; +} // namespace ocl + +#endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP -- cgit v1.2.3