Fillwave  10.0.0
AllocatorHeap.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <memory>
25 
26 namespace flw {
27 
28 /*
29  * Limitations
30  *
31  * - TValueType must have a default cconstructor
32  * - Size is limited to 16kB
33  *
34  * */
35 
36 template <class TValueType>
38 
39  public:
40 
41  using value_type = TValueType;
42 
43  using pointer = TValueType*;
44 
45  using difference_type = typename std::pointer_traits<pointer>::difference_type;
46 
47  using size_type = std::make_unsigned_t<difference_type>;
48 
49  using propagate_on_container_copy_assignment = std::true_type;
50 
51  using propagate_on_container_move_assignment = std::true_type;
52 
53  using propagate_on_container_swap = std::true_type;
54 
55  using is_always_equal = std::true_type;
56 
57  AllocatorHeap() noexcept {
58  mValues = new TValueType[mSizeElements];
59  }
60 
61  AllocatorHeap(AllocatorHeap const& allocator) noexcept = default;
62 
63  template <class TAllocatorType>
64  AllocatorHeap(AllocatorHeap<TAllocatorType> const& allocator) noexcept {
65  *this = allocator;
66  }
67 
68  ~AllocatorHeap() {
69  delete [] mValues;
70  }
71 
72  template <class... Args>
73  void construct(TValueType* p, Args&& ... args) {
74  *p = TValueType(std::forward<Args...>(args...));
75  }
76 
77  void destroy(TValueType* ) {
78  // nothing
79  }
80 
81  size_type max_size() {
82  return mSizeElements;
83  }
84 
85  TValueType* allocate(size_t) {
86  return static_cast<TValueType*>(mValues);
87  }
88 
89  void deallocate(TValueType*, size_t) {
90  // nothing
91  }
92 
93  private:
94 
95  static constexpr size_t mSizeBytes = 1 << 14;
96 
97  static constexpr size_t mSizeElements = mSizeBytes / sizeof(TValueType);
98 
99  TValueType* mValues;
100 };
101 
102 template <class TValueType>
103 bool operator == (AllocatorHeap<TValueType> const&, AllocatorHeap<TValueType> const&) {
104  return true;
105 }
106 
107 template <class TValueType>
108 bool operator != (AllocatorHeap<TValueType> const&, AllocatorHeap<TValueType> const&) {
109  return false;
110 }
111 
112 } /* flw */
Definition: Aliases.h:30
Definition: AllocatorHeap.h:37