port_posix.h 4.0 KB

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