作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.8 KiB

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