Home | History | Annotate | Download | only in port
      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 typedef SSIZE_T ssize_t;
     26 #if !defined(__clang__) && _MSC_VER <= 1700
     27 # define va_copy(a, b) do { (a) = (b); } while (0)
     28 #endif
     29 #endif
     30 
     31 namespace leveldb {
     32 namespace port {
     33 
     34 // Chromium only supports little endian.
     35 static const bool kLittleEndian = true;
     36 
     37 class Mutex {
     38  public:
     39   Mutex();
     40   ~Mutex();
     41   void Lock();
     42   void Unlock();
     43   void AssertHeld();
     44 
     45  private:
     46   base::Lock mu_;
     47 
     48   friend class CondVar;
     49   DISALLOW_COPY_AND_ASSIGN(Mutex);
     50 };
     51 
     52 class CondVar {
     53  public:
     54   explicit CondVar(Mutex* mu);
     55   ~CondVar();
     56   void Wait();
     57   void Signal();
     58   void SignalAll();
     59 
     60  private:
     61   base::ConditionVariable cv_;
     62 
     63   DISALLOW_COPY_AND_ASSIGN(CondVar);
     64 };
     65 
     66 class AtomicPointer {
     67  private:
     68   typedef base::subtle::AtomicWord Rep;
     69   Rep rep_;
     70  public:
     71   AtomicPointer() { }
     72   explicit AtomicPointer(void* p) : rep_(reinterpret_cast<Rep>(p)) {}
     73   inline void* Acquire_Load() const {
     74     return reinterpret_cast<void*>(::base::subtle::Acquire_Load(&rep_));
     75   }
     76   inline void Release_Store(void* v) {
     77     ::base::subtle::Release_Store(&rep_, reinterpret_cast<Rep>(v));
     78   }
     79   inline void* NoBarrier_Load() const {
     80     return reinterpret_cast<void*>(::base::subtle::NoBarrier_Load(&rep_));
     81   }
     82   inline void NoBarrier_Store(void* v) {
     83     ::base::subtle::NoBarrier_Store(&rep_, reinterpret_cast<Rep>(v));
     84   }
     85 };
     86 
     87 // Implementation of OnceType and InitOnce() pair, this is equivalent to
     88 // pthread_once_t and pthread_once().
     89 typedef ::base::subtle::Atomic32 OnceType;
     90 
     91 enum {
     92   ONCE_STATE_UNINITIALIZED = 0,
     93   ONCE_STATE_EXECUTING_CLOSURE = 1,
     94   ONCE_STATE_DONE = 2
     95 };
     96 
     97 #define LEVELDB_ONCE_INIT   leveldb::port::ONCE_STATE_UNINITIALIZED
     98 
     99 // slow code path
    100 void InitOnceImpl(OnceType* once, void (*initializer)());
    101 
    102 static inline void InitOnce(OnceType* once, void (*initializer)()) {
    103   if (::base::subtle::Acquire_Load(once) != ONCE_STATE_DONE)
    104     InitOnceImpl(once, initializer);
    105 }
    106 
    107 bool Snappy_Compress(const char* input, size_t input_length,
    108                      std::string* output);
    109 bool Snappy_GetUncompressedLength(const char* input, size_t length,
    110                                   size_t* result);
    111 bool Snappy_Uncompress(const char* input_data, size_t input_length,
    112                        char* output);
    113 
    114 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
    115   return false;
    116 }
    117 
    118 }
    119 }
    120 
    121 #endif  // STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_
    122