Documentation: Get rid of trailing whitespaces
[coreboot.git] / src / commonlib / mem_pool.c
blobd82ab18bd79688aca3cc51678e3ab3f28bc25bc2
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/helpers.h>
4 #include <commonlib/mem_pool.h>
6 void *mem_pool_alloc(struct mem_pool *mp, size_t sz)
8 void *p;
10 if (mp->alignment == 0)
11 return NULL;
13 /* We assume that mp->buf started mp->alignment aligned */
14 sz = ALIGN_UP(sz, mp->alignment);
16 /* Determine if any space available. */
17 if ((mp->size - mp->free_offset) < sz)
18 return NULL;
20 p = &mp->buf[mp->free_offset];
22 mp->free_offset += sz;
23 mp->second_to_last_alloc = mp->last_alloc;
24 mp->last_alloc = p;
26 return p;
29 void mem_pool_free(struct mem_pool *mp, void *p)
31 /* Determine if p was the most recent allocation. */
32 if (p == NULL || mp->last_alloc != p)
33 return;
35 mp->free_offset = mp->last_alloc - mp->buf;
36 mp->last_alloc = mp->second_to_last_alloc;
37 /* No way to track allocation before this one. */
38 mp->second_to_last_alloc = NULL;