Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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-2019 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 #include "platform/mbed_assert.h"
00033 #include "Kernel.h"
00034 
00035 
00036 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
00037 namespace rtos {
00038 /** \addtogroup rtos-public-api */
00039 /** @{*/
00040 
00041 /**
00042  * \defgroup rtos_MemoryPool MemoryPool class
00043  * @{
00044  */
00045 
00046 /** Define and manage fixed-size memory pools of objects of a given type.
00047   @tparam  T         data type of a single object (element).
00048   @tparam  queue_sz  maximum number of objects (elements) in the memory pool.
00049 
00050  @note
00051  Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
00052  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00053 */
00054 template<typename T, uint32_t pool_sz>
00055 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
00056     MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
00057 public:
00058     /** Create and Initialize a memory pool.
00059      *
00060      * @note You cannot call this function from ISR context.
00061     */
00062     MemoryPool()
00063     {
00064         memset(_pool_mem, 0, sizeof(_pool_mem));
00065         osMemoryPoolAttr_t attr = { 0 };
00066         attr.mp_mem = _pool_mem;
00067         attr.mp_size = sizeof(_pool_mem);
00068         attr.cb_mem = &_obj_mem;
00069         attr.cb_size = sizeof(_obj_mem);
00070         _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr);
00071         MBED_ASSERT(_id);
00072     }
00073 
00074     /** Destroy a memory pool
00075      *
00076      * @note You cannot call this function from ISR context.
00077     */
00078     ~MemoryPool()
00079     {
00080         osMemoryPoolDelete(_id);
00081     }
00082 
00083     /** Allocate a memory block from a memory pool, without blocking.
00084       @return  address of the allocated memory block or nullptr in case of no memory available.
00085 
00086       @note You may call this function from ISR context.
00087     */
00088     T *alloc(void)
00089     {
00090         return (T *)osMemoryPoolAlloc(_id, 0);
00091     }
00092 
00093     /** Allocate a memory block from a memory pool, optionally blocking.
00094       @param   millisec  timeout value (osWaitForever to wait forever)
00095       @return  address of the allocated memory block or nullptr in case of no memory available.
00096 
00097       @note You may call this function from ISR context if the millisec parameter is set to 0.
00098     */
00099     T *alloc_for(uint32_t millisec)
00100     {
00101         return (T *)osMemoryPoolAlloc(_id, millisec);
00102     }
00103 
00104     /** Allocate a memory block from a memory pool, blocking.
00105       @param   millisec absolute timeout time, referenced to Kernel::get_ms_count().
00106       @return  address of the allocated memory block or nullptr in case of no memory available.
00107 
00108       @note You cannot call this function from ISR context.
00109       @note the underlying RTOS may have a limit to the maximum wait time
00110         due to internal 32-bit computations, but this is guaranteed to work if the
00111         wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00112         the wait will time out earlier than specified.
00113     */
00114     T *alloc_until(uint64_t millisec)
00115     {
00116         uint64_t now = Kernel::get_ms_count();
00117         uint32_t delay;
00118         if (now >= millisec) {
00119             delay = 0;
00120         } else if (millisec - now >= osWaitForever) {
00121             delay = osWaitForever - 1;
00122         } else {
00123             delay = millisec - now;
00124         }
00125         return alloc_for(delay);
00126     }
00127 
00128     /** Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
00129       @return  address of the allocated memory block or nullptr in case of no memory available.
00130 
00131       @note You may call this function from ISR context.
00132     */
00133     T *calloc(void)
00134     {
00135         T *item = alloc();
00136         if (item != nullptr) {
00137             memset(item, 0, sizeof(T));
00138         }
00139         return item;
00140     }
00141 
00142     /** Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
00143       @param   millisec  timeout value (osWaitForever to wait forever)
00144       @return  address of the allocated memory block or nullptr in case of no memory available.
00145 
00146       @note You may call this function from ISR context if the millisec parameter is set to 0.
00147     */
00148     T *calloc_for(uint32_t millisec)
00149     {
00150         T *item = alloc_for(millisec);
00151         if (item != nullptr) {
00152             memset(item, 0, sizeof(T));
00153         }
00154         return item;
00155     }
00156 
00157     /** Allocate a memory block from a memory pool, blocking, and set memory block to zero.
00158       @param   millisec absolute timeout time, referenced to Kernel::get_ms_count().
00159       @return  address of the allocated memory block or nullptr in case of no memory available.
00160 
00161       @note You cannot call this function from ISR context.
00162       @note the underlying RTOS may have a limit to the maximum wait time
00163         due to internal 32-bit computations, but this is guaranteed to work if the
00164         wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00165         the wait will time out earlier than specified.
00166     */
00167     T *calloc_until(uint64_t millisec)
00168     {
00169         T *item = alloc_until(millisec);
00170         if (item != nullptr) {
00171             memset(item, 0, sizeof(T));
00172         }
00173         return item;
00174     }
00175 
00176     /** Free a memory block.
00177       @param   block  address of the allocated memory block to be freed.
00178       @return         osOK on successful deallocation, osErrorParameter if given memory block id
00179                       is nullptr or invalid, or osErrorResource if given memory block is in an
00180                       invalid memory pool state.
00181 
00182       @note You may call this function from ISR context.
00183     */
00184     osStatus free(T *block)
00185     {
00186         return osMemoryPoolFree(_id, block);
00187     }
00188 
00189 private:
00190     osMemoryPoolId_t             _id;
00191     char                         _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz, sizeof(T))];
00192     mbed_rtos_storage_mem_pool_t _obj_mem;
00193 };
00194 /** @}*/
00195 /** @}*/
00196 }
00197 #endif
00198 #endif