作者: 韩晨旭 10225101440 李畅 10225102463

122 lignes
2.7 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. // TODO(gabor): Implement actual compress
  73. inline bool Snappy_Compress(
  74. const char* input,
  75. size_t input_length,
  76. std::string* output) {
  77. return false;
  78. }
  79. // TODO(gabor): Implement actual uncompress
  80. inline bool Snappy_Uncompress(
  81. const char* input_data,
  82. size_t input_length,
  83. std::string* output) {
  84. return false;
  85. }
  86. inline void SHA1_Hash(const char* data, size_t len, char* hash_array) {
  87. SHA1_CTX sha1_ctx;
  88. SHA1Init(&sha1_ctx);
  89. SHA1Update(&sha1_ctx, (const u_char*)data, len);
  90. SHA1Final((u_char*)hash_array, &sha1_ctx);
  91. }
  92. inline uint64_t ThreadIdentifier() {
  93. pthread_t tid = pthread_self();
  94. uint64_t r = 0;
  95. memcpy(&r, &tid, sizeof(r) < sizeof(tid) ? sizeof(r) : sizeof(tid));
  96. return r;
  97. }
  98. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  99. return false;
  100. }
  101. }
  102. }
  103. #endif // STORAGE_LEVELDB_PORT_PORT_ANDROID_H_