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