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 of type T from a memory pool.
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 of type T from a memory pool and set memory block to zero.
89  @return address of the allocated memory block or NULL in case of no memory available.
90 
91  @note You may call this function from ISR context.
92  */
93  T *calloc(void)
94  {
95  T *item = (T *)osMemoryPoolAlloc(_id, 0);
96  if (item != NULL) {
97  memset(item, 0, sizeof(T));
98  }
99  return item;
100  }
101 
102  /** Free a memory block.
103  @param block address of the allocated memory block to be freed.
104  @return osOK on successful deallocation, osErrorParameter if given memory block id
105  is NULL or invalid, or osErrorResource if given memory block is in an
106  invalid memory pool state.
107 
108  @note You may call this function from ISR context.
109  */
110  osStatus free(T *block)
111  {
112  return osMemoryPoolFree(_id, (void *)block);
113  }
114 
115 private:
116  osMemoryPoolId_t _id;
117  /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */
118  char _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz];
119  mbed_rtos_storage_mem_pool_t _obj_mem;
120 };
121 /** @}*/
122 /** @}*/
123 
124 }
125 #endif
126 
127 
~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
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:110
#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 of type T from a memory pool and set memory block to zero.
Definition: MemoryPool.h:93
MemoryPool()
Create and Initialize a memory pool.
Definition: MemoryPool.h:57
T * alloc(void)
Allocate a memory block of type T from a memory pool.
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.