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