Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemoryPool.h Source File

MemoryPool.h

00001 /* Copyright (c) 2012 mbed.org */
00002 #ifndef MEMORYPOOL_H
00003 #define MEMORYPOOL_H 
00004 
00005 #include <stdint.h>
00006 #include <string.h>
00007 
00008 #include "cmsis_os.h"
00009 
00010 namespace rtos {
00011 
00012 /*! Define and manage fixed-size memory pools of objects of a given type.
00013   \tparam  T         data type of a single object (element).
00014   \tparam  queue_sz  maximum number of objects (elements) in the memory pool.
00015 */
00016 template<typename T, uint32_t pool_sz>
00017 class MemoryPool  {
00018 public:
00019     /*! Create and Initialize a memory pool. */
00020     MemoryPool () {
00021     #ifdef CMSIS_OS_RTX
00022         memset(_pool_m, 0, sizeof(_pool_m));
00023         _pool_def.pool = _pool_m;
00024         
00025         _pool_def.pool_sz = pool_sz;
00026         _pool_def.item_sz =  sizeof(T);
00027     #endif
00028         _pool_id = osPoolCreate(&_pool_def);
00029     }
00030     
00031     /*! Allocate a memory block of type T from a memory pool.
00032       \return  address of the allocated memory block or NULL in case of no memory available.
00033     */
00034     T* alloc (void) {
00035         return (T*)osPoolAlloc(_pool_id);
00036     }
00037     
00038     /*! Allocate a memory block of type T from a memory pool and set memory block to zero.
00039       \return  address of the allocated memory block or NULL in case of no memory available. 
00040     */
00041     T* calloc (void) {
00042         return (T*)osPoolCAlloc(_pool_id);
00043     }
00044     
00045     /*! Return an allocated memory block back to a specific memory pool.
00046       \param   address of the allocated memory block that is returned to the memory pool.
00047       \return  status code that indicates the execution status of the function. 
00048     */
00049     osStatus free (T *block) {
00050         return osPoolFree(_pool_id, (void*)block);
00051     }
00052 
00053 private:
00054     osPoolId    _pool_id;
00055     osPoolDef_t _pool_def;
00056 #ifdef CMSIS_OS_RTX
00057     uint32_t    _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
00058 #endif
00059 };
00060 
00061 }
00062 #endif