update Allocator
This commit is contained in:
parent
764b67d4d3
commit
ee165a234f
|
|
@ -26,7 +26,7 @@ namespace kiwano
|
||||||
|
|
||||||
CanvasPtr Canvas::Create(const Size& size)
|
CanvasPtr Canvas::Create(const Size& size)
|
||||||
{
|
{
|
||||||
void* mem = memory::Alloc<Canvas>();
|
void* mem = memory::Alloc(sizeof(Canvas));
|
||||||
CanvasPtr ptr = ::new (mem) Canvas;
|
CanvasPtr ptr = ::new (mem) Canvas;
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,26 @@ namespace memory
|
||||||
|
|
||||||
MemoryAllocator* current_allocator_ = nullptr;
|
MemoryAllocator* current_allocator_ = nullptr;
|
||||||
|
|
||||||
|
MemoryAllocator* GetGlobalAllocator()
|
||||||
|
{
|
||||||
|
class KGE_API GlobalAllocator : public MemoryAllocator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void* Alloc(size_t size) override
|
||||||
|
{
|
||||||
|
return ::operator new(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Free(void* ptr, size_t size) override
|
||||||
|
{
|
||||||
|
::operator delete(ptr, size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static GlobalAllocator global_allocator;
|
||||||
|
return &global_allocator;
|
||||||
|
}
|
||||||
|
|
||||||
MemoryAllocator* GetAllocator()
|
MemoryAllocator* GetAllocator()
|
||||||
{
|
{
|
||||||
if (!current_allocator_)
|
if (!current_allocator_)
|
||||||
|
|
@ -42,22 +62,5 @@ void SetAllocator(MemoryAllocator* allocator)
|
||||||
current_allocator_ = allocator;
|
current_allocator_ = allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalAllocator* GetGlobalAllocator()
|
|
||||||
{
|
|
||||||
static GlobalAllocator global_allocator;
|
|
||||||
return &global_allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* GlobalAllocator::Alloc(size_t size)
|
|
||||||
{
|
|
||||||
return ::malloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GlobalAllocator::Free(void* ptr, size_t size)
|
|
||||||
{
|
|
||||||
KGE_NOT_USED(size);
|
|
||||||
::free(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace memory
|
} // namespace memory
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,9 @@
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <utility>
|
#include <utility> // std::forward
|
||||||
#include <type_traits>
|
#include <limits> // std::numeric_limits
|
||||||
|
#include <memory> // std::addressof
|
||||||
#include <kiwano/macros.h>
|
#include <kiwano/macros.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
|
|
@ -52,29 +53,16 @@ void SetAllocator(MemoryAllocator* allocator);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 使用当前内存分配器分配内存
|
/// @brief 使用当前内存分配器分配内存
|
||||||
template <typename _Ty>
|
inline void* Alloc(size_t size)
|
||||||
inline void* Alloc()
|
|
||||||
{
|
{
|
||||||
return memory::GetAllocator()->Alloc(sizeof(_Ty));
|
return memory::GetAllocator()->Alloc(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 使用当前内存分配器释放内存
|
/// @brief 使用当前内存分配器释放内存
|
||||||
inline void Free(void* ptr, size_t size)
|
inline void Free(void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
return memory::GetAllocator()->Free(ptr, size);
|
memory::GetAllocator()->Free(ptr, size);
|
||||||
}
|
|
||||||
|
|
||||||
/// \~chinese
|
|
||||||
/// @brief 构造对象
|
|
||||||
template <typename _Ty>
|
|
||||||
inline _Ty* Construct(void* addr)
|
|
||||||
{
|
|
||||||
if (addr)
|
|
||||||
{
|
|
||||||
return ::new (addr) _Ty;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
|
|
@ -93,25 +81,12 @@ inline void Destroy(_Ty* ptr)
|
||||||
ptr->~_Ty();
|
ptr->~_Ty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \~chinese
|
|
||||||
/// @brief 使用当前内存分配器创建对象
|
|
||||||
template <typename _Ty>
|
|
||||||
inline _Ty* New()
|
|
||||||
{
|
|
||||||
void* ptr = memory::Alloc<_Ty>();
|
|
||||||
if (ptr)
|
|
||||||
{
|
|
||||||
return memory::Construct<_Ty>(ptr);
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 使用当前内存分配器创建对象
|
/// @brief 使用当前内存分配器创建对象
|
||||||
template <typename _Ty, typename... _Args>
|
template <typename _Ty, typename... _Args>
|
||||||
inline _Ty* New(_Args&&... args)
|
inline _Ty* New(_Args&&... args)
|
||||||
{
|
{
|
||||||
void* ptr = memory::Alloc<_Ty>();
|
void* ptr = memory::Alloc(sizeof(_Ty));
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
return memory::Construct<_Ty>(ptr, std::forward<_Args>(args)...);
|
return memory::Construct<_Ty>(ptr, std::forward<_Args>(args)...);
|
||||||
|
|
@ -131,23 +106,114 @@ inline void Delete(_Ty* ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 全局内存分配器,使用malloc和free分配内存
|
/// @brief ·ÖÅäÆ÷
|
||||||
class KGE_API GlobalAllocator : public MemoryAllocator
|
template <typename _Ty>
|
||||||
|
class Allocator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
typedef _Ty value_type;
|
||||||
/// @brief 申请内存
|
typedef _Ty* pointer;
|
||||||
virtual void* Alloc(size_t size) override;
|
typedef const _Ty* const_pointer;
|
||||||
|
typedef _Ty& reference;
|
||||||
|
typedef const _Ty& const_reference;
|
||||||
|
|
||||||
/// \~chinese
|
using size_type = size_t;
|
||||||
/// @brief 释放内存
|
using difference_type = ptrdiff_t;
|
||||||
virtual void Free(void* ptr, size_t size) override;
|
|
||||||
|
template <class _Other>
|
||||||
|
struct rebind
|
||||||
|
{
|
||||||
|
using other = Allocator<_Other>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \~chinese
|
Allocator() noexcept {}
|
||||||
/// @brief 获取全局内存分配器
|
|
||||||
GlobalAllocator* GetGlobalAllocator();
|
Allocator(const Allocator&) noexcept = default;
|
||||||
|
|
||||||
|
template <class _Other>
|
||||||
|
Allocator(const Allocator<_Other>&) noexcept
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline _Ty* allocate(size_t count)
|
||||||
|
{
|
||||||
|
if (count > 0)
|
||||||
|
{
|
||||||
|
return static_cast<_Ty*>(memory::Alloc(sizeof(_Ty) * count));
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void* allocate(size_t count, const void*)
|
||||||
|
{
|
||||||
|
return allocate(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void deallocate(void* ptr, size_t count)
|
||||||
|
{
|
||||||
|
memory::Free(ptr, sizeof(_Ty) * count);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename _UTy, typename... _Args>
|
||||||
|
inline void construct(_UTy* ptr, _Args&&... args)
|
||||||
|
{
|
||||||
|
memory::Construct<_UTy>(ptr, std::forward<_Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename _UTy>
|
||||||
|
inline void destroy(_UTy* ptr)
|
||||||
|
{
|
||||||
|
memory::Destroy<_UTy>(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t max_size() const noexcept
|
||||||
|
{
|
||||||
|
return std::numeric_limits<size_t>::max() / sizeof(_Ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
_Ty* address(_Ty& val) const noexcept
|
||||||
|
{
|
||||||
|
return std::addressof(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
const _Ty* address(const _Ty& val) const noexcept
|
||||||
|
{
|
||||||
|
return std::addressof(val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Allocator<void>
|
||||||
|
template <>
|
||||||
|
class Allocator<void>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = void;
|
||||||
|
typedef void* pointer;
|
||||||
|
typedef const void* const_pointer;
|
||||||
|
|
||||||
|
using size_type = size_t;
|
||||||
|
using difference_type = ptrdiff_t;
|
||||||
|
|
||||||
|
template <class _Other>
|
||||||
|
struct rebind
|
||||||
|
{
|
||||||
|
using other = Allocator<_Other>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _Ty, class _Other>
|
||||||
|
bool operator==(const Allocator<_Ty>&, const Allocator<_Other>&) noexcept
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class _Ty, class _Other>
|
||||||
|
bool operator!=(const Allocator<_Ty>&, const Allocator<_Other>&) noexcept
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace memory
|
} // namespace memory
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue