diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 11:10:20 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 11:10:20 +0200 |
| commit | 79f9ae989ecd62a875e2282b010a73abef1d1cf1 (patch) | |
| tree | d2428a33aea931be4de4e5301262b6b857773987 /dev/lib/core | |
| parent | e69eeb7fe7076d968b5dccfb500cf5cb7607bf28 (diff) | |
feat: `allocator_system` container for custom allocator needs.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/lib/core')
| -rw-r--r-- | dev/lib/core/allocator_system.hpp | 75 |
1 files changed, 75 insertions, 0 deletions
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 <lib/core/includes.hpp> +#include <stdexcept> +#include <memory> + +namespace ocl +{ + struct new_op final + { + template <typename type> + inline auto operator()() -> type* + { + return new type; + } + + template <typename type, typename... var_type> + inline auto operator()(var_type&&... args) -> type* + { + return new type{std::forward(args...)}; + } + }; + + struct delete_op final + { + template <typename type> + inline auto operator()(type* t) -> void + { + delete t; + } + }; + + template <typename allocator_new, typename allocator_delete> + 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 <typename ret_type> + ret_type* claim() noexcept + { + return allocator_new(sizeof(ret_type)); + } + + template <typename ret_type, typename... var_type> + auto construct(var_type&&... args) -> std::shared_ptr<ret_type> + { + return std::shared_ptr<ret_type>(allocator_new(ret_type{std::forward(args...)}), del_()); + } + + template <typename ret_type> + void unclaim(ret_type* ptr) + { + del_(ptr); + } + }; + + using standard_allocator_type = allocator_system<new_op, delete_op>; +} // namespace ocl + +#endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP |
