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