Line data Source code
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 :
81 : static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
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 0 : 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_;
113 : Mutex* mu_;
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 0 : 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 0 : return false;
131 : }
132 :
133 0 : 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 0 : return false;
139 : #endif
140 : }
141 :
142 0 : 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 0 : return false;
148 : #endif
149 : }
150 :
151 : inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
152 : return false;
153 : }
154 :
155 : } // namespace port
156 : } // namespace leveldb
157 :
158 : #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|