Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2014 Google, Inc. |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -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_array" |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 20 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 21 | #include "osi/include/array.h" |
| 22 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 23 | #include <base/logging.h> |
Etan Cohen | 3e59b5b | 2015-03-31 17:15:53 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 26 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 27 | #include "osi/include/allocator.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 28 | #include "osi/include/log.h" |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 29 | |
| 30 | struct array_t { |
| 31 | size_t element_size; |
| 32 | size_t length; |
| 33 | size_t capacity; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 34 | uint8_t* data; |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 35 | uint8_t internal_storage[]; |
| 36 | }; |
| 37 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 38 | static bool grow(array_t* array); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 39 | |
| 40 | static const size_t INTERNAL_ELEMENTS = 16; |
| 41 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 42 | array_t* array_new(size_t element_size) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 43 | CHECK(element_size > 0); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 44 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 45 | array_t* array = static_cast<array_t*>( |
| 46 | osi_calloc(sizeof(array_t) + element_size * INTERNAL_ELEMENTS)); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 47 | |
| 48 | array->element_size = element_size; |
| 49 | array->capacity = INTERNAL_ELEMENTS; |
| 50 | array->data = array->internal_storage; |
| 51 | return array; |
| 52 | } |
| 53 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 54 | void array_free(array_t* array) { |
| 55 | if (!array) return; |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 56 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 57 | if (array->data != array->internal_storage) free(array->data); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 58 | |
| 59 | osi_free(array); |
| 60 | } |
| 61 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 62 | void* array_ptr(const array_t* array) { return array_at(array, 0); } |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 63 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 64 | void* array_at(const array_t* array, size_t index) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 65 | CHECK(array != NULL); |
| 66 | CHECK(index < array->length); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 67 | return array->data + (index * array->element_size); |
| 68 | } |
| 69 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 70 | size_t array_length(const array_t* array) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 71 | CHECK(array != NULL); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 72 | return array->length; |
| 73 | } |
| 74 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 75 | bool array_append_value(array_t* array, uint32_t value) { |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 76 | return array_append_ptr(array, &value); |
| 77 | } |
| 78 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 79 | bool array_append_ptr(array_t* array, void* data) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 80 | CHECK(array != NULL); |
| 81 | CHECK(data != NULL); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 82 | |
| 83 | if (array->length == array->capacity && !grow(array)) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 84 | LOG_ERROR(LOG_TAG, |
| 85 | "%s unable to grow array past current capacity of %zu elements " |
| 86 | "of size %zu.", |
| 87 | __func__, array->capacity, array->element_size); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
| 91 | ++array->length; |
| 92 | memcpy(array_at(array, array->length - 1), data, array->element_size); |
| 93 | return true; |
| 94 | } |
| 95 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 96 | static bool grow(array_t* array) { |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 97 | const size_t new_capacity = array->capacity + (array->capacity / 2); |
| 98 | const bool is_moving = (array->data == array->internal_storage); |
| 99 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 100 | void* new_data = realloc(is_moving ? NULL : array->data, |
| 101 | new_capacity * array->element_size); |
| 102 | if (!new_data) return false; |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 103 | |
| 104 | if (is_moving) |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 105 | memcpy(new_data, array->internal_storage, |
| 106 | array->length * array->element_size); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 107 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 108 | array->data = static_cast<uint8_t*>(new_data); |
Sharvil Nanavati | badeb92 | 2014-11-13 17:29:21 -0800 | [diff] [blame] | 109 | array->capacity = new_capacity; |
| 110 | return true; |
| 111 | } |