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