Added very useful and convenient macro for parsing attribute IDs
[AROS.git] / compiler / alib / liballocaligned.c
blob0ddc6174527b3095e1370c673bd913cccc49bf91
1 /*
2 * Copyright (C) 2012, The AROS Development Team
3 * All right reserved.
4 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
6 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
7 */
9 #include <proto/exec.h>
11 /*****************************************************************************
13 NAME */
14 #include <proto/alib.h>
16 APTR LibAllocAligned (
18 /* SYNOPSIS */
19 ULONG memSize,
20 ULONG requirements,
21 IPTR alignBytes)
23 /* FUNCTION
25 INPUTS
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!
31 RESULT
33 Pointer to the newly alloctated area, or
34 NULL if no saisfying memory can be found.
36 Free this pointer using FreeMem(..., memSize)
38 NOTES
40 If alignBytes is not a power of two, NULL is returned.
42 If memSize is not a multiple of alignBytes, NULL is returned.
44 EXAMPLE
46 BUGS
48 SEE ALSO
50 INTERNALS
52 HISTORY
54 ******************************************************************************/
56 APTR ptr;
57 IPTR alignMask;
59 /* Verify that alignBytes is a power of two */
60 if ((alignBytes & (alignBytes-1)) != 0)
61 return NULL;
63 /* Verify that memSize is modulo alignBytes */
64 if ((memSize & (alignBytes - 1)) != 0)
65 return NULL;
67 alignMask = alignBytes - 1;
69 if ((ptr = AllocMem(memSize + alignMask, requirements))) {
70 APTR aptr = (APTR)((((IPTR)ptr) + alignMask) & ~alignMask);
71 if (aptr != ptr) {
72 Forbid();
73 FreeMem(ptr, memSize + alignMask);
74 ptr = AllocAbs(memSize, aptr);
75 Permit();
79 return ptr;