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) || 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
36 #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
47 #include "port/atomic_pointer.h"
49 #ifndef PLATFORM_IS_LITTLE_ENDIAN
50 #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
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
62 #if defined(OS_FREEBSD) ||\
63 defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
64 // Use fsync() on platforms without fdatasync()
65 #define fdatasync fsync
68 #if defined(OS_MACOSX)
69 #define fdatasync(fd) fcntl(fd, F_FULLFSYNC, 0)
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
81 static const bool kLittleEndian
= PLATFORM_IS_LITTLE_ENDIAN
;
82 #undef PLATFORM_IS_LITTLE_ENDIAN
101 void operator=(const Mutex
&);
106 explicit CondVar(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
) {
123 output
->resize(snappy::MaxCompressedLength(length
));
125 snappy::RawCompress(input
, length
, &(*output
)[0], &outlen
);
126 output
->resize(outlen
);
133 inline bool Snappy_GetUncompressedLength(const char* input
, size_t length
,
136 return snappy::GetUncompressedLength(input
, length
, result
);
142 inline bool Snappy_Uncompress(const char* input
, size_t length
,
145 return snappy::RawUncompress(input
, length
, output
);
151 inline bool GetHeapProfile(void (*func
)(void*, const char*, int), void* arg
) {
155 bool HasAcceleratedCRC32C();
156 uint32_t AcceleratedCRC32C(uint32_t crc
, const char* buf
, size_t size
);
159 } // namespace leveldb
161 #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_