Open FFBoard
Open source force feedback firmware
mem_pool.hpp
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4 *
5 * This file is part of the FreeRTOS Add-ons project.
6 *
7 * Source Code:
8 * https://github.com/michaelbecker/freertos-addons
9 *
10 * Project Page:
11 * http://michaelbecker.github.io/freertos-addons/
12 *
13 * On-line Documentation:
14 * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files
18 * (the "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so,subject to the
22 * following conditions:
23 *
24 * + The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
26 * + Credit is appreciated, but not required, if you find this project
27 * useful enough to include in your application, product, device, etc.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 ***************************************************************************/
38
39
40#ifndef MEM_POOL_HPP_
41#define MEM_POOL_HPP_
42
50#ifndef CPP_FREERTOS_NO_EXCEPTIONS
51#include <exception>
52#include <string>
53#include <cstdio>
54#ifdef CPP_FREERTOS_NO_CPP_STRINGS
55#error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
56#endif
57#endif
58#include <list>
59#include "FreeRTOS.h"
60#include "mutex.hpp"
61
62namespace cpp_freertos {
63
64
65#ifndef CPP_FREERTOS_NO_EXCEPTIONS
69class MemoryPoolMallocException : public std::exception {
70
71 public:
76 {
77 sprintf(errorString, "MemoryPool malloc Failed");
78 }
79
84 virtual const char *what() const throw()
85 {
86 return errorString;
87 }
88
89 private:
93 char errorString[80];
94};
95
100class MemoryPoolBadAlignmentException : public std::exception {
101
102 public:
107 {
108 sprintf(errorString, "MemoryPool Bad Alignment");
109 }
110
115 virtual const char *what() const throw()
116 {
117 return errorString;
118 }
119
120 private:
124 char errorString[80];
125};
126
127#endif
128
129
141
143 //
144 // Public API
145 //
147 public:
148
163 MemoryPool( int itemSize,
164 int itemCount,
165 int alignment);
166
182 MemoryPool( int itemSize,
183 void *preallocatedMemory,
184 int preallocatedMemorySize,
185 int alignment);
186
195 void AddMemory(int itemCount);
196
207 void AddMemory( void *preallocatedMemory,
208 int preallocatedMemorySize);
209
215 void *Allocate();
216
223 void Free(void *item);
224
226 //
227 // Private API
228 // The internals of this class.
229 //
231 private:
232
237
242
247
251 std::list<void *>FreeItems;
252
258
262 void CalculateItemSize();
263
264//
265// If we are using C++11 or later, take advantage of the
266// newer features to find bugs.
267//
268#if __cplusplus >= 201103L
275 ~MemoryPool() = delete;
276#else
283#endif
284
285
286};
287
288
289}
290
291#endif
292
virtual const char * what() const
Definition: mem_pool.hpp:115
std::list< void * > FreeItems
Definition: mem_pool.hpp:251
void AddMemory(int itemCount)
Definition: cmem_pool.cpp:170
MemoryPool(int itemSize, int itemCount, int alignment)
Definition: cmem_pool.cpp:96
void Free(void *item)
Definition: cmem_pool.cpp:163
virtual const char * what() const
Definition: mem_pool.hpp:84