HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SYS_Align.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_Align.h ( SYS Library, C++)
7  *
8  * COMMENTS: System specific methods of creating aligned data.
9  *
10  */
11 
12 #ifndef __SYS_Align__
13 #define __SYS_Align__
14 
15 #include "SYS_API.h"
16 #include <stdlib.h>
17 
18 /// These definitions are system specific methods of forcing data to be
19 /// aligned. Some processors require data to be aligned on 16 byte boundaries
20 /// for certain operations. These macros and inline functions provide a
21 /// consistent approach to doing this.
22 ///
23 /// Example 1:
24 /// class Foo {
25 /// SYS_ALIGN16 int a[4];
26 /// int b;
27 /// SYS_ALIGN16 char c;
28 /// };
29 /// In the struct, both the A and C members will be aligned on a 16 byte
30 /// boundary. This will force the size of the class to be 48 bytes.
31 ///
32 /// Example 2:
33 /// fpreal32 *list = SYSamalloc(4*sizeof(fpreal32));
34 /// This will guarantee that the list pointer is aligned on a 16 byte boundary.
35 ///
36 /// Example 3:
37 /// fpreal32 *list = SYSamalloc(4*sizeof(fpreal32), 128);
38 /// This will guarantee that the list is aligned to a 128 byte boundary.
39 
40 #if defined(WIN32)
41  #include <malloc.h>
42  #include <string.h>
43 
44  #define SYS_ALIGN16 __declspec(align(16))
45  #define SYS_ALIGN(b) __declspec(align(b))
46 
47  inline void *SYSamalloc(size_t b)
48  { return _aligned_malloc(b, 16); }
49  inline void *SYSacalloc(size_t n, size_t b)
50  {
51  b *= n;
52  void *p = _aligned_malloc(b, 16);
53  memset(p, 0, b);
54  return p;
55  }
56  inline void SYSafree(void *p)
57  { _aligned_free(p); }
58 
59  inline void *SYSamalloc(size_t b, size_t a)
60  { return _aligned_malloc(b, a); }
61 
62 #elif defined(LINUX)
63  #include <malloc.h>
64  #include <string.h>
65 
66  #define SYS_ALIGN16 __attribute__ ((aligned(16)))
67  #define SYS_ALIGN(b) __attribute__ ((aligned(b)))
68 
69 // 64-bit libc guarantees 16-byte alignment for all calls to malloc. 32-/64-bit
70 // jemalloc and 32-bit libc guarantee only 8-byte alignment (though 64-bit
71 // jemalloc appears to always 16-byte-align sufficiently large allocations).
72 // Also, jemalloc aligns memory to twice its argument, whereas libc aligns it to
73 // the argument.
74 //
75 // There are no aligned versions of realloc and calloc in libc. There are
76 // "experimental" versions in jemalloc.
77 
78  inline void *SYSamalloc(size_t b)
79  { return memalign(16, b); }
80  inline void *SYSacalloc(size_t n, size_t b)
81  {
82  b *= n;
83  void *p = memalign(16, b);
84  memset(p, 0, b);
85  return p;
86  }
87  // This is not guaranteed to return aligned memory!
88  inline void SYSafree(void *p) { free(p); }
89  inline void *SYSamalloc(size_t b, size_t a)
90  {
91 #ifdef UT_DEBUG
92  void *p = memalign(a, b);
93  memset(p, 0xCD, b);
94  return p;
95 #else
96  return memalign(a, b);
97 #endif
98  }
99 
100 #elif defined(MBSD)
101  #include <stdlib.h>
102 
103  #define SYS_ALIGN16 __attribute__ ((aligned(16)))
104  #define SYS_ALIGN(b) __attribute__ ((aligned(b)))
105 
106  inline void *SYSamalloc(size_t b) { return malloc(b); }
107  inline void *SYSacalloc(size_t n, size_t b) { return calloc(n, b); }
108  inline void SYSafree(void *p) { free(p); }
109  inline void *SYSamalloc(size_t b, size_t a)
110  {
111  // There's no memalign on OS X, however all memory allocations
112  // are 16-bit aligned.
113  return malloc(b);
114  }
115 
116 #else
117  #define SYS_ALIGN16
118  #define SYS_ALIGN(b)
119 
120  inline void *SYSamalloc(size_t b) { return malloc(b); }
121  inline void *SYSacalloc(size_t n, size_t b) { return calloc(n, b); }
122  inline void SYSafree(void *p) { free(p); }
123  inline void *SYSamalloc(size_t b, size_t) { return malloc(b); }
124 #endif
125 
126 #endif
127 
void SYSafree(void *p)
Definition: SYS_Align.h:122
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
void * SYSamalloc(size_t b)
Definition: SYS_Align.h:120
void * SYSacalloc(size_t n, size_t b)
Definition: SYS_Align.h:121
GLdouble n
Definition: glcorearb.h:2007
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221