HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <stdlib.h>
16 
17 /// These definitions are system specific methods of forcing data to be
18 /// aligned. Some processors require data to be aligned on 16 byte boundaries
19 /// for certain operations. These macros and inline functions provide a
20 /// consistent approach to doing this.
21 ///
22 /// Example 1:
23 /// class Foo {
24 /// SYS_ALIGN16 int a[4];
25 /// int b;
26 /// SYS_ALIGN16 char c;
27 /// };
28 /// In the struct, both the A and C members will be aligned on a 16 byte
29 /// boundary. This will force the size of the class to be 48 bytes.
30 ///
31 /// Example 2:
32 /// fpreal32 *list = SYSamalloc(4*sizeof(fpreal32));
33 /// This will guarantee that the list pointer is aligned on a 16 byte boundary.
34 ///
35 /// Example 3:
36 /// fpreal32 *list = SYSamalloc(4*sizeof(fpreal32), 128);
37 /// This will guarantee that the list is aligned to a 128 byte boundary.
38 
39 #if defined(WIN32)
40  #include <malloc.h>
41  #include <string.h>
42 
43  #define SYS_ALIGN16 __declspec(align(16))
44  #define SYS_ALIGN(b) __declspec(align(b))
45 
46  inline void *SYSamalloc(size_t b, size_t a=16)
47  { return _aligned_malloc(b, a); }
48 
49  inline void *SYSacalloc(size_t n, size_t b, size_t a=16)
50  {
51  b *= n;
52  void *p = _aligned_malloc(b, a);
53  memset(p, 0, b);
54  return p;
55  }
56  inline void SYSafree(void *p)
57  { _aligned_free(p); }
58 
59 #elif defined(LINUX) || defined(MBSD)
60  #include <malloc.h>
61  #include <stdlib.h>
62  #include <string.h>
63 
64  #define SYS_ALIGN16 __attribute__ ((aligned(16)))
65  #define SYS_ALIGN(b) __attribute__ ((aligned(b)))
66 
67 // 64-bit libc guarantees 16-byte alignment for all calls to malloc. 32-/64-bit
68 // jemalloc and 32-bit libc guarantee only 8-byte alignment (though 64-bit
69 // jemalloc appears to always 16-byte-align sufficiently large allocations).
70 // Also, jemalloc aligns memory to twice its argument, whereas libc aligns it to
71 // the argument.
72 //
73 // There are no aligned versions of realloc and calloc in libc. There are
74 // "experimental" versions in jemalloc.
75 
76  inline void *SYSamalloc(size_t b, size_t a=16)
77  {
78  void *p;
79  if (posix_memalign(&p, a, b) != 0)
80  return nullptr; // Invalid alignment or out of memory
81 #ifdef UT_DEBUG
82  memset(p, 0xCD, b);
83 #endif
84  return p;
85  }
86  inline void *SYSacalloc(size_t n, size_t b, size_t a=16)
87  {
88  void *p;
89  if (posix_memalign(&p, a, b*n) != 0)
90  return nullptr; // Invalid alignment or out of memory
91  memset(p, 0, b);
92  return p;
93  }
94  // posix_memalign() guarantees the memory can be released using free()
95  inline void SYSafree(void *p)
96  {
97  free(p);
98  }
99 #else
100  #define SYS_ALIGN16
101  #define SYS_ALIGN(b)
102 
103  inline void *SYSamalloc(size_t b) { return malloc(b); }
104  inline void *SYSacalloc(size_t n, size_t b) { return calloc(n, b); }
105  inline void SYSafree(void *p) { free(p); }
106  inline void *SYSamalloc(size_t b, size_t) { return malloc(b); }
107 #endif
108 
109 #endif
void SYSafree(void *p)
Definition: SYS_Align.h:105
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
void * SYSamalloc(size_t b)
Definition: SYS_Align.h:103
void * SYSacalloc(size_t n, size_t b)
Definition: SYS_Align.h:104
GLdouble n
Definition: glcorearb.h:2008
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222