openPMD-api
Memory.hpp
1 /* Copyright 2017-2021 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 namespace openPMD
32 {
33 namespace auxiliary
34 {
35  inline std::unique_ptr<void, std::function<void(void *)> >
36  allocatePtr(Datatype dtype, uint64_t numPoints)
37  {
38  void *data = nullptr;
39  std::function<void(void *)> del = [](void *) {};
40  switch (dtype)
41  {
42  using DT = Datatype;
43  case DT::VEC_STRING:
44  data = new char *[numPoints];
45  del = [](void *p) { delete[] static_cast<char **>(p); };
46  break;
47  case DT::VEC_LONG_DOUBLE:
48  case DT::LONG_DOUBLE:
49  data = new long double[numPoints];
50  del = [](void *p) { delete[] static_cast<long double *>(p); };
51  break;
52  case DT::ARR_DBL_7:
53  case DT::VEC_DOUBLE:
54  case DT::DOUBLE:
55  data = new double[numPoints];
56  del = [](void *p) { delete[] static_cast<double *>(p); };
57  break;
58  case DT::VEC_FLOAT:
59  case DT::FLOAT:
60  data = new float[numPoints];
61  del = [](void *p) { delete[] static_cast<float *>(p); };
62  break;
63  case DT::VEC_CLONG_DOUBLE:
64  case DT::CLONG_DOUBLE:
65  data = new std::complex<long double>[numPoints];
66  del = [](void *p) {
67  delete[] static_cast<std::complex<long double> *>(p);
68  };
69  break;
70  case DT::VEC_CDOUBLE:
71  case DT::CDOUBLE:
72  data = new std::complex<double>[numPoints];
73  del = [](void *p) {
74  delete[] static_cast<std::complex<double> *>(p);
75  };
76  break;
77  case DT::VEC_CFLOAT:
78  case DT::CFLOAT:
79  data = new std::complex<float>[numPoints];
80  del = [](void *p) {
81  delete[] static_cast<std::complex<float> *>(p);
82  };
83  break;
84  case DT::VEC_SHORT:
85  case DT::SHORT:
86  data = new short[numPoints];
87  del = [](void *p) { delete[] static_cast<short *>(p); };
88  break;
89  case DT::VEC_INT:
90  case DT::INT:
91  data = new int[numPoints];
92  del = [](void *p) { delete[] static_cast<int *>(p); };
93  break;
94  case DT::VEC_LONG:
95  case DT::LONG:
96  data = new long[numPoints];
97  del = [](void *p) { delete[] static_cast<long *>(p); };
98  break;
99  case DT::VEC_LONGLONG:
100  case DT::LONGLONG:
101  data = new long long[numPoints];
102  del = [](void *p) { delete[] static_cast<long long *>(p); };
103  break;
104  case DT::VEC_USHORT:
105  case DT::USHORT:
106  data = new unsigned short[numPoints];
107  del = [](void *p) { delete[] static_cast<unsigned short *>(p); };
108  break;
109  case DT::VEC_UINT:
110  case DT::UINT:
111  data = new unsigned int[numPoints];
112  del = [](void *p) { delete[] static_cast<unsigned int *>(p); };
113  break;
114  case DT::VEC_ULONG:
115  case DT::ULONG:
116  data = new unsigned long[numPoints];
117  del = [](void *p) { delete[] static_cast<unsigned long *>(p); };
118  break;
119  case DT::VEC_ULONGLONG:
120  case DT::ULONGLONG:
121  data = new unsigned long long[numPoints];
122  del = [](void *p) {
123  delete[] static_cast<unsigned long long *>(p);
124  };
125  break;
126  case DT::VEC_CHAR:
127  case DT::CHAR:
128  data = new char[numPoints];
129  del = [](void *p) { delete[] static_cast<char *>(p); };
130  break;
131  case DT::VEC_UCHAR:
132  case DT::UCHAR:
133  data = new unsigned char[numPoints];
134  del = [](void *p) { delete[] static_cast<unsigned char *>(p); };
135  break;
136  case DT::BOOL:
137  data = new bool[numPoints];
138  del = [](void *p) { delete[] static_cast<bool *>(p); };
139  break;
140  case DT::STRING:
141  /* user assigns c_str pointer */
142  break;
143  case DT::UNDEFINED:
144  default:
145  throw std::runtime_error(
146  "Unknown Attribute datatype (Pointer allocation)");
147  }
148 
149  return std::unique_ptr<void, std::function<void(void *)> >(data, del);
150  }
151 
152  inline std::unique_ptr<void, std::function<void(void *)> >
153  allocatePtr(Datatype dtype, Extent const &e)
154  {
155  uint64_t numPoints = 1u;
156  for (auto const &dimensionSize : e)
157  numPoints *= dimensionSize;
158  return allocatePtr(dtype, numPoints);
159  }
160 
161 } // namespace auxiliary
162 } // namespace openPMD
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
Public definitions of openPMD-api.
Definition: Date.cpp:28