Angel Pons | ebda03e | 2020-04-02 23:48:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 127525c | 2015-03-26 12:29:12 -0500 | [diff] [blame] | 3 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 5 | #include <commonlib/mem_pool.h> |
Aaron Durbin | 127525c | 2015-03-26 12:29:12 -0500 | [diff] [blame] | 6 | |
| 7 | void *mem_pool_alloc(struct mem_pool *mp, size_t sz) |
| 8 | { |
| 9 | void *p; |
| 10 | |
| 11 | /* Make all allocations be at least 8 byte aligned. */ |
| 12 | sz = ALIGN_UP(sz, 8); |
| 13 | |
| 14 | /* Determine if any space available. */ |
| 15 | if ((mp->size - mp->free_offset) < sz) |
| 16 | return NULL; |
| 17 | |
| 18 | p = &mp->buf[mp->free_offset]; |
| 19 | |
| 20 | mp->free_offset += sz; |
| 21 | mp->last_alloc = p; |
| 22 | |
| 23 | return p; |
| 24 | } |
| 25 | |
| 26 | void mem_pool_free(struct mem_pool *mp, void *p) |
| 27 | { |
| 28 | /* Determine if p was the most recent allocation. */ |
| 29 | if (p == NULL || mp->last_alloc != p) |
| 30 | return; |
| 31 | |
| 32 | mp->free_offset = mp->last_alloc - mp->buf; |
| 33 | /* No way to track allocation before this one. */ |
| 34 | mp->last_alloc = NULL; |
| 35 | } |