The code from https://github.com/vpcola/Nucleo

Committer:
sinrab
Date:
Wed Oct 08 11:00:24 2014 +0000
Revision:
0:5464d5e415e5
The code from https://github.com/vpcola/Nucleo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sinrab 0:5464d5e415e5 1 /* mbed Microcontroller Library
sinrab 0:5464d5e415e5 2 * Copyright (c) 2006-2012 ARM Limited
sinrab 0:5464d5e415e5 3 *
sinrab 0:5464d5e415e5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
sinrab 0:5464d5e415e5 5 * of this software and associated documentation files (the "Software"), to deal
sinrab 0:5464d5e415e5 6 * in the Software without restriction, including without limitation the rights
sinrab 0:5464d5e415e5 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sinrab 0:5464d5e415e5 8 * copies of the Software, and to permit persons to whom the Software is
sinrab 0:5464d5e415e5 9 * furnished to do so, subject to the following conditions:
sinrab 0:5464d5e415e5 10 *
sinrab 0:5464d5e415e5 11 * The above copyright notice and this permission notice shall be included in
sinrab 0:5464d5e415e5 12 * all copies or substantial portions of the Software.
sinrab 0:5464d5e415e5 13 *
sinrab 0:5464d5e415e5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sinrab 0:5464d5e415e5 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sinrab 0:5464d5e415e5 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sinrab 0:5464d5e415e5 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sinrab 0:5464d5e415e5 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sinrab 0:5464d5e415e5 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
sinrab 0:5464d5e415e5 20 * SOFTWARE.
sinrab 0:5464d5e415e5 21 */
sinrab 0:5464d5e415e5 22 #ifndef MEMORYPOOL_H
sinrab 0:5464d5e415e5 23 #define MEMORYPOOL_H
sinrab 0:5464d5e415e5 24
sinrab 0:5464d5e415e5 25 #include <stdint.h>
sinrab 0:5464d5e415e5 26 #include <string.h>
sinrab 0:5464d5e415e5 27
sinrab 0:5464d5e415e5 28 #include "cmsis_os.h"
sinrab 0:5464d5e415e5 29
sinrab 0:5464d5e415e5 30 namespace rtos {
sinrab 0:5464d5e415e5 31
sinrab 0:5464d5e415e5 32 /** Define and manage fixed-size memory pools of objects of a given type.
sinrab 0:5464d5e415e5 33 @tparam T data type of a single object (element).
sinrab 0:5464d5e415e5 34 @tparam queue_sz maximum number of objects (elements) in the memory pool.
sinrab 0:5464d5e415e5 35 */
sinrab 0:5464d5e415e5 36 template<typename T, uint32_t pool_sz>
sinrab 0:5464d5e415e5 37 class MemoryPool {
sinrab 0:5464d5e415e5 38 public:
sinrab 0:5464d5e415e5 39 /** Create and Initialize a memory pool. */
sinrab 0:5464d5e415e5 40 MemoryPool() {
sinrab 0:5464d5e415e5 41 #ifdef CMSIS_OS_RTX
sinrab 0:5464d5e415e5 42 memset(_pool_m, 0, sizeof(_pool_m));
sinrab 0:5464d5e415e5 43 _pool_def.pool = _pool_m;
sinrab 0:5464d5e415e5 44
sinrab 0:5464d5e415e5 45 _pool_def.pool_sz = pool_sz;
sinrab 0:5464d5e415e5 46 _pool_def.item_sz = sizeof(T);
sinrab 0:5464d5e415e5 47 #endif
sinrab 0:5464d5e415e5 48 _pool_id = osPoolCreate(&_pool_def);
sinrab 0:5464d5e415e5 49 }
sinrab 0:5464d5e415e5 50
sinrab 0:5464d5e415e5 51 /** Allocate a memory block of type T from a memory pool.
sinrab 0:5464d5e415e5 52 @return address of the allocated memory block or NULL in case of no memory available.
sinrab 0:5464d5e415e5 53 */
sinrab 0:5464d5e415e5 54 T* alloc(void) {
sinrab 0:5464d5e415e5 55 return (T*)osPoolAlloc(_pool_id);
sinrab 0:5464d5e415e5 56 }
sinrab 0:5464d5e415e5 57
sinrab 0:5464d5e415e5 58 /** Allocate a memory block of type T from a memory pool and set memory block to zero.
sinrab 0:5464d5e415e5 59 @return address of the allocated memory block or NULL in case of no memory available.
sinrab 0:5464d5e415e5 60 */
sinrab 0:5464d5e415e5 61 T* calloc(void) {
sinrab 0:5464d5e415e5 62 return (T*)osPoolCAlloc(_pool_id);
sinrab 0:5464d5e415e5 63 }
sinrab 0:5464d5e415e5 64
sinrab 0:5464d5e415e5 65 /** Return an allocated memory block back to a specific memory pool.
sinrab 0:5464d5e415e5 66 @param address of the allocated memory block that is returned to the memory pool.
sinrab 0:5464d5e415e5 67 @return status code that indicates the execution status of the function.
sinrab 0:5464d5e415e5 68 */
sinrab 0:5464d5e415e5 69 osStatus free(T *block) {
sinrab 0:5464d5e415e5 70 return osPoolFree(_pool_id, (void*)block);
sinrab 0:5464d5e415e5 71 }
sinrab 0:5464d5e415e5 72
sinrab 0:5464d5e415e5 73 private:
sinrab 0:5464d5e415e5 74 osPoolId _pool_id;
sinrab 0:5464d5e415e5 75 osPoolDef_t _pool_def;
sinrab 0:5464d5e415e5 76 #ifdef CMSIS_OS_RTX
sinrab 0:5464d5e415e5 77 uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
sinrab 0:5464d5e415e5 78 #endif
sinrab 0:5464d5e415e5 79 };
sinrab 0:5464d5e415e5 80
sinrab 0:5464d5e415e5 81 }
sinrab 0:5464d5e415e5 82 #endif
sinrab 0:5464d5e415e5 83