小组成员:谢瑞阳、徐翔宇
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.

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