Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

159 rader
3.8 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 <unistd.h>
  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. // TODO(user): ATTRIBUTE_WEAK is undefined, so this fails to build on
  69. // non-ARMV6_OR_7. We may be able to replace it with __attribute__((weak)) for
  70. // older ARM builds, but x86 builds will require a different memory barrier.
  71. LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier ATTRIBUTE_WEAK =
  72. (LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
  73. #endif
  74. // Storage for a lock-free pointer
  75. class AtomicPointer {
  76. private:
  77. void* rep_;
  78. inline void MemoryBarrier() const {
  79. // TODO(gabor): This only works on Android instruction sets >= V6
  80. #ifdef ARMV6_OR_7
  81. __asm__ __volatile__("dmb" : : : "memory");
  82. #else
  83. pLinuxKernelMemoryBarrier();
  84. #endif
  85. }
  86. public:
  87. AtomicPointer() { }
  88. explicit AtomicPointer(void* v) : rep_(v) { }
  89. inline void* Acquire_Load() const {
  90. void* r = rep_;
  91. MemoryBarrier();
  92. return r;
  93. }
  94. inline void Release_Store(void* v) {
  95. MemoryBarrier();
  96. rep_ = v;
  97. }
  98. inline void* NoBarrier_Load() const {
  99. void* r = rep_;
  100. return r;
  101. }
  102. inline void NoBarrier_Store(void* v) {
  103. rep_ = v;
  104. }
  105. };
  106. // TODO(gabor): Implement compress
  107. inline bool Snappy_Compress(
  108. const char* input,
  109. size_t input_length,
  110. std::string* output) {
  111. return false;
  112. }
  113. // TODO(gabor): Implement uncompress
  114. inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
  115. size_t* result) {
  116. return false;
  117. }
  118. // TODO(gabor): Implement uncompress
  119. inline bool Snappy_Uncompress(
  120. const char* input_data,
  121. size_t input_length,
  122. char* output) {
  123. return false;
  124. }
  125. inline uint64_t ThreadIdentifier() {
  126. pthread_t tid = pthread_self();
  127. uint64_t r = 0;
  128. memcpy(&r, &tid, sizeof(r) < sizeof(tid) ? sizeof(r) : sizeof(tid));
  129. return r;
  130. }
  131. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  132. return false;
  133. }
  134. } // namespace port
  135. } // namespace leveldb
  136. #endif // STORAGE_LEVELDB_PORT_PORT_ANDROID_H_