Atlas Runtime
util.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 
17 #ifndef _UTIL_H
18 #define _UTIL_H
19 
20 #include <stdint.h>
21 #include <string.h>
22 #include <map>
23 #include <set>
24 #include <atomic>
25 
26 typedef std::pair<uint64_t,uint64_t> UInt64Pair;
27 class CmpUInt64
28 {
29 public:
30  bool operator()(const UInt64Pair & c1, const UInt64Pair & c2) const
31  {
32  return (c1.first < c2.first) && (c1.second < c2.second);
33  }
34 };
35 typedef std::map<UInt64Pair,uint32_t,CmpUInt64> MapInterval;
36 
37 typedef std::pair<void*,size_t> AddrSizePairType;
39 {
40 public:
41  bool operator()(const AddrSizePairType & c1, const AddrSizePairType & c2) const
42  {
43  if ((uintptr_t)c1.first < (uintptr_t)c2.first) return true;
44  if ((uintptr_t)c1.first > (uintptr_t)c2.first) return false;
45  if (c1.second < c2.second) return true;
46  return false;
47  }
48 };
49 typedef std::set<AddrSizePairType, CmpAddrSizePair> SetOfPairs;
50 
51 // This is not thread safe. Currently ok to call from recovery code but
52 // not from anywhere else.
53 inline void InsertToMapInterval(
54  MapInterval *m, uint64_t e1, uint64_t e2, uint32_t e3)
55 {
56  (*m)[std::make_pair(e1,e2)] = e3;
57 }
58 
59 // This can be called from anywhere.
60 inline MapInterval::const_iterator FindInMapInterval(
61  const MapInterval & m, const uint64_t e1, const uint64_t e2)
62 {
63  return m.find(std::make_pair(e1, e2));
64 }
65 
66 inline void InsertSetOfPairs(SetOfPairs *setp, void *addr, size_t sz)
67 {
68  (*setp).insert(std::make_pair(addr, sz));
69 }
70 
71 inline SetOfPairs::const_iterator FindSetOfPairs(
72  const SetOfPairs & setp, void *addr, size_t sz)
73 {
74  return setp.find(std::make_pair(addr, sz));
75 }
76 
77 typedef std::set<uint64_t> SetOfInts;
78 
79 template <class ElemType>
80 class ElemInfo
81 {
82 public:
83  ElemInfo(void *addr, const ElemType & elem)
84  { Addr_ = addr; Elem_ = new ElemType(elem); Next_ = 0; }
85 private:
86  void *Addr_;
87  std::atomic<ElemType*> Elem_;
88  ElemInfo *Next_;
89 };
90 
91 template <class ElemType>
93 {
94 public:
95  SimpleHashTable(uint32_t size=0)
96  {
97  if (size) Size_ = size;
98  Tab_ = new std::atomic<ElemInfo<ElemType>*> [Size_];
99  memset((void*)Tab_, 0, Size_*sizeof(std::atomic<ElemInfo<ElemType>*>));
100  }
101  void Insert(void*, const ElemType &);
102  ElemInfo<ElemType> *Find(void*);
103 private:
104  std::atomic<ElemInfo<ElemType>*> *Tab_;
105  static uint32_t Size_;
106 
107  std::atomic<ElemInfo<ElemType>*> *GetTableEntry(void *addr)
108  { return (Tab_ + (((uintptr_t(addr)+128) >> 3) & (Size_-1))); }
109 
110  ElemInfo<ElemType> *GetElemInfoHeader(void *addr)
111  {
112  std::atomic<ElemInfo<ElemType>*> *entry = GetTableEntry(addr);
113  return (*entry).load(std::memory_order_acquire);
114  }
115  std::atomic<ElemInfo<ElemType>*> *GetPointerToElemInfoHeader(void *addr)
116  {
117  return GetTableEntry(addr);
118  }
119 };
120 
121 char *NVM_GetRegionTablePath();
122 char *NVM_GetUserDir();
123 char *NVM_GetLogDir();
124 void NVM_CreateUserDir();
125 void NVM_CreateLogDir();
126 char *NVM_GetFullyQualifiedRegionName(const char *name);
127 
128 // Derive the log name from the program name for the running process
129 char *NVM_GetLogRegionName();
130 char *NVM_GetLogRegionName(const char *prog_name);
131 bool NVM_doesLogExist(const char *log_path_name);
132 void NVM_qualifyPathName(char *s, const char *name);
133 
134 #endif
Definition: util.hpp:38
void InsertToMapInterval(MapInterval *m, uint64_t e1, uint64_t e2, uint32_t e3)
Definition: util.hpp:53
char * NVM_GetFullyQualifiedRegionName(const char *name)
Definition: util.cpp:145
SetOfPairs::const_iterator FindSetOfPairs(const SetOfPairs &setp, void *addr, size_t sz)
Definition: util.hpp:71
char * NVM_GetLogRegionName()
Definition: util.cpp:159
std::map< UInt64Pair, uint32_t, CmpUInt64 > MapInterval
Definition: util.hpp:35
void InsertSetOfPairs(SetOfPairs *setp, void *addr, size_t sz)
Definition: util.hpp:66
void NVM_CreateLogDir()
Definition: util.cpp:97
char * NVM_GetRegionTablePath()
Definition: util.cpp:37
ElemInfo(void *addr, const ElemType &elem)
Definition: util.hpp:83
bool operator()(const UInt64Pair &c1, const UInt64Pair &c2) const
Definition: util.hpp:30
bool operator()(const AddrSizePairType &c1, const AddrSizePairType &c2) const
Definition: util.hpp:41
Definition: util.hpp:27
MapInterval::const_iterator FindInMapInterval(const MapInterval &m, const uint64_t e1, const uint64_t e2)
Definition: util.hpp:60
bool NVM_doesLogExist(const char *log_path_name)
Definition: util.cpp:182
std::set< AddrSizePairType, CmpAddrSizePair > SetOfPairs
Definition: util.hpp:49
int size(COW_AL *cal)
Definition: cow_array_list.c:183
std::set< uint64_t > SetOfInts
Definition: util.hpp:77
Definition: util.hpp:92
SimpleHashTable(uint32_t size=0)
Definition: util.hpp:95
char * NVM_GetUserDir()
Definition: util.cpp:50
void NVM_CreateUserDir()
Definition: util.cpp:80
Definition: util.hpp:80
std::pair< uint64_t, uint64_t > UInt64Pair
Definition: util.hpp:26
std::pair< void *, size_t > AddrSizePairType
Definition: util.hpp:37
void NVM_qualifyPathName(char *s, const char *name)
Definition: util.cpp:193
char * NVM_GetLogDir()
Definition: util.cpp:62