Refactored aligned memory allocation. Added support for Android. Fixes #8815.