1 /****************************************************************************** 2 * 3 * Copyright 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 "osi/include/buffer.h" 22 23 #include <base/logging.h> 24 #include <stdint.h> 25 26 #include "osi/include/allocator.h" 27 #include "osi/include/log.h" 28 29 struct buffer_t { 30 buffer_t* root; 31 size_t refcount; 32 size_t length; 33 uint8_t data[]; 34 }; 35 buffer_new(size_t size)36buffer_t* buffer_new(size_t size) { 37 CHECK(size > 0); 38 39 buffer_t* buffer = 40 static_cast<buffer_t*>(osi_calloc(sizeof(buffer_t) + size)); 41 42 buffer->root = buffer; 43 buffer->refcount = 1; 44 buffer->length = size; 45 46 return buffer; 47 } 48 buffer_new_ref(const buffer_t * buf)49buffer_t* buffer_new_ref(const buffer_t* buf) { 50 CHECK(buf != NULL); 51 return buffer_new_slice(buf, buf->length); 52 } 53 buffer_new_slice(const buffer_t * buf,size_t slice_size)54buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size) { 55 CHECK(buf != NULL); 56 CHECK(slice_size > 0); 57 CHECK(slice_size <= buf->length); 58 59 buffer_t* ret = static_cast<buffer_t*>(osi_calloc(sizeof(buffer_t))); 60 61 ret->root = buf->root; 62 ret->refcount = SIZE_MAX; 63 ret->length = slice_size; 64 65 ++buf->root->refcount; 66 67 return ret; 68 } 69 buffer_free(buffer_t * buffer)70void buffer_free(buffer_t* buffer) { 71 if (!buffer) return; 72 73 if (buffer->root != buffer) { 74 // We're a leaf node. Delete the root node if we're the last referent. 75 if (--buffer->root->refcount == 0) osi_free(buffer->root); 76 osi_free(buffer); 77 } else if (--buffer->refcount == 0) { 78 // We're a root node. Roots are only deleted when their refcount goes to 0. 79 osi_free(buffer); 80 } 81 } 82 buffer_ptr(const buffer_t * buf)83void* buffer_ptr(const buffer_t* buf) { 84 CHECK(buf != NULL); 85 return buf->root->data + buf->root->length - buf->length; 86 } 87 buffer_length(const buffer_t * buf)88size_t buffer_length(const buffer_t* buf) { 89 CHECK(buf != NULL); 90 return buf->length; 91 } 92