1 #pragma once
2 
3 #include "common/platform.h"
4 
5 #include "master/hstring_storage.h"
6 
7 #include <set>
8 
9 namespace hstorage {
10 
11 /*! \brief In-memory storage for hstring
12  *
13  * This class implements in-memory storage for hstring.
14  * Data stored in handle is interpreted as a C-style string + 16 bits of its hash.
15  */
16 class MemStorage : public Storage {
17 	static_assert(sizeof(void *) <= 8, "This class supports only <= 64bit architectures");
18 public:
19 	/* On 32bit it is possible to use 32 bits, but we limit ourselves to 16 bits
20 	 * to be consistent with other storage implementations.
21 	 */
22 	typedef Handle::HashType HashType;
23 	typedef Handle::ValueType ValueType;
24 
25 	bool compare(const Handle &handle, const HString &str) override;
26 	::std::string get(const Handle &handle) override;
27 	void copy(Handle &handle, const Handle &other) override;
28 	void bind(Handle &handle, const HString &str) override;
29 	void unbind(Handle &handle) override;
30 	::std::string name() const override;
31 
hash(const Handle & handle)32 	static HashType hash(const Handle &handle) {
33 		return static_cast<HashType>(handle.data() >> kShift);
34 	}
35 
c_str(const Handle & handle)36 	static const char *c_str(const Handle &handle) {
37 		return reinterpret_cast<const char *>(handle.data() & kMask);
38 	}
39 
c_str(Handle & handle)40 	static char *c_str(Handle &handle) {
41 		return reinterpret_cast<char *>(handle.data() & kMask);
42 	}
43 
44 private:
45 	ValueType encode(const char *ptr, HashType hash);
46 
47 	static constexpr const char *kName = "MemStorage";
48 	static const ValueType kShift = 64 - 8 * sizeof(HashType);
49 	static const ValueType kMask = ((static_cast<ValueType>(1) << kShift) - static_cast<ValueType>(1));
50 
51 #if !defined(NDEBUG) || defined(LIZARDFS_TEST_POINTER_OBFUSCATION)
52 	static std::set<char *> *debug_ptr_; /*!< Set with unobfuscated pointers to stored strings.
53 	                                            Just to make valgrind happy. */
54 #endif
55 };
56 
57 } //namespace hstring
58