BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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