Intel HEXL
Intel Homomorphic Encryption Acceleration Library, accelerating the modular arithmetic operations used in homomorphic encryption.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
aligned-allocator.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <cstdlib>
7 #include <memory>
8 #include <vector>
9 
11 #include "hexl/util/allocator.hpp"
12 #include "hexl/util/defines.hpp"
13 
14 namespace intel {
15 namespace hexl {
16 
19  void* allocate(size_t bytes_count) final { return std::malloc(bytes_count); }
20 
21  void deallocate(void* p, size_t n) final {
22  HEXL_UNUSED(n);
23  std::free(p);
24  }
25 };
26 
27 using AllocatorStrategyPtr = std::shared_ptr<AllocatorBase>;
29 
32 template <typename T, uint64_t Alignment>
34  public:
35  template <typename, uint64_t>
36  friend class AlignedAllocator;
37 
38  using value_type = T;
39 
40  explicit AlignedAllocator(AllocatorStrategyPtr strategy = nullptr) noexcept
41  : m_alloc_impl((strategy != nullptr) ? strategy : mallocStrategy) {}
42 
44  : m_alloc_impl(src.m_alloc_impl) {}
45 
46  template <typename U>
48  : m_alloc_impl(src.m_alloc_impl) {}
49 
51 
52  template <typename U>
53  struct rebind {
55  };
56 
57  bool operator==(const AlignedAllocator&) { return true; }
58 
59  bool operator!=(const AlignedAllocator&) { return false; }
60 
63  T* allocate(size_t n) {
64  if (!IsPowerOfTwo(Alignment)) {
65  return nullptr;
66  }
67  // Allocate enough space to ensure the alignment can be satisfied
68  size_t buffer_size = sizeof(T) * n + Alignment;
69  // Additionally, allocate a prefix to store the memory location of the
70  // unaligned buffer
71  size_t alloc_size = buffer_size + sizeof(void*);
72  void* buffer = m_alloc_impl->allocate(alloc_size);
73  if (!buffer) {
74  return nullptr;
75  }
76 
77  // Reserve first location for pointer to originally-allocated space
78  void* aligned_buffer = static_cast<char*>(buffer) + sizeof(void*);
79  std::align(Alignment, sizeof(T) * n, aligned_buffer, buffer_size);
80  if (!aligned_buffer) {
81  return nullptr;
82  }
83 
84  // Store allocated buffer address at aligned_buffer - sizeof(void*).
85  void* store_buffer_addr =
86  static_cast<char*>(aligned_buffer) - sizeof(void*);
87  *(static_cast<void**>(store_buffer_addr)) = buffer;
88 
89  return static_cast<T*>(aligned_buffer);
90  }
91 
92  void deallocate(T* p, size_t n) {
93  if (!p) {
94  return;
95  }
96  void* store_buffer_addr = (reinterpret_cast<char*>(p) - sizeof(void*));
97  void* free_address = *(static_cast<void**>(store_buffer_addr));
98  m_alloc_impl->deallocate(free_address, n);
99  }
100 
101  private:
102  AllocatorStrategyPtr m_alloc_impl;
103 };
104 
106 template <typename T>
107 using AlignedVector64 = std::vector<T, AlignedAllocator<T, 64> >;
108 
109 } // namespace hexl
110 } // namespace intel
bool operator!=(const AlignedAllocator &)
Definition: aligned-allocator.hpp:59
Allocates memory aligned to Alignment-byte sized boundaries.
Definition: aligned-allocator.hpp:33
Base class for custom memory allocator.
Definition: allocator.hpp:12
void * allocate(size_t bytes_count) final
Allocates byte_count bytes of memory.
Definition: aligned-allocator.hpp:19
std::shared_ptr< AllocatorBase > AllocatorStrategyPtr
Definition: aligned-allocator.hpp:27
std::vector< T, AlignedAllocator< T, 64 > > AlignedVector64
64-byte aligned memory allocator
Definition: aligned-allocator.hpp:107
T * allocate(size_t n)
Allocates n elements aligned to Alignment-byte boundaries.
Definition: aligned-allocator.hpp:63
~AlignedAllocator()
Definition: aligned-allocator.hpp:50
void deallocate(void *p, size_t n) final
Deallocate memory.
Definition: aligned-allocator.hpp:21
Allocater implementation using malloc and free.
Definition: aligned-allocator.hpp:18
bool IsPowerOfTwo(uint64_t num)
Returns whether or not num is a power of two.
Definition: number-theory.hpp:55
Definition: aligned-allocator.hpp:53
AlignedAllocator(AllocatorStrategyPtr strategy=nullptr) noexcept
Definition: aligned-allocator.hpp:40
Definition: eltwise-add-mod.hpp:8
void deallocate(T *p, size_t n)
Definition: aligned-allocator.hpp:92
uint64_t value_type
Definition: aligned-allocator.hpp:38
AlignedAllocator(const AlignedAllocator &src)
Definition: aligned-allocator.hpp:43
AllocatorStrategyPtr mallocStrategy
#define HEXL_UNUSED(x)
Definition: defines.hpp:13
AlignedAllocator(const AlignedAllocator< U, Alignment > &src)
Definition: aligned-allocator.hpp:47
bool operator==(const AlignedAllocator &)
Definition: aligned-allocator.hpp:57