GEOSX
MallocBuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "LvArrayConfig.hpp"
17 #include "Macros.hpp"
18 #include "bufferManipulation.hpp"
19 
20 // System includes
21 #include <stddef.h>
22 
23 namespace LvArray
24 {
25 
34 template< typename T >
36 {
37 public:
38 
40  using value_type = T;
41 
43  static constexpr bool hasShallowCopy = true;
44 
50  LVARRAY_HOST_DEVICE inline constexpr
51  MallocBuffer( bool=true ):
52  m_data( nullptr ),
53  m_capacity( 0 )
54  {}
55 
59  MallocBuffer( MallocBuffer const & ) = default;
60 
65  MallocBuffer( MallocBuffer const & src, std::ptrdiff_t ):
66  MallocBuffer( src )
67  {}
68 
73  LVARRAY_HOST_DEVICE inline constexpr
75  m_data( src.m_data ),
76  m_capacity( src.m_capacity )
77  {
78  src.m_capacity = 0;
79  src.m_data = nullptr;
80  }
81 
87  template< typename _T=T, typename=std::enable_if_t< std::is_const< _T >::value > >
88  LVARRAY_HOST_DEVICE inline constexpr
89  MallocBuffer( MallocBuffer< std::remove_const_t< T > > const & src ):
90  m_data( src.data() ),
91  m_capacity( src.capacity() )
92  {}
93 
99  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
101  {
102  m_capacity = src.m_capacity;
103  m_data = src.m_data;
104  return *this;
105  }
106 
112  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
114  {
115  m_capacity = src.m_capacity;
116  m_data = src.m_data;
117  src.m_capacity = 0;
118  src.m_data = nullptr;
119  return *this;
120  }
121 
127  void reallocate( std::ptrdiff_t const size, std::ptrdiff_t const newCapacity )
128  {
129  // TODO: If std::is_trivially_copyable_v< T > then we could use std::realloc.
130  T * const newPtr = reinterpret_cast< T * >( std::malloc( newCapacity * sizeof( T ) ) );
131 
132  std::ptrdiff_t const overlapAmount = std::min( newCapacity, size );
133  arrayManipulation::uninitializedMove( newPtr, overlapAmount, m_data );
134  arrayManipulation::destroy( m_data, size );
135 
136  std::free( m_data );
137  m_capacity = newCapacity;
138  m_data = newPtr;
139  }
140 
145  LVARRAY_HOST_DEVICE inline
146  void free()
147  {
148  std::free( m_data );
149  m_capacity = 0;
150  m_data = nullptr;
151  }
152 
156  LVARRAY_HOST_DEVICE inline
157  std::ptrdiff_t capacity() const
158  { return m_capacity; }
159 
163  LVARRAY_HOST_DEVICE inline constexpr
164  T * data() const
165  { return m_data; }
166 
173  template< typename INDEX_TYPE >
174  LVARRAY_HOST_DEVICE inline constexpr
175  T & operator[]( INDEX_TYPE const i ) const
176  { return m_data[ i ]; }
177 
178 private:
179 
181  T * LVARRAY_RESTRICT m_data = nullptr;
182 
184  std::ptrdiff_t m_capacity = 0;
185 };
186 
187 } // namespace LvArray
void free()
Free the data in the buffer but does not destroy any values.
T value_type
Alias used in the bufferManipulation functions.
constexpr MallocBuffer(bool=true)
Constructor for creating an empty or uninitialized buffer.
LVARRAY_INTEL_CONSTEXPR MallocBuffer & operator=(MallocBuffer const &src)
Copy assignment operator, creates a shallow copy.
static constexpr bool hasShallowCopy
Signifies that the MallocBuffer&#39;s copy semantics are shallow.
void uninitializedMove(T *const LVARRAY_RESTRICT dst, std::ptrdiff_t const size, T *const LVARRAY_RESTRICT src)
Move construct values from the source to the destination.
constexpr MallocBuffer(MallocBuffer< std::remove_const_t< T > > const &src)
Create a copy of src with const T.
This class implements the default behavior for the Buffer methods related to execution space...
void reallocate(std::ptrdiff_t const size, std::ptrdiff_t const newCapacity)
Reallocate the buffer to the new capacity.
void destroy(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size)
Destory the values in the array.
void free(BUFFER &buf, std::ptrdiff_t const size)
Destroy the values in the buffer and free it&#39;s memory.
Contains functions for manipulating buffers.
std::ptrdiff_t capacity() const
constexpr T & operator[](INDEX_TYPE const i) const
The top level namespace.
Definition: Array.hpp:24
constexpr MallocBuffer(MallocBuffer &&src)
Move constructor, creates a shallow copy.
LVARRAY_INTEL_CONSTEXPR MallocBuffer & operator=(MallocBuffer &&src)
Move assignment operator, creates a shallow copy.
Contains a bunch of macro definitions.
MallocBuffer(MallocBuffer const &src, std::ptrdiff_t)
Sized copy constructor, creates a shallow copy.
constexpr T * data() const
constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > min(T const a, T const b)
Definition: math.hpp:65
Implements the Buffer interface using malloc and free.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389