1 /*
2 * Medical Image Registration ToolKit (MIRTK)
3 *
4 * Copyright 2013-2016 Imperial College London
5 * Copyright 2013-2016 Andreas Schuh
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #ifndef MIRTK_Memory_H
21 #define MIRTK_Memory_H
22
23
24 // =============================================================================
25 // C/C++ library functions
26 // =============================================================================
27
28 #include <cstring>
29 #include <utility>
30 #include <memory>
31
32 namespace mirtk {
33
34
35 template <class T>
36 using UniquePtr = std::unique_ptr<T>;
37
38 template <class T>
39 using SharedPtr = std::shared_ptr<T>;
40
41 template <class T>
42 using WeakPtr = std::weak_ptr<T>;
43
44 template <class T, class... Args>
NewUnique(Args &&...args)45 UniquePtr<T> NewUnique(Args&&... args)
46 {
47 #if __cplusplus == 201103L
48 return UniquePtr<T>(new T(std::forward<Args>(args)...));
49 #else
50 return std::make_unique<T>(std::forward<Args>(args)...);
51 #endif
52 }
53
54 template <class T, class... Args>
NewShared(Args &&...args)55 SharedPtr<T> NewShared(Args&&... args)
56 {
57 return std::make_shared<T>(std::forward<Args>(args)...);
58 }
59
60 using std::memset;
61 using std::memcpy;
62 using std::memmove;
63 using std::memcmp;
64 using std::swap;
65
66 /// Byte order of each word in memory
67 enum ByteOrder
68 {
69 UnknownByteOrder,
70 LittleEndian,
71 BigEndian
72 };
73
74 /// Get byte order of this system
75 ByteOrder GetByteOrder();
76
77 /// Swap bytes of a single word
78 void swap16(char *, char *, long);
79
80 /// Swap bytes of two word
81 void swap32(char *, char *, long);
82
83 /// Swap bytes of four word
84 void swap64(char *, char *, long);
85
86 /// Returns the peak (maximum so far) resident set size (physical
87 /// memory use) measured in bytes, or zero if the value cannot be
88 /// determined on this OS.
89 size_t GetPeakRSS();
90
91 /// Returns the current resident set size (physical memory use) measured
92 /// in bytes, or zero if the value cannot be determined on this OS.
93 size_t GetCurrentRSS();
94
95
96 } // namespace mirtk
97
98 // =============================================================================
99 // Allocate/Deallocate N-D arrays, Delete
100 // =============================================================================
101
102 #include "mirtk/Allocate.h"
103 #include "mirtk/Deallocate.h"
104
105
106 #endif // MIRTK_Memory_H
107