OSDN Git Service

Limit the maximum number of entries in the remote bdaddr cache to 1024
authorPavlin Radoslavov <pavlin@google.com>
Tue, 29 Aug 2017 00:42:46 +0000 (17:42 -0700)
committerPavlin Radoslavov <pavlin@google.com>
Thu, 31 Aug 2017 18:35:50 +0000 (11:35 -0700)
Also, renamed p_dev_cb to remote_bdaddr_cache.

Bug: 64975965
Test: BLE scanning
Change-Id: I518390c53c5ff2a24ac9f010464225d763b33228
(cherry picked from commit a375c31ae34dd49c48f12a15d4af003a864bd979)

btif/src/btif_ble_scanner.cc

index baad278..02538c6 100644 (file)
@@ -63,31 +63,32 @@ extern const btgatt_callbacks_t* bt_gatt_callbacks;
     }                                                                \
   } while (0)
 
-namespace std {
-template <>
-struct hash<RawAddress> {
-  size_t operator()(const RawAddress& f) const {
-    return f.address[0] + f.address[1] + f.address[2] + f.address[3] +
-           f.address[4] + f.address[5];
-  }
-};
-
-}
-
 namespace {
 
 // all access to this variable should be done on the jni thread
-std::unordered_set<RawAddress> p_dev_cb;
+std::set<RawAddress> remote_bdaddr_cache;
+std::queue<RawAddress> remote_bdaddr_cache_ordered;
+const size_t remote_bdaddr_cache_max_size = 1024;
 
 void btif_gattc_add_remote_bdaddr(const RawAddress& p_bda, uint8_t addr_type) {
-  p_dev_cb.insert(p_bda);
+  // Remove the oldest entries
+  while (remote_bdaddr_cache.size() >= remote_bdaddr_cache_max_size) {
+    const RawAddress& raw_address = remote_bdaddr_cache_ordered.front();
+    remote_bdaddr_cache.erase(raw_address);
+    remote_bdaddr_cache_ordered.pop();
+  }
+  remote_bdaddr_cache.insert(p_bda);
+  remote_bdaddr_cache_ordered.push(p_bda);
 }
 
 bool btif_gattc_find_bdaddr(const RawAddress& p_bda) {
-  return (p_dev_cb.count(p_bda) != 0);
+  return (remote_bdaddr_cache.find(p_bda) != remote_bdaddr_cache.end());
 }
 
-void btif_gattc_init_dev_cb(void) { p_dev_cb.clear(); }
+void btif_gattc_init_dev_cb(void) {
+  remote_bdaddr_cache.clear();
+  remote_bdaddr_cache_ordered = {};
+}
 
 void btif_gatts_upstreams_evt(uint16_t event, char* p_param) {
   LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event);