Fillwave  10.0.0
AllocatorStack.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 #include <array>
26 
27 namespace flw {
28 
29 /*
30  * Limitations
31  *
32  * - TValueType must have a default cconstructor
33  * - Size is limited to 1kB
34  *
35  * */
36 
37 template <class TValueType>
39 
40  public:
41 
42  using value_type = TValueType;
43 
44  using pointer = TValueType*;
45 
46  using difference_type = typename std::pointer_traits<pointer>::difference_type;
47 
48  using size_type = std::make_unsigned_t<difference_type>;
49 
50  using propagate_on_container_copy_assignment = std::true_type;
51 
52  using propagate_on_container_move_assignment = std::true_type;
53 
54  using propagate_on_container_swap = std::true_type;
55 
56  using is_always_equal = std::true_type;
57 
58  AllocatorStack() noexcept {
59  // nothing
60  }
61 
62  AllocatorStack(AllocatorStack const& allocator) noexcept {
63  *this = allocator;
64  }
65 
66  template <class TAllocatorType>
67  AllocatorStack(const AllocatorStack<TAllocatorType> & allocator) noexcept {
68  // nothing
69  }
70 
71  ~AllocatorStack() {
72  // nothing
73  }
74 
75  template <class... Args>
76  void construct(TValueType* p, Args&& ... args) {
77  *p = TValueType(std::forward<Args...>(args...));
78  }
79 
80  void destroy(TValueType* ) {
81  // nothing
82  }
83 
84  size_type max_size() {
85  return mSizeElements;
86  }
87 
88  TValueType* allocate(size_t) {
89  return static_cast<TValueType*>(mValues.data());
90  }
91 
92  void deallocate(TValueType*, size_t) {
93  // nothing
94  }
95 
96  private:
97 
98  static constexpr size_t mSizeBytes = 1 << 10;
99 
100  static constexpr size_t mSizeElements = mSizeBytes / sizeof(TValueType);
101 
102  std::array<TValueType, mSizeElements> mValues;
103 };
104 
105 template <class TValueType>
106 bool operator == (AllocatorStack<TValueType> const&, AllocatorStack<TValueType> const&) {
107  return true;
108 }
109 
110 template <class TValueType>
111 bool operator != (AllocatorStack<TValueType> const&, AllocatorStack<TValueType> const&) {
112  return false;
113 }
114 
115 } /* flw */
Definition: Aliases.h:30
Definition: AllocatorStack.h:38