Brandon Donohue / Mbed 2 deprecated IoTBattleship

Dependencies:   mbed

Committer:
bdonohue
Date:
Sat Apr 28 22:07:24 2018 +0000
Revision:
0:912c221cc186
working final ver

Who changed what in which revision?

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