Doubea Pierre / Mbed 2 deprecated HW3

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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