Bitcoin
port_posix.h
Go to the documentation of this file.
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_POSIX_H_
8 #define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
9 
10 #undef PLATFORM_IS_LITTLE_ENDIAN
11 #if defined(OS_MACOSX)
12  #include <machine/endian.h>
13  #if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
14  #define PLATFORM_IS_LITTLE_ENDIAN \
15  (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
16  #endif
17 #elif defined(OS_SOLARIS)
18  #include <sys/isa_defs.h>
19  #ifdef _LITTLE_ENDIAN
20  #define PLATFORM_IS_LITTLE_ENDIAN true
21  #else
22  #define PLATFORM_IS_LITTLE_ENDIAN false
23  #endif
24 #elif defined(OS_FREEBSD) || defined(OS_OPENBSD) ||\
25  defined(OS_NETBSD) || defined(OS_DRAGONFLYBSD)
26  #include <sys/types.h>
27  #include <sys/endian.h>
28  #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
29 #elif defined(OS_HPUX)
30  #define PLATFORM_IS_LITTLE_ENDIAN false
31 #elif defined(OS_ANDROID)
32  // Due to a bug in the NDK x86 <sys/endian.h> definition,
33  // _BYTE_ORDER must be used instead of __BYTE_ORDER on Android.
34  // See http://code.google.com/p/android/issues/detail?id=39824
35  #include <endian.h>
36  #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
37 #else
38  #include <endian.h>
39 #endif
40 
41 #include <pthread.h>
42 #ifdef SNAPPY
43 #include <snappy.h>
44 #endif
45 #include <stdint.h>
46 #include <string>
47 #include "port/atomic_pointer.h"
48 
49 #ifndef PLATFORM_IS_LITTLE_ENDIAN
50 #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
51 #endif
52 
53 #if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
54  defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
55  defined(OS_ANDROID) || defined(OS_HPUX) || defined(CYGWIN)
56 // Use fread/fwrite/fflush on platforms without _unlocked variants
57 #define fread_unlocked fread
58 #define fwrite_unlocked fwrite
59 #define fflush_unlocked fflush
60 #endif
61 
62 #if defined(OS_FREEBSD) ||\
63  defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
64 // Use fsync() on platforms without fdatasync()
65 #define fdatasync fsync
66 #endif
67 
68 #if defined(OS_MACOSX)
69 #define fdatasync(fd) fcntl(fd, F_FULLFSYNC, 0)
70 #endif
71 
72 #if defined(OS_ANDROID) && __ANDROID_API__ < 9
73 // fdatasync() was only introduced in API level 9 on Android. Use fsync()
74 // when targetting older platforms.
75 #define fdatasync fsync
76 #endif
77 
78 namespace leveldb {
79 namespace port {
80 
82 #undef PLATFORM_IS_LITTLE_ENDIAN
83 
84 class CondVar;
85 
86 class Mutex {
87  public:
88  Mutex();
89  ~Mutex();
90 
91  void Lock();
92  void Unlock();
93  void AssertHeld() { }
94 
95  private:
96  friend class CondVar;
97  pthread_mutex_t mu_;
98 
99  // No copying
100  Mutex(const Mutex&);
101  void operator=(const Mutex&);
102 };
103 
104 class CondVar {
105  public:
106  explicit CondVar(Mutex* mu);
107  ~CondVar();
108  void Wait();
109  void Signal();
110  void SignalAll();
111  private:
112  pthread_cond_t cv_;
114 };
115 
116 typedef pthread_once_t OnceType;
117 #define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
118 extern void InitOnce(OnceType* once, void (*initializer)());
119 
120 inline bool Snappy_Compress(const char* input, size_t length,
121  ::std::string* output) {
122 #ifdef SNAPPY
123  output->resize(snappy::MaxCompressedLength(length));
124  size_t outlen;
125  snappy::RawCompress(input, length, &(*output)[0], &outlen);
126  output->resize(outlen);
127  return true;
128 #endif
129 
130  return false;
131 }
132 
133 inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
134  size_t* result) {
135 #ifdef SNAPPY
136  return snappy::GetUncompressedLength(input, length, result);
137 #else
138  return false;
139 #endif
140 }
141 
142 inline bool Snappy_Uncompress(const char* input, size_t length,
143  char* output) {
144 #ifdef SNAPPY
145  return snappy::RawUncompress(input, length, output);
146 #else
147  return false;
148 #endif
149 }
150 
151 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
152  return false;
153 }
154 
155 bool HasAcceleratedCRC32C();
156 uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
157 
158 } // namespace port
159 } // namespace leveldb
160 
161 #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_
pthread_mutex_t mu_
Definition: port_posix.h:97
Definition: autocompact_test.cc:11
void AssertHeld()
Definition: port_posix.h:93
bool Snappy_Uncompress(const char *input_data, size_t input_length, char *output)
Definition: port_posix.h:142
#define PLATFORM_IS_LITTLE_ENDIAN
Definition: port_posix.h:50
CondVar(Mutex *mu)
Definition: port_posix.cc:33
void Unlock()
Definition: port_posix.cc:31
static port::OnceType once
Definition: comparator.cc:69
~Mutex()
Definition: port_posix.cc:27
Mutex()
Definition: port_posix.cc:25
bool Snappy_GetUncompressedLength(const char *input, size_t length, size_t *result)
Definition: port_posix.h:133
bool HasAcceleratedCRC32C()
Definition: port_posix.cc:56
void Signal()
Definition: port_posix.cc:44
void Wait()
Definition: port_posix.cc:40
static const bool kLittleEndian
Definition: port_example.h:21
void Lock()
Definition: port_posix.cc:29
pthread_cond_t cv_
Definition: port_posix.h:112
unsigned int uint32_t
Definition: stdint.h:21
bool Snappy_Compress(const char *input, size_t input_length, std::string *output)
void InitOnce(port::OnceType *, void(*initializer)())
Definition: port_posix.cc:52
bool GetHeapProfile(void(*func)(void *, const char *, int), void *arg)
Definition: port_posix.h:151
uint32_t AcceleratedCRC32C(uint32_t crc, const char *buf, size_t size)
Definition: port_posix_sse.cc:54
intptr_t OnceType
Definition: port_example.h:69
void SignalAll()
Definition: port_posix.cc:48
Mutex * mu_
Definition: port_posix.h:113
Definition: port_example.h:45
~CondVar()
Definition: port_posix.cc:38
void operator=(const Mutex &)
int port
Definition: zmq_sub.py:37
Definition: port_example.h:26