blob: 39bfe71cda7a8b187090d597c3b9e1b10a55f0c0 [file] [log] [blame]
Angel Ponsebda03e2020-04-02 23:48:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin127525c2015-03-26 12:29:12 -05003
Aaron Durbinca0a6762015-12-15 17:49:12 -06004#include <commonlib/helpers.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -05005#include <commonlib/mem_pool.h>
Aaron Durbin127525c2015-03-26 12:29:12 -05006
7void *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
26void 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}