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