]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/cmsis_os2_MemPool.txt
RTX5: Moved SVC/PendSV handler priority setup from osKernelInitialize to osKernelStart
[cmsis] / CMSIS / DoxyGen / RTOS2 / src / cmsis_os2_MemPool.txt
1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 //  ==== Memory Pool Management ====
3 /** 
4 \addtogroup CMSIS_RTOS_PoolMgmt Memory Pool
5 \ingroup CMSIS_RTOS
6 \brief Manage thread-safe fixed-size blocks of dynamic memory.
7 \details
8 \b Memory \b Pools are fixed-size blocks of memory that are thread-safe. They operate much faster than the dynamically
9 allocated heap and do not suffer from fragmentation. Being thread-safe, they can be accessed from threads and ISRs alike.
10
11 A Memory Pool can be seen as a linked list of available (unused) memory blocks of fixed and equal size. Allocating memory
12 from a pool (using \ref osMemoryPoolAlloc) simply unchains a block from the list and hands over control to the user. Freeing
13 memory to the pool (using \ref osMemoryPoolFree) simply rechains the block into the list.
14
15 \image html "mempool.png" "CMSIS-RTOS Memory Pools"
16
17 \note One must not write to freed block. It is up to the implementation to reuse the memory of unused blocks for internal
18 control data, i.e. linked list pointers.
19
20 \b Shared \b memory is one of the basic models to exchange information between threads. Using memory pools for exchanging
21 data, you can share more complex objects between threads if compared to a \ref CMSIS_RTOS_Message. Memory pool management
22 functions are used to define and manage such fixed-sized memory pools.
23
24 \note The functions \ref osMemoryPoolAlloc, \ref osMemoryPoolFree, \ref osMemoryPoolGetCapacity,
25 \ref osMemoryPoolGetBlockSize, \ref osMemoryPoolGetCount, \ref osMemoryPoolGetSpace can be called from
26 \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
27 \note Refer to \ref memPoolConfig for RTX5 configuration options.
28
29 @{
30 */
31
32 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
33 /** 
34 \typedef osMemoryPoolId_t
35 \details 
36 Returned by:
37 - \ref osMemoryPoolNew
38 */
39
40 /** 
41 \struct osMemoryPoolAttr_t
42 \details 
43 */
44
45 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
46 /** 
47 \fn osMemoryPoolId_t osMemoryPoolNew (uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr)
48 \details
49 The function \b osMemoryPoolNew creates and initializes a memory pool object and returns the pointer to the memory pool
50 object identifier or \token{NULL} in case of an error. It can be safely called before the RTOS is
51 started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
52
53 The total amount of memory needed is at least <code>block_count * block_size</code>. Memory from the pool can only be
54 allocated/freed in fixed portions of \c block_size.
55
56 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
57
58 \b Code \b Example
59 \code
60 #include "cmsis_os2.h"                                        // CMSIS RTOS header file
61  
62 /*----------------------------------------------------------------------------
63  *      Memory Pool creation & usage
64  *---------------------------------------------------------------------------*/
65  
66 #define MEMPOOL_OBJECTS      16                               // number of Memory Pool Objects
67  
68 typedef struct {                                              // object data type
69   uint8_t Buf[32];
70   uint8_t Idx;
71 } MEM_BLOCK_t;
72  
73 void Thread_MemPool (void *argument);                         // thread function
74 osThreadId_t tid_Thread_MemPool;                              // thread id
75  
76 osMemoryPoolId_t mpid_MemPool;                                // memory pool id
77  
78 int Init_MemPool (void)
79 {
80  
81   mpid_MemPool = osMemoryPoolNew(MEMPOOL_OBJECTS,sizeof(MEM_BLOCK_t), NULL);
82   if (mpid_MemPool == NULL) {
83     ; // MemPool object not created, handle failure
84   }
85  
86   tid_Thread_MemPool = osThreadNew (Thread_MemPool,NULL , NULL);
87   if (tid_Thread_MemPool == NULL) {
88     return(-1);
89   }
90  
91   return(0);
92 }
93  
94 void Thread_MemPool (void *argument)
95 {
96   osStatus_t     status;
97   MEM_BLOCK_t *pMem = 0;
98  
99   while (1) {
100     ; // Insert thread code here...
101  
102     pMem = (MEM_BLOCK_t *)osMemoryPoolAlloc (mpid_MemPool, NULL);    // get Mem Block
103     if (pMem) {                                                      // Mem Block was available
104       pMem->Buf[0] = 0x55;                                           // do some work...
105       pMem->Idx    = 0;
106  
107       status = osMemoryPoolFree (mpid_MemPool, pMem);                // free mem block
108       switch (status)  {
109       case osOK:
110         break;
111       case osErrorParameter:
112         break;
113       case osErrorNoMemory:
114         break;
115       default:
116         break;
117       }
118     }
119  
120     osThreadYield ();                                                // suspend thread
121   }
122 }
123 \endcode
124 */
125
126 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
127 /**
128 \fn const char *osMemoryPoolGetName (osMemoryPoolId_t mp_id)
129 \details
130 The function \b osMemoryPoolGetName returns the pointer to the name string of the memory pool identified by parameter \a
131 mp_id or \token{NULL} in case of an error.
132
133 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
134 */
135
136 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
137 /** 
138 \fn void *osMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout)
139 \details
140 The blocking function \b osMemoryPoolAlloc allocates the memory pool parameter \a mp_id and returns a pointer to the address
141 of the allocated memory or \token{0} in case of an error.
142
143 The parameter \a timeout specifies how long the system waits to allocate the memory. While the system waits, the thread
144 that is calling this function is put into the \ref ThreadStates "BLOCKED" state. The thread will become \ref ThreadStates "READY"
145 as soon as at least one block of memory gets available.
146
147 The parameter \ref CMSIS_RTOS_TimeOutValue "timeout" can have the following values:
148  - when \a timeout is \token{0}, the function returns instantly (i.e. try semantics).
149  - when \a timeout is set to \b osWaitForever the function will wait for an infinite time until the memory is allocated (i.e. wait semantics).
150  - all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).
151
152 The result is the pointer to the memory block allocated, or NULL if no memory is available.
153
154 \note It is in the responsibility of the user to respect the block size, i.e. not access memory beyond the blocks limit.
155  
156 \note May be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines" if the parameter \a timeout is set to
157 \token{0}.
158
159 \b Code \b Example
160
161 Refer to \ref osMemoryPoolNew.
162 */
163
164 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
165 /** 
166 \fn osStatus_t osMemoryPoolFree (osMemoryPoolId_t mp_id, void *block)
167 \details
168 The function \b osMemoryPoolFree frees the memory pool block specified by the parameter \a block in the memory pool object
169 specified by the parameter \a mp_id. The memory block is put back to the list of available blocks.
170
171 If another thread is waiting for memory to become available the thread is put to \ref ThreadStates "READY" state.
172
173 Possible \ref osStatus_t return values:
174  - \em osOK: the memory has been freed.
175  - \em osErrorParameter: parameter \a mp_id is \token{NULL} or invalid, \a block points to invalid memory.
176  - \em osErrorResource: the memory pool specified by parameter \a mp_id is in an invalid memory pool state.
177
178 \note \b osMemoryPoolFree may perform certain checks on the \a block pointer given. But using \b osMemoryPoolFree 
179 with a pointer other than one received from \ref osMemoryPoolAlloc has \b UNPREDICTED behaviour.
180
181 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
182
183 \b Code \b Example
184
185 Refer to \ref osMemoryPoolNew.
186 */
187
188 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
189 /** 
190 \fn uint32_t osMemoryPoolGetCapacity (osMemoryPoolId_t mp_id)
191 \details
192 The function \b osMemoryPoolGetCapacity returns the maximum number of memory blocks in the memory pool object specified by
193 parameter \a mp_id or \token{0} in case of an error.
194
195 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
196 */
197
198 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
199 /** 
200 \fn uint32_t osMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id)
201 \details
202 The function \b osMemoryPoolGetBlockSize returns the memory block size in bytes in the memory pool object specified by
203 parameter \a mp_id or \token{0} in case of an error.
204
205 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
206 */
207
208 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
209 /** 
210 \fn uint32_t osMemoryPoolGetCount (osMemoryPoolId_t mp_id)
211 \details
212 The function \b osMemoryPoolGetCount returns the number of memory blocks used in the memory pool object specified by
213 parameter \a mp_id or \token{0} in case of an error.
214
215 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
216 */
217
218 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
219 /** 
220 \fn uint32_t osMemoryPoolGetSpace (osMemoryPoolId_t mp_id)
221 \details
222 The function \b osMemoryPoolGetSpace returns the number of memory blocks available in the memory pool object specified by
223 parameter \a mp_id or \token{0} in case of an error.
224
225 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
226 */
227
228 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
229 /** 
230 \fn osStatus_t osMemoryPoolDelete (osMemoryPoolId_t mp_id)
231 \details
232 The function \b osMemoryPoolDelete deletes a memory pool object specified by parameter \a mp_id. It releases internal
233 memory obtained for memory pool handling. After this call, the \a mp_id is no longer valid and cannot be used. The
234 memory pool may be created again using the function \ref osMemoryPoolNew.
235
236 Possible \ref osStatus_t return values:
237  - \em osOK: the memory pool object has been deleted.
238  - \em osErrorParameter: parameter \a mp_id is \token{NULL} or invalid.
239  - \em osErrorResource: the memory pool specified by parameter \a mp_id is in an invalid memory pool state.
240  - \em osErrorISR: \b osMemoryPoolDelete cannot be called from interrupt service routines.
241
242 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
243 */
244 /// @}
245
246 // these struct members must stay outside the group to avoid double entries in documentation
247 /**
248 \var osMemoryPoolAttr_t::attr_bits
249 \details
250 Reserved for future use (set to '0').\n
251 Default: \token{0}.
252
253 \var osMemoryPoolAttr_t::cb_mem
254 \details
255 Pointer to a memory location for the memory pool control block object. This can optionally be used for custom memory management systems.\n
256 Default: \token{NULL} (uses kernel memory management).
257
258 \var osMemoryPoolAttr_t::cb_size
259 \details
260 The size of the memory block passed with \ref cb_mem. Must be the size of a memory pool control block object or larger.
261
262 \var osMemoryPoolAttr_t::name
263 \details
264 Pointer to a string with a human readable name of the memory pool object.\n
265 Default: \token{NULL}.
266
267 \var osMemoryPoolAttr_t::mp_mem
268 \details
269 Pointer to a memory location for the data of the memory pool object.\n
270 Default: \token{NULL}.
271
272 \var osMemoryPoolAttr_t::mp_size
273 \details
274 The size of the memory passed with \ref mp_mem.
275 */