blob: 2ad1099d6b1ced1a9c3065b6f0e00295e4b47afa [file] [log] [blame]
Angel Ponsebda03e2020-04-02 23:48:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin127525c2015-03-26 12:29:12 -05002
Aaron Durbinca0a6762015-12-15 17:49:12 -06003#include <commonlib/helpers.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -05004#include <commonlib/mem_pool.h>
Aaron Durbin127525c2015-03-26 12:29:12 -05005
6void *mem_pool_alloc(struct mem_pool *mp, size_t sz)
7{
8 void *p;
9
10 /* Make all allocations be at least 8 byte aligned. */
11 sz = ALIGN_UP(sz, 8);
12
13 /* Determine if any space available. */
14 if ((mp->size - mp->free_offset) < sz)
15 return NULL;
16
17 p = &mp->buf[mp->free_offset];
18
19 mp->free_offset += sz;
20 mp->last_alloc = p;
21
22 return p;
23}
24
25void mem_pool_free(struct mem_pool *mp, void *p)
26{
27 /* Determine if p was the most recent allocation. */
28 if (p == NULL || mp->last_alloc != p)
29 return;
30
31 mp->free_offset = mp->last_alloc - mp->buf;
32 /* No way to track allocation before this one. */
33 mp->last_alloc = NULL;
34}