[libc-commits] [libc] [llvm] [libc] Implement a Vector container (PR #243109)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Nov 17 02:46:09 PST 2026


================
@@ -0,0 +1,284 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// A growable array, which reports allocation failures to the caller.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_VECTOR_H
+#define LLVM_LIBC_SRC___SUPPORT_VECTOR_H
+
+#include "hdr/func/free.h"
+#include "hdr/func/malloc.h"
+#include "hdr/func/realloc.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/CPP/iterator.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/CPP/utility.h"
+#include "src/__support/checked.h"
+#include "src/__support/libc_assert.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h> // max_align_t, ptrdiff_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+/// A heap-allocated array which grows as needed, similar to std::vector.
+///
+/// Operations which may need to allocate memory return a Checked object, which
+/// the caller must check. If such an operation fails, the vector is left
+/// unchanged. The other operations cannot fail, but they have the same
+/// preconditions as in std::vector (e.g., pop_back() requires a non-empty
+/// vector). Where practical, these are checked with LIBC_ASSERT.
+///
+/// Copying a vector requires an allocation, so Vector is not copy-constructible
+/// or copy-assignable. Use assign() instead.
+///
+/// The constructors of T that Vector uses (the default constructor in resize(),
+/// the copy and move constructors, and the converting constructor in append())
+/// must not fail (e.g., by allocating memory). Over-aligned types (whose
+/// alignment exceeds alignof(max_align_t)) are not supported. Like with
+/// std::vector, T may be incomplete at the point where Vector<T> is
+/// instantiated, as long as it is completed before the vector is used.
+template <typename T> class Vector {
+  T *items = nullptr;
+  size_t count = 0;
+  size_t cap = 0;
+
+  // Limiting the storage size to PTRDIFF_MAX bytes guarantees that differences
+  // between pointers into it are representable. It also means that doubling
+  // the capacity cannot overflow.
+  LIBC_INLINE static constexpr size_t max_size() {
+    return static_cast<size_t>(cpp::numeric_limits<ptrdiff_t>::max()) /
+           sizeof(T);
+  }
+
+  LIBC_INLINE static void destroy(T *first, T *last) {
+    for (; first != last; ++first)
+      first->~T();
+  }
+
+  LIBC_INLINE static void value_initialize(T *first, T *last) {
+    for (; first != last; ++first)
+      new (first) T();
----------------
labath wrote:

Yes, for operations that are expected to default-construct a value (though that's moot now as I'm going with trivial types for the time being).

https://github.com/llvm/llvm-project/pull/243109


More information about the libc-commits mailing list