scripts: prepare new configuration for x86_64 on vault13 server using pre build cross...
[AROS.git] / compiler / alib / liballocaligned.c
blobcc1a7d907b182d75bbf9450c65d1b957ce2b9592
1 /*
2 * Copyright (C) 2012-2015, 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 IPTR memSize,
20 ULONG requirements,
21 IPTR alignBytes)
23 /* FUNCTION
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
26 FreeMem().
28 INPUTS
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!
34 RESULT
35 Pointer to the newly allocated area, or NULL if no satisfying memory
36 can be found.
38 NOTES
39 If alignBytes is not a power of two, NULL is returned.
41 If memSize is not a multiple of alignBytes, NULL is returned.
43 EXAMPLE
45 BUGS
47 SEE ALSO
48 exec.library/AllocMem, exec.library/FreeMem().
50 INTERNALS
52 ******************************************************************************/
54 APTR ptr;
55 IPTR alignMask;
57 /* Verify that alignBytes is a power of two */
58 if ((alignBytes & (alignBytes-1)) != 0)
59 return NULL;
61 /* Verify that memSize is modulo alignBytes */
62 if ((memSize & (alignBytes - 1)) != 0)
63 return NULL;
65 alignMask = alignBytes - 1;
67 if ((ptr = AllocMem(memSize + alignMask, requirements)))
69 APTR aptr = (APTR)((((IPTR)ptr) + alignMask) & ~alignMask);
70 Forbid();
71 FreeMem(ptr, memSize + alignMask);
72 ptr = AllocAbs(memSize, aptr);
73 Permit();
76 return ptr;