2016-09-26 09:37:39 -04:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#include <stdint.h>
|
2020-02-05 03:08:29 -05:00
|
|
|
#include "bt_common.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/allocator.h"
|
|
|
|
#include "osi/buffer.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
struct buffer_t {
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *root;
|
|
|
|
size_t refcount;
|
|
|
|
size_t length;
|
|
|
|
uint8_t data[];
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *buffer_new(size_t size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *buffer = osi_malloc(sizeof(buffer_t) + size);
|
|
|
|
if (!buffer) {
|
2018-04-24 22:11:06 -04:00
|
|
|
OSI_TRACE_ERROR("%s unable to allocate buffer of %zu bytes.", __func__, size);
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer->root = buffer;
|
|
|
|
buffer->refcount = 1;
|
|
|
|
buffer->length = size;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return buffer;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *buffer_new_ref(const buffer_t *buf)
|
|
|
|
{
|
|
|
|
assert(buf != NULL);
|
|
|
|
return buffer_new_slice(buf, buf->length);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *buffer_new_slice(const buffer_t *buf, size_t slice_size)
|
|
|
|
{
|
|
|
|
assert(buf != NULL);
|
|
|
|
assert(slice_size > 0);
|
|
|
|
assert(slice_size <= buf->length);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
buffer_t *ret = osi_calloc(sizeof(buffer_t));
|
|
|
|
if (!ret) {
|
2018-04-24 22:11:06 -04:00
|
|
|
OSI_TRACE_ERROR("%s unable to allocate new buffer for slice of length %zu.", __func__, slice_size);
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
ret->root = buf->root;
|
|
|
|
ret->refcount = SIZE_MAX;
|
|
|
|
ret->length = slice_size;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
++buf->root->refcount;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return ret;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void buffer_free(buffer_t *buffer)
|
|
|
|
{
|
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (buffer->root != buffer) {
|
|
|
|
// We're a leaf node. Delete the root node if we're the last referent.
|
|
|
|
if (--buffer->root->refcount == 0) {
|
|
|
|
osi_free(buffer->root);
|
|
|
|
}
|
|
|
|
osi_free(buffer);
|
|
|
|
} else if (--buffer->refcount == 0) {
|
|
|
|
// We're a root node. Roots are only deleted when their refcount goes to 0.
|
|
|
|
osi_free(buffer);
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void *buffer_ptr(const buffer_t *buf)
|
|
|
|
{
|
|
|
|
assert(buf != NULL);
|
|
|
|
return buf->root->data + buf->root->length - buf->length;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
size_t buffer_length(const buffer_t *buf)
|
|
|
|
{
|
|
|
|
assert(buf != NULL);
|
|
|
|
return buf->length;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|