1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 // 5 // See port_example.h for documentation for the following types/functions. 6 7 #ifndef STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_ 8 #define STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_ 9 10 #include <stdint.h> 11 #include <string> 12 #include <cstring> 13 #include "base/atomicops.h" 14 #include "base/basictypes.h" 15 #include "base/synchronization/condition_variable.h" 16 #include "base/synchronization/lock.h" 17 18 // Linux's ThreadIdentifier() needs this. 19 #if defined(OS_LINUX) 20 # include <linux/unistd.h> 21 #endif 22 23 #if defined(OS_WIN) 24 #define snprintf _snprintf 25 #if !defined(__clang__) 26 # define va_copy(a, b) do { (a) = (b); } while (0) 27 #endif 28 #endif 29 30 namespace leveldb { 31 namespace port { 32 33 // Chromium only supports little endian. 34 static const bool kLittleEndian = true; 35 36 class Mutex { 37 public: 38 Mutex(); 39 ~Mutex(); 40 void Lock(); 41 void Unlock(); 42 void AssertHeld(); 43 44 private: 45 base::Lock mu_; 46 47 friend class CondVar; 48 DISALLOW_COPY_AND_ASSIGN(Mutex); 49 }; 50 51 class CondVar { 52 public: 53 explicit CondVar(Mutex* mu); 54 ~CondVar(); 55 void Wait(); 56 void Signal(); 57 void SignalAll(); 58 59 private: 60 base::ConditionVariable cv_; 61 62 DISALLOW_COPY_AND_ASSIGN(CondVar); 63 }; 64 65 class AtomicPointer { 66 private: 67 typedef base::subtle::AtomicWord Rep; 68 Rep rep_; 69 public: 70 AtomicPointer() { } 71 explicit AtomicPointer(void* p) : rep_(reinterpret_cast<Rep>(p)) {} 72 inline void* Acquire_Load() const { 73 return reinterpret_cast<void*>(::base::subtle::Acquire_Load(&rep_)); 74 } 75 inline void Release_Store(void* v) { 76 ::base::subtle::Release_Store(&rep_, reinterpret_cast<Rep>(v)); 77 } 78 inline void* NoBarrier_Load() const { 79 return reinterpret_cast<void*>(::base::subtle::NoBarrier_Load(&rep_)); 80 } 81 inline void NoBarrier_Store(void* v) { 82 ::base::subtle::NoBarrier_Store(&rep_, reinterpret_cast<Rep>(v)); 83 } 84 }; 85 86 // Implementation of OnceType and InitOnce() pair, this is equivalent to 87 // pthread_once_t and pthread_once(). 88 typedef ::base::subtle::Atomic32 OnceType; 89 90 enum { 91 ONCE_STATE_UNINITIALIZED = 0, 92 ONCE_STATE_EXECUTING_CLOSURE = 1, 93 ONCE_STATE_DONE = 2 94 }; 95 96 #define LEVELDB_ONCE_INIT leveldb::port::ONCE_STATE_UNINITIALIZED 97 98 // slow code path 99 void InitOnceImpl(OnceType* once, void (*initializer)()); 100 101 static inline void InitOnce(OnceType* once, void (*initializer)()) { 102 if (::base::subtle::Acquire_Load(once) != ONCE_STATE_DONE) 103 InitOnceImpl(once, initializer); 104 } 105 106 bool Snappy_Compress(const char* input, size_t input_length, 107 std::string* output); 108 bool Snappy_GetUncompressedLength(const char* input, size_t length, 109 size_t* result); 110 bool Snappy_Uncompress(const char* input_data, size_t input_length, 111 char* output); 112 113 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) { 114 return false; 115 } 116 117 } 118 } 119 120 #endif // STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_ 121