port_posix.h 2.9 KB
Newer Older
J
jorlow@chromium.org 已提交
1 2 3 4 5 6 7 8 9
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// See port_example.h for documentation for the following types/functions.

#ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_
#define STORAGE_LEVELDB_PORT_PORT_POSIX_H_

10
#if defined(OS_MACOSX)
11 12 13 14 15 16 17 18
  #include <machine/endian.h>
#elif defined(OS_SOLARIS)
  #include <sys/isa_defs.h>
  #ifdef _LITTLE_ENDIAN
    #define LITTLE_ENDIAN
  #else
    #define BIG_ENDIAN
  #endif
19 20 21 22
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
      defined(OS_DRAGONFLYBSD)
  #include <sys/types.h>
  #include <sys/endian.h>
23 24 25
#else
  #include <endian.h>
#endif
J
jorlow@chromium.org 已提交
26
#include <pthread.h>
27 28 29
#ifdef SNAPPY
#include <snappy.h>
#endif
J
jorlow@chromium.org 已提交
30 31
#include <stdint.h>
#include <string>
32 33 34 35 36 37 38 39
#include "port/atomic_pointer.h"

#ifdef LITTLE_ENDIAN
#define IS_LITTLE_ENDIAN true
#else
#define IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#endif

40 41 42
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
    defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fread/fwrite/fflush on platforms without _unlocked variants
43 44 45 46 47
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif

48 49 50
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
    defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
51 52
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
53 54 55 56

namespace leveldb {
namespace port {

57
static const bool kLittleEndian = IS_LITTLE_ENDIAN;
J
jorlow@chromium.org 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

class CondVar;

class Mutex {
 public:
  Mutex();
  ~Mutex();

  void Lock();
  void Unlock();
  void AssertHeld() { }

 private:
  friend class CondVar;
  pthread_mutex_t mu_;

  // No copying
  Mutex(const Mutex&);
  void operator=(const Mutex&);
};

class CondVar {
 public:
  explicit CondVar(Mutex* mu);
  ~CondVar();
  void Wait();
  void Signal();
  void SignalAll();
 private:
  pthread_cond_t cv_;
  Mutex* mu_;
};

91
inline bool Snappy_Compress(const char* input, size_t length,
92
                            ::std::string* output) {
93
#ifdef SNAPPY
94
  output->resize(snappy::MaxCompressedLength(length));
95
  size_t outlen;
96
  snappy::RawCompress(input, length, &(*output)[0], &outlen);
97 98 99 100
  output->resize(outlen);
  return true;
#endif

J
jorlow@chromium.org 已提交
101
  return false;
J
jorlow@chromium.org 已提交
102 103
}

104 105
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
                                         size_t* result) {
106
#ifdef SNAPPY
107 108 109
  return snappy::GetUncompressedLength(input, length, result);
#else
  return false;
110
#endif
111
}
112

113 114 115 116 117
inline bool Snappy_Uncompress(const char* input, size_t length,
                              char* output) {
#ifdef SNAPPY
  return snappy::RawUncompress(input, length, output);
#else
J
jorlow@chromium.org 已提交
118
  return false;
119
#endif
J
jorlow@chromium.org 已提交
120 121 122 123 124 125
}

inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  return false;
}

126 127
} // namespace port
} // namespace leveldb
J
jorlow@chromium.org 已提交
128 129

#endif  // STORAGE_LEVELDB_PORT_PORT_POSIX_H_