RavEngine
Loading...
Searching...
No Matches
allocator.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_MEMORY_ALLOCATOR_H_
29#define OZZ_OZZ_BASE_MEMORY_ALLOCATOR_H_
30
31#include <cstddef>
32#include <new>
33#include <utility>
34
35#include "ozz/base/platform.h"
36
37namespace ozz {
38namespace memory {
39
40// Forwards declare Allocator class.
41class Allocator;
42
43// Defines the default allocator accessor.
44OZZ_BASE_DLL Allocator* default_allocator();
45
46// Set the default allocator, used for all dynamic allocation inside ozz.
47// Returns current memory allocator, such that in can be restored if needed.
48OZZ_BASE_DLL Allocator* SetDefaulAllocator(Allocator* _allocator);
49
50// Defines an abstract allocator class.
51// Implements helper methods to allocate/deallocate POD typed objects instead of
52// raw memory.
53// Implements New and Delete function to allocate C++ objects, as a replacement
54// of new and delete operators.
55class OZZ_BASE_DLL Allocator {
56 public:
57 // Default virtual destructor.
58 virtual ~Allocator() {}
59
60 // Next functions are the pure virtual functions that must be implemented by
61 // allocator concrete classes.
62
63 // Allocates _size bytes on the specified _alignment boundaries.
64 // Allocate function conforms with standard malloc function specifications.
65 virtual void* Allocate(size_t _size, size_t _alignment) = 0;
66
67 // Frees a block that was allocated with Allocate or Reallocate.
68 // Argument _block can be nullptr.
69 // Deallocate function conforms with standard free function specifications.
70 virtual void Deallocate(void* _block) = 0;
71};
72} // namespace memory
73
74// ozz replacement for c++ operator new with, used to allocate with an
75// ozz::memory::Allocator. Delete must be used to deallocate such object.
76// It can be used for constructor with no argument:
77// Type* object = New<Type>();
78// or any number of argument:
79// Type* object = New<Type>(1,2,3,4);
80template <typename _Ty, typename... _Args>
81_Ty* New(_Args&&... _args) {
82 void* alloc =
83 memory::default_allocator()->Allocate(sizeof(_Ty), alignof(_Ty));
84 return new (alloc) _Ty(std::forward<_Args>(_args)...);
85}
86
87template <typename _Ty>
88void Delete(_Ty* _object) {
89 if (_object) {
90 // Prevents from false "unreferenced parameter" warning when _Ty has no
91 // explicit destructor.
92 (void)_object;
93 _object->~_Ty();
94 memory::default_allocator()->Deallocate(_object);
95 }
96}
97
98} // namespace ozz
99#endif // OZZ_OZZ_BASE_MEMORY_ALLOCATOR_H_
Definition allocator.h:55