lib/os/heap: minimize initial overallocation in the aligned case
The biggest required padding is equal to `align - chunk_header_bytes` and not `align - 1` given that the header already contributes to the padding. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
parent
cf017378fe
commit
9dcd76a650
2 changed files with 12 additions and 2 deletions
|
@ -261,7 +261,8 @@ void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
|
||||||
* We over-allocate to account for alignment and then free
|
* We over-allocate to account for alignment and then free
|
||||||
* the extra allocations afterwards.
|
* the extra allocations afterwards.
|
||||||
*/
|
*/
|
||||||
size_t padded_sz = bytes_to_chunksz(h, bytes + align - 1);
|
size_t padded_sz =
|
||||||
|
bytes_to_chunksz(h, bytes + align - chunk_header_bytes(h));
|
||||||
chunkid_t c0 = alloc_chunk(h, padded_sz);
|
chunkid_t c0 = alloc_chunk(h, padded_sz);
|
||||||
|
|
||||||
if (c0 == 0) {
|
if (c0 == 0) {
|
||||||
|
|
|
@ -60,7 +60,7 @@ static void check_heap_align(struct sys_heap *h,
|
||||||
static void test_aligned_alloc(void)
|
static void test_aligned_alloc(void)
|
||||||
{
|
{
|
||||||
struct sys_heap heap = {};
|
struct sys_heap heap = {};
|
||||||
void *p;
|
void *p, *q;
|
||||||
|
|
||||||
sys_heap_init(&heap, heapmem, HEAP_SZ);
|
sys_heap_init(&heap, heapmem, HEAP_SZ);
|
||||||
|
|
||||||
|
@ -87,6 +87,15 @@ static void test_aligned_alloc(void)
|
||||||
memset(p, 0, 12);
|
memset(p, 0, 12);
|
||||||
zassert_true(sys_heap_validate(&heap), "heap invalid");
|
zassert_true(sys_heap_validate(&heap), "heap invalid");
|
||||||
sys_heap_free(&heap, p);
|
sys_heap_free(&heap, p);
|
||||||
|
|
||||||
|
/* corner case with minimizing the overallocation before alignment */
|
||||||
|
p = sys_heap_aligned_alloc(&heap, 16, 16);
|
||||||
|
q = sys_heap_aligned_alloc(&heap, 16, 17);
|
||||||
|
memset(p, 0, 16);
|
||||||
|
memset(q, 0, 17);
|
||||||
|
zassert_true(sys_heap_validate(&heap), "heap invalid");
|
||||||
|
sys_heap_free(&heap, p);
|
||||||
|
sys_heap_free(&heap, q);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_main(void)
|
void test_main(void)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue