1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2018, Linaro Limited 4 */ 5 6 #ifndef __MEMPOOL_H 7 #define __MEMPOOL_H 8 9 #include <types_ext.h> 10 11 /* 12 * struct mempool_item - internal struct to keep track of an item 13 */ 14 struct mempool_item { 15 size_t size; 16 ssize_t prev_item_offset; 17 ssize_t next_item_offset; 18 }; 19 20 struct mempool; 21 22 #define MEMPOOL_ALIGN __alignof__(long) 23 24 /* 25 * mempool_alloc_pool() - Allocate a new memory pool 26 * @data: a block of memory to carve out items from, must 27 * have an alignment of MEMPOOL_ALIGN. 28 * @size: size fo the block of memory 29 * @release_mem: function to call when the pool has been emptied, 30 * ignored if NULL. 31 * returns a pointer to a valid pool on success or NULL on failure. 32 */ 33 struct mempool *mempool_alloc_pool(void *data, size_t size, 34 void (*release_mem)(void *ptr, size_t size)); 35 36 /* 37 * mempool_alloc() - Allocate an item from a memory pool 38 * @pool: A memory pool created with mempool_alloc_pool() 39 * @size: Size in bytes of the item to allocate 40 * return a valid pointer on success or NULL on failure. 41 */ 42 void *mempool_alloc(struct mempool *pool, size_t size); 43 44 /* 45 * mempool_calloc() - Allocate and zero initialize an array of elements from a 46 * memory pool 47 * @pool: A memory pool created with mempool_alloc_pool() 48 * @nmemb: Number of elements in the array 49 * @size: Size in bytes of each element in the array 50 * return a valid pointer on success or NULL on failure. 51 */ 52 void *mempool_calloc(struct mempool *pool, size_t nmemb, size_t size); 53 54 /* 55 * mempool_free() - Frees a previously allocated item 56 * @pool: A memory pool create with mempool_alloc_pool() 57 * @ptr: A pointer to a previously allocated item 58 */ 59 void mempool_free(struct mempool *pool, void *ptr); 60 61 #endif /*__MEMPOOL_H*/ 62