Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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