AAX SDK 2.6.1
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 Avid Technology, Inc.
4 * All rights reserved.
5 *
6 * CONFIDENTIAL: this document contains confidential information of Avid. Do
7 * not disclose to any third party. Use of the information contained in this
8 * document is subject to an Avid SDK license.
9 */
10
17/*================================================================================================*/
18
19#ifndef AAX_ALIGNMENT_H
20#define AAX_ALIGNMENT_H
21
22#include <stddef.h>
23
24namespace AAX
25{
26
27 inline void alignFree(void *p)
28 {
29 char** aTempPtr=reinterpret_cast<char**>(p);
30 aTempPtr--; //backup 4 bytes past the beginning of the buffer
31 char* aRealPtr = aTempPtr[0]; //Get the real address
32
33 if(aRealPtr)
34 ::delete[] aRealPtr;
35 }
36
37 template <class T>
38 T* alignMalloc(int iArraySize, int iAlignment)
39 {
40 // We can seriously mess ourselves up if alignment is not a power of 2
41 if ((iAlignment != 2) && (iAlignment != 4) && (iAlignment != 8) && (iAlignment != 16) && (iAlignment != 32)) {
42 return 0;
43 }
44 // We can't allocate a negative-size array
45 if (iArraySize <= 0) {
46 return 0;
47 }
48
49 const unsigned int cSizeOfPointer = sizeof(char*);
50 // Over-allocate memory by the maximum offset we could be from our requested alignment
51 char* aRealPtr = ::new char[iArraySize*sizeof(T) + iAlignment + cSizeOfPointer];
52 if (!aRealPtr) {
53 return 0;
54 }
55 char* p=aRealPtr;
56 p+=cSizeOfPointer; //Skip four bytes (we store the real base address here)
57 size_t mod = size_t(p) & (iAlignment-1);
58 if (mod)
59 p += (iAlignment - mod);
60 *reinterpret_cast<char**>(p-cSizeOfPointer)=aRealPtr; //Save the real address. We'll need it for delete.
61 return (T*) p;
62 }
63} // namespace AAX
64
65#endif //AAX_ALIGNMENT_H
Definition: AAX_EnvironmentUtilities.h:59
void alignFree(void *p)
Definition: AAX_Alignment.h:27
T * alignMalloc(int iArraySize, int iAlignment)
Definition: AAX_Alignment.h:38