port_posix.h 2.6 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) || defined(OS_FREEBSD)
11 12 13 14 15 16 17 18 19 20 21
  #include <machine/endian.h>
#elif defined(OS_SOLARIS)
  #include <sys/isa_defs.h>
  #ifdef _LITTLE_ENDIAN
    #define LITTLE_ENDIAN
  #else
    #define BIG_ENDIAN
  #endif
#else
  #include <endian.h>
#endif
J
jorlow@chromium.org 已提交
22
#include <pthread.h>
23 24 25
#ifdef SNAPPY
#include <snappy.h>
#endif
J
jorlow@chromium.org 已提交
26 27
#include <stdint.h>
#include <string>
28 29 30 31 32 33 34 35
#include "port/atomic_pointer.h"

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

36
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD)
37 38 39 40 41
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif

42
#if defined(OS_MACOSX) || defined(OS_FREEBSD)
43 44
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
45 46 47 48

namespace leveldb {
namespace port {

49
static const bool kLittleEndian = IS_LITTLE_ENDIAN;
J
jorlow@chromium.org 已提交
50 51 52 53 54 55 56 57 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

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_;
};

83
inline bool Snappy_Compress(const char* input, size_t length,
84
                            ::std::string* output) {
85
#ifdef SNAPPY
86
  output->resize(snappy::MaxCompressedLength(length));
87
  size_t outlen;
88
  snappy::RawCompress(input, length, &(*output)[0], &outlen);
89 90 91 92
  output->resize(outlen);
  return true;
#endif

J
jorlow@chromium.org 已提交
93
  return false;
J
jorlow@chromium.org 已提交
94 95
}

96 97
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
                                         size_t* result) {
98
#ifdef SNAPPY
99 100 101
  return snappy::GetUncompressedLength(input, length, result);
#else
  return false;
102
#endif
103
}
104

105 106 107 108 109
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 已提交
110
  return false;
111
#endif
J
jorlow@chromium.org 已提交
112 113 114 115 116 117
}

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

118 119
} // namespace port
} // namespace leveldb
J
jorlow@chromium.org 已提交
120 121

#endif  // STORAGE_LEVELDB_PORT_PORT_POSIX_H_