Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
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 MemoryPool() { 00055 memset(_pool_mem, 0, sizeof(_pool_mem)); 00056 memset(&_obj_mem, 0, sizeof(_obj_mem)); 00057 osMemoryPoolAttr_t attr = { 0 }; 00058 attr.mp_mem = _pool_mem; 00059 attr.mp_size = sizeof(_pool_mem); 00060 attr.cb_mem = &_obj_mem; 00061 attr.cb_size = sizeof(_obj_mem); 00062 _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr); 00063 MBED_ASSERT(_id); 00064 } 00065 00066 /** Destroy a memory pool */ 00067 ~MemoryPool() { 00068 osMemoryPoolDelete(_id); 00069 } 00070 00071 /** Allocate a memory block of type T from a memory pool. 00072 @return address of the allocated memory block or NULL in case of no memory available. 00073 */ 00074 T* alloc(void) { 00075 return (T*)osMemoryPoolAlloc(_id, 0); 00076 } 00077 00078 /** Allocate a memory block of type T from a memory pool and set memory block to zero. 00079 @return address of the allocated memory block or NULL in case of no memory available. 00080 */ 00081 T* calloc(void) { 00082 T *item = (T*)osMemoryPoolAlloc(_id, 0); 00083 if (item != NULL) { 00084 memset(item, 0, sizeof(T)); 00085 } 00086 return item; 00087 } 00088 00089 /** Free a memory block. 00090 @param block address of the allocated memory block to be freed. 00091 @return osOK on successful deallocation, osErrorParameter if given memory block id 00092 is NULL or invalid, or osErrorResource if given memory block is in an 00093 invalid memory pool state. 00094 00095 */ 00096 osStatus free(T *block) { 00097 return osMemoryPoolFree(_id, (void*)block); 00098 } 00099 00100 private: 00101 osMemoryPoolId_t _id; 00102 /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */ 00103 char _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz]; 00104 mbed_rtos_storage_mem_pool_t _obj_mem; 00105 }; 00106 /** @}*/ 00107 /** @}*/ 00108 00109 } 00110 #endif 00111 00112
Generated on Sun Jul 17 2022 08:25:28 by 1.7.2