openPMD-api
Memory.hpp
1 /* Copyright 2017-2020 Fabian Koller
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/Dataset.hpp"
24 #include "openPMD/Datatype.hpp"
25 
26 #include <complex>
27 #include <functional>
28 #include <memory>
29 #include <utility>
30 
31 
32 namespace openPMD
33 {
34 namespace auxiliary
35 {
36 inline std::unique_ptr< void, std::function< void(void*) > >
37 allocatePtr(Datatype dtype, uint64_t numPoints)
38 {
39  void* data = nullptr;
40  std::function< void(void*) > del = [](void*){};
41  switch( dtype )
42  {
43  using DT = Datatype;
44  case DT::VEC_STRING:
45  data = new char*[numPoints];
46  del = [](void* p){ delete[] static_cast< char** >(p); };
47  break;
48  case DT::VEC_LONG_DOUBLE:
49  case DT::LONG_DOUBLE:
50  data = new long double[numPoints];
51  del = [](void* p){ delete[] static_cast< long double* >(p); };
52  break;
53  case DT::ARR_DBL_7:
54  case DT::VEC_DOUBLE:
55  case DT::DOUBLE:
56  data = new double[numPoints];
57  del = [](void* p){ delete[] static_cast< double* >(p); };
58  break;
59  case DT::VEC_FLOAT:
60  case DT::FLOAT:
61  data = new float[numPoints];
62  del = [](void* p){ delete[] static_cast< float* >(p); };
63  break;
64  case DT::VEC_CLONG_DOUBLE:
65  case DT::CLONG_DOUBLE:
66  data = new std::complex<long double>[numPoints];
67  del = [](void* p){ delete[] static_cast< std::complex<long double>* >(p); };
68  break;
69  case DT::VEC_CDOUBLE:
70  case DT::CDOUBLE:
71  data = new std::complex<double>[numPoints];
72  del = [](void* p){ delete[] static_cast< std::complex<double>* >(p); };
73  break;
74  case DT::VEC_CFLOAT:
75  case DT::CFLOAT:
76  data = new std::complex<float>[numPoints];
77  del = [](void* p){ delete[] static_cast< std::complex<float>* >(p); };
78  break;
79  case DT::VEC_SHORT:
80  case DT::SHORT:
81  data = new short[numPoints];
82  del = [](void* p){ delete[] static_cast< short* >(p); };
83  break;
84  case DT::VEC_INT:
85  case DT::INT:
86  data = new int[numPoints];
87  del = [](void* p){ delete[] static_cast< int* >(p); };
88  break;
89  case DT::VEC_LONG:
90  case DT::LONG:
91  data = new long[numPoints];
92  del = [](void* p){ delete[] static_cast< long* >(p); };
93  break;
94  case DT::VEC_LONGLONG:
95  case DT::LONGLONG:
96  data = new long long[numPoints];
97  del = [](void* p){ delete[] static_cast< long long* >(p); };
98  break;
99  case DT::VEC_USHORT:
100  case DT::USHORT:
101  data = new unsigned short[numPoints];
102  del = [](void* p){ delete[] static_cast< unsigned short* >(p); };
103  break;
104  case DT::VEC_UINT:
105  case DT::UINT:
106  data = new unsigned int[numPoints];
107  del = [](void* p){ delete[] static_cast< unsigned int* >(p); };
108  break;
109  case DT::VEC_ULONG:
110  case DT::ULONG:
111  data = new unsigned long[numPoints];
112  del = [](void* p){ delete[] static_cast< unsigned long* >(p); };
113  break;
114  case DT::VEC_ULONGLONG:
115  case DT::ULONGLONG:
116  data = new unsigned long long[numPoints];
117  del = [](void* p){ delete[] static_cast< unsigned long long* >(p); };
118  break;
119  case DT::VEC_CHAR:
120  case DT::CHAR:
121  data = new char[numPoints];
122  del = [](void* p){ delete[] static_cast< char* >(p); };
123  break;
124  case DT::VEC_UCHAR:
125  case DT::UCHAR:
126  data = new unsigned char[numPoints];
127  del = [](void* p){ delete[] static_cast< unsigned char* >(p); };
128  break;
129  case DT::BOOL:
130  data = new bool[numPoints];
131  del = [](void* p){ delete[] static_cast< bool* >(p); };
132  break;
133  case DT::STRING:
134  /* user assigns c_str pointer */
135  break;
136  case DT::UNDEFINED:
137  default:
138  throw std::runtime_error("Unknown Attribute datatype (Pointer allocation)");
139  }
140 
141  return std::unique_ptr< void, std::function< void(void*) > >(data, del);
142 }
143 
144 inline std::unique_ptr< void, std::function< void(void*) > >
145 allocatePtr(Datatype dtype, Extent const& e)
146 {
147  uint64_t numPoints = 1u;
148  for( auto const& dimensionSize : e )
149  numPoints *= dimensionSize;
150  return allocatePtr(dtype, numPoints);
151 }
152 
153 } // auxiliary
154 } // openPMD
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:42
Public definitions of openPMD-api.
Definition: Date.cpp:29