// // YaPB - Counter-Strike Bot based on PODBot by Markus Klinge. // Copyright © 2004-2020 YaPB Project . // // SPDX-License-Identifier: MIT // #pragma once #include #include #include // provide placment new to avoid stdc++ header inline void *operator new (const size_t, void *ptr) noexcept { return ptr; } CR_NAMESPACE_BEGIN // default allocator for cr-objects class Allocator : public Singleton { public: Allocator () = default; ~Allocator () = default; public: template T *allocate (const size_t length = 1) { auto memory = reinterpret_cast (malloc (cr::max (1u, length * sizeof (T)))); if (!memory) { plat.abort (); } return memory; } template void deallocate (T *memory) { free (memory); memory = nullptr; } public: template void construct (T *memory, Args &&...args) { new (memory) T (cr::forward (args)...); } template void destruct (T *memory) { memory->~T (); } public: template T *create (Args &&...args) { auto memory = allocate (); new (memory) T (cr::forward (args)...); return memory; } template T *createArray (const size_t amount) { auto memory = allocate (amount); for (size_t i = 0; i < amount; ++i) { new (memory + i) T (); } return memory; } template void destroy (T *memory) { if (memory) { destruct (memory); deallocate (memory); } } }; CR_EXPOSE_GLOBAL_SINGLETON (Allocator, alloc); template class UniquePtr; // implment singleton with UniquePtr template T &Singleton ::instance () { static const UniquePtr instance_ { alloc.create () }; return *instance_; } CR_NAMESPACE_END