Mistake on this page?
Report an issue in GitHub or email us
MemoryPool.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef MEMORYPOOL_H
23 #define MEMORYPOOL_H
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "rtos/mbed_rtos_types.h"
29 #include "rtos/mbed_rtos1_types.h"
30 #include "rtos/mbed_rtos_storage.h"
31 #include "platform/NonCopyable.h"
32 #include "platform/mbed_assert.h"
33 #include "Kernel.h"
34 
35 
36 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
37 namespace rtos {
38 /** \addtogroup rtos-public-api */
39 /** @{*/
40 
41 /**
42  * \defgroup rtos_MemoryPool MemoryPool class
43  * @{
44  */
45 
46 /** Define and manage fixed-size memory pools of objects of a given type.
47  @tparam T data type of a single object (element).
48  @tparam queue_sz maximum number of objects (elements) in the memory pool.
49 
50  @note
51  Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
52  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
53 */
54 template<typename T, uint32_t pool_sz>
55 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
56  MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
57 public:
58  /** Create and Initialize a memory pool.
59  *
60  * @note You cannot call this function from ISR context.
61  */
63  {
64  memset(_pool_mem, 0, sizeof(_pool_mem));
65  osMemoryPoolAttr_t attr = { 0 };
66  attr.mp_mem = _pool_mem;
67  attr.mp_size = sizeof(_pool_mem);
68  attr.cb_mem = &_obj_mem;
69  attr.cb_size = sizeof(_obj_mem);
70  _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr);
71  MBED_ASSERT(_id);
72  }
73 
74  /** Destroy a memory pool
75  *
76  * @note You cannot call this function from ISR context.
77  */
79  {
80  osMemoryPoolDelete(_id);
81  }
82 
83  /** Allocate a memory block from a memory pool, without blocking.
84  @return address of the allocated memory block or nullptr in case of no memory available.
85 
86  @note You may call this function from ISR context.
87  */
88  T *alloc(void)
89  {
90  return (T *)osMemoryPoolAlloc(_id, 0);
91  }
92 
93  /** Allocate a memory block from a memory pool, optionally blocking.
94  @param millisec timeout value (osWaitForever to wait forever)
95  @return address of the allocated memory block or nullptr in case of no memory available.
96 
97  @note You may call this function from ISR context if the millisec parameter is set to 0.
98  */
99  T *alloc_for(uint32_t millisec)
100  {
101  return (T *)osMemoryPoolAlloc(_id, millisec);
102  }
103 
104  /** Allocate a memory block from a memory pool, blocking.
105  @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
106  @return address of the allocated memory block or nullptr in case of no memory available.
107 
108  @note You cannot call this function from ISR context.
109  @note the underlying RTOS may have a limit to the maximum wait time
110  due to internal 32-bit computations, but this is guaranteed to work if the
111  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
112  the wait will time out earlier than specified.
113  */
114  T *alloc_until(uint64_t millisec)
115  {
116  uint64_t now = Kernel::get_ms_count();
117  uint32_t delay;
118  if (now >= millisec) {
119  delay = 0;
120  } else if (millisec - now >= osWaitForever) {
121  delay = osWaitForever - 1;
122  } else {
123  delay = millisec - now;
124  }
125  return alloc_for(delay);
126  }
127 
128  /** Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
129  @return address of the allocated memory block or nullptr in case of no memory available.
130 
131  @note You may call this function from ISR context.
132  */
133  T *calloc(void)
134  {
135  T *item = alloc();
136  if (item != nullptr) {
137  memset(item, 0, sizeof(T));
138  }
139  return item;
140  }
141 
142  /** Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
143  @param millisec timeout value (osWaitForever to wait forever)
144  @return address of the allocated memory block or nullptr in case of no memory available.
145 
146  @note You may call this function from ISR context if the millisec parameter is set to 0.
147  */
148  T *calloc_for(uint32_t millisec)
149  {
150  T *item = alloc_for(millisec);
151  if (item != nullptr) {
152  memset(item, 0, sizeof(T));
153  }
154  return item;
155  }
156 
157  /** Allocate a memory block from a memory pool, blocking, and set memory block to zero.
158  @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
159  @return address of the allocated memory block or nullptr in case of no memory available.
160 
161  @note You cannot call this function from ISR context.
162  @note the underlying RTOS may have a limit to the maximum wait time
163  due to internal 32-bit computations, but this is guaranteed to work if the
164  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
165  the wait will time out earlier than specified.
166  */
167  T *calloc_until(uint64_t millisec)
168  {
169  T *item = alloc_until(millisec);
170  if (item != nullptr) {
171  memset(item, 0, sizeof(T));
172  }
173  return item;
174  }
175 
176  /** Free a memory block.
177  @param block address of the allocated memory block to be freed.
178  @return osOK on successful deallocation, osErrorParameter if given memory block id
179  is nullptr or invalid, or osErrorResource if given memory block is in an
180  invalid memory pool state.
181 
182  @note You may call this function from ISR context.
183  */
184  osStatus free(T *block)
185  {
186  return osMemoryPoolFree(_id, block);
187  }
188 
189 private:
190  osMemoryPoolId_t _id;
191  char _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz, sizeof(T))];
193 };
194 /** @}*/
195 /** @}*/
196 }
197 #endif
198 #endif
~MemoryPool()
Destroy a memory pool.
Definition: MemoryPool.h:78
Memory Pool Control Block.
Definition: rtx_os.h:223
Define and manage fixed-size memory pools of objects of a given type.
Definition: MemoryPool.h:55
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
Definition: MemoryPool.h:167
Attributes structure for memory pool.
Definition: cmsis_os2.h:285
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
Definition: MemoryPool.h:114
T * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
Definition: MemoryPool.h:99
void * cb_mem
memory for control block
Definition: cmsis_os2.h:288
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
void * mp_mem
memory for data storage
Definition: cmsis_os2.h:290
uint32_t cb_size
size of provided memory for control block
Definition: cmsis_os2.h:289
osStatus free(T *block)
Free a memory block.
Definition: MemoryPool.h:184
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:65
T * calloc(void)
Allocate a memory block from a memory pool, without blocking, and set memory block to zero...
Definition: MemoryPool.h:133
uint64_t get_ms_count()
Read the current RTOS kernel millisecond tick count.
Definition: TaskBase.h:25
MemoryPool()
Create and Initialize a memory pool.
Definition: MemoryPool.h:62
T * calloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero...
Definition: MemoryPool.h:148
T * alloc(void)
Allocate a memory block from a memory pool, without blocking.
Definition: MemoryPool.h:88
uint32_t mp_size
size of provided memory for data storage
Definition: cmsis_os2.h:291
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.