2 * This file is part of the coreboot project.
4 * Copyright 2015 Google Inc.
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.
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.
16 #include <commonlib/helpers.h>
17 #include <commonlib/mem_pool.h>
19 void *mem_pool_alloc(struct mem_pool
*mp
, size_t sz
)
23 /* Make all allocations be at least 8 byte aligned. */
26 /* Determine if any space available. */
27 if ((mp
->size
- mp
->free_offset
) < sz
)
30 p
= &mp
->buf
[mp
->free_offset
];
32 mp
->free_offset
+= sz
;
38 void mem_pool_free(struct mem_pool
*mp
, void *p
)
40 /* Determine if p was the most recent allocation. */
41 if (p
== NULL
|| mp
->last_alloc
!= p
)
44 mp
->free_offset
= mp
->last_alloc
- mp
->buf
;
45 /* No way to track allocation before this one. */
46 mp
->last_alloc
= NULL
;