129 satır
2.9 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. #if defined(OS_MACOSX)
  9. #include <machine/endian.h>
  10. #elif defined(OS_SOLARIS)
  11. #include <sys/isa_defs.h>
  12. #ifdef _LITTLE_ENDIAN
  13. #define LITTLE_ENDIAN
  14. #else
  15. #define BIG_ENDIAN
  16. #endif
  17. #elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
  18. defined(OS_DRAGONFLYBSD)
  19. #include <sys/types.h>
  20. #include <sys/endian.h>
  21. #else
  22. #include <endian.h>
  23. #endif
  24. #include <pthread.h>
  25. #ifdef SNAPPY
  26. #include <snappy.h>
  27. #endif
  28. #include <stdint.h>
  29. #include <string>
  30. #include "port/atomic_pointer.h"
  31. #ifdef LITTLE_ENDIAN
  32. #define IS_LITTLE_ENDIAN true
  33. #else
  34. #define IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
  35. #endif
  36. #if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
  37. defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
  38. // Use fread/fwrite/fflush on platforms without _unlocked variants
  39. #define fread_unlocked fread
  40. #define fwrite_unlocked fwrite
  41. #define fflush_unlocked fflush
  42. #endif
  43. #if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
  44. defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
  45. // Use fsync() on platforms without fdatasync()
  46. #define fdatasync fsync
  47. #endif
  48. namespace leveldb {
  49. namespace port {
  50. static const bool kLittleEndian = IS_LITTLE_ENDIAN;
  51. class CondVar;
  52. class Mutex {
  53. public:
  54. Mutex();
  55. ~Mutex();
  56. void Lock();
  57. void Unlock();
  58. void AssertHeld() { }
  59. private:
  60. friend class CondVar;
  61. pthread_mutex_t mu_;
  62. // No copying
  63. Mutex(const Mutex&);
  64. void operator=(const Mutex&);
  65. };
  66. class CondVar {
  67. public:
  68. explicit CondVar(Mutex* mu);
  69. ~CondVar();
  70. void Wait();
  71. void Signal();
  72. void SignalAll();
  73. private:
  74. pthread_cond_t cv_;
  75. Mutex* mu_;
  76. };
  77. inline bool Snappy_Compress(const char* input, size_t length,
  78. ::std::string* output) {
  79. #ifdef SNAPPY
  80. output->resize(snappy::MaxCompressedLength(length));
  81. size_t outlen;
  82. snappy::RawCompress(input, length, &(*output)[0], &outlen);
  83. output->resize(outlen);
  84. return true;
  85. #endif
  86. return false;
  87. }
  88. inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
  89. size_t* result) {
  90. #ifdef SNAPPY
  91. return snappy::GetUncompressedLength(input, length, result);
  92. #else
  93. return false;
  94. #endif
  95. }
  96. inline bool Snappy_Uncompress(const char* input, size_t length,
  97. char* output) {
  98. #ifdef SNAPPY
  99. return snappy::RawUncompress(input, length, output);
  100. #else
  101. return false;
  102. #endif
  103. }
  104. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  105. return false;
  106. }
  107. } // namespace port
  108. } // namespace leveldb
  109. #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_