Bluetooth: Add reference counting for buffers

In certain scenarios we want to keep the buffers around for longer
and avoid the default bt_buf_put() calls from putting them back to the
available buffers queue. This patch adds reference counting for the
buffers, along with a bt_buf_hold() API to increment the reference
count. Now bt_buf_put() will only put the buffer back to the pool if
the reference count hits 0.

Change-Id: I1590c5574e18600939f55e5339a6da3d061f7682
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-04-30 12:26:01 +03:00 committed by Anas Nashif
commit 89a083f888
2 changed files with 18 additions and 1 deletions

View file

@ -68,6 +68,9 @@ struct bt_buf {
struct bt_buf_acl_data acl;
};
/* Reference count */
uint8_t ref;
/* Type of data contained in the buffer */
uint8_t type;
@ -85,6 +88,9 @@ struct bt_buf *bt_buf_get(enum bt_buf_type type, size_t reserve_head);
/* Place buffer back into the available buffers pool */
void bt_buf_put(struct bt_buf *buf);
/* Increment the reference count of a buffer */
struct bt_buf *bt_buf_hold(struct bt_buf *buf);
/* Prepare data to be added at the end of the buffer */
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len);

View file

@ -87,6 +87,7 @@ struct bt_buf *bt_buf_get(enum bt_buf_type type, size_t reserve_head)
memset(buf, 0, sizeof(*buf));
buf->ref = 1;
buf->type = type;
buf->data = buf->buf + reserve_head;
@ -102,7 +103,10 @@ void bt_buf_put(struct bt_buf *buf)
struct nano_fifo *avail = get_avail(buf->type);
uint16_t handle;
BT_DBG("buf %p type %d\n", buf, buf->type);
BT_DBG("buf %p ref %u type %d\n", buf, buf->ref, buf->type);
if (--buf->ref)
return;
handle = buf->acl.handle;
nano_fifo_put(avail, buf);
@ -129,6 +133,13 @@ void bt_buf_put(struct bt_buf *buf)
bt_hci_cmd_send(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS, buf);
}
struct bt_buf *bt_buf_hold(struct bt_buf *buf)
{
BT_DBG("buf %p (old) ref %u type %d\n", buf, buf->ref, buf->type);
buf->ref++;
return buf;
}
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len)
{
uint8_t *tail = buf->data + buf->len;