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