作者: 谢瑞阳 10225101483 徐翔宇 10225101535

158 lines
3.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_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. // Collapse the plethora of ARM flavors available to an easier to manage set
  16. // Defs reference is at https://wiki.edubuntu.org/ARM/Thumb2PortingHowto
  17. #if defined(__ARM_ARCH_6__) || \
  18. defined(__ARM_ARCH_6J__) || \
  19. defined(__ARM_ARCH_6K__) || \
  20. defined(__ARM_ARCH_6Z__) || \
  21. defined(__ARM_ARCH_6T2__) || \
  22. defined(__ARM_ARCH_6ZK__) || \
  23. defined(__ARM_ARCH_7__) || \
  24. defined(__ARM_ARCH_7R__) || \
  25. defined(__ARM_ARCH_7A__)
  26. #define ARMV6_OR_7 1
  27. #endif
  28. extern "C" {
  29. size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d);
  30. size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d);
  31. int fflush_unlocked(FILE *f);
  32. int fdatasync (int fd);
  33. }
  34. namespace leveldb {
  35. namespace port {
  36. static const bool kLittleEndian = __BYTE_ORDER == __LITTLE_ENDIAN;
  37. class CondVar;
  38. class Mutex {
  39. public:
  40. Mutex();
  41. ~Mutex();
  42. void Lock();
  43. void Unlock();
  44. void AssertHeld() {
  45. //TODO(gabor): How can I implement this?
  46. }
  47. private:
  48. friend class CondVar;
  49. pthread_mutex_t mu_;
  50. // No copying
  51. Mutex(const Mutex&);
  52. void operator=(const Mutex&);
  53. };
  54. class CondVar {
  55. public:
  56. explicit CondVar(Mutex* mu);
  57. ~CondVar();
  58. void Wait();
  59. void Signal();
  60. void SignalAll();
  61. private:
  62. Mutex* mu_;
  63. pthread_cond_t cv_;
  64. };
  65. #ifndef ARMV6_OR_7
  66. // On ARM chipsets <V6, 0xffff0fa0 is the hard coded address of a
  67. // memory barrier function provided by the kernel.
  68. typedef void (*LinuxKernelMemoryBarrierFunc)(void);
  69. LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier ATTRIBUTE_WEAK =
  70. (LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
  71. #endif
  72. // Storage for a lock-free pointer
  73. class AtomicPointer {
  74. private:
  75. void* rep_;
  76. inline void MemoryBarrier() const {
  77. // TODO(gabor): This only works on Android instruction sets >= V6
  78. #ifdef ARMV6_OR_7
  79. __asm__ __volatile__("dmb" : : : "memory");
  80. #else
  81. pLinuxKernelMemoryBarrier();
  82. #endif
  83. }
  84. public:
  85. AtomicPointer() { }
  86. explicit AtomicPointer(void* v) : rep_(v) { }
  87. inline void* Acquire_Load() const {
  88. void* r = rep_;
  89. MemoryBarrier();
  90. return r;
  91. }
  92. inline void Release_Store(void* v) {
  93. MemoryBarrier();
  94. rep_ = v;
  95. }
  96. inline void* NoBarrier_Load() const {
  97. void* r = rep_;
  98. return r;
  99. }
  100. inline void NoBarrier_Store(void* v) {
  101. rep_ = v;
  102. }
  103. };
  104. // TODO(gabor): Implement compress
  105. inline bool Snappy_Compress(
  106. const char* input,
  107. size_t input_length,
  108. std::string* output) {
  109. return false;
  110. }
  111. // TODO(gabor): Implement uncompress
  112. inline bool Snappy_Uncompress(
  113. const char* input_data,
  114. size_t input_length,
  115. std::string* output) {
  116. return false;
  117. }
  118. inline void SHA1_Hash(const char* data, size_t len, char* hash_array) {
  119. SHA1_CTX sha1_ctx;
  120. SHA1Init(&sha1_ctx);
  121. SHA1Update(&sha1_ctx, (const u_char*)data, len);
  122. SHA1Final((u_char*)hash_array, &sha1_ctx);
  123. }
  124. inline uint64_t ThreadIdentifier() {
  125. pthread_t tid = pthread_self();
  126. uint64_t r = 0;
  127. memcpy(&r, &tid, sizeof(r) < sizeof(tid) ? sizeof(r) : sizeof(tid));
  128. return r;
  129. }
  130. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  131. return false;
  132. }
  133. }
  134. }
  135. #endif // STORAGE_LEVELDB_PORT_PORT_ANDROID_H_