Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 "cmsis_os2.h" 00029 #include "mbed_rtos1_types.h" 00030 #include "mbed_rtos_storage.h" 00031 #include "platform/NonCopyable.h" 00032 00033 namespace rtos { 00034 /** \addtogroup rtos */ 00035 /** @{*/ 00036 /** 00037 * \defgroup rtos_MemoryPool MemoryPool class 00038 * @{ 00039 */ 00040 00041 /** Define and manage fixed-size memory pools of objects of a given type. 00042 @tparam T data type of a single object (element). 00043 @tparam queue_sz maximum number of objects (elements) in the memory pool. 00044 00045 @note 00046 Memory considerations: The memory pool data store and control structures will be created on current thread's stack, 00047 both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). 00048 */ 00049 template<typename T, uint32_t pool_sz> 00050 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > { 00051 MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0."); 00052 public: 00053 /** Create and Initialize a memory pool. 00054 * 00055 * @note You cannot call this function from ISR context. 00056 */ 00057 MemoryPool() { 00058 memset(_pool_mem, 0, sizeof(_pool_mem)); 00059 memset(&_obj_mem, 0, sizeof(_obj_mem)); 00060 osMemoryPoolAttr_t attr = { 0 }; 00061 attr.mp_mem = _pool_mem; 00062 attr.mp_size = sizeof(_pool_mem); 00063 attr.cb_mem = &_obj_mem; 00064 attr.cb_size = sizeof(_obj_mem); 00065 _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr); 00066 MBED_ASSERT(_id); 00067 } 00068 00069 /** Destroy a memory pool 00070 * 00071 * @note You cannot call this function from ISR context. 00072 */ 00073 ~MemoryPool() { 00074 osMemoryPoolDelete(_id); 00075 } 00076 00077 /** Allocate a memory block of type T from a memory pool. 00078 @return address of the allocated memory block or NULL in case of no memory available. 00079 00080 @note You may call this function from ISR context. 00081 */ 00082 T* alloc(void) { 00083 return (T*)osMemoryPoolAlloc(_id, 0); 00084 } 00085 00086 /** Allocate a memory block of type T from a memory pool and set memory block to zero. 00087 @return address of the allocated memory block or NULL in case of no memory available. 00088 00089 @note You may call this function from ISR context. 00090 */ 00091 T* calloc(void) { 00092 T *item = (T*)osMemoryPoolAlloc(_id, 0); 00093 if (item != NULL) { 00094 memset(item, 0, sizeof(T)); 00095 } 00096 return item; 00097 } 00098 00099 /** Free a memory block. 00100 @param block address of the allocated memory block to be freed. 00101 @return osOK on successful deallocation, osErrorParameter if given memory block id 00102 is NULL or invalid, or osErrorResource if given memory block is in an 00103 invalid memory pool state. 00104 00105 @note You may call this function from ISR context. 00106 */ 00107 osStatus free(T *block) { 00108 return osMemoryPoolFree(_id, (void*)block); 00109 } 00110 00111 private: 00112 osMemoryPoolId_t _id; 00113 /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */ 00114 char _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz]; 00115 mbed_rtos_storage_mem_pool_t _obj_mem; 00116 }; 00117 /** @}*/ 00118 /** @}*/ 00119 00120 } 00121 #endif 00122 00123
Generated on Tue Jul 12 2022 13:30:56 by
