mn_basic_allocator_stack.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINILIB_ALLOCATOR_STACK_H__
19 #define __MINILIB_ALLOCATOR_STACK_H__
20 
21 #include "../mn_config.hpp"
22 
23 #include "mn_basic_allocator.hpp"
25 
26 namespace mn {
27  namespace memory {
28 
39  template <int TBUFFERSIZE>
41  public:
44 
45  static void first() noexcept { }
46 
47  static void* allocate(size_t size, size_t alignment) noexcept {
48  MN_UNUSED_VARIABLE(alignment);
49 
50  if(m_bufferTop < TBUFFERSIZE) {
51  if( (m_bufferTop+size) <= TBUFFERSIZE) {
52  char* ret = &m_aBuffer[0] + m_bufferTop;
53  m_bufferTop += size;
54 
55  return (void*)ret;
56  }
57  }
58  return nullptr;
59  }
60 
61  static void deallocate(void* ptr, size_t size, size_t alignment) noexcept {
62  MN_UNUSED_VARIABLE(size);
63  MN_UNUSED_VARIABLE(alignment);
64  MN_UNUSED_VARIABLE(ptr);
65  }
66 
67  static size_t max_node_size() {
68  return size_t(-1);
69  }
70  static size_t get_max_alocator_size() {
71  return __SIZE_MAX__;
72  }
73  private:
74  static size_t m_bufferTop;
75  static char* m_aBuffer[TBUFFERSIZE];
76  };
77 
78  template <int TBUFFERSIZE>
80  template <int TBUFFERSIZE>
82 
83  template <int TBUFFERSIZE, class TFilter = basic_allocator_filter>
85 
86  }
87 }
88 
89 
90 
91 #endif // __MINILIB_ALLOCATOR_STACK_H__
Stack based allocator.
Definition: mn_basic_allocator_stack.hpp:40
static size_t max_node_size()
Definition: mn_basic_allocator_stack.hpp:67
static size_t m_bufferTop
Definition: mn_basic_allocator_stack.hpp:74
static char * m_aBuffer[TBUFFERSIZE]
Definition: mn_basic_allocator_stack.hpp:75
static size_t get_max_alocator_size()
Definition: mn_basic_allocator_stack.hpp:70
static void first() noexcept
Definition: mn_basic_allocator_stack.hpp:45
std::false_type is_thread_safe
Definition: mn_basic_allocator_stack.hpp:43
std_allocator_tag() allocator_category
Definition: mn_basic_allocator_stack.hpp:42
static void * allocate(size_t size, size_t alignment) noexcept
Definition: mn_basic_allocator_stack.hpp:47
static void deallocate(void *ptr, size_t size, size_t alignment) noexcept
Definition: mn_basic_allocator_stack.hpp:61
Definition: mn_basic_allocator.hpp:49
#define MN_UNUSED_VARIABLE(var)
Definition: mn_defines.hpp:40
Definition: mn_allocator_typetraits.hpp:27
Definition: mn_allocator_typetraits.hpp:25
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
integral_constant< bool, false > false_type
Definition: mn_typetraits.hpp:130