作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Não pode escolher mais do que 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.

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