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.
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)
17 #elif defined(OS_SOLARIS)
18 #include <sys/isa_defs.h>
20 #define PLATFORM_IS_LITTLE_ENDIAN true
22 #define PLATFORM_IS_LITTLE_ENDIAN false
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
39 #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
50 #include "port/atomic_pointer.h"
52 #ifndef PLATFORM_IS_LITTLE_ENDIAN
53 #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
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
65 #if defined(OS_FREEBSD) ||\
66 defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
67 // Use fsync() on platforms without fdatasync()
68 #define fdatasync fsync
71 #if defined(OS_MACOSX)
72 #define fdatasync(fd) fcntl(fd, F_FULLFSYNC, 0)
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
84 static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
85 #undef PLATFORM_IS_LITTLE_ENDIAN
104 void operator=(const Mutex&);
109 explicit CondVar(Mutex* mu);
119 typedef pthread_once_t OnceType;
120 #define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
121 extern void InitOnce(OnceType* once, void (*initializer)());
123 inline bool Snappy_Compress(const char* input, size_t length,
124 ::std::string* output) {
126 output->resize(snappy::MaxCompressedLength(length));
128 snappy::RawCompress(input, length, &(*output)[0], &outlen);
129 output->resize(outlen);
136 inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
139 return snappy::GetUncompressedLength(input, length, result);
145 inline bool Snappy_Uncompress(const char* input, size_t length,
148 return snappy::RawUncompress(input, length, output);
154 inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
159 } // namespace leveldb
161 #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_