[libc-commits] [libc] e5ae5ce - [libc][Obvious] Remove the unused cpp::vector class.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Tue Dec 20 15:26:59 PST 2022
Author: Siva Chandra Reddy
Date: 2022-12-20T23:26:12Z
New Revision: e5ae5cec060fb0cef89d4c86464e3b882e8c4b81
URL: https://github.com/llvm/llvm-project/commit/e5ae5cec060fb0cef89d4c86464e3b882e8c4b81
DIFF: https://github.com/llvm/llvm-project/commit/e5ae5cec060fb0cef89d4c86464e3b882e8c4b81.diff
LOG: [libc][Obvious] Remove the unused cpp::vector class.
This can be easily resurrected if required in future. It calls malloc
directly. So, instead of updating an unused piece to the newer pattern
of using the custom operator new, this patch just deletes it.
Added:
Modified:
libc/src/__support/CPP/CMakeLists.txt
libc/test/src/__support/CPP/CMakeLists.txt
Removed:
libc/src/__support/CPP/vector.h
libc/test/src/__support/CPP/vector_test.cpp
################################################################################
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index 3bab78c4000c..c271a5bd8609 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -81,14 +81,6 @@ add_header_library(
utility.h
)
-add_header_library(
- vector
- HDRS
- vector.h
- DEPENDS
- libc.include.stdlib
-)
-
add_header_library(
atomic
HDRS
diff --git a/libc/src/__support/CPP/vector.h b/libc/src/__support/CPP/vector.h
deleted file mode 100644
index e8873d1c14ad..000000000000
--- a/libc/src/__support/CPP/vector.h
+++ /dev/null
@@ -1,92 +0,0 @@
-//===-- A self contained equivalent of std::vector --------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_VECTOR_H
-#define LLVM_LIBC_SRC_SUPPORT_CPP_VECTOR_H
-
-#include <stddef.h> // For size_t.
-
-#include <stdlib.h> // For malloc/realloc/free
-
-namespace __llvm_libc {
-namespace cpp {
-
-// This implementation does not have a templated allocator since that feature
-// isn't relevant for a libc setting.
-
-// Vector is a templated dynamically resizable array. This implementation is
-// only meant for primitives or structs, and will not call destructors on held
-// objects.
-template <class T> class vector {
- T *data_array;
- size_t array_size;
- size_t num_elements = 0;
- static constexpr size_t DEFAULT_SIZE = 16;
- static constexpr size_t GROWTH_FACTOR = 2;
- static constexpr size_t MAX_SIZE = ~size_t(0);
-
-public:
- constexpr vector<T>() : array_size{DEFAULT_SIZE} {
- data_array = static_cast<T *>(malloc(DEFAULT_SIZE * sizeof(T)));
- }
-
- constexpr vector<T>(const vector<T> &other) = delete;
- constexpr vector<T>(const vector<T> &&other) = delete;
-
- ~vector() { free(data_array); }
-
- constexpr vector &operator=(vector &other) = delete;
- constexpr vector &operator=(vector &&other) = delete;
-
- constexpr void reserve(size_t new_size) {
- if (new_size >= array_size)
- increase_size(new_size + 1);
- }
-
- constexpr void push_back(const T &value) {
- if (num_elements >= array_size)
- increase_size(num_elements + 1);
- data_array[num_elements] = value;
- ++num_elements;
- }
-
- constexpr T &operator[](size_t pos) { return data_array[pos]; }
- constexpr T *data() { return data_array; }
- constexpr const T *data() const { return data_array; }
-
- constexpr bool empty() const { return num_elements == 0; }
-
- constexpr size_t size() const { return num_elements; }
- constexpr size_t max_size() const { return MAX_SIZE; }
-
- constexpr size_t capacity() const { return array_size; }
-
-private:
- static constexpr size_t MAX_DIV_BY_GROWTH = MAX_SIZE / GROWTH_FACTOR;
-
- // new_size is treated as the minimum size for the new array. This function
- // will increase array_size by GROWTH_FACTOR until there is space for new_size
- // items.
- constexpr void increase_size(size_t new_size) {
- size_t temp_size = array_size;
- if (new_size >= MAX_DIV_BY_GROWTH) {
- temp_size = new_size;
- } else {
- if (temp_size == 0)
- temp_size = 1;
- while (temp_size <= new_size)
- temp_size = temp_size * GROWTH_FACTOR;
- }
- array_size = temp_size;
- data_array = static_cast<T *>(realloc(data_array, array_size * sizeof(T)));
- }
-};
-} // namespace cpp
-} // namespace __llvm_libc
-
-#endif // LLVM_LIBC_SRC_SUPPORT_CPP_VECTOR_H
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index 99ad84939525..b9696baef382 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -41,16 +41,6 @@ add_libc_unittest(
libc.src.__support.uint
)
-add_libc_unittest(
- vector_test
- SUITE
- libc_cpp_utils_unittests
- SRCS
- vector_test.cpp
- DEPENDS
- libc.src.__support.CPP.vector
-)
-
add_libc_unittest(
int_seq_test
SUITE
diff --git a/libc/test/src/__support/CPP/vector_test.cpp b/libc/test/src/__support/CPP/vector_test.cpp
deleted file mode 100644
index a27eafa151bc..000000000000
--- a/libc/test/src/__support/CPP/vector_test.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-//===-- Unittests for vector ----------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/__support/CPP/vector.h"
-#include "utils/UnitTest/Test.h"
-
-TEST(LlvmLibcVectorTest, SimpleConstructor) {
- __llvm_libc::cpp::vector<int> vec;
-}
-
-TEST(LlvmLibcVectorTest, OrderedWriteOrderedReadTest) {
- __llvm_libc::cpp::vector<size_t> vec;
-
- for (size_t i = 0; i < 100; i = i + 2) {
- vec.push_back(i);
- }
- ASSERT_EQ(vec.size(), size_t(50));
- ASSERT_GE(vec.capacity(), vec.size());
- for (size_t j = 0; j < vec.size(); ++j) {
- ASSERT_EQ(vec[j], j * 2);
- }
-}
-
-TEST(LlvmLibcVectorTest, ReserveTest) {
- __llvm_libc::cpp::vector<bool> vec;
-
- size_t prev_capacity = vec.capacity();
-
- vec.reserve(prev_capacity * 2);
-
- ASSERT_GT(vec.capacity(), prev_capacity * 2);
-}
More information about the libc-commits
mailing list