// // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Copyright (c) Yet Another POD-Bot Contributors . // // This software is licensed under the MIT license. // Additional exceptions apply. For full license details, see LICENSE.txt // #pragma once #include #include #include #include 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 ptr = reinterpret_cast (calloc (length, sizeof (T))); if (!ptr) { plat.abort (); } // calloc on linux with debug enabled doesn't always zero out memory #if defined (CR_DEBUG) && !defined (CR_WINDOWS) plat.bzero (ptr, length); #endif return ptr; } template void deallocate (T *ptr) { if (ptr) { free (reinterpret_cast (ptr)); } } public: template void construct (T *d, Args &&...args) { new (d) T (cr::forward (args)...); } template void destruct (T *d) { d->~T (); } public: template T *create (Args &&...args) { auto d = allocate (); new (d) T (cr::forward (args)...); return d; } template void destroy (T *ptr) { if (ptr) { destruct (ptr); deallocate (ptr); } } }; static auto &alloc = Allocator::get (); CR_NAMESPACE_END