Squashed 'src/leveldb/' changes from a31c8aa40..196962ff0
[bitcoinplatinum.git] / port / port_posix.h
blob7e8213b22ecaf216067040a818d4cdb786d8ca98
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.
7 #ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_
8 #define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
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
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"
49 #ifndef PLATFORM_IS_LITTLE_ENDIAN
50 #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
51 #endif
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
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
68 #if defined(OS_MACOSX)
69 #define fdatasync(fd) fcntl(fd, F_FULLFSYNC, 0)
70 #endif
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
78 namespace leveldb {
79 namespace port {
81 static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
82 #undef PLATFORM_IS_LITTLE_ENDIAN
84 class CondVar;
86 class Mutex {
87 public:
88 Mutex();
89 ~Mutex();
91 void Lock();
92 void Unlock();
93 void AssertHeld() { }
95 private:
96 friend class CondVar;
97 pthread_mutex_t mu_;
99 // No copying
100 Mutex(const Mutex&);
101 void operator=(const Mutex&);
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_;
113 Mutex* mu_;
116 typedef pthread_once_t OnceType;
117 #define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
118 extern void InitOnce(OnceType* once, void (*initializer)());
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
130 return false;
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
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
151 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
152 return false;
155 uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
157 } // namespace port
158 } // namespace leveldb
160 #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_