Mistake on this page?
Report an issue in GitHub or email us
MemoryPool.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 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 "cmsis_os2.h"
29 #include "mbed_rtos1_types.h"
30 #include "mbed_rtos_storage.h"
31 #include "platform/NonCopyable.h"
32 
33 namespace rtos {
34 /** \addtogroup rtos */
35 /** @{*/
36 /**
37  * \defgroup rtos_MemoryPool MemoryPool class
38  * @{
39  */
40 
41 /** Define and manage fixed-size memory pools of objects of a given type.
42  @tparam T data type of a single object (element).
43  @tparam queue_sz maximum number of objects (elements) in the memory pool.
44 
45  @note
46  Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
47  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
48 */
49 template<typename T, uint32_t pool_sz>
50 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
51  MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
52 public:
53  /** Create and Initialize a memory pool.
54  *
55  * @note You cannot call this function from ISR context.
56  */
58  {
59  memset(_pool_mem, 0, sizeof(_pool_mem));
60  osMemoryPoolAttr_t attr = { 0 };
61  attr.mp_mem = _pool_mem;
62  attr.mp_size = sizeof(_pool_mem);
63  attr.cb_mem = &_obj_mem;
64  attr.cb_size = sizeof(_obj_mem);
65  _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr);
66  MBED_ASSERT(_id);
67  }
68 
69  /** Destroy a memory pool
70  *
71  * @note You cannot call this function from ISR context.
72  */
74  {
75  osMemoryPoolDelete(_id);
76  }
77 
78  /** Allocate a memory block from a memory pool, without blocking.
79  @return address of the allocated memory block or NULL in case of no memory available.
80 
81  @note You may call this function from ISR context.
82  */
83  T *alloc(void)
84  {
85  return (T *)osMemoryPoolAlloc(_id, 0);
86  }
87 
88  /** Allocate a memory block from a memory pool, optionally blocking.
89  @param millisec timeout value (osWaitForever to wait forever)
90  @return address of the allocated memory block or NULL in case of no memory available.
91 
92  @note You may call this function from ISR context if the millisec parameter is set to 0.
93  */
94  T *alloc_for(uint32_t millisec)
95  {
96  return (T *)osMemoryPoolAlloc(_id, millisec);
97  }
98 
99  /** Allocate a memory block from a memory pool, blocking.
100  @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
101  @return address of the allocated memory block or NULL in case of no memory available.
102 
103  @note You cannot call this function from ISR context.
104  @note the underlying RTOS may have a limit to the maximum wait time
105  due to internal 32-bit computations, but this is guaranteed to work if the
106  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
107  the wait will time out earlier than specified.
108  */
109  T *alloc_until(uint64_t millisec)
110  {
111  uint64_t now = Kernel::get_ms_count();
112  uint32_t delay;
113  if (now >= millisec) {
114  delay = 0;
115  } else if (millisec - now >= osWaitForever) {
116  delay = osWaitForever - 1;
117  } else {
118  delay = millisec - now;
119  }
120  return alloc_for(delay);
121  }
122 
123  /** Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
124  @return address of the allocated memory block or NULL in case of no memory available.
125 
126  @note You may call this function from ISR context.
127  */
128  T *calloc(void)
129  {
130  T *item = alloc();
131  if (item != NULL) {
132  memset(item, 0, sizeof(T));
133  }
134  return item;
135  }
136 
137  /** Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
138  @param millisec timeout value (osWaitForever to wait forever)
139  @return address of the allocated memory block or NULL in case of no memory available.
140 
141  @note You may call this function from ISR context if the millisec parameter is set to 0.
142  */
143  T *calloc_for(uint32_t millisec)
144  {
145  T *item = alloc_for(millisec);
146  if (item != NULL) {
147  memset(item, 0, sizeof(T));
148  }
149  return item;
150  }
151 
152  /** Allocate a memory block from a memory pool, blocking, and set memory block to zero.
153  @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
154  @return address of the allocated memory block or NULL in case of no memory available.
155 
156  @note You cannot call this function from ISR context.
157  @note the underlying RTOS may have a limit to the maximum wait time
158  due to internal 32-bit computations, but this is guaranteed to work if the
159  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
160  the wait will time out earlier than specified.
161  */
162  T *calloc_until(uint64_t millisec)
163  {
164  T *item = alloc_until(millisec);
165  if (item != NULL) {
166  memset(item, 0, sizeof(T));
167  }
168  return item;
169  }
170 
171  /** Free a memory block.
172  @param block address of the allocated memory block to be freed.
173  @return osOK on successful deallocation, osErrorParameter if given memory block id
174  is NULL or invalid, or osErrorResource if given memory block is in an
175  invalid memory pool state.
176 
177  @note You may call this function from ISR context.
178  */
179  osStatus free(T *block)
180  {
181  return osMemoryPoolFree(_id, block);
182  }
183 
184 private:
185  osMemoryPoolId_t _id;
186  char _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz, sizeof(T))];
187  mbed_rtos_storage_mem_pool_t _obj_mem;
188 };
189 /** @}*/
190 /** @}*/
191 
192 }
193 #endif
~MemoryPool()
Destroy a memory pool.
Definition: MemoryPool.h:73
Define and manage fixed-size memory pools of objects of a given type.
Definition: MemoryPool.h:50
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
Definition: MemoryPool.h:162
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
Definition: MemoryPool.h:109
T * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
Definition: MemoryPool.h:94
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
osStatus free(T *block)
Free a memory block.
Definition: MemoryPool.h:179
#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:128
uint64_t get_ms_count()
Read the current RTOS kernel millisecond tick count.
MemoryPool()
Create and Initialize a memory pool.
Definition: MemoryPool.h:57
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:143
T * alloc(void)
Allocate a memory block from a memory pool, without blocking.
Definition: MemoryPool.h:83
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.