blob: 0aa821b24ff68dc14daf9a7ff694d3b7940f3c7a [file] [log] [blame]
Aaron Durbin127525c2015-03-26 12:29:12 -05001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin127525c2015-03-26 12:29:12 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Aaron Durbin127525c2015-03-26 12:29:12 -050012 */
13
Aaron Durbinca0a6762015-12-15 17:49:12 -060014#include <commonlib/helpers.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050015#include <commonlib/mem_pool.h>
Aaron Durbin127525c2015-03-26 12:29:12 -050016
17void *mem_pool_alloc(struct mem_pool *mp, size_t sz)
18{
19 void *p;
20
21 /* Make all allocations be at least 8 byte aligned. */
22 sz = ALIGN_UP(sz, 8);
23
24 /* Determine if any space available. */
25 if ((mp->size - mp->free_offset) < sz)
26 return NULL;
27
28 p = &mp->buf[mp->free_offset];
29
30 mp->free_offset += sz;
31 mp->last_alloc = p;
32
33 return p;
34}
35
36void mem_pool_free(struct mem_pool *mp, void *p)
37{
38 /* Determine if p was the most recent allocation. */
39 if (p == NULL || mp->last_alloc != p)
40 return;
41
42 mp->free_offset = mp->last_alloc - mp->buf;
43 /* No way to track allocation before this one. */
44 mp->last_alloc = NULL;
45}