2019-07-27 17:36:24 +03:00
|
|
|
//
|
|
|
|
|
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
|
2019-09-21 23:20:33 +03:00
|
|
|
// Copyright (c) Yet Another POD-Bot Contributors <yapb@entix.io>.
|
2019-07-27 17:36:24 +03:00
|
|
|
//
|
2019-09-21 23:20:33 +03:00
|
|
|
// This software is licensed under the MIT license.
|
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt
|
2019-07-27 17:36:24 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
|
|
#include <crlib/cr-basic.h>
|
|
|
|
|
#include <crlib/cr-movable.h>
|
|
|
|
|
#include <crlib/cr-platform.h>
|
|
|
|
|
|
|
|
|
|
CR_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
// default allocator for cr-objects
|
|
|
|
|
class Allocator : public Singleton <Allocator> {
|
|
|
|
|
public:
|
|
|
|
|
Allocator () = default;
|
|
|
|
|
~Allocator () = default;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
template <typename T> T *allocate (const size_t length = 1) {
|
|
|
|
|
auto ptr = reinterpret_cast <T *> (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)
|
2019-09-21 23:32:49 +03:00
|
|
|
plat.bzero (ptr, length);
|
2019-07-27 17:36:24 +03:00
|
|
|
#endif
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T> void deallocate (T *ptr) {
|
|
|
|
|
if (ptr) {
|
|
|
|
|
free (reinterpret_cast <T *> (ptr));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
template <typename T, typename ...Args> void construct (T *d, Args &&...args) {
|
|
|
|
|
new (d) T (cr::forward <Args> (args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T> void destruct (T *d) {
|
|
|
|
|
d->~T ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
template <typename T, typename ...Args> T *create (Args &&...args) {
|
|
|
|
|
auto d = allocate <T> ();
|
|
|
|
|
new (d) T (cr::forward <Args> (args)...);
|
|
|
|
|
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T> void destroy (T *ptr) {
|
|
|
|
|
if (ptr) {
|
|
|
|
|
destruct (ptr);
|
|
|
|
|
deallocate (ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static auto &alloc = Allocator::get ();
|
|
|
|
|
|
|
|
|
|
CR_NAMESPACE_END
|