作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

131 lines
3.0 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_ANDROID_H_
  7. #define STORAGE_LEVELDB_PORT_PORT_ANDROID_H_
  8. #include <endian.h>
  9. #include <pthread.h>
  10. #include <stdint.h>
  11. #include <sha1.h>
  12. #include <cstdatomic>
  13. #include <string>
  14. #include <cctype>
  15. extern "C" {
  16. size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d);
  17. size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d);
  18. int fflush_unlocked(FILE *f);
  19. int fdatasync (int fd);
  20. }
  21. namespace leveldb {
  22. namespace port {
  23. static const bool kLittleEndian = __BYTE_ORDER == __LITTLE_ENDIAN;
  24. class CondVar;
  25. class Mutex {
  26. public:
  27. Mutex();
  28. ~Mutex();
  29. void Lock();
  30. void Unlock();
  31. void AssertHeld() {
  32. //TODO(gabor): How can I implement this?
  33. }
  34. private:
  35. friend class CondVar;
  36. pthread_mutex_t mu_;
  37. // No copying
  38. Mutex(const Mutex&);
  39. void operator=(const Mutex&);
  40. };
  41. class CondVar {
  42. public:
  43. explicit CondVar(Mutex* mu);
  44. ~CondVar();
  45. void Wait();
  46. void Signal();
  47. void SignalAll();
  48. private:
  49. Mutex* mu_;
  50. pthread_cond_t cv_;
  51. };
  52. // Storage for a lock-free pointer
  53. class AtomicPointer {
  54. private:
  55. std::atomic<void*> rep_;
  56. public:
  57. AtomicPointer() { }
  58. explicit AtomicPointer(void* v) : rep_(v) { }
  59. inline void* Acquire_Load() const {
  60. return rep_.load(std::memory_order_acquire);
  61. }
  62. inline void Release_Store(void* v) {
  63. rep_.store(v, std::memory_order_release);
  64. }
  65. inline void* NoBarrier_Load() const {
  66. return rep_.load(std::memory_order_relaxed);
  67. }
  68. inline void NoBarrier_Store(void* v) {
  69. rep_.store(v, std::memory_order_relaxed);
  70. }
  71. };
  72. /**
  73. * TODO(gabor): Implement actual compress
  74. * This is a hack - it just copies input to output.
  75. * No actual compression occurs.
  76. */
  77. inline void Lightweight_Compress(
  78. const char* input,
  79. size_t input_length,
  80. std::string* output) {
  81. output->copy((char*)input,0,input_length);
  82. }
  83. /**
  84. * TODO(gabor): Implement actual compress
  85. * This is a hack - it just copies input to output.
  86. * No actual uncompression occurs.
  87. */
  88. inline bool Lightweight_Uncompress(
  89. const char* input_data,
  90. size_t input_length,
  91. std::string* output) {
  92. output->copy((char*)input_data,0,input_length);
  93. return (bool)1;
  94. }
  95. inline void SHA1_Hash(const char* data, size_t len, char* hash_array) {
  96. SHA1_CTX sha1_ctx;
  97. SHA1Init(&sha1_ctx);
  98. SHA1Update(&sha1_ctx, (const u_char*)data, len);
  99. SHA1Final((u_char*)hash_array, &sha1_ctx);
  100. }
  101. inline uint64_t ThreadIdentifier() {
  102. pthread_t tid = pthread_self();
  103. uint64_t r = 0;
  104. memcpy(&r, &tid, sizeof(r) < sizeof(tid) ? sizeof(r) : sizeof(tid));
  105. return r;
  106. }
  107. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  108. return false;
  109. }
  110. }
  111. }
  112. #endif // STORAGE_LEVELDB_PORT_PORT_ANDROID_H_