HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
kalmar_aligned_alloc.h
1 //===----------------------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #pragma once
9 
10 #include <memory>
11 #include <stdlib.h>
12 
14 namespace Kalmar {
15 
16 constexpr inline bool kalmar_is_alignment(std::size_t value) noexcept {
17  return (value > 0) && ((value & (value - 1)) == 0);
18 }
19 
20 inline void* kalmar_aligned_alloc(std::size_t alignment, std::size_t size) noexcept {
21  assert(kalmar_is_alignment(alignment));
22  enum {
23  N = std::alignment_of<void*>::value
24  };
25  if (alignment < N) {
26  alignment = N;
27  }
28  void* memptr = NULL;
29  // posix_memalign shall return 0 upon successfully allocate aligned memory
30  posix_memalign(&memptr, alignment, size);
31  assert(memptr);
32 
33  return memptr;
34 }
35 
36 inline void kalmar_aligned_free(void* ptr) noexcept {
37  if (ptr) {
38  free(ptr);
39  }
40 }
41 
42 } // namespace Kalmar
namespace for internal classes of Kalmar compiler / runtime
Definition: hc.hpp:42