Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2014 Google, Inc. |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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 | |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 19 | #define LOG_TAG "bt_osi_buffer" |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 20 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 21 | #include "osi/include/buffer.h" |
| 22 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 23 | #include <base/logging.h> |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | |
| 26 | #include "osi/include/allocator.h" |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 27 | #include "osi/include/log.h" |
| 28 | |
| 29 | struct buffer_t { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 30 | buffer_t* root; |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 31 | size_t refcount; |
| 32 | size_t length; |
| 33 | uint8_t data[]; |
| 34 | }; |
| 35 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 36 | buffer_t* buffer_new(size_t size) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 37 | CHECK(size > 0); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 38 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 39 | buffer_t* buffer = |
| 40 | static_cast<buffer_t*>(osi_calloc(sizeof(buffer_t) + size)); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 41 | |
| 42 | buffer->root = buffer; |
| 43 | buffer->refcount = 1; |
| 44 | buffer->length = size; |
| 45 | |
| 46 | return buffer; |
| 47 | } |
| 48 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 49 | buffer_t* buffer_new_ref(const buffer_t* buf) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 50 | CHECK(buf != NULL); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 51 | return buffer_new_slice(buf, buf->length); |
| 52 | } |
| 53 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 54 | buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 55 | CHECK(buf != NULL); |
| 56 | CHECK(slice_size > 0); |
| 57 | CHECK(slice_size <= buf->length); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 58 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 59 | buffer_t* ret = static_cast<buffer_t*>(osi_calloc(sizeof(buffer_t))); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 70 | void buffer_free(buffer_t* buffer) { |
| 71 | if (!buffer) return; |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 72 | |
| 73 | if (buffer->root != buffer) { |
| 74 | // We're a leaf node. Delete the root node if we're the last referent. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 75 | if (--buffer->root->refcount == 0) osi_free(buffer->root); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 83 | void* buffer_ptr(const buffer_t* buf) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 84 | CHECK(buf != NULL); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 85 | return buf->root->data + buf->root->length - buf->length; |
| 86 | } |
| 87 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 88 | size_t buffer_length(const buffer_t* buf) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 89 | CHECK(buf != NULL); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 90 | return buf->length; |
| 91 | } |