port_example.h raw

   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  // This file contains the specification, but not the implementations,
   6  // of the types/operations/etc. that should be defined by a platform
   7  // specific port_<platform>.h file.  Use this file as a reference for
   8  // how to port this package to a new platform.
   9  
  10  #ifndef STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
  11  #define STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
  12  
  13  #include "port/thread_annotations.h"
  14  
  15  namespace leveldb {
  16  namespace port {
  17  
  18  // TODO(jorlow): Many of these belong more in the environment class rather than
  19  //               here. We should try moving them and see if it affects perf.
  20  
  21  // ------------------ Threading -------------------
  22  
  23  // A Mutex represents an exclusive lock.
  24  class LOCKABLE Mutex {
  25   public:
  26    Mutex();
  27    ~Mutex();
  28  
  29    // Lock the mutex.  Waits until other lockers have exited.
  30    // Will deadlock if the mutex is already locked by this thread.
  31    void Lock() EXCLUSIVE_LOCK_FUNCTION();
  32  
  33    // Unlock the mutex.
  34    // REQUIRES: This mutex was locked by this thread.
  35    void Unlock() UNLOCK_FUNCTION();
  36  
  37    // Optionally crash if this thread does not hold this mutex.
  38    // The implementation must be fast, especially if NDEBUG is
  39    // defined.  The implementation is allowed to skip all checks.
  40    void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
  41  };
  42  
  43  class CondVar {
  44   public:
  45    explicit CondVar(Mutex* mu);
  46    ~CondVar();
  47  
  48    // Atomically release *mu and block on this condition variable until
  49    // either a call to SignalAll(), or a call to Signal() that picks
  50    // this thread to wakeup.
  51    // REQUIRES: this thread holds *mu
  52    void Wait();
  53  
  54    // If there are some threads waiting, wake up at least one of them.
  55    void Signal();
  56  
  57    // Wake up all waiting threads.
  58    void SignallAll();
  59  };
  60  
  61  // ------------------ Compression -------------------
  62  
  63  // Store the snappy compression of "input[0,input_length-1]" in *output.
  64  // Returns false if snappy is not supported by this port.
  65  bool Snappy_Compress(const char* input, size_t input_length,
  66                       std::string* output);
  67  
  68  // If input[0,input_length-1] looks like a valid snappy compressed
  69  // buffer, store the size of the uncompressed data in *result and
  70  // return true.  Else return false.
  71  bool Snappy_GetUncompressedLength(const char* input, size_t length,
  72                                    size_t* result);
  73  
  74  // Attempt to snappy uncompress input[0,input_length-1] into *output.
  75  // Returns true if successful, false if the input is invalid lightweight
  76  // compressed data.
  77  //
  78  // REQUIRES: at least the first "n" bytes of output[] must be writable
  79  // where "n" is the result of a successful call to
  80  // Snappy_GetUncompressedLength.
  81  bool Snappy_Uncompress(const char* input_data, size_t input_length,
  82                         char* output);
  83  
  84  // ------------------ Miscellaneous -------------------
  85  
  86  // If heap profiling is not supported, returns false.
  87  // Else repeatedly calls (*func)(arg, data, n) and then returns true.
  88  // The concatenation of all "data[0,n-1]" fragments is the heap profile.
  89  bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
  90  
  91  // Extend the CRC to include the first n bytes of buf.
  92  //
  93  // Returns zero if the CRC cannot be extended using acceleration, else returns
  94  // the newly extended CRC value (which may also be zero).
  95  uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
  96  
  97  }  // namespace port
  98  }  // namespace leveldb
  99  
 100  #endif  // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
 101