2 * Copyright (C) 2012-2015, The AROS Development Team
4 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
6 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
9 #include <proto/exec.h>
11 /*****************************************************************************
14 #include <proto/alib.h>
16 APTR
LibAllocAligned (
24 Allocate a memory block with a start address and size that are
25 multiples of a power-of-two. The returned memory must be freed using
29 memSize - Size in bytes of the aligned area
30 requirements - Memory requirements (same as AllocMem())
31 alignBytes - Required alignment, in bytes.
32 This must be a power of 2!
35 Pointer to the newly allocated area, or NULL if no satisfying memory
39 If alignBytes is not a power of two, NULL is returned.
41 If memSize is not a multiple of alignBytes, NULL is returned.
48 exec.library/AllocMem, exec.library/FreeMem().
52 ******************************************************************************/
57 /* Verify that alignBytes is a power of two */
58 if ((alignBytes
& (alignBytes
-1)) != 0)
61 /* Verify that memSize is modulo alignBytes */
62 if ((memSize
& (alignBytes
- 1)) != 0)
65 alignMask
= alignBytes
- 1;
67 if ((ptr
= AllocMem(memSize
+ alignMask
, requirements
)))
69 APTR aptr
= (APTR
)((((IPTR
)ptr
) + alignMask
) & ~alignMask
);
71 FreeMem(ptr
, memSize
+ alignMask
);
72 ptr
= AllocAbs(memSize
, aptr
);