Bluetooth: Use connection reference as free indicator

If ref is zero other struct bt_conn fields should be considered
garbage. Using ref count instead of address is also faster.

Change-Id: Ic3b30c0fdbce8f93f81095d3671be0e54eac1455
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
This commit is contained in:
Szymon Janc 2015-08-19 18:44:32 +02:00 committed by Anas Nashif
commit 8e645f9e7c

View file

@ -278,7 +278,7 @@ struct bt_conn *bt_conn_add(const bt_addr_le_t *peer, uint8_t role)
int i; int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) { for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (!bt_addr_le_cmp(&conns[i].dst, BT_ADDR_LE_ANY)) { if (!atomic_get(&conns[i].ref)) {
conn = &conns[i]; conn = &conns[i];
break; break;
} }
@ -392,6 +392,10 @@ struct bt_conn *bt_conn_lookup_handle(uint16_t handle)
int i; int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) { for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (!atomic_get(&conns[i].ref)) {
continue;
}
/* We only care about connections with a valid handle */ /* We only care about connections with a valid handle */
if (conns[i].state != BT_CONN_CONNECTED && if (conns[i].state != BT_CONN_CONNECTED &&
conns[i].state != BT_CONN_DISCONNECT) { conns[i].state != BT_CONN_DISCONNECT) {
@ -411,6 +415,10 @@ struct bt_conn *bt_conn_lookup_addr_le(const bt_addr_le_t *peer)
int i; int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) { for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (!atomic_get(&conns[i].ref)) {
continue;
}
if (!bt_addr_le_cmp(peer, &conns[i].dst)) { if (!bt_addr_le_cmp(peer, &conns[i].dst)) {
return bt_conn_get(&conns[i]); return bt_conn_get(&conns[i]);
} }
@ -425,7 +433,7 @@ struct bt_conn *bt_conn_lookup_state(const bt_addr_le_t *peer,
int i; int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) { for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (!bt_addr_le_cmp(&conns[i].dst, BT_ADDR_LE_ANY)) { if (!atomic_get(&conns[i].ref)) {
continue; continue;
} }
@ -453,17 +461,9 @@ struct bt_conn *bt_conn_get(struct bt_conn *conn)
void bt_conn_put(struct bt_conn *conn) void bt_conn_put(struct bt_conn *conn)
{ {
atomic_val_t old_ref; atomic_dec(&conn->ref);
old_ref = atomic_dec(&conn->ref);
BT_DBG("handle %u ref %u\n", conn->handle, atomic_get(&conn->ref)); BT_DBG("handle %u ref %u\n", conn->handle, atomic_get(&conn->ref));
if (old_ref > 1) {
return;
}
bt_addr_le_copy(&conn->dst, BT_ADDR_LE_ANY);
} }
const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn) const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn)