Vector Optimized Library of Kernels  3.0.0
Architecture-tuned implementations of math kernels
volk_alloc.hh
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 /*
3  * Copyright 2019 Free Software Foundation, Inc.
4  *
5  * This file is part of VOLK
6  *
7  * SPDX-License-Identifier: LGPL-3.0-or-later
8  */
9 
10 #ifndef INCLUDED_VOLK_ALLOC_H
11 #define INCLUDED_VOLK_ALLOC_H
12 
13 #include <cstdlib>
14 #include <limits>
15 #include <new>
16 #include <vector>
17 
18 #include <volk/volk.h>
19 
20 namespace volk {
21 
28 template <class T>
29 struct alloc {
30  typedef T value_type;
31 
32  alloc() = default;
33 
34  template <class U>
35  constexpr alloc(alloc<U> const&) noexcept
36  {
37  }
38 
39  T* allocate(std::size_t n)
40  {
41  if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
42  throw std::bad_alloc();
43 
44  if (auto p = static_cast<T*>(volk_malloc(n * sizeof(T), volk_get_alignment())))
45  return p;
46 
47  throw std::bad_alloc();
48  }
49 
50  void deallocate(T* p, std::size_t) noexcept { volk_free(p); }
51 };
52 
53 template <class T, class U>
54 bool operator==(alloc<T> const&, alloc<U> const&)
55 {
56  return true;
57 }
58 
59 template <class T, class U>
60 bool operator!=(alloc<T> const&, alloc<U> const&)
61 {
62  return false;
63 }
64 
65 
73 template <class T>
74 using vector = std::vector<T, alloc<T>>;
75 
76 } // namespace volk
77 #endif // INCLUDED_VOLK_ALLOC_H
Definition: volk_alloc.hh:20
bool operator==(alloc< T > const &, alloc< U > const &)
Definition: volk_alloc.hh:54
std::vector< T, alloc< T > > vector
type alias for std::vector using volk::alloc
Definition: volk_alloc.hh:74
bool operator!=(alloc< T > const &, alloc< U > const &)
Definition: volk_alloc.hh:60
C++11 allocator using volk_malloc and volk_free.
Definition: volk_alloc.hh:29
void deallocate(T *p, std::size_t) noexcept
Definition: volk_alloc.hh:50
T value_type
Definition: volk_alloc.hh:30
T * allocate(std::size_t n)
Definition: volk_alloc.hh:39
alloc()=default
constexpr alloc(alloc< U > const &) noexcept
Definition: volk_alloc.hh:35
size_t volk_get_alignment(void)
Get the machine alignment in bytes.
Definition: volk.tmpl.c:90
__VOLK_DECL_BEGIN VOLK_API void * volk_malloc(size_t size, size_t alignment)
Allocate size bytes of data aligned to alignment.
Definition: volk_malloc.c:38
VOLK_API void volk_free(void *aptr)
Free's memory allocated by volk_malloc.
Definition: volk_malloc.c:80