AAX SDK  2.4.1
Avid Audio Extensions Development Kit
AAX_Alignment.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  * Copyright 2009-2015 by Avid Technology, Inc.
4  * All rights reserved.
5  *
6  * CONFIDENTIAL: This document contains confidential information. Do not
7  * read or examine this document unless you are an Avid Technology employee
8  * or have signed a non-disclosure agreement with Avid Technology which protects
9  * the confidentiality of this document. DO NOT DISCLOSE ANY INFORMATION
10  * CONTAINED IN THIS DOCUMENT TO ANY THIRD-PARTY WITHOUT THE PRIOR WRITTEN CONSENT
11  * OF Avid Technology, INC.
12  */
13 
20 /*================================================================================================*/
21 
22 #ifndef AAX_ALIGNMENT_H
23 #define AAX_ALIGNMENT_H
24 
25 #include <stddef.h>
26 
27 namespace AAX
28 {
29 
30  inline void alignFree(void *p)
31  {
32  char** aTempPtr=reinterpret_cast<char**>(p);
33  aTempPtr--; //backup 4 bytes past the beginning of the buffer
34  char* aRealPtr = aTempPtr[0]; //Get the real address
35 
36  if(aRealPtr)
37  ::delete[] aRealPtr;
38  }
39 
40  template <class T>
41  T* alignMalloc(int iArraySize, int iAlignment)
42  {
43  // We can seriously mess ourselves up if alignment is not a power of 2
44  if ((iAlignment != 2) && (iAlignment != 4) && (iAlignment != 8) && (iAlignment != 16) && (iAlignment != 32)) {
45  return 0;
46  }
47  // We can't allocate a negative-size array
48  if (iArraySize <= 0) {
49  return 0;
50  }
51 
52  const unsigned int cSizeOfPointer = sizeof(char*);
53  // Over-allocate memory by the maximum offset we could be from our requested alignment
54  char* aRealPtr = ::new char[iArraySize*sizeof(T) + iAlignment + cSizeOfPointer];
55  if (!aRealPtr) {
56  return 0;
57  }
58  char* p=aRealPtr;
59  p+=cSizeOfPointer; //Skip four bytes (we store the real base address here)
60  size_t mod = size_t(p) & (iAlignment-1);
61  if (mod)
62  p += (iAlignment - mod);
63  *reinterpret_cast<char**>(p-cSizeOfPointer)=aRealPtr; //Save the real address. We'll need it for delete.
64  return (T*) p;
65  }
66 } // namespace AAX
67 
68 #endif //AAX_ALIGNMENT_H
Definition: AAX_Exception.h:42
void alignFree(void *p)
Definition: AAX_Alignment.h:30
T * alignMalloc(int iArraySize, int iAlignment)
Definition: AAX_Alignment.h:41