Jameson Miller | 065feab | 2018-04-11 18:37:55 +0000 | [diff] [blame] | 1 | #ifndef MEM_POOL_H |
| 2 | #define MEM_POOL_H |
| 3 | |
| 4 | struct mp_block { |
| 5 | struct mp_block *next_block; |
| 6 | char *next_free; |
| 7 | char *end; |
| 8 | uintmax_t space[FLEX_ARRAY]; /* more */ |
| 9 | }; |
| 10 | |
| 11 | struct mem_pool { |
| 12 | struct mp_block *mp_block; |
| 13 | |
| 14 | /* |
| 15 | * The amount of available memory to grow the pool by. |
| 16 | * This size does not include the overhead for the mp_block. |
| 17 | */ |
| 18 | size_t block_alloc; |
| 19 | |
| 20 | /* The total amount of memory allocated by the pool. */ |
| 21 | size_t pool_alloc; |
| 22 | }; |
| 23 | |
| 24 | /* |
Jameson Miller | 158dfef | 2018-07-02 19:49:34 +0000 | [diff] [blame] | 25 | * Initialize mem_pool with specified initial size. |
| 26 | */ |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 27 | void mem_pool_init(struct mem_pool *pool, size_t initial_size); |
Jameson Miller | 158dfef | 2018-07-02 19:49:34 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 30 | * Discard all the memory the memory pool is responsible for. |
Jameson Miller | 158dfef | 2018-07-02 19:49:34 +0000 | [diff] [blame] | 31 | */ |
Elijah Newren | f87bf28 | 2020-08-15 17:37:57 +0000 | [diff] [blame] | 32 | void mem_pool_discard(struct mem_pool *pool, int invalidate_memory); |
Jameson Miller | 158dfef | 2018-07-02 19:49:34 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
Jameson Miller | 065feab | 2018-04-11 18:37:55 +0000 | [diff] [blame] | 35 | * Alloc memory from the mem_pool. |
| 36 | */ |
| 37 | void *mem_pool_alloc(struct mem_pool *pool, size_t len); |
| 38 | |
| 39 | /* |
| 40 | * Allocate and zero memory from the memory pool. |
| 41 | */ |
| 42 | void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size); |
| 43 | |
Jameson Miller | 0e58301 | 2018-07-02 19:49:35 +0000 | [diff] [blame] | 44 | /* |
Elijah Newren | a762c8c | 2020-08-15 17:37:55 +0000 | [diff] [blame] | 45 | * Allocate memory from the memory pool and copy str into it. |
| 46 | */ |
| 47 | char *mem_pool_strdup(struct mem_pool *pool, const char *str); |
| 48 | char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len); |
| 49 | |
| 50 | /* |
René Scharfe | 8d25663 | 2024-02-25 12:39:44 +0100 | [diff] [blame] | 51 | * Allocate memory from the memory pool and format a string into it. |
| 52 | */ |
Junio C Hamano | 99c7de7 | 2024-06-08 11:37:47 -0700 | [diff] [blame] | 53 | __attribute__((format (printf, 2, 3))) |
René Scharfe | 8d25663 | 2024-02-25 12:39:44 +0100 | [diff] [blame] | 54 | char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...); |
| 55 | |
| 56 | /* |
Jameson Miller | 0e58301 | 2018-07-02 19:49:35 +0000 | [diff] [blame] | 57 | * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src' |
| 58 | * pool will be empty and not contain any memory. It still needs to be free'd |
| 59 | * with a call to `mem_pool_discard`. |
| 60 | */ |
| 61 | void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src); |
| 62 | |
| 63 | /* |
| 64 | * Check if a memory pointed at by 'mem' is part of the range of |
| 65 | * memory managed by the specified mem_pool. |
| 66 | */ |
Elijah Newren | f87bf28 | 2020-08-15 17:37:57 +0000 | [diff] [blame] | 67 | int mem_pool_contains(struct mem_pool *pool, void *mem); |
Jameson Miller | 0e58301 | 2018-07-02 19:49:35 +0000 | [diff] [blame] | 68 | |
Jameson Miller | 065feab | 2018-04-11 18:37:55 +0000 | [diff] [blame] | 69 | #endif |