AAX SDK 2.8.0
Avid Audio Extensions Development Kit
Loading...
Searching...
No Matches
AAX_Alignment.h
Go to the documentation of this file.
1/*================================================================================================*/
2/*
3 * Copyright 2009-2015, 2023-2024 Avid Technology, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the Avid AAX SDK.
7 *
8 * The AAX SDK is subject to commercial or open-source licensing.
9 *
10 * By using the AAX SDK, you agree to the terms of both the Avid AAX SDK License
11 * Agreement and Avid Privacy Policy.
12 *
13 * AAX SDK License: https://developer.avid.com/aax
14 * Privacy Policy: https://www.avid.com/legal/privacy-policy-statement
15 *
16 * Or: You may also use this code under the terms of the GPL v3 (see
17 * www.gnu.org/licenses).
18 *
19 * THE AAX SDK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 * EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 * DISCLAIMED.
22 */
23
30/*================================================================================================*/
31
32#ifndef AAX_ALIGNMENT_H
33#define AAX_ALIGNMENT_H
34
35#include <stddef.h>
36
37namespace AAX
38{
39
40 inline void alignFree(void *p)
41 {
42 char** aTempPtr=reinterpret_cast<char**>(p);
43 aTempPtr--; //backup 4 bytes past the beginning of the buffer
44 char* aRealPtr = aTempPtr[0]; //Get the real address
45
46 if(aRealPtr)
47 ::delete[] aRealPtr;
48 }
49
50 template <class T>
51 T* alignMalloc(int iArraySize, int iAlignment)
52 {
53 // We can seriously mess ourselves up if alignment is not a power of 2
54 if ((iAlignment != 2) && (iAlignment != 4) && (iAlignment != 8) && (iAlignment != 16) && (iAlignment != 32)) {
55 return 0;
56 }
57 // We can't allocate a negative-size array
58 if (iArraySize <= 0) {
59 return 0;
60 }
61
62 const unsigned int cSizeOfPointer = sizeof(char*);
63 // Over-allocate memory by the maximum offset we could be from our requested alignment
64 char* aRealPtr = ::new char[iArraySize*sizeof(T) + iAlignment + cSizeOfPointer];
65 if (!aRealPtr) {
66 return 0;
67 }
68 char* p=aRealPtr;
69 p+=cSizeOfPointer; //Skip four bytes (we store the real base address here)
70 size_t mod = size_t(p) & (iAlignment-1);
71 if (mod)
72 p += (iAlignment - mod);
73 *reinterpret_cast<char**>(p-cSizeOfPointer)=aRealPtr; //Save the real address. We'll need it for delete.
74 return (T*) p;
75 }
76} // namespace AAX
77
78#endif //AAX_ALIGNMENT_H
Definition: AAX_EnvironmentUtilities.h:72
void alignFree(void *p)
Definition: AAX_Alignment.h:40
T * alignMalloc(int iArraySize, int iAlignment)
Definition: AAX_Alignment.h:51