• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_array"
20 
21 #include "osi/include/array.h"
22 
23 #include <base/logging.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "check.h"
28 #include "osi/include/allocator.h"
29 #include "osi/include/log.h"
30 
31 struct array_t {
32   size_t element_size;
33   size_t length;
34   size_t capacity;
35   uint8_t* data;
36   uint8_t internal_storage[];
37 };
38 
39 static bool grow(array_t* array);
40 
41 static const size_t INTERNAL_ELEMENTS = 16;
42 
array_new(size_t element_size)43 array_t* array_new(size_t element_size) {
44   CHECK(element_size > 0);
45 
46   array_t* array = static_cast<array_t*>(
47       osi_calloc(sizeof(array_t) + element_size * INTERNAL_ELEMENTS));
48 
49   array->element_size = element_size;
50   array->capacity = INTERNAL_ELEMENTS;
51   array->data = array->internal_storage;
52   return array;
53 }
54 
array_free(array_t * array)55 void array_free(array_t* array) {
56   if (!array) return;
57 
58   if (array->data != array->internal_storage) free(array->data);
59 
60   osi_free(array);
61 }
62 
array_ptr(const array_t * array)63 void* array_ptr(const array_t* array) { return array_at(array, 0); }
64 
array_at(const array_t * array,size_t index)65 void* array_at(const array_t* array, size_t index) {
66   CHECK(array != NULL);
67   CHECK(index < array->length);
68   return array->data + (index * array->element_size);
69 }
70 
array_length(const array_t * array)71 size_t array_length(const array_t* array) {
72   CHECK(array != NULL);
73   return array->length;
74 }
75 
array_append_value(array_t * array,uint32_t value)76 bool array_append_value(array_t* array, uint32_t value) {
77   return array_append_ptr(array, &value);
78 }
79 
array_append_ptr(array_t * array,void * data)80 bool array_append_ptr(array_t* array, void* data) {
81   CHECK(array != NULL);
82   CHECK(data != NULL);
83 
84   if (array->length == array->capacity && !grow(array)) {
85     LOG_ERROR(
86         "%s unable to grow array past current capacity of %zu elements "
87         "of size %zu.",
88         __func__, array->capacity, array->element_size);
89     return false;
90   }
91 
92   ++array->length;
93   memcpy(array_at(array, array->length - 1), data, array->element_size);
94   return true;
95 }
96 
grow(array_t * array)97 static bool grow(array_t* array) {
98   const size_t new_capacity = array->capacity + (array->capacity / 2);
99   const bool is_moving = (array->data == array->internal_storage);
100 
101   void* new_data = realloc(is_moving ? NULL : array->data,
102                            new_capacity * array->element_size);
103   if (!new_data) return false;
104 
105   if (is_moving)
106     memcpy(new_data, array->internal_storage,
107            array->length * array->element_size);
108 
109   array->data = static_cast<uint8_t*>(new_data);
110   array->capacity = new_capacity;
111   return true;
112 }
113