RTOS lib used for Eurobot 2012
MemoryPool.h@1:b270f864114f, 2019-08-01 (annotated)
- Committer:
- eembed
- Date:
- Thu Aug 01 07:49:38 2019 +0000
- Revision:
- 1:b270f864114f
- Parent:
- 0:e477ba491a3b
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 0:e477ba491a3b | 1 | /* Copyright (c) 2012 mbed.org */ |
narshu | 0:e477ba491a3b | 2 | #ifndef MEMORYPOOL_H |
narshu | 0:e477ba491a3b | 3 | #define MEMORYPOOL_H |
narshu | 0:e477ba491a3b | 4 | |
narshu | 0:e477ba491a3b | 5 | #include <stdint.h> |
narshu | 0:e477ba491a3b | 6 | #include <string.h> |
narshu | 0:e477ba491a3b | 7 | |
narshu | 0:e477ba491a3b | 8 | #include "cmsis_os.h" |
narshu | 0:e477ba491a3b | 9 | |
narshu | 0:e477ba491a3b | 10 | namespace rtos { |
narshu | 0:e477ba491a3b | 11 | |
narshu | 0:e477ba491a3b | 12 | /*! Define and manage fixed-size memory pools of objects of a given type. |
narshu | 0:e477ba491a3b | 13 | \tparam T data type of a single object (element). |
narshu | 0:e477ba491a3b | 14 | \tparam queue_sz maximum number of objects (elements) in the memory pool. |
narshu | 0:e477ba491a3b | 15 | */ |
narshu | 0:e477ba491a3b | 16 | template<typename T, uint32_t pool_sz> |
narshu | 0:e477ba491a3b | 17 | class MemoryPool { |
narshu | 0:e477ba491a3b | 18 | public: |
narshu | 0:e477ba491a3b | 19 | /*! Create and Initialize a memory pool. */ |
narshu | 0:e477ba491a3b | 20 | MemoryPool() { |
narshu | 0:e477ba491a3b | 21 | #ifdef CMSIS_OS_RTX |
narshu | 0:e477ba491a3b | 22 | memset(_pool_m, 0, sizeof(_pool_m)); |
narshu | 0:e477ba491a3b | 23 | _pool_def.pool = _pool_m; |
narshu | 0:e477ba491a3b | 24 | |
narshu | 0:e477ba491a3b | 25 | _pool_def.pool_sz = pool_sz; |
narshu | 0:e477ba491a3b | 26 | _pool_def.item_sz = sizeof(T); |
narshu | 0:e477ba491a3b | 27 | #endif |
narshu | 0:e477ba491a3b | 28 | _pool_id = osPoolCreate(&_pool_def); |
narshu | 0:e477ba491a3b | 29 | } |
narshu | 0:e477ba491a3b | 30 | |
narshu | 0:e477ba491a3b | 31 | /*! Allocate a memory block of type T from a memory pool. |
narshu | 0:e477ba491a3b | 32 | \return address of the allocated memory block or NULL in case of no memory available. |
narshu | 0:e477ba491a3b | 33 | */ |
narshu | 0:e477ba491a3b | 34 | T* alloc(void) { |
narshu | 0:e477ba491a3b | 35 | return (T*)osPoolAlloc(_pool_id); |
narshu | 0:e477ba491a3b | 36 | } |
narshu | 0:e477ba491a3b | 37 | |
narshu | 0:e477ba491a3b | 38 | /*! Allocate a memory block of type T from a memory pool and set memory block to zero. |
narshu | 0:e477ba491a3b | 39 | \return address of the allocated memory block or NULL in case of no memory available. |
narshu | 0:e477ba491a3b | 40 | */ |
narshu | 0:e477ba491a3b | 41 | T* calloc(void) { |
narshu | 0:e477ba491a3b | 42 | return (T*)osPoolCAlloc(_pool_id); |
narshu | 0:e477ba491a3b | 43 | } |
narshu | 0:e477ba491a3b | 44 | |
narshu | 0:e477ba491a3b | 45 | /*! Return an allocated memory block back to a specific memory pool. |
narshu | 0:e477ba491a3b | 46 | \param address of the allocated memory block that is returned to the memory pool. |
narshu | 0:e477ba491a3b | 47 | \return status code that indicates the execution status of the function. |
narshu | 0:e477ba491a3b | 48 | */ |
narshu | 0:e477ba491a3b | 49 | osStatus free(T *block) { |
narshu | 0:e477ba491a3b | 50 | return osPoolFree(_pool_id, (void*)block); |
narshu | 0:e477ba491a3b | 51 | } |
narshu | 0:e477ba491a3b | 52 | |
narshu | 0:e477ba491a3b | 53 | private: |
narshu | 0:e477ba491a3b | 54 | osPoolId _pool_id; |
narshu | 0:e477ba491a3b | 55 | osPoolDef_t _pool_def; |
narshu | 0:e477ba491a3b | 56 | #ifdef CMSIS_OS_RTX |
narshu | 0:e477ba491a3b | 57 | uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)]; |
narshu | 0:e477ba491a3b | 58 | #endif |
narshu | 0:e477ba491a3b | 59 | }; |
narshu | 0:e477ba491a3b | 60 | |
narshu | 0:e477ba491a3b | 61 | } |
narshu | 0:e477ba491a3b | 62 | #endif |