mm/mm_heap/mm_memalign : Add re-search the aligned address inside the node
Even though there is next aligned address available in the current free node, memalign will fail only after checking the first-met aligned address.
So add re-search logic inside the selected node.
@@ -176,7 +176,11 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, size_t size)
/* Now, traverse the list in reverse direction, towards bigger size nodes */
for ( ; node; node = node->blink) {
- alignchunk = (FAR struct mm_allocnode_s *)(((size_t)node + SIZEOF_MM_ALLOCNODE + mask) & ~mask);
+ /* Search the suitable aligned address in the same node. */
+ for (alignchunk = (FAR struct mm_allocnode_s *)(((size_t)node + SIZEOF_MM_ALLOCNODE + mask) & ~mask);
+ (uintptr_t)(alignchunk + alignment) < (uintptr_t)(node + node->size);
+ alignchunk = alignchunk + alignment) {
+
size_t alignsize = (size_t)alignchunk - (size_t)node + size;
size_t remainsize = (size_t)alignchunk - SIZEOF_MM_ALLOCNODE - (size_t)node;
@@ -194,6 +198,11 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, size_t size)
}
}
+ if (found_align) {
+ break;
+ }
+ }
+
if (found_align) {
FAR struct mm_allocnode_s *newnode = (FAR struct mm_allocnode_s *)((size_t)alignchunk - SIZEOF_MM_ALLOCNODE);
/* Get the next node after the allocation. */
