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