1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_osi_buffer"
20 
21 #include <assert.h>
22 #include <stdint.h>
23 
24 #include "osi/include/allocator.h"
25 #include "osi/include/buffer.h"
26 #include "osi/include/log.h"
27 
28 struct buffer_t {
29   buffer_t *root;
30   size_t refcount;
31   size_t length;
32   uint8_t data[];
33 };
34 
buffer_new(size_t size)35 buffer_t *buffer_new(size_t size) {
36   assert(size > 0);
37 
38   buffer_t *buffer = osi_calloc(sizeof(buffer_t) + size);
39   if (!buffer) {
40     LOG_ERROR("%s unable to allocate buffer of %zu bytes.", __func__, size);
41     return NULL;
42   }
43 
44   buffer->root = buffer;
45   buffer->refcount = 1;
46   buffer->length = size;
47 
48   return buffer;
49 }
50 
buffer_new_ref(const buffer_t * buf)51 buffer_t *buffer_new_ref(const buffer_t *buf) {
52   assert(buf != NULL);
53   return buffer_new_slice(buf, buf->length);
54 }
55 
buffer_new_slice(const buffer_t * buf,size_t slice_size)56 buffer_t *buffer_new_slice(const buffer_t *buf, size_t slice_size) {
57   assert(buf != NULL);
58   assert(slice_size > 0);
59   assert(slice_size <= buf->length);
60 
61   buffer_t *ret = osi_calloc(sizeof(buffer_t));
62   if (!ret) {
63     LOG_ERROR("%s unable to allocate new buffer for slice of length %zu.", __func__, slice_size);
64     return NULL;
65   }
66 
67   ret->root = buf->root;
68   ret->refcount = SIZE_MAX;
69   ret->length = slice_size;
70 
71   ++buf->root->refcount;
72 
73   return ret;
74 }
75 
buffer_free(buffer_t * buffer)76 void buffer_free(buffer_t *buffer) {
77   if (!buffer)
78     return;
79 
80   if (buffer->root != buffer) {
81     // We're a leaf node. Delete the root node if we're the last referent.
82     if (--buffer->root->refcount == 0)
83       osi_free(buffer->root);
84     osi_free(buffer);
85   } else if (--buffer->refcount == 0) {
86     // We're a root node. Roots are only deleted when their refcount goes to 0.
87     osi_free(buffer);
88   }
89 }
90 
buffer_ptr(const buffer_t * buf)91 void *buffer_ptr(const buffer_t *buf) {
92   assert(buf != NULL);
93   return buf->root->data + buf->root->length - buf->length;
94 }
95 
buffer_length(const buffer_t * buf)96 size_t buffer_length(const buffer_t *buf) {
97   assert(buf != NULL);
98   return buf->length;
99 }
100