mainboard/google/kahlee: Remove usb_oc.asl files
[coreboot.git] / src / commonlib / mem_pool.c
blobcb3e726f25ed4cdf1a16783d6555c8d87e02e475
1 /*
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)
21 void *p;
23 /* Make all allocations be at least 8 byte aligned. */
24 sz = ALIGN_UP(sz, 8);
26 /* Determine if any space available. */
27 if ((mp->size - mp->free_offset) < sz)
28 return NULL;
30 p = &mp->buf[mp->free_offset];
32 mp->free_offset += sz;
33 mp->last_alloc = p;
35 return p;
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)
42 return;
44 mp->free_offset = mp->last_alloc - mp->buf;
45 /* No way to track allocation before this one. */
46 mp->last_alloc = NULL;