Files
shadowsocks-libev/tests/test_core_arena.cpp
T
Max Lv bcb3731968 C++20 port 1/14: build, ownership core and allocation audit (#3065)
* Enable C++20 alongside C11 in the build

Add CXX to the project with C++20 (no extensions) and build C++ sources
with -Wextra -fno-exceptions -fno-rtti -fno-threadsafe-statics on top of
the existing warning flags. Give every toolchain a C++ compiler: the zig
Windows toolchain, the static macOS/Windows CI jobs, the static musl
image (clang++ plus libstdc++ headers) and the RPM spec.

* Add C++20 ownership and allocation core

Header-only primitives for the no-heap-after-startup design:
Result/Status, Region, ObjectPool with PoolPtr (std::unique_ptr with a
pool deleter), generation-checked Handle and asynchronous close support,
intrusive RefPtr, FixedBuffer, MonotonicArena, FixedHashMap and FixedLru,
IntrusiveList, and a bounded FixedHeap for third-party allocator hooks.

Unit tests cover each primitive (test_core_*). scripts/check_no_heap.py
rejects heap allocation in C++ sources under src/ and runs as a ctest.
clang-tidy now also analyses C++ sources and the core headers.

* Add heap allocation audit for relayed connections

alloc_counter is a preloadable malloc counter (glibc, macOS) that writes
a snapshot on SIGUSR2. measure_alloc.py warms ss-local and ss-server up,
snapshots both, relays more connections and reports allocations per
connection. It runs report-only as the alloc_audit_report ctest.
interop.peers() now yields the started processes.

* Document the C++20 port and allocation baseline

docs/cpp-core.md describes the language profile, the core primitives,
the phase plan and the measured C baseline (77-89 allocations per
connection in ss-server, 27-39 in ss-local). The compatibility contract
now keeps the C11 public header instead of a C11 code base. CLAUDE.md
drops stale libev, submodule and gnu99 references.

* Fix CI for the C++ core: libstdc++ headers, static unwinder, newer clang

- docker/static: Alpine's libstdc++ headers come with g++, and the C++
  tests' static libstdc++ needs an unwinder that compiler-rt lacks; add
  g++ and llvm-libunwind.
- FixedBuffer: drop the unused kCapacity (clang-tidy 18 reports it as a
  dynamically initialized header static).
- ss-server on Windows: manager_addr was only read in POSIX-only code,
  which newer clang rejects (-Wunused-but-set-global); say that the
  option is ignored there.

* CI: allow running the test and build workflows by hand

Stacked PRs target their parent branch, which these workflows do not
trigger on; workflow_dispatch lets each layer run the full matrix.

* CI: let clang-tidy ignore GCC-only warning flags

The compile database comes from a GCC build, whose flags (for example
-Wno-class-memaccess) clang-tidy does not know and reports as errors.
2026-10-15 09:45:54 +08:00

64 lines
1.5 KiB
C++

/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <cstdint>
#include <cstring>
#include "core/arena.hpp"
#include "test_helpers.hpp"
namespace {
struct Pair {
int a;
double b;
};
void test_allocate_alignment()
{
ss::MonotonicArena arena;
SS_CHECK(arena.reserve(256));
auto c = arena.allocate(1, 1);
SS_CHECK(c);
auto d = arena.allocate(8, 64);
SS_CHECK(d);
SS_CHECK(reinterpret_cast<std::uintptr_t>(d->data()) % 64 == 0);
auto p = arena.make<Pair>(Pair{ 3, 1.5 });
SS_CHECK(p && (*p)->a == 3 && (*p)->b == 1.5);
SS_CHECK(!arena.allocate(8, 3));
}
void test_exhaustion_and_reset()
{
ss::MonotonicArena arena;
SS_CHECK(!arena.allocate(1));
SS_CHECK(arena.reserve(64));
SS_CHECK(arena.allocate(64, 1));
auto over = arena.allocate(1, 1);
SS_CHECK(!over && over.error().code == ss::Errc::exhausted);
arena.reset();
SS_CHECK(arena.used() == 0 && arena.peak() == 64);
SS_CHECK(arena.allocate(32, 1));
}
void test_intern_and_arrays()
{
ss::MonotonicArena arena;
SS_CHECK(arena.reserve(128));
auto s = arena.intern("aes-256-gcm");
SS_CHECK(s && *s == "aes-256-gcm");
SS_CHECK(s->data()[s->size()] == '\0');
auto empty = arena.intern("");
SS_CHECK(empty && empty->empty());
auto ports = arena.make_array<std::uint16_t>(4);
SS_CHECK(ports && ports->size() == 4 && (*ports)[3] == 0);
}
} // namespace
int main()
{
SS_RUN(test_allocate_alignment);
SS_RUN(test_exhaustion_and_reset);
SS_RUN(test_intern_and_arrays);
return 0;
}