Fluorideの実装 メモリ管理について調べてみました。
FluorideはAndroid6以降で使用されているBluetoothプロトコルスタックです。
Fluorideの前身のBluetootuプロトコルスタックであるBluedroidではGKIと呼ばれるレイヤーでメモリ管理をしていましたが、Googleによって書き直されたので、どんな感じか調べてみました。
Bluedroidのメモリ管理については下記で調べました。
ソースコード
-
https://android.googlesource.com/platform/system/bt/+/master
- https://android.googlesource.com/platform/system/bt/+/master/osi/include/allocator.h
- https://android.googlesource.com/platform/system/bt/+/master/osi/src/allocator.c
- https://android.googlesource.com/platform/system/bt/+/master/osi/include/allocation_tracker.h
- https://android.googlesource.com/platform/system/bt/+/master/osi/src/allocation_tracker.c
メモリ取得
- allocater.cのosi_malloc()
- mallocのラッパー、canary用に余分にmallocする。
- tracker用にid, アドレス、サイズを通知する。
allocater.c
void *osi_malloc(size_t size) {
size_t real_size = allocation_tracker_resize_for_canary(size);
void *ptr = malloc(real_size);
assert(ptr);
return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
}
メモリ解放
- allocater.cのosi_free()
- tracker用に、id、アドレスを通知する
allocater.c
void osi_free(void *ptr) {
free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
}
allocation_trackerについて
- trackする情報はhash_mapを使って検索する。 hash_mapのサイズは1024。足りなくなったらcallocで増やす。
- trackする情報は、id、freeの有無、サイズ、ポインタ
- canaryの情報はメモリ先頭部分と最後部分の2箇所にある。 canaryは炭鉱のカナリアみたく、領域をはみ出してメモリ破壊をしていないかをチェックする。
- 解放時は、track情報の有無、二重解放、idのチェック、先頭と最後のcanaryのチェックを行う。
その他
- その他のbuffer.cというのもあるのですが、今回は割愛します。
- osiは色々べんりなコードが多いので使い方を勉強しとこう。