FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
cmem_pool.cpp
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 
41 
42 #include <stdlib.h>
43 #include "mem_pool.hpp"
44 
45 
46 using namespace cpp_freertos;
47 
48 
50 {
54  if (Alignment < (int)sizeof(unsigned char *)) {
55  Alignment = (int)sizeof(unsigned char *);
56  }
57 
58  int alignmentBit = 0x1;
59  int i;
60 
61  for (i = 0; i < 31; i++) {
62  if (Alignment == alignmentBit) {
63  break;
64  }
65  alignmentBit <<= 1;
66  }
67 
68  if (i >= 31) {
69 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
71 #else
72  configASSERT(!"MemoryPool Bad Alignment");
73 #endif
74  }
75 }
76 
77 
79 {
80  if (ItemSize <= Alignment) {
81 
83  }
84  else {
85 
86  int alignmentCount = ItemSize / Alignment;
87  if (ItemSize % Alignment != 0) {
88  alignmentCount++;
89  }
90 
91  ItemSize = alignmentCount * Alignment;
92  }
93 }
94 
95 
96 MemoryPool::MemoryPool( int itemSize,
97  int itemCount,
98  int alignment)
99  : ItemSize(itemSize),
100  Alignment(alignment)
101 {
103 
105 
106  unsigned char *address = (unsigned char *)malloc(ItemSize * itemCount);
107 
108  if (address == NULL) {
109 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
111 #else
112  configASSERT(!"MemoryPool malloc Failed");
113 #endif
114  }
115 
116  for (int i = 0; i < itemCount; i++) {
117  FreeItems.push_back(address);
118  address += ItemSize;
119  }
120 
121  Lock = new MutexStandard();
122 }
123 
124 
126  void *preallocatedMemory,
127  int preallocatedMemorySize,
128  int alignment)
129  : ItemSize(itemSize),
130  Alignment(alignment)
131 {
133 
135 
136  unsigned char *address = (unsigned char *)preallocatedMemory;
137 
138  while (preallocatedMemorySize >= ItemSize) {
139 
140  FreeItems.push_back(address);
141  address += ItemSize;
142  preallocatedMemorySize -= ItemSize;
143  }
144 
145  Lock = new MutexStandard();
146 }
147 
148 
150 {
151  LockGuard guard(*Lock);
152 
153  if (FreeItems.empty())
154  return NULL;
155 
156  void *item = FreeItems.front();
157  FreeItems.pop_front();
158 
159  return item;
160 }
161 
162 
163 void MemoryPool::Free(void *item)
164 {
165  LockGuard guard(*Lock);
166  FreeItems.push_back(item);
167 }
168 
169 
170 void MemoryPool::AddMemory(int itemCount)
171 {
172  unsigned char *address = (unsigned char *)malloc(ItemSize * itemCount);
173 
174  if (address == NULL) {
175 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
177 #else
178  configASSERT(!"MemoryPool AddMemory Failed");
179 #endif
180  }
181 
182  for (int i = 0; i < itemCount; i++) {
183 
184  LockGuard guard(*Lock);
185 
186  FreeItems.push_back(address);
187  address += ItemSize;
188  }
189 }
190 
191 
192 void MemoryPool::AddMemory( void *preallocatedMemory,
193  int preallocatedMemorySize)
194 {
195  unsigned char *address = (unsigned char *)preallocatedMemory;
196 
197  while (preallocatedMemorySize >= ItemSize) {
198 
199  LockGuard guard(*Lock);
200 
201  FreeItems.push_back(address);
202  address += ItemSize;
203  preallocatedMemorySize -= ItemSize;
204  }
205 }
206 
207 
std::list< void * > FreeItems
Definition: mem_pool.hpp:251
MemoryPool(int itemSize, int itemCount, int alignment)
Definition: cmem_pool.cpp:96
void Free(void *item)
Definition: cmem_pool.cpp:163
void AddMemory(int itemCount)
Definition: cmem_pool.cpp:170