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