Daoyu_Sofiane Yao_Belouka / mbed-rtos

Dependents:   Mecatro_Gyro_Programme_Codeur_HC06

Committer:
daoyu_sofiane
Date:
Fri Apr 16 09:25:33 2021 +0000
Revision:
0:a8ed743bc1e1
Projet Gyropode

Who changed what in which revision?

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