Open FFBoard
Open source force feedback firmware
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
#include "FreeRTOS.h"
42
#include <stdlib.h>
43
#include "
mem_pool.hpp
"
44
45
46
using namespace
cpp_freertos
;
47
48
49
void
MemoryPool::CalculateValidAlignment()
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
70
throw
MemoryPoolBadAlignmentException
();
71
#else
72
configASSERT(!
"MemoryPool Bad Alignment"
);
73
#endif
74
}
75
}
76
77
78
void
MemoryPool::CalculateItemSize
()
79
{
80
if
(
ItemSize
<=
Alignment
) {
81
82
ItemSize
=
Alignment
;
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
{
102
CalculateValidAlignment
();
103
104
CalculateItemSize
();
105
106
unsigned
char
*address = (
unsigned
char
*)
malloc
(
ItemSize
* itemCount);
107
108
if
(address == NULL) {
109
#ifndef CPP_FREERTOS_NO_EXCEPTIONS
110
throw
MemoryPoolMallocException
();
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
125
MemoryPool::MemoryPool
(
int
itemSize,
126
void
*preallocatedMemory,
127
int
preallocatedMemorySize,
128
int
alignment)
129
: ItemSize(itemSize),
130
Alignment(alignment)
131
{
132
CalculateValidAlignment
();
133
134
CalculateItemSize
();
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
149
void
*
MemoryPool::Allocate
()
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
176
throw
MemoryPoolMallocException
();
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
cpp_freertos::LockGuard
Definition:
mutex.hpp:262
cpp_freertos::MemoryPoolBadAlignmentException
Definition:
mem_pool.hpp:100
cpp_freertos::MemoryPool::Allocate
void * Allocate()
Definition:
cmem_pool.cpp:149
cpp_freertos::MemoryPool::FreeItems
std::list< void * > FreeItems
Definition:
mem_pool.hpp:251
cpp_freertos::MemoryPool::AddMemory
void AddMemory(int itemCount)
Definition:
cmem_pool.cpp:170
cpp_freertos::MemoryPool::MemoryPool
MemoryPool(int itemSize, int itemCount, int alignment)
Definition:
cmem_pool.cpp:96
cpp_freertos::MemoryPool::Free
void Free(void *item)
Definition:
cmem_pool.cpp:163
cpp_freertos::MemoryPool::CalculateItemSize
void CalculateItemSize()
Definition:
cmem_pool.cpp:78
cpp_freertos::MemoryPool::Lock
Mutex * Lock
Definition:
mem_pool.hpp:236
cpp_freertos::MemoryPool::ItemSize
int ItemSize
Definition:
mem_pool.hpp:241
cpp_freertos::MemoryPool::CalculateValidAlignment
void CalculateValidAlignment()
Definition:
cmem_pool.cpp:49
cpp_freertos::MemoryPool::Alignment
int Alignment
Definition:
mem_pool.hpp:246
cpp_freertos::MemoryPoolMallocException
Definition:
mem_pool.hpp:69
cpp_freertos::MutexStandard
Definition:
mutex.hpp:169
malloc
void * malloc(size_t size)
Definition:
cppmain.cpp:130
mem_pool.hpp
cpp_freertos
Definition:
condition_variable.hpp:55
Firmware
FFBoard
Src
cmem_pool.cpp
Generated by
1.9.2