1 /* 2 * Copyright 2023-2024 NXP 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef MEM_POOL_H_ 9 #define MEM_POOL_H_ 10 11 #include <osa.h> 12 13 /** 14 * @brief Amount of memory reserved for overhead 15 */ 16 #define POOL_OVERHEAD (sizeof(uint32_t)) 17 18 #include "stack_simple.h" 19 20 /** 21 * The actual Memory Pool data structure. 22 * 23 * This is a variable length data structure. 24 */ 25 typedef struct MemPool_t_ 26 { 27 /** 28 * We need a lock to make this thread safe. 29 */ 30 OSA_MUTEX_HANDLE_DEFINE(Lock); 31 32 /** 33 * Memory blocks are stored on a stack. 34 */ 35 Stack_t Stack; 36 37 /** 38 * Save the item size for additions. 39 */ 40 int ItemSize; 41 42 /** 43 * The overall alignment of an item. 44 */ 45 int Alignment; 46 47 /** 48 * The begining of the actual memory pool itself. 49 */ 50 unsigned char Buffer[1]; 51 52 } MemPool_t; 53 54 /** 55 * Create a MemoryPool 56 * 57 * @param ItemSize How big is an allocation. 58 * @param PreallocatedMemory Pointer to the preallocated memory 59 * you are dedicating to this pool. 60 * @param PreallocatedMemorySize How big is the buffer you are 61 * passing in. 62 * @param Alignment Power of 2 value denoting on which address boundary the 63 * memory will be aligned to. Must be at least sizeof(unsigned char *). 64 * @return A Handle to the pool, or NULL on failure. 65 */ 66 MemoryPool_t OSA_MemoryPoolCreate( 67 MemPool_t *MemPool, int ItemSize, void *PreallocatedMemory, int PreallocatedMemorySize, int Alignment); 68 69 /** 70 * Get a memory buffer from the pool. 71 * 72 * Note that this can block, and cannnot be used from ISR context. 73 * 74 * @param pool A handle to a MemoryPool. 75 * @return A pointer or NULL on failure. 76 */ 77 void *OSA_MemoryPoolAllocate(MemoryPool_t pool); 78 79 /** 80 * Return a memory buffer to the pool. 81 * 82 * @note This can block, and cannnot be used from ISR context. 83 * @note There is no check that the memory passed in is valid. 84 * 85 * @param pool A handle to a MemoryPool. 86 * @param memory memory obtained from OSA_MemoryPoolAllocate(). 87 */ 88 void OSA_MemoryPoolFree(MemoryPool_t pool, void *memory); 89 90 #endif 91