[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:44:24 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();
+ }
+
+ // Moves the elements in [first, last) to the uninitialized storage starting
+ // at `dest`, and destroys the originals.
+ LIBC_INLINE static void relocate(T *first, T *last, T *dest) {
+ for (; first != last; ++first, ++dest) {
+ new (dest) T(cpp::move(*first));
+ first->~T();
+ }
+ }
+
+ LIBC_INLINE void truncate(size_t n) {
+ LIBC_ASSERT(n <= count);
+ destroy(items + n, items + count);
+ count = n;
+ }
+
+ // Moves the elements to new storage with room for `new_cap` elements.
+ LIBC_INLINE bool reallocate(size_t new_cap) {
+ static_assert(alignof(T) <= alignof(max_align_t),
+ "over-aligned types are not supported");
+ // Besides being needed for moving the elements, this guarantees that
+ // trivially copyable types are implicit-lifetime types, whose objects
+ // realloc implicitly creates in the new block.
+ static_assert(cpp::is_move_constructible_v<T>,
+ "T must be move-constructible");
+ LIBC_ASSERT(count <= new_cap && new_cap <= max_size());
+ // realloc(p, 0) may free p and return nullptr.
+ LIBC_ASSERT(new_cap > 0);
+ T *new_items;
+ if constexpr (cpp::is_trivially_copyable_v<T>) {
+ // realloc can avoid the copy if it manages to resize the block in place.
+ new_items = static_cast<T *>(::realloc(items, new_cap * sizeof(T)));
----------------
labath wrote:
Funny you should mention that, as https://github.com/llvm/llvm-project/blob/d7ab4588d83033d5541a546d505ba9bb8cc64e7a/libc/src/__support/regex/regex_expr_pool.h#L65 could arguably be implemented as a vector<vector<Block>>. I'm not going to lead with that though, as there are a lot of cases where the trivial-type version is going to work fine, and there are also other ways that this particular pattern could be implemented.
https://github.com/llvm/llvm-project/pull/243109
More information about the libc-commits
mailing list