Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
pool_base.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_IN_IPOOL_H__
30 #error This header is a private element of etl::pool & etl::ipool
31 #endif
32 
33 #ifndef __ETL_POOL_BASE__
34 #define __ETL_POOL_BASE__
35 
36 #include <stddef.h>
37 
38 #include "exception.h"
39 
40 #ifndef ETL_THROW_EXCEPTIONS
41 #include "error_handler.h"
42 #endif
43 
44 namespace etl
45 {
46  //***************************************************************************
49  //***************************************************************************
50  class pool_exception : public exception
51  {
52  public:
53 
54  pool_exception(const char* what)
55  : exception(what)
56  {}
57  };
58 
59  //***************************************************************************
62  //***************************************************************************
64  {
65  public:
66 
68  : pool_exception("pool: no allocation")
69  {}
70  };
71 
72  //***************************************************************************
75  //***************************************************************************
77  {
78  public:
79 
81  : pool_exception("pool: not in pool")
82  {}
83  };
84 
85  //*************************************************************************
88  //*************************************************************************
89  class pool_base
90  {
91  public:
92 
93  //*************************************************************************
95  //*************************************************************************
96  size_t available() const
97  {
98  return MAX_SIZE - items_allocated;
99  }
100 
101  //*************************************************************************
104  //*************************************************************************
105  bool empty() const
106  {
107  return items_allocated == MAX_SIZE;
108  }
109 
110  protected:
111 
112  //*************************************************************************
114  //*************************************************************************
115  pool_base(size_t max_size)
116  : next_free(0),
117  items_allocated(0),
118  MAX_SIZE(max_size)
119  {
120  }
121 
122  size_t next_free;
124  const size_t MAX_SIZE;
125  };
126 }
127 #endif
128 
size_t available() const
Returns the number of free items in the pool.
Definition: pool_base.h:96
Definition: pool_base.h:63
Definition: pool_base.h:89
const size_t MAX_SIZE
The maximum number of objects that can be allocated.
Definition: pool_base.h:124
exception(value_type reason)
Constructor.
Definition: exception.h:51
Definition: pool_base.h:76
Definition: algorithm.h:43
size_t items_allocated
The number of items allocated.
Definition: pool_base.h:123
Definition: exception.h:42
bool empty() const
Definition: pool_base.h:105
value_type what() const
Definition: exception.h:60
size_t next_free
The position of the next free item in the pool.
Definition: pool_base.h:122
pool_base(size_t max_size)
Constructor.
Definition: pool_base.h:115
Definition: pool_base.h:50