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