FreeRTOS Addons
Loading...
Searching...
No Matches
mem_pool.hpp
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#include "freertos-config.h"
41
42#ifndef MEM_POOL_HPP_
43#define MEM_POOL_HPP_
44
52#ifndef CPP_FREERTOS_NO_EXCEPTIONS
53#include <exception>
54#include <string>
55#include <cstdio>
56#ifdef CPP_FREERTOS_NO_CPP_STRINGS
57#error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
58#endif
59#endif
60#include <list>
61#include "FreeRTOS.h"
62#include "mutex.hpp"
63
64namespace cpp_freertos {
65
66
67#ifndef CPP_FREERTOS_NO_EXCEPTIONS
71class MemoryPoolMallocException : public std::exception {
72
73 public:
78 {
79 sprintf(errorString, "MemoryPool malloc Failed");
80 }
81
86 virtual const char *what() const throw()
87 {
88 return errorString;
89 }
90
91 private:
95 char errorString[80];
96};
97
102class MemoryPoolBadAlignmentException : public std::exception {
103
104 public:
109 {
110 sprintf(errorString, "MemoryPool Bad Alignment");
111 }
112
117 virtual const char *what() const throw()
118 {
119 return errorString;
120 }
121
122 private:
126 char errorString[80];
127};
128
129#endif
130
131
143
145 //
146 // Public API
147 //
149 public:
150
165 MemoryPool( int itemSize,
166 int itemCount,
167 int alignment);
168
184 MemoryPool( int itemSize,
185 void *preallocatedMemory,
186 int preallocatedMemorySize,
187 int alignment);
188
197 void AddMemory(int itemCount);
198
209 void AddMemory( void *preallocatedMemory,
210 int preallocatedMemorySize);
211
217 void *Allocate();
218
225 void Free(void *item);
226
228 //
229 // Private API
230 // The internals of this class.
231 //
233 private:
234
238 Mutex *Lock;
239
243 int ItemSize;
244
248 int Alignment;
249
253 std::list<void *>FreeItems;
254
259 void CalculateValidAlignment();
260
264 void CalculateItemSize();
265
266//
267// If we are using C++11 or later, take advantage of the
268// newer features to find bugs.
269//
270#if __cplusplus >= 201103L
277 ~MemoryPool() = delete;
278#else
284 ~MemoryPool();
285#endif
286
287
288};
289
290
291}
292
293#endif
294
MemoryPoolBadAlignmentException()
Definition: mem_pool.hpp:108
virtual const char * what() const
Definition: mem_pool.hpp:117
Definition: mem_pool.hpp:142
void * Allocate()
Definition: cmem_pool.cpp:149
void AddMemory(int itemCount)
Definition: cmem_pool.cpp:170
void Free(void *item)
Definition: cmem_pool.cpp:163
Definition: mem_pool.hpp:71
virtual const char * what() const
Definition: mem_pool.hpp:86
MemoryPoolMallocException()
Definition: mem_pool.hpp:77
Definition: mutex.hpp:109
Definition: condition_variable.hpp:57