ese519

Dependencies:   mbed

Committer:
Jing_Qiu
Date:
Sat Mar 21 02:54:25 2015 +0000
Revision:
0:95aa779116f0
ese519

Who changed what in which revision?

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