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