mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-24 21:54:01 +00:00
malloc: improve memalign fragmentation fix
Commit 4f144a4164
"malloc: work around some memalign fragmentation
issues" enhanced memalign() so that it can succeed in more cases where
heap fragmentation is present. However, it did not solve as many cases
as it could. This patch enhances the code to cover more cases.
The alignment code works by allocating more space than the user requests,
then adjusting the returned pointer to achieve alignment. In general, one
must allocate "alignment" bytes more than the user requested in order to
guarantee that alignment is possible. This is what the original code does.
The previous enhancement attempted a second allocation if the padded
allocation failed, and succeeded if that allocation just happened to be
aligned; a fluke that happened often in practice. There are still cases
where this could fail, yet where it is still possible to honor the user's
allocation request. In particular, if the heap contains a free region that
is large enough for the user's request, and for leading padding to ensure
alignment, but has no or little space for any trailing padding. In this
case, we can make a third(!) allocation attempt after calculating exactly
the size of the leading padding required to achieve alignment, which is
the minimal over-allocation needed for the overall memalign() operation to
succeed if the third and second allocations end up at the same location.
This patch isn't checkpatch-clean, since it conforms to the existing
coding style in dlmalloc.c, which is different to the rest of U-Boot.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
This commit is contained in:
parent
83b6a43e6e
commit
034eda867f
1 changed files with 21 additions and 2 deletions
|
@ -1909,6 +1909,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
|
|||
* fulfill the user's request.
|
||||
*/
|
||||
if (m == NULL) {
|
||||
size_t extra, extra2;
|
||||
/*
|
||||
* Use bytes not nb, since mALLOc internally calls request2size too, and
|
||||
* each call increases the size to allocate, to account for the header.
|
||||
|
@ -1917,9 +1918,27 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
|
|||
/* Aligned -> return it */
|
||||
if ((((unsigned long)(m)) % alignment) == 0)
|
||||
return m;
|
||||
/* Otherwise, fail */
|
||||
/*
|
||||
* Otherwise, try again, requesting enough extra space to be able to
|
||||
* acquire alignment.
|
||||
*/
|
||||
fREe(m);
|
||||
m = NULL;
|
||||
/* Add in extra bytes to match misalignment of unexpanded allocation */
|
||||
extra = alignment - (((unsigned long)(m)) % alignment);
|
||||
m = (char*)(mALLOc(bytes + extra));
|
||||
/*
|
||||
* m might not be the same as before. Validate that the previous value of
|
||||
* extra still works for the current value of m.
|
||||
* If (!m), extra2=alignment so
|
||||
*/
|
||||
if (m) {
|
||||
extra2 = alignment - (((unsigned long)(m)) % alignment);
|
||||
if (extra2 > extra) {
|
||||
fREe(m);
|
||||
m = NULL;
|
||||
}
|
||||
}
|
||||
/* Fall through to original NULL check and chunk splitting logic */
|
||||
}
|
||||
|
||||
if (m == NULL) return NULL; /* propagate failure */
|
||||
|
|
Loading…
Reference in a new issue