Dave Lu / Mbed 2 deprecated FYDP

Dependencies:   mbed

Fork of FYDP_Final2 by Dave Lu

Committer:
tntmarket
Date:
Wed Mar 25 18:11:09 2015 +0000
Revision:
11:425dff6a4af9
Parent:
8:3d5a84b897be
Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
majik 0:21019d94ad33 1 /* mbed Microcontroller Library
majik 0:21019d94ad33 2 * Copyright (c) 2006-2012 ARM Limited
majik 0:21019d94ad33 3 *
majik 0:21019d94ad33 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
majik 0:21019d94ad33 5 * of this software and associated documentation files (the "Software"), to deal
majik 0:21019d94ad33 6 * in the Software without restriction, including without limitation the rights
majik 0:21019d94ad33 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
majik 0:21019d94ad33 8 * copies of the Software, and to permit persons to whom the Software is
majik 0:21019d94ad33 9 * furnished to do so, subject to the following conditions:
majik 0:21019d94ad33 10 *
majik 0:21019d94ad33 11 * The above copyright notice and this permission notice shall be included in
majik 0:21019d94ad33 12 * all copies or substantial portions of the Software.
majik 0:21019d94ad33 13 *
majik 0:21019d94ad33 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
majik 0:21019d94ad33 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
majik 0:21019d94ad33 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
majik 0:21019d94ad33 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
majik 0:21019d94ad33 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
majik 0:21019d94ad33 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
majik 0:21019d94ad33 20 * SOFTWARE.
majik 0:21019d94ad33 21 */
majik 0:21019d94ad33 22 #ifndef MEMORYPOOL_H
majik 0:21019d94ad33 23 #define MEMORYPOOL_H
majik 0:21019d94ad33 24
majik 0:21019d94ad33 25 #include <stdint.h>
majik 0:21019d94ad33 26 #include <string.h>
majik 0:21019d94ad33 27
majik 0:21019d94ad33 28 #include "cmsis_os.h"
majik 0:21019d94ad33 29
majik 0:21019d94ad33 30 namespace rtos {
majik 0:21019d94ad33 31
majik 0:21019d94ad33 32 /** Define and manage fixed-size memory pools of objects of a given type.
majik 0:21019d94ad33 33 @tparam T data type of a single object (element).
majik 0:21019d94ad33 34 @tparam queue_sz maximum number of objects (elements) in the memory pool.
majik 0:21019d94ad33 35 */
majik 0:21019d94ad33 36 template<typename T, uint32_t pool_sz>
majik 0:21019d94ad33 37 class MemoryPool {
majik 0:21019d94ad33 38 public:
majik 0:21019d94ad33 39 /** Create and Initialize a memory pool. */
majik 0:21019d94ad33 40 MemoryPool() {
majik 0:21019d94ad33 41 #ifdef CMSIS_OS_RTX
majik 0:21019d94ad33 42 memset(_pool_m, 0, sizeof(_pool_m));
majik 0:21019d94ad33 43 _pool_def.pool = _pool_m;
majik 0:21019d94ad33 44
majik 0:21019d94ad33 45 _pool_def.pool_sz = pool_sz;
majik 0:21019d94ad33 46 _pool_def.item_sz = sizeof(T);
majik 0:21019d94ad33 47 #endif
majik 0:21019d94ad33 48 _pool_id = osPoolCreate(&_pool_def);
majik 0:21019d94ad33 49 }
majik 0:21019d94ad33 50
majik 0:21019d94ad33 51 /** Allocate a memory block of type T from a memory pool.
majik 0:21019d94ad33 52 @return address of the allocated memory block or NULL in case of no memory available.
majik 0:21019d94ad33 53 */
majik 0:21019d94ad33 54 T* alloc(void) {
majik 0:21019d94ad33 55 return (T*)osPoolAlloc(_pool_id);
majik 0:21019d94ad33 56 }
majik 0:21019d94ad33 57
majik 0:21019d94ad33 58 /** Allocate a memory block of type T from a memory pool and set memory block to zero.
majik 0:21019d94ad33 59 @return address of the allocated memory block or NULL in case of no memory available.
majik 0:21019d94ad33 60 */
majik 0:21019d94ad33 61 T* calloc(void) {
majik 0:21019d94ad33 62 return (T*)osPoolCAlloc(_pool_id);
majik 0:21019d94ad33 63 }
majik 0:21019d94ad33 64
majik 0:21019d94ad33 65 /** Return an allocated memory block back to a specific memory pool.
majik 0:21019d94ad33 66 @param address of the allocated memory block that is returned to the memory pool.
majik 0:21019d94ad33 67 @return status code that indicates the execution status of the function.
majik 0:21019d94ad33 68 */
majik 0:21019d94ad33 69 osStatus free(T *block) {
majik 0:21019d94ad33 70 return osPoolFree(_pool_id, (void*)block);
majik 0:21019d94ad33 71 }
majik 0:21019d94ad33 72
majik 0:21019d94ad33 73 private:
majik 0:21019d94ad33 74 osPoolId _pool_id;
majik 0:21019d94ad33 75 osPoolDef_t _pool_def;
majik 0:21019d94ad33 76 #ifdef CMSIS_OS_RTX
majik 0:21019d94ad33 77 uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
majik 0:21019d94ad33 78 #endif
majik 0:21019d94ad33 79 };
majik 0:21019d94ad33 80
majik 0:21019d94ad33 81 }
majik 0:21019d94ad33 82 #endif