Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

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