Ok

Dependencies:   mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemoryPool.h Source File

MemoryPool.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MEMORYPOOL_H
00023 #define MEMORYPOOL_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "rtos/mbed_rtos_types.h"
00029 #include "rtos/mbed_rtos1_types.h"
00030 #include "rtos/mbed_rtos_storage.h"
00031 #include "platform/NonCopyable.h"
00032 
00033 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
00034 namespace rtos {
00035 /** \addtogroup rtos */
00036 /** @{*/
00037 /**
00038  * \defgroup rtos_MemoryPool MemoryPool class
00039  * @{
00040  */
00041 
00042 /** Define and manage fixed-size memory pools of objects of a given type.
00043   @tparam  T         data type of a single object (element).
00044   @tparam  queue_sz  maximum number of objects (elements) in the memory pool.
00045 
00046  @note
00047  Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
00048  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00049 */
00050 template<typename T, uint32_t pool_sz>
00051 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
00052     MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
00053 public:
00054     /** Create and Initialize a memory pool.
00055      *
00056      * @note You cannot call this function from ISR context.
00057     */
00058     MemoryPool()
00059     {
00060         memset(_pool_mem, 0, sizeof(_pool_mem));
00061         osMemoryPoolAttr_t attr = { 0 };
00062         attr.mp_mem = _pool_mem;
00063         attr.mp_size = sizeof(_pool_mem);
00064         attr.cb_mem = &_obj_mem;
00065         attr.cb_size = sizeof(_obj_mem);
00066         _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr);
00067         MBED_ASSERT(_id);
00068     }
00069 
00070     /** Destroy a memory pool
00071      *
00072      * @note You cannot call this function from ISR context.
00073     */
00074     ~MemoryPool()
00075     {
00076         osMemoryPoolDelete(_id);
00077     }
00078 
00079     /** Allocate a memory block from a memory pool, without blocking.
00080       @return  address of the allocated memory block or nullptr in case of no memory available.
00081 
00082       @note You may call this function from ISR context.
00083     */
00084     T *alloc(void)
00085     {
00086         return (T *)osMemoryPoolAlloc(_id, 0);
00087     }
00088 
00089     /** Allocate a memory block from a memory pool, optionally blocking.
00090       @param   millisec  timeout value (osWaitForever to wait forever)
00091       @return  address of the allocated memory block or nullptr in case of no memory available.
00092 
00093       @note You may call this function from ISR context if the millisec parameter is set to 0.
00094     */
00095     T *alloc_for(uint32_t millisec)
00096     {
00097         return (T *)osMemoryPoolAlloc(_id, millisec);
00098     }
00099 
00100     /** Allocate a memory block from a memory pool, blocking.
00101       @param   millisec absolute timeout time, referenced to Kernel::get_ms_count().
00102       @return  address of the allocated memory block or nullptr in case of no memory available.
00103 
00104       @note You cannot call this function from ISR context.
00105       @note the underlying RTOS may have a limit to the maximum wait time
00106         due to internal 32-bit computations, but this is guaranteed to work if the
00107         wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00108         the wait will time out earlier than specified.
00109     */
00110     T *alloc_until(uint64_t millisec)
00111     {
00112         uint64_t now = Kernel::get_ms_count();
00113         uint32_t delay;
00114         if (now >= millisec) {
00115             delay = 0;
00116         } else if (millisec - now >= osWaitForever) {
00117             delay = osWaitForever - 1;
00118         } else {
00119             delay = millisec - now;
00120         }
00121         return alloc_for(delay);
00122     }
00123 
00124     /** Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
00125       @return  address of the allocated memory block or nullptr in case of no memory available.
00126 
00127       @note You may call this function from ISR context.
00128     */
00129     T *calloc(void)
00130     {
00131         T *item = alloc();
00132         if (item != nullptr) {
00133             memset(item, 0, sizeof(T));
00134         }
00135         return item;
00136     }
00137 
00138     /** Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
00139       @param   millisec  timeout value (osWaitForever to wait forever)
00140       @return  address of the allocated memory block or nullptr in case of no memory available.
00141 
00142       @note You may call this function from ISR context if the millisec parameter is set to 0.
00143     */
00144     T *calloc_for(uint32_t millisec)
00145     {
00146         T *item = alloc_for(millisec);
00147         if (item != nullptr) {
00148             memset(item, 0, sizeof(T));
00149         }
00150         return item;
00151     }
00152 
00153     /** Allocate a memory block from a memory pool, blocking, and set memory block to zero.
00154       @param   millisec absolute timeout time, referenced to Kernel::get_ms_count().
00155       @return  address of the allocated memory block or nullptr in case of no memory available.
00156 
00157       @note You cannot call this function from ISR context.
00158       @note the underlying RTOS may have a limit to the maximum wait time
00159         due to internal 32-bit computations, but this is guaranteed to work if the
00160         wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00161         the wait will time out earlier than specified.
00162     */
00163     T *calloc_until(uint64_t millisec)
00164     {
00165         T *item = alloc_until(millisec);
00166         if (item != nullptr) {
00167             memset(item, 0, sizeof(T));
00168         }
00169         return item;
00170     }
00171 
00172     /** Free a memory block.
00173       @param   block  address of the allocated memory block to be freed.
00174       @return         osOK on successful deallocation, osErrorParameter if given memory block id
00175                       is nullptr or invalid, or osErrorResource if given memory block is in an
00176                       invalid memory pool state.
00177 
00178       @note You may call this function from ISR context.
00179     */
00180     osStatus free(T *block)
00181     {
00182         return osMemoryPoolFree(_id, block);
00183     }
00184 
00185 private:
00186     osMemoryPoolId_t             _id;
00187     char                         _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz, sizeof(T))];
00188     mbed_rtos_storage_mem_pool_t _obj_mem;
00189 };
00190 /** @}*/
00191 /** @}*/
00192 
00193 }
00194 #endif
00195 #endif
00196