Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
ipool.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_IPOOL__
30 #define __ETL_IPOOL__
31 #define __ETL_IN_IPOOL_H__
32 
33 #include "pool_base.h"
34 #include "nullptr.h"
35 
36 #ifndef ETL_THROW_EXCEPTIONS
37 #include "error_handler.h"
38 #endif
39 
40 namespace etl
41 {
42  //*************************************************************************
44  //*************************************************************************
45  template <typename T>
46  class ipool : public pool_base
47  {
48  public:
49 
50  //*************************************************************************
55  //*************************************************************************
56  T* allocate()
57  {
58  if (next_free != MAX_SIZE)
59  {
60  T* result = &p_buffer[next_free];
61  in_use_flags.set(next_free);
62  next_free = in_use_flags.find_first(false);
64  return result;
65  }
66  else
67  {
68 #ifdef ETL_THROW_EXCEPTIONS
69  throw pool_no_allocation();
70 #else
72 #endif
73  return nullptr;
74  }
75  }
76 
77  //*************************************************************************
82  //*************************************************************************
83  void release(const T& object)
84  {
85  release(&object);
86  }
87 
88  //*************************************************************************
93  //*************************************************************************
94  void release(const T* const p_object)
95  {
96  // Does it belong to me?
97  if (is_in_pool(p_object))
98  {
99  typename std::iterator_traits<T*>::difference_type distance = p_object - p_buffer;
100 
101  // Mark the object as available.
102  next_free = static_cast<size_t>(distance);
103  in_use_flags.reset(next_free);
104  --items_allocated;
105  }
106  else
107  {
108 #ifdef ETL_THROW_EXCEPTIONS
109  throw pool_object_not_in_pool();
110 #else
112 #endif
113  }
114  }
115 
116  //*************************************************************************
120  //*************************************************************************
121  bool is_in_pool(const T& object) const
122  {
123  return is_in_pool(&object);
124  }
125 
126  //*************************************************************************
130  //*************************************************************************
131  bool is_in_pool(const T* const p_object) const
132  {
133  // Does this object belong to this pool?
134  typename std::iterator_traits<T*>::difference_type distance = p_object - p_buffer;
135 
136  // Within the range of the buffer?
137  return ((distance >= 0) && (distance < static_cast<typename std::iterator_traits<T*>::difference_type>(MAX_SIZE)));
138  }
139 
140  protected:
141 
142  //*************************************************************************
144  //*************************************************************************
145  ipool(T* p_buffer, ibitset& in_use_flags, size_t size)
146  : pool_base(size),
147  p_buffer(nullptr),
148  in_use_flags(in_use_flags)
149  {
150  }
151 
152  T* p_buffer;
153  ibitset& in_use_flags;
154  };
155 }
156 #endif
157 
virtual ibitset & set(size_t position, bool value=true)=0
Set the bit at the position.
void release(const T &object)
Definition: ipool.h:83
Definition: pool_base.h:63
Definition: pool_base.h:89
ipool(T *p_buffer, ibitset &in_use_flags, size_t size)
Constructor.
Definition: ipool.h:145
const size_t MAX_SIZE
The maximum number of objects that can be allocated.
Definition: pool_base.h:124
Definition: pool_base.h:76
Definition: algorithm.h:43
size_t items_allocated
The number of items allocated.
Definition: pool_base.h:123
T * allocate()
Definition: ipool.h:56
Definition: ibitset.h:38
virtual size_t find_first(bool state) const =0
Definition: ipool.h:46
static void error(const exception &e)
Definition: error_handler.cpp:50
bool is_in_pool(const T *const p_object) const
Definition: ipool.h:131
size_t next_free
The position of the next free item in the pool.
Definition: pool_base.h:122
bool is_in_pool(const T &object) const
Definition: ipool.h:121
void release(const T *const p_object)
Definition: ipool.h:94
virtual ibitset & reset(size_t position)=0
Reset the bit at the position.