Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/rtos/MemoryPool.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2006-2012 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
marcozecchini | 0:9fca2b23d0ba | 5 | * of this software and associated documentation files (the "Software"), to deal |
marcozecchini | 0:9fca2b23d0ba | 6 | * in the Software without restriction, including without limitation the rights |
marcozecchini | 0:9fca2b23d0ba | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
marcozecchini | 0:9fca2b23d0ba | 8 | * copies of the Software, and to permit persons to whom the Software is |
marcozecchini | 0:9fca2b23d0ba | 9 | * furnished to do so, subject to the following conditions: |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * The above copyright notice and this permission notice shall be included in |
marcozecchini | 0:9fca2b23d0ba | 12 | * all copies or substantial portions of the Software. |
marcozecchini | 0:9fca2b23d0ba | 13 | * |
marcozecchini | 0:9fca2b23d0ba | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
marcozecchini | 0:9fca2b23d0ba | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
marcozecchini | 0:9fca2b23d0ba | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
marcozecchini | 0:9fca2b23d0ba | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
marcozecchini | 0:9fca2b23d0ba | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
marcozecchini | 0:9fca2b23d0ba | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
marcozecchini | 0:9fca2b23d0ba | 20 | * SOFTWARE. |
marcozecchini | 0:9fca2b23d0ba | 21 | */ |
marcozecchini | 0:9fca2b23d0ba | 22 | #ifndef MEMORYPOOL_H |
marcozecchini | 0:9fca2b23d0ba | 23 | #define MEMORYPOOL_H |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | #include <stdint.h> |
marcozecchini | 0:9fca2b23d0ba | 26 | #include <string.h> |
marcozecchini | 0:9fca2b23d0ba | 27 | |
marcozecchini | 0:9fca2b23d0ba | 28 | #include "cmsis_os2.h" |
marcozecchini | 0:9fca2b23d0ba | 29 | #include "mbed_rtos1_types.h" |
marcozecchini | 0:9fca2b23d0ba | 30 | #include "mbed_rtos_storage.h" |
marcozecchini | 0:9fca2b23d0ba | 31 | #include "platform/NonCopyable.h" |
marcozecchini | 0:9fca2b23d0ba | 32 | |
marcozecchini | 0:9fca2b23d0ba | 33 | namespace rtos { |
marcozecchini | 0:9fca2b23d0ba | 34 | /** \addtogroup rtos */ |
marcozecchini | 0:9fca2b23d0ba | 35 | /** @{*/ |
marcozecchini | 0:9fca2b23d0ba | 36 | /** |
marcozecchini | 0:9fca2b23d0ba | 37 | * \defgroup rtos_MemoryPool MemoryPool class |
marcozecchini | 0:9fca2b23d0ba | 38 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 39 | */ |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | /** Define and manage fixed-size memory pools of objects of a given type. |
marcozecchini | 0:9fca2b23d0ba | 42 | @tparam T data type of a single object (element). |
marcozecchini | 0:9fca2b23d0ba | 43 | @tparam queue_sz maximum number of objects (elements) in the memory pool. |
marcozecchini | 0:9fca2b23d0ba | 44 | |
marcozecchini | 0:9fca2b23d0ba | 45 | @note |
marcozecchini | 0:9fca2b23d0ba | 46 | Memory considerations: The memory pool data store and control structures will be created on current thread's stack, |
marcozecchini | 0:9fca2b23d0ba | 47 | both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
marcozecchini | 0:9fca2b23d0ba | 48 | */ |
marcozecchini | 0:9fca2b23d0ba | 49 | template<typename T, uint32_t pool_sz> |
marcozecchini | 0:9fca2b23d0ba | 50 | class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > { |
marcozecchini | 0:9fca2b23d0ba | 51 | MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0."); |
marcozecchini | 0:9fca2b23d0ba | 52 | public: |
marcozecchini | 0:9fca2b23d0ba | 53 | /** Create and Initialize a memory pool. */ |
marcozecchini | 0:9fca2b23d0ba | 54 | MemoryPool() { |
marcozecchini | 0:9fca2b23d0ba | 55 | memset(_pool_mem, 0, sizeof(_pool_mem)); |
marcozecchini | 0:9fca2b23d0ba | 56 | memset(&_obj_mem, 0, sizeof(_obj_mem)); |
marcozecchini | 0:9fca2b23d0ba | 57 | osMemoryPoolAttr_t attr = { 0 }; |
marcozecchini | 0:9fca2b23d0ba | 58 | attr.mp_mem = _pool_mem; |
marcozecchini | 0:9fca2b23d0ba | 59 | attr.mp_size = sizeof(_pool_mem); |
marcozecchini | 0:9fca2b23d0ba | 60 | attr.cb_mem = &_obj_mem; |
marcozecchini | 0:9fca2b23d0ba | 61 | attr.cb_size = sizeof(_obj_mem); |
marcozecchini | 0:9fca2b23d0ba | 62 | _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr); |
marcozecchini | 0:9fca2b23d0ba | 63 | MBED_ASSERT(_id); |
marcozecchini | 0:9fca2b23d0ba | 64 | } |
marcozecchini | 0:9fca2b23d0ba | 65 | |
marcozecchini | 0:9fca2b23d0ba | 66 | /** Destroy a memory pool */ |
marcozecchini | 0:9fca2b23d0ba | 67 | ~MemoryPool() { |
marcozecchini | 0:9fca2b23d0ba | 68 | osMemoryPoolDelete(_id); |
marcozecchini | 0:9fca2b23d0ba | 69 | } |
marcozecchini | 0:9fca2b23d0ba | 70 | |
marcozecchini | 0:9fca2b23d0ba | 71 | /** Allocate a memory block of type T from a memory pool. |
marcozecchini | 0:9fca2b23d0ba | 72 | @return address of the allocated memory block or NULL in case of no memory available. |
marcozecchini | 0:9fca2b23d0ba | 73 | */ |
marcozecchini | 0:9fca2b23d0ba | 74 | T* alloc(void) { |
marcozecchini | 0:9fca2b23d0ba | 75 | return (T*)osMemoryPoolAlloc(_id, 0); |
marcozecchini | 0:9fca2b23d0ba | 76 | } |
marcozecchini | 0:9fca2b23d0ba | 77 | |
marcozecchini | 0:9fca2b23d0ba | 78 | /** Allocate a memory block of type T from a memory pool and set memory block to zero. |
marcozecchini | 0:9fca2b23d0ba | 79 | @return address of the allocated memory block or NULL in case of no memory available. |
marcozecchini | 0:9fca2b23d0ba | 80 | */ |
marcozecchini | 0:9fca2b23d0ba | 81 | T* calloc(void) { |
marcozecchini | 0:9fca2b23d0ba | 82 | T *item = (T*)osMemoryPoolAlloc(_id, 0); |
marcozecchini | 0:9fca2b23d0ba | 83 | if (item != NULL) { |
marcozecchini | 0:9fca2b23d0ba | 84 | memset(item, 0, sizeof(T)); |
marcozecchini | 0:9fca2b23d0ba | 85 | } |
marcozecchini | 0:9fca2b23d0ba | 86 | return item; |
marcozecchini | 0:9fca2b23d0ba | 87 | } |
marcozecchini | 0:9fca2b23d0ba | 88 | |
marcozecchini | 0:9fca2b23d0ba | 89 | /** Free a memory block. |
marcozecchini | 0:9fca2b23d0ba | 90 | @param block address of the allocated memory block to be freed. |
marcozecchini | 0:9fca2b23d0ba | 91 | @return osOK on successful deallocation, osErrorParameter if given memory block id |
marcozecchini | 0:9fca2b23d0ba | 92 | is NULL or invalid, or osErrorResource if given memory block is in an |
marcozecchini | 0:9fca2b23d0ba | 93 | invalid memory pool state. |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | */ |
marcozecchini | 0:9fca2b23d0ba | 96 | osStatus free(T *block) { |
marcozecchini | 0:9fca2b23d0ba | 97 | return osMemoryPoolFree(_id, (void*)block); |
marcozecchini | 0:9fca2b23d0ba | 98 | } |
marcozecchini | 0:9fca2b23d0ba | 99 | |
marcozecchini | 0:9fca2b23d0ba | 100 | private: |
marcozecchini | 0:9fca2b23d0ba | 101 | osMemoryPoolId_t _id; |
marcozecchini | 0:9fca2b23d0ba | 102 | /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */ |
marcozecchini | 0:9fca2b23d0ba | 103 | char _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz]; |
marcozecchini | 0:9fca2b23d0ba | 104 | mbed_rtos_storage_mem_pool_t _obj_mem; |
marcozecchini | 0:9fca2b23d0ba | 105 | }; |
marcozecchini | 0:9fca2b23d0ba | 106 | /** @}*/ |
marcozecchini | 0:9fca2b23d0ba | 107 | /** @}*/ |
marcozecchini | 0:9fca2b23d0ba | 108 | |
marcozecchini | 0:9fca2b23d0ba | 109 | } |
marcozecchini | 0:9fca2b23d0ba | 110 | #endif |
marcozecchini | 0:9fca2b23d0ba | 111 | |
marcozecchini | 0:9fca2b23d0ba | 112 |