INTERNAL_SIZE_T nb;
#ifdef CONFIG_SYS_MALLOC_F_LEN
- if (gd && !(gd->flags & GD_FLG_FULL_MALLOC_INIT))
+ if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
return malloc_simple(bytes);
#endif
nb = request2size(bytes);
m = (char*)(mALLOc(nb + alignment + MINSIZE));
+ /*
+ * The attempt to over-allocate (with a size large enough to guarantee the
+ * ability to find an aligned region within allocated memory) failed.
+ *
+ * Try again, this time only allocating exactly the size the user wants. If
+ * the allocation now succeeds and just happens to be aligned, we can still
+ * fulfill the user's request.
+ */
+ if (m == NULL) {
+ /*
+ * Use bytes not nb, since mALLOc internally calls request2size too, and
+ * each call increases the size to allocate, to account for the header.
+ */
+ m = (char*)(mALLOc(bytes));
+ /* Aligned -> return it */
+ if ((((unsigned long)(m)) % alignment) == 0)
+ return m;
+ /* Otherwise, fail */
+ fREe(m);
+ m = NULL;
+ }
+
if (m == NULL) return NULL; /* propagate failure */
p = mem2chunk(m);