Lee Shen / FTHR_USB_serial_qSPI
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemoryPool.h Source File

MemoryPool.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MEMORYPOOL_H
00023 #define MEMORYPOOL_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "cmsis_os2.h"
00029 #include "mbed_rtos1_types.h"
00030 #include "mbed_rtos_storage.h"
00031 #include "platform/NonCopyable.h"
00032 
00033 namespace rtos {
00034 /** \addtogroup rtos */
00035 /** @{*/
00036 
00037 /** Define and manage fixed-size memory pools of objects of a given type.
00038   @tparam  T         data type of a single object (element).
00039   @tparam  queue_sz  maximum number of objects (elements) in the memory pool.
00040 
00041  @note
00042  Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
00043  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00044 */
00045 template<typename T, uint32_t pool_sz>
00046 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
00047     MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
00048 public:
00049     /** Create and Initialize a memory pool. */
00050     MemoryPool() {
00051         memset(_pool_mem, 0, sizeof(_pool_mem));
00052         memset(&_obj_mem, 0, sizeof(_obj_mem));
00053         memset(&_attr, 0, sizeof(_attr));
00054         _attr.mp_mem = _pool_mem;
00055         _attr.mp_size = sizeof(_pool_mem);
00056         _attr.cb_mem = &_obj_mem;
00057         _attr.cb_size = sizeof(_obj_mem);
00058         _id = osMemoryPoolNew(pool_sz, sizeof(T), &_attr);
00059         MBED_ASSERT(_id);
00060     }
00061 
00062     /** Destroy a memory pool */
00063     ~MemoryPool() {
00064         osMemoryPoolDelete(_id);
00065     }
00066 
00067     /** Allocate a memory block of type T from a memory pool.
00068       @return  address of the allocated memory block or NULL in case of no memory available.
00069     */
00070     T* alloc(void) {
00071         return (T*)osMemoryPoolAlloc(_id, 0);
00072     }
00073 
00074     /** Allocate a memory block of type T from a memory pool and set memory block to zero.
00075       @return  address of the allocated memory block or NULL in case of no memory available.
00076     */
00077     T* calloc(void) {
00078         T *item = (T*)osMemoryPoolAlloc(_id, 0);
00079         if (item != NULL) {
00080             memset(item, 0, sizeof(T));
00081         }
00082         return item;
00083     }
00084 
00085     /** Free a memory block.
00086       @param   block  address of the allocated memory block to be freed.
00087       @return         osOK on successful deallocation, osErrorParameter if given memory block id
00088                       is NULL or invalid, or osErrorResource if given memory block is in an
00089                       invalid memory pool state.
00090 
00091     */
00092     osStatus free(T *block) {
00093         return osMemoryPoolFree(_id, (void*)block);
00094     }
00095 
00096 private:
00097     osMemoryPoolId_t             _id;
00098     osMemoryPoolAttr_t           _attr;
00099     /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */
00100     char                         _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz];
00101     mbed_rtos_storage_mem_pool_t _obj_mem;
00102 };
00103 
00104 }
00105 #endif
00106 
00107 /** @}*/