2 * Copyright (C) 2012, 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 (
26 memSize - Size in bytes of the aligned area
27 requirements - Memory requirements (same as AllocMem())
28 alignBytes - Required alignment, in bytes.
29 This must be a power of 2!
33 Pointer to the newly alloctated area, or
34 NULL if no saisfying memory can be found.
36 Free this pointer using FreeMem(..., memSize)
40 If alignBytes is not a power of two, NULL is returned.
42 If memSize is not a multiple of alignBytes, NULL is returned.
54 ******************************************************************************/
59 /* Verify that alignBytes is a power of two */
60 if ((alignBytes
& (alignBytes
-1)) != 0)
63 /* Verify that memSize is modulo alignBytes */
64 if ((memSize
& (alignBytes
- 1)) != 0)
67 alignMask
= alignBytes
- 1;
69 if ((ptr
= AllocMem(memSize
+ alignMask
, requirements
))) {
70 APTR aptr
= (APTR
)((((IPTR
)ptr
) + alignMask
) & ~alignMask
);
73 FreeMem(ptr
, memSize
+ alignMask
);
74 ptr
= AllocAbs(memSize
, aptr
);