Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e7091eb authored by David Duarte's avatar David Duarte
Browse files

Remove unused osi array library

Bug: 294615573
Test: mmm packages/modules/Bluetooth
Change-Id: I2ee3c32699960d081e49cea283b08bac2a0b6a10
parent 502c50a3
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ cc_library_static {
        "src/alarm.cc",
        "src/allocation_tracker.cc",
        "src/allocator.cc",
        "src/array.cc",
        "src/buffer.cc",
        "src/config.cc",
        "src/fixed_queue.cc",
@@ -133,7 +132,6 @@ cc_test {
        "test/alarm_test.cc",
        "test/allocation_tracker_test.cc",
        "test/allocator_test.cc",
        "test/array_test.cc",
        "test/config_test.cc",
        "test/fixed_queue_test.cc",
        "test/future_test.cc",
+0 −2
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ static_library("osi") {
    "src/alarm.cc",
    "src/allocation_tracker.cc",
    "src/allocator.cc",
    "src/array.cc",
    "src/buffer.cc",
    "src/compat.cc",
    "src/config.cc",
@@ -75,7 +74,6 @@ if (use.test) {
      "test/alarm_test.cc",
      "test/allocation_tracker_test.cc",
      "test/allocator_test.cc",
      "test/array_test.cc",
      "test/config_test.cc",
      "test/future_test.cc",
      "test/hash_map_utils_test.cc",

system/osi/include/array.h

deleted100644 → 0
+0 −58
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

typedef struct array_t array_t;

// Returns a new array object that stores elements of size |element_size|. The
// returned object must be freed with |array_free|. |element_size| must be
// greater than 0. Returns NULL on failure.
array_t* array_new(size_t element_size);

// Frees an array that was allocated with |array_new|. |array| may be NULL.
void array_free(array_t* array);

// Returns a pointer to the first stored element in |array|. |array| must not be
// NULL.
void* array_ptr(const array_t* array);

// Returns a pointer to the |index|th element of |array|. |index| must be less
// than the array's length. |array| must not be NULL.
void* array_at(const array_t* array, size_t index);

// Returns the number of elements stored in |array|. |array| must not be NULL.
size_t array_length(const array_t* array);

// Inserts an element to the end of |array| by value. For example, a caller
// may simply call array_append_value(array, 5) instead of storing 5 into a
// variable and then inserting by pointer. Although |value| is a uint32_t,
// only the lowest |element_size| bytes will be stored. |array| must not be
// NULL. Returns true if the element could be inserted into the array, false
// on error.
bool array_append_value(array_t* array, uint32_t value);

// Inserts an element to the end of |array|. The value pointed to by |data| must
// be at least |element_size| bytes long and will be copied into the array.
// Neither |array| nor |data| may be NULL. Returns true if the element could be
// inserted into the array, false on error.
bool array_append_ptr(array_t* array, void* data);

system/osi/src/array.cc

deleted100644 → 0
+0 −112
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "bt_osi_array"

#include "osi/include/array.h"

#include <base/logging.h>
#include <stdlib.h>
#include <string.h>

#include "check.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"

struct array_t {
  size_t element_size;
  size_t length;
  size_t capacity;
  uint8_t* data;
  uint8_t internal_storage[];
};

static bool grow(array_t* array);

static const size_t INTERNAL_ELEMENTS = 16;

array_t* array_new(size_t element_size) {
  CHECK(element_size > 0);

  array_t* array = static_cast<array_t*>(
      osi_calloc(sizeof(array_t) + element_size * INTERNAL_ELEMENTS));

  array->element_size = element_size;
  array->capacity = INTERNAL_ELEMENTS;
  array->data = array->internal_storage;
  return array;
}

void array_free(array_t* array) {
  if (!array) return;

  if (array->data != array->internal_storage) free(array->data);

  osi_free(array);
}

void* array_ptr(const array_t* array) { return array_at(array, 0); }

void* array_at(const array_t* array, size_t index) {
  CHECK(array != NULL);
  CHECK(index < array->length);
  return array->data + (index * array->element_size);
}

size_t array_length(const array_t* array) {
  CHECK(array != NULL);
  return array->length;
}

bool array_append_value(array_t* array, uint32_t value) {
  return array_append_ptr(array, &value);
}

bool array_append_ptr(array_t* array, void* data) {
  CHECK(array != NULL);
  CHECK(data != NULL);

  if (array->length == array->capacity && !grow(array)) {
    LOG_ERROR(
        "%s unable to grow array past current capacity of %zu elements "
        "of size %zu.",
        __func__, array->capacity, array->element_size);
    return false;
  }

  ++array->length;
  memcpy(array_at(array, array->length - 1), data, array->element_size);
  return true;
}

static bool grow(array_t* array) {
  const size_t new_capacity = array->capacity + (array->capacity / 2);
  const bool is_moving = (array->data == array->internal_storage);

  void* new_data = realloc(is_moving ? NULL : array->data,
                           new_capacity * array->element_size);
  if (!new_data) return false;

  if (is_moving)
    memcpy(new_data, array->internal_storage,
           array->length * array->element_size);

  array->data = static_cast<uint8_t*>(new_data);
  array->capacity = new_capacity;
  return true;
}

system/osi/test/array_test.cc

deleted100644 → 0
+0 −82
Original line number Diff line number Diff line
#include "osi/include/array.h"

#include <android-base/silent_death_test.h>
#include <gtest/gtest.h>

#include "AllocationTestHarness.h"

class ArrayTest : public AllocationTestHarness {};

using ArrayDeathTest = ArrayTest;

TEST_F(ArrayTest, test_new_free_simple) {
  array_t* array = array_new(4);
  ASSERT_TRUE(array != NULL);
  array_free(array);
}

TEST_F(ArrayTest, test_free_null) { array_free(NULL); }

TEST_F(ArrayDeathTest, test_invalid_ptr) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  array_t* array = array_new(4);
  {
    // this will silent SIGABRT sent in EXPECT_DEATH below
    ScopedSilentDeath _silentDeath;

    ASSERT_DEATH(array_ptr(array), "");
  }
  array_free(array);
}

TEST_F(ArrayDeathTest, test_invalid_at) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  array_t* array = array_new(4);
  {
    // this will silent SIGABRT sent in EXPECT_DEATH below
    ScopedSilentDeath _silentDeath;

    ASSERT_DEATH(array_at(array, 1), "");
  }
  array_free(array);
}

TEST_F(ArrayTest, test_append_value) {
  array_t* array = array_new(sizeof(int));
  for (int i = 0; i < 100; ++i) {
    array_append_value(array, i * i);
  }
  for (int i = 0; i < 100; ++i) {
    EXPECT_EQ(*(int*)array_at(array, i), i * i);
  }
  array_free(array);
}

TEST_F(ArrayTest, test_append_ptr) {
  int items[100];
  array_t* array = array_new(sizeof(int));
  for (int i = 0; i < 100; ++i) {
    items[i] = i * i;
    array_append_ptr(array, &items[i]);
  }
  for (int i = 0; i < 100; ++i) {
    EXPECT_EQ(*(int*)array_at(array, i), i * i);
  }
  array_free(array);
}

TEST_F(ArrayTest, test_large_element) {
  char strings[][128] = {
      "string 1", "string 2", "string 3", "string 4",
      "string 5", "string 6", "string 7", "string 8",
  };

  array_t* array = array_new(128);
  for (int i = 0; i < 100; ++i) {
    array_append_ptr(array, strings[i % 8]);
  }
  for (int i = 0; i < 100; ++i) {
    EXPECT_TRUE(!memcmp(array_at(array, i), strings[i % 8], 128));
  }
  array_free(array);
}
Loading