Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/include/sof/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* Align the number to the nearest alignment value */
#define IS_ALIGNED(size, alignment) ((size) % (alignment) == 0)
#define ALIGN_UP(size, alignment) \
((size) + (((alignment) - ((size) % (alignment))) % (alignment)))
((size) + (alignment) - 1 - ((size) + (alignment) - 1) % (alignment))
#define ALIGN_DOWN(size, alignment) \
((size) - ((size) % (alignment)))
#define ALIGN ALIGN_UP
Expand Down
27 changes: 13 additions & 14 deletions src/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,14 @@ static void *rmalloc_sys(struct mm_heap *heap, uint32_t flags, int caps, size_t
static void *align_ptr(struct mm_heap *heap, uint32_t alignment,
void *ptr, struct block_hdr *hdr)
{
int mod_align = 0;

/* Save unaligned ptr to block hdr */
hdr->unaligned_ptr = ptr;

/* If ptr is not already aligned we calculate alignment shift */
if (alignment && (uintptr_t)ptr % alignment)
mod_align = alignment - ((uintptr_t)ptr % alignment);
if (alignment <= 1)
return ptr;

/* Calculate aligned pointer */
return (char *)ptr + mod_align;
return (void *)ALIGN((uintptr_t)ptr, alignment);
}

/* allocate single block */
Expand Down Expand Up @@ -812,13 +809,16 @@ static void *alloc_heap_buffer(struct mm_heap *heap, uint32_t flags,
platform_shared_commit(map, sizeof(*map));
}

/* size of requested buffer is adjusted for alignment purposes
* since we span more blocks we have to assume worst case scenario
*/
bytes += alignment;

/* request spans > 1 block */
if (!ptr) {
/* size of requested buffer is adjusted for alignment purposes
* since we span more blocks we have to assume worst case scenario
*/
bytes += alignment;

if (heap->size < bytes)
return NULL;

/*
* Find the best block size for request. We know, that we failed
* to find a single large enough block, so, skip those.
Expand All @@ -827,7 +827,7 @@ static void *alloc_heap_buffer(struct mm_heap *heap, uint32_t flags,
map = &heap->map[i];

/* allocate if block size is smaller than request */
if (heap->size >= bytes && map->block_size < bytes) {
if (map->block_size < bytes) {
ptr = alloc_cont_blocks(heap, i, caps,
bytes, alignment);
if (ptr) {
Expand All @@ -846,8 +846,6 @@ static void *alloc_heap_buffer(struct mm_heap *heap, uint32_t flags,
bzero(ptr, temp_bytes);
#endif

platform_shared_commit(heap, sizeof(*heap));

return ptr;
}

Expand All @@ -868,6 +866,7 @@ static void *_balloc_unlocked(uint32_t flags, uint32_t caps, size_t bytes,
break;

ptr = alloc_heap_buffer(heap, flags, caps, bytes, alignment);
platform_shared_commit(heap, sizeof(*heap));
if (ptr)
break;

Expand Down