記事ディレクトリ
-
- 1. AndroidThreads.h
- 2.Atomic.h
- 3.ビットセット.h
- 4.ByteOrder.h
- 5.CallStack.h
- 6. 互換性
- 7. 条件.h
- 8. デバッグ.h
- 9. エンディアン.h
- 10. エラー.h
- 11. FastStrcmp.h
- 12. ファイルマップ.h
- 13. Flattenable.h
- 14. ファンクター.h
- 15.ジェンキンスハッシュ.h
- 16.KeyedVector.h
- 17.LightRefBase.h
- 18. リスト.h
- 19.ログ.h
- 20.ルーパー.h
- 21.LruCache.h
- 22. その他
- 23.ミューテックス.h
- 24.NativeHandle.h
- 25. プリンター.h
- 26.ProcessCallStack.h
- 27.PropertyMap.h
- 28.RefBase.h
- 29. RWLock.h
- 30.シングルトン.h
- 31. ソートベクター.h
- 32. ストップウォッチ.h
- 33. 文字列16.h
- 34. 文字列8.h
- 35.ストロングポインター.h
- 36. システムクロック.h
- 37. ThreadDefs.h
- 38. スレッド.h
- 39. スレッド.h
- 40.タイマー.h
- 41.トークナイザー.h
- 42. トレース.h
- 43. TypeHelpers.h
- 44. ユニコード.h
- 45. ベクター.h
- 46.VectorImpl.h
1. AndroidThreads.h
作成、優先順位の設定、名前の設定など、スレッド関連の機能をカプセル化します。c および c++ インターフェイスを提供します。
// c interfaces
extern int androidCreateThread(android_thread_func_t, void *);
extern int androidCreateThreadEtc(android_thread_func_t entryFunction,
void *userData,
const char* threadName,
int32_t threadPriority,
size_t threadStackSize,
android_thread_id_t *threadId);
extern android_thread_id_t androidGetThreadId();
extern int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
void *userData,
const char* threadName,
int32_t threadPriority,
size_t threadStackSize,
android_thread_id_t *threadId);
extern void androidSetThreadName(const char* name);
typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
void *userData,
const char* threadName,
int32_t threadPriority,
size_t threadStackSize,
android_thread_id_t *threadId);
extern void androidSetCreateThreadFunc(android_create_thread_fn func);
extern int androidSetThreadPriority(pid_t tid, int prio);
extern int androidGetThreadPriority(pid_t tid);
// cpp interfaces
inline bool createThread(thread_func_t f, void *a)
inline bool createThreadEtc(thread_func_t entryFunction,
void *userData,
const char* threadName = "android:unnamed_thread",
int32_t threadPriority = PRIORITY_DEFAULT,
size_t threadStackSize = 0,
thread_id_t *threadId = nullptr)
inline thread_id_t getThreadId()
2.Atomic.h
#include <cutils/atomic.h>
3.ビットセット.h
ビット操作ヘルパー関数。
DO NOT USE: std::bitset<32> or std::bitset<64> preferred
4.ByteOrder.h
参照:<android-base/endian.h>
システム/コア/ベース ライブラリの紹介
5.CallStack.h
スレッドのコールスタックを収集して出力します。
参照: ProcessCallStack.h、プロセス内のすべてのスレッド コールスタックを収集します。
class CallStack {
public:
CallStack();
CallStack(const char* logtag, int32_t ignoreDepth = 1);
~CallStack();
void clear() {
mFrameLines.clear(); }
void update(int32_t ignoreDepth = 1, pid_t tid = BACKTRACE_CURRENT_THREAD);
void log(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
const char* prefix = nullptr) const;
void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
String8 toString(const char* prefix = nullptr) const;
void print(Printer& printer) const;
size_t size() const {
return mFrameLines.size(); }
}
6. 互換性
Mac OS には off64_t はありません。デフォルトの off_t は 64 ビットです。互換性適応。
/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
typedef off_t off64_t;
static inline off64_t lseek64(int fd, off64_t offset, int whence) {
return lseek(fd, offset, whence);
}
static inline int ftruncate64(int fd, off64_t length) {
return ftruncate(fd, length);
}
static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset);
static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset);
static inline void* mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset);
7. 条件.h
// DO NOT USE: please use std::condition_variable instead.
8. デバッグ.h
CompileTimeAssert マクロ、レガシー コード。
// 使用しないでください: 代わりに static_assert を使用してください
#ifdef __cplusplus
template<bool> struct CompileTimeAssert;
template<> struct CompileTimeAssert<true> {
};
#define COMPILE_TIME_ASSERT(_exp) \
template class CompileTimeAssert< (_exp) >;
#endif
// DO NOT USE: Please use static_assert instead
#define COMPILE_TIME_ASSERT_FUNCTION_SCOPE(_exp) \
CompileTimeAssert<( _exp )>();
9. エンディアン.h
#include <endian.h>
10. エラー.h
Linux または Win32 に対応する Android のエラー コードを定義します。
enum {
OK = 0, // Preferred constant for checking success.
NO_ERROR = OK, // Deprecated synonym for `OK`. Prefer `OK` because it doesn't conflict with Windows.
UNKNOWN_ERROR = (-2147483647-1), // INT32_MIN value
NO_MEMORY = -ENOMEM,
INVALID_OPERATION = -ENOSYS,
BAD_VALUE = -EINVAL,
// 还有若干error code...
}
std::string statusToString(status_t status);
std::string statusToString(status_t s) {
#define STATUS_CASE(STATUS) \
case STATUS: \
return #STATUS
switch (s) {
STATUS_CASE(OK);
STATUS_CASE(UNKNOWN_ERROR);
...
#undef STATUS_CASE
}
return std::to_string(s) + " (" + strerror(-s) + ")";
}
11. FastStrcmp.h
strcpm関数に最適化されたテンプレート関数。
// Usage is of the form:
// fastcmp<strncmp>(str1, str2, len)
template <int (*cmp)(const char* l, const char* r, const size_t s)>
static inline int fastcmp(const char* l, const char* r, const size_t s);
template <int (*cmp)(const char* l, const char* r, const size_t s)>
static inline int fasticmp(const char* l, const char* r, const size_t s);
template <int (*cmp)(const void* l, const void* r, const size_t s)>
static inline int fastcmp(const void* lv, const void* rv, const size_t s);
template <int (*cmp)(const char* l, const char* r)>
static inline int fastcmp(const char* l, const char* r) {
return (*l != *r) || (__predict_true(*l) && cmp(l + 1, r + 1));
}
template <int (*cmp)(const char* l, const char* r)>
static inline int fasticmp(const char* l, const char* r) {
return (tolower(*l) != tolower(*r)) || (__predict_true(*l) && cmp(l + 1, r + 1));
}
12. ファイルマップ.h
メモリ マップ ファイル (mmap64、munmap) のカプセル化。ファイル全体またはファイルの一部をマップできます。
class FileMap {
public:
FileMap(void);
FileMap(FileMap&& f) noexcept;
FileMap& operator=(FileMap&& f) noexcept;
bool create(const char* origFileName, int fd,
off64_t offset, size_t length, bool readOnly);
~FileMap(void);
const char* getFileName(void) const {
return mFileName; }
void* getDataPtr(void) const {
return mDataPtr; }
size_t getDataLength(void) const {
return mDataLength; }
off64_t getDataOffset(void) const {
return mDataOffset; }
int advise(MapAdvice advice);
}
13. Flattenable.h
// DO NOT USE: please use parcelable instead
// This code is deprecated and will not be supported via AIDL code gen. For data
// to be sent over binder, please use parcelables.
14. ファンクター.h
カスタムファンクター。
// DO NOT USE: please use
// - C++ lambda
// - class with well-defined and specific functionality and semantics
15.ジェンキンスハッシュ.h
いくつかのハッシュ関数。
inline uint32_t JenkinsHashMix(uint32_t hash, uint32_t data);
hash_t JenkinsHashWhiten(uint32_t hash);
uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size);
uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size);
16.KeyedVector.h
DO NOT USE: please use std::map
17.LightRefBase.h
参见:28. RefBase.h
18. リスト.h
使用しないでください:
二重リンクリストをカスタマイズするには std::list を使用してください。
19.ログ.h
#include <log/log.h>
20.ルーパー.h
全て
21.LruCache.h
LruCache を実装するテンプレート クラス。
class LruCache {
public:
explicit LruCache(uint32_t maxCapacity);
virtual ~LruCache();
void setOnEntryRemovedListener(OnEntryRemoved<TKey, TValue>* listener);
size_t size() const;
const TValue& get(const TKey& key);
bool put(const TKey& key, const TValue& value);
bool remove(const TKey& key);
bool removeOldest();
void clear();
const TValue& peekOldestValue();
}
22. その他
システムプロパティ登録リスナーと変更通知機能。
typedef void (*sysprop_change_callback)(void);
void add_sysprop_change_callback(sysprop_change_callback cb, int priority);
void report_sysprop_change();
23.ミューテックス.h
いくつかのスレッド関連の属性マクロ。
Win32 プラットフォームで使用されるミューテックス。
#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
24.NativeHandle.h
Native_handle_t のカプセル化<cutils/native_handle.h>
クラス。
class NativeHandle : public LightRefBase<NativeHandle> {
public:
static sp<NativeHandle> create(native_handle_t* handle, bool ownsHandle);
const native_handle_t* handle() const;
}
25. プリンター.h
データ ストリームを出力するための補助クラス。logcat、ファイル、文字列に出力したり、他のプリンターにプレフィックスを追加したりできます。
// Interface for printing to an arbitrary data stream
class Printer {
public:
virtual void printLine(const char* string = "") = 0;
virtual void printFormatLine(const char* format, ...) __attribute__((format (printf, 2, 3)));
}; // class Printer
// Print to logcat
class LogPrinter : public Printer {
public:
LogPrinter(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
const char* prefix = nullptr,
bool ignoreBlankLines = false);
virtual void printLine(const char* string);
}; // class LogPrinter
// Print to a file descriptor
class FdPrinter : public Printer {
public:
FdPrinter(int fd, unsigned int indent = 0, const char* prefix = nullptr);
virtual void printLine(const char* string);
}; // class FdPrinter
// Print to a String8
class String8Printer : public Printer {
public:
String8Printer(String8* target, const char* prefix = nullptr);
virtual void printLine(const char* string);
}; // class String8Printer
// Print to an existing Printer by adding a prefix to each line
class PrefixPrinter : public Printer {
public:
PrefixPrinter(Printer& printer, const char* prefix);
virtual void printLine(const char* string);
};
26.ProcessCallStack.h
プロセス内のすべてのスレッドのスタック トレースを収集して出力します。
参照: CallStack.h
class ProcessCallStack {
public:
ProcessCallStack();
ProcessCallStack(const ProcessCallStack& rhs);
~ProcessCallStack();
void update();
void log(const char* logtag, android_LogPriority priority = ANDROID_LOG_DEBUG,
const char* prefix = nullptr) const;
void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
String8 toString(const char* prefix = nullptr) const;
void print(Printer& printer) const;
size_t size() const;
}
27.PropertyMap.h
プロパティファイルに対する操作(解析、追加、変更など)、ファイル内容:
property1 = value1
property2 = value2
28.RefBase.h
LightRefBase.h
StrongPointer.h
いくつかの関連するクラスがまとめられています。
1) ファイル内容の構成:
(1) RefBase.h には、クラス RefBase、クラスweakref_type (RefBase内部クラス)、テンプレート クラスwp定義が含まれます。
(2) RefBase.cpp にはweakref_typeの派生クラスweakref_implが定義されており、weakref_typeは一部のインタフェースのみを定義しており、weakref_implが実装クラスとなります。
2) クラス関数と依存関係:
(1) クラス RefBase、強参照基本クラスは、テンプレート クラス sp で使用できる incStrong、decStrong、およびその他のインターフェイスを定義します。RefBase を継承することにより、派生には強力な ref 関連の機能が与えられます。
(2) 弱参照基本クラスであるweakref_typeクラスは、テンプレートクラスwpで使用できるincWeak、decWeakおよびその他のインターフェイスを定義します。
(3) クラスweakref_implはいくつかのデバッグ関連関数を実装しており、デバッグマクロスイッチがオンになっていない場合、関数はweakref_typeと一致します。
(4) テンプレート クラス wp、弱いポインタ テンプレート クラス。RefBase.h で定義されます。
(5) テンプレート クラス sp、ストロング ポインター テンプレート クラス。StrongPointer.h で定義されます。
wp と sp は、参照カウント (それぞれ強参照と弱参照) を持つスマート ポインターです。
(6) LightRefBase (派生テンプレート クラス VirtualLightRefBase) は、RefBase の簡略化された実装です。
(7) RefBase とweakref_type の両方には、相互に変換できるメンバー メソッドがあります。
(8) 強参照と弱参照に基づいて、extendObjectLifetime() メソッドを呼び出して、オブジェクトのライフ サイクルを延長できます。
Binder の実装には多くのアプリケーションがあります。
// RefBase.h
class RefBase
{
public:
void incStrong(const void* id) const;
void decStrong(const void* id) const;
void forceIncStrong(const void* id) const;
class weakref_type
{
public:
RefBase* refBase() const;
void incWeak(const void* id);
void decWeak(const void* id);
bool attemptIncStrong(const void* id);
bool attemptIncWeak(const void* id);
};
weakref_type* createWeak(const void* id) const;
weakref_type* getWeakRefs() const;
protected:
RefBase();
virtual ~RefBase();
void extendObjectLifetime(int32_t mode);
virtual void onFirstRef();
virtual void onLastStrongRef(const void* id);
virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
virtual void onLastWeakRef(const void* id);
private:
friend class weakref_type;
class weakref_impl;
weakref_impl* const mRefs;
};
template <typename T>
class wp
{
public:
typedef typename RefBase::weakref_type weakref_type;
wp(T* other); // NOLINT(implicit)
~wp();
// 构造、赋值构造、移动构造以及操作符定义,省略
void set_object_and_refs(T* other, weakref_type* refs);
sp<T> promote() const;
void clear();
inline weakref_type* get_refs() const {
return m_refs; }
inline T* unsafe_get() const {
return m_ptr; }
// 各种比较操作符,省略
private:
template<typename Y> friend class sp;
template<typename Y> friend class wp;
T* m_ptr;
weakref_type* m_refs;
};
// StrongPointer.h ---------------------------------------------------------------------
template<typename T>
class sp {
public:
sp(T* other); // NOLINT(implicit)
~sp();
// 构造、赋值构造、移动构造以及操作符定义,省略
//! Special optimization for use by ProcessState (and nobody else).
void force_set(T* other);
void clear();
inline T& operator* () const {
return *m_ptr; }
inline T* operator-> () const {
return m_ptr; }
inline T* get() const {
return m_ptr; }
inline explicit operator bool () const {
return m_ptr != nullptr; }
// 内部仅定义==和!=,外部函数定义了包括==,!=等全部比较运算符。
template<typename U>
inline bool operator == (const wp<U>& o) const {
return o == *this;
}
template<typename U>
inline bool operator != (const wp<U>& o) const {
return o != *this;
}
private:
template<typename Y> friend class sp;
template<typename Y> friend class wp;
void set_pointer(T* ptr);
T* m_ptr;
};
// LightRefBase.h ---------------------------------------------------------------------
template <class T>
class LightRefBase
{
public:
inline LightRefBase();
inline void incStrong(__attribute__((unused)) const void* id) const;
inline void decStrong(__attribute__((unused)) const void* id) const;
private:
mutable std::atomic<int32_t> mCount;
};
// This is a wrapper around LightRefBase that simply enforces a virtual
// destructor to eliminate the template requirement of LightRefBase
class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
public:
virtual ~VirtualLightRefBase() = default;
};
29. RWLock.h
ミューテックスの実装、RWLock/AutoRLock/AutoWLock、おそらくレガシー コード。
30.シングルトン.h
// DO NOT USE: Please use scoped static initialization. For instance:
// MyClass& getInstance() {
// static MyClass gInstance(...);
// return gInstance;
// }
31. ソートベクター.h
// DO NOT USE: please use std::set
32. ストップウォッチ.h
カスタム StopWatch、時間を取得し、systemTime(); を呼び出します。
class StopWatch
{
public:
StopWatch(const char* name, int clock = SYSTEM_TIME_MONOTONIC);
~StopWatch();
const char* name() const;
nsecs_t lap();
nsecs_t elapsedTime() const;
void reset();
}
33. 文字列16.h
// DO NOT USE: please use std::u16string
34. 文字列8.h
// DO NOT USE: please use std::string
35.ストロングポインター.h
参见:28. RefBase.h
36. システムクロック.h
Clock_gettime(CLOCK_BOOTTIME, &ts); を呼び出して、システムの起動時間を取得します。
int64_t uptimeMillis();
int64_t elapsedRealtime();
int64_t elapsedRealtimeNano();
37. ThreadDefs.h
一部のスレッド関連の型定義 (thread_id_t、thread_func_t など) および優先順位列挙型定義。
38. スレッド.h
// DO NOT USE: please use std::thread
39. スレッド.h
#include <utils/AndroidThreads.h>
#include <utils/Condition.h>
#include <utils/Errors.h>
#include <utils/Mutex.h>
#include <utils/RWLock.h>
#include <utils/Thread.h>
40.タイマー.h
システム時刻と、さまざまな単位時間のいくつかの変換方法を取得します。
enum {
SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock
SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock
SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock
SYSTEM_TIME_BOOTTIME = 4 // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time
};
// return the system-time according to the specified clock
nsecs_t systemTime(int clock);
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime);
41.トークナイザー.h
テキスト ファイルを 1 行ずつ読み取り、単語 (トークン) に解析します。
42. トレース.h
トレース補助関数。スコープ内で自動的に開始および停止し、コンストラクターで atrace_begin を呼び出し、デストラクターで atrace_end を呼び出します。
のメソッドと組み合わせて使用されます<cutils/trace.h>
。
class ScopedTrace {
public:
inline ScopedTrace(uint64_t tag, const char* name) : mTag(tag) {
atrace_begin(mTag, name);
}
inline ~ScopedTrace() {
atrace_end(mTag);
}
};
43. TypeHelpers.h
一部のカスタム型特性は、C++ への早期の補足となる必要があります。
次のカスタム Vector 実装を支援します。
44. ユニコード.h
char16_t、char32_t比較、長さ取得などの補助機能。
そして、utf8、utf16、utf32変換およびその他の機能。
int strcmp16(const char16_t *, const char16_t *);
int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
size_t strlen16(const char16_t *);
size_t strnlen16(const char16_t *, size_t);
char16_t *strcpy16(char16_t *, const char16_t *);
char16_t *strstr16(const char16_t*, const char16_t*);
int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
size_t strlen32(const char32_t *);
size_t strnlen32(const char32_t *, size_t);
ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len);
int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index);
ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len);
void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len);
ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen, bool overreadIsFatal = false);
char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen);
char16_t *utf8_to_utf16(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen);
ssize_t utf8_length(const char *src);
45. ベクター.h
レガシーコード。
* DO NOT USE: please use std::vector
46.VectorImpl.h
同上。