bilateral system

Dependencies:   mbed

Committer:
eembed
Date:
Fri Aug 30 11:55:47 2019 +0000
Revision:
2:a921792d9913
Parent:
0:5459cdde6298
commit before edit; ~JKD;

Who changed what in which revision?

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