IC Eurobot 2012 Program published from Shuto's account

Dependents:   Eurobot2012_Primary

Committer:
narshu
Date:
Tue Aug 07 10:25:51 2012 +0000
Revision:
0:62626fd22b30
[mbed] converted /Eurobot_2012_Primary/rtos

Who changed what in which revision?

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