Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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