@encharm/cws 4.5.2 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/CLAUDE.md +81 -0
- package/README.md +10 -5
- package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node127.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node147.node +0 -0
- package/dist/bindings/cws_linux_arm64_node115.node +0 -0
- package/dist/bindings/cws_linux_arm64_node127.node +0 -0
- package/dist/bindings/cws_linux_arm64_node137.node +0 -0
- package/dist/bindings/cws_linux_arm64_node147.node +0 -0
- package/dist/bindings/cws_linux_x64_node115.node +0 -0
- package/dist/bindings/cws_linux_x64_node127.node +0 -0
- package/dist/bindings/cws_linux_x64_node137.node +0 -0
- package/dist/bindings/cws_linux_x64_node147.node +0 -0
- package/dist/bindings/cws_win32_x64_node115.node +0 -0
- package/dist/bindings/cws_win32_x64_node127.node +0 -0
- package/dist/bindings/cws_win32_x64_node137.node +0 -0
- package/dist/bindings/cws_win32_x64_node147.node +0 -0
- package/package.json +1 -1
- package/src/Addon.cpp +2 -1
- package/src/Addon.h +70 -14
- package/src/Hub.h +1 -0
- package/src/Networking.h +11 -0
- package/src/Socket.h +201 -6
- package/src/WebSocket.cpp +4 -1
- package/src/cSNode.cpp +2 -0
- package/src/cSNode.h +4 -0
- package/src/headers/24/tracing/agent.h +195 -0
- package/src/headers/24/tracing/trace_event.h +722 -0
- package/src/headers/26/acorn_version.h +6 -0
- package/src/headers/26/aliased_buffer-inl.h +264 -0
- package/src/headers/26/aliased_buffer.h +205 -0
- package/src/headers/26/aliased_struct-inl.h +143 -0
- package/src/headers/26/aliased_struct.h +248 -0
- package/src/headers/26/amaro_version.h +6 -0
- package/src/headers/26/async_context_frame.h +33 -0
- package/src/headers/26/async_wrap-inl.h +123 -0
- package/src/headers/26/async_wrap.h +254 -0
- package/src/headers/26/base_object-inl.h +333 -0
- package/src/headers/26/base_object.h +339 -0
- package/src/headers/26/base_object_types.h +76 -0
- package/src/headers/26/blob_serializer_deserializer-inl.h +385 -0
- package/src/headers/26/blob_serializer_deserializer.h +142 -0
- package/src/headers/26/builtin_info.h +57 -0
- package/src/headers/26/callback_queue-inl.h +97 -0
- package/src/headers/26/callback_queue.h +79 -0
- package/src/headers/26/cares_wrap.h +475 -0
- package/src/headers/26/cleanup_queue-inl.h +36 -0
- package/src/headers/26/cleanup_queue.h +79 -0
- package/src/headers/26/compile_cache.h +134 -0
- package/src/headers/26/connect_wrap.h +26 -0
- package/src/headers/26/connection_wrap.h +30 -0
- package/src/headers/26/cppgc_helpers-inl.h +69 -0
- package/src/headers/26/cppgc_helpers.h +162 -0
- package/src/headers/26/crypto/crypto_aes.h +107 -0
- package/src/headers/26/crypto/crypto_argon2.h +88 -0
- package/src/headers/26/crypto/crypto_bio.h +193 -0
- package/src/headers/26/crypto/crypto_chacha20_poly1305.h +62 -0
- package/src/headers/26/crypto/crypto_cipher.h +291 -0
- package/src/headers/26/crypto/crypto_common.h +48 -0
- package/src/headers/26/crypto/crypto_context.h +219 -0
- package/src/headers/26/crypto/crypto_dh.h +104 -0
- package/src/headers/26/crypto/crypto_dsa.h +50 -0
- package/src/headers/26/crypto/crypto_ec.h +102 -0
- package/src/headers/26/crypto/crypto_hash.h +157 -0
- package/src/headers/26/crypto/crypto_hkdf.h +62 -0
- package/src/headers/26/crypto/crypto_hmac.h +92 -0
- package/src/headers/26/crypto/crypto_kem.h +131 -0
- package/src/headers/26/crypto/crypto_keygen.h +382 -0
- package/src/headers/26/crypto/crypto_keys.h +366 -0
- package/src/headers/26/crypto/crypto_kmac.h +79 -0
- package/src/headers/26/crypto/crypto_pbkdf2.h +76 -0
- package/src/headers/26/crypto/crypto_pqc.h +39 -0
- package/src/headers/26/crypto/crypto_random.h +127 -0
- package/src/headers/26/crypto/crypto_rsa.h +108 -0
- package/src/headers/26/crypto/crypto_scrypt.h +85 -0
- package/src/headers/26/crypto/crypto_sig.h +159 -0
- package/src/headers/26/crypto/crypto_spkac.h +21 -0
- package/src/headers/26/crypto/crypto_timing.h +20 -0
- package/src/headers/26/crypto/crypto_tls.h +342 -0
- package/src/headers/26/crypto/crypto_turboshake.h +105 -0
- package/src/headers/26/crypto/crypto_util.h +775 -0
- package/src/headers/26/crypto/crypto_x509.h +140 -0
- package/src/headers/26/dataqueue/queue.h +311 -0
- package/src/headers/26/debug_utils-inl.h +291 -0
- package/src/headers/26/debug_utils.h +200 -0
- package/src/headers/26/diagnosticfilename-inl.h +33 -0
- package/src/headers/26/embedded_data.h +17 -0
- package/src/headers/26/encoding_binding.h +59 -0
- package/src/headers/26/env-inl.h +956 -0
- package/src/headers/26/env.h +1294 -0
- package/src/headers/26/env_properties.h +562 -0
- package/src/headers/26/ffi/data.h +30 -0
- package/src/headers/26/ffi/fast.h +96 -0
- package/src/headers/26/ffi/jit_memory.h +26 -0
- package/src/headers/26/ffi/types.h +99 -0
- package/src/headers/26/handle_wrap.h +123 -0
- package/src/headers/26/histogram-inl.h +211 -0
- package/src/headers/26/histogram.h +474 -0
- package/src/headers/26/inspector/dom_storage_agent.h +68 -0
- package/src/headers/26/inspector/inspector_object_utils.h +38 -0
- package/src/headers/26/inspector/io_agent.h +30 -0
- package/src/headers/26/inspector/main_thread_interface.h +116 -0
- package/src/headers/26/inspector/network_agent.h +110 -0
- package/src/headers/26/inspector/network_inspector.h +45 -0
- package/src/headers/26/inspector/network_requests_buffer.h +195 -0
- package/src/headers/26/inspector/network_resource_manager.h +29 -0
- package/src/headers/26/inspector/node_json.h +24 -0
- package/src/headers/26/inspector/node_string.h +101 -0
- package/src/headers/26/inspector/notification_emitter.h +38 -0
- package/src/headers/26/inspector/protocol_helper.h +27 -0
- package/src/headers/26/inspector/runtime_agent.h +41 -0
- package/src/headers/26/inspector/storage_agent.h +33 -0
- package/src/headers/26/inspector/target_agent.h +59 -0
- package/src/headers/26/inspector/target_manager.h +70 -0
- package/src/headers/26/inspector/tracing_agent.h +43 -0
- package/src/headers/26/inspector/worker_agent.h +40 -0
- package/src/headers/26/inspector/worker_inspector.h +124 -0
- package/src/headers/26/inspector_agent.h +168 -0
- package/src/headers/26/inspector_io.h +78 -0
- package/src/headers/26/inspector_profiler.h +149 -0
- package/src/headers/26/inspector_socket.h +60 -0
- package/src/headers/26/inspector_socket_server.h +110 -0
- package/src/headers/26/js_native_api.h +634 -0
- package/src/headers/26/js_native_api_types.h +244 -0
- package/src/headers/26/js_native_api_v8.h +485 -0
- package/src/headers/26/js_native_api_v8_internals.h +45 -0
- package/src/headers/26/js_stream.h +57 -0
- package/src/headers/26/json_utils.h +172 -0
- package/src/headers/26/large_pages/node_large_page.h +34 -0
- package/src/headers/26/lru_cache-inl.h +69 -0
- package/src/headers/26/memory_tracker-inl.h +461 -0
- package/src/headers/26/memory_tracker.h +341 -0
- package/src/headers/26/module_wrap.h +231 -0
- package/src/headers/26/ncrypto.h +2008 -0
- package/src/headers/26/node.h +1693 -0
- package/src/headers/26/node_api.h +265 -0
- package/src/headers/26/node_api_internals.h +48 -0
- package/src/headers/26/node_api_types.h +58 -0
- package/src/headers/26/node_binding.h +170 -0
- package/src/headers/26/node_blob.h +160 -0
- package/src/headers/26/node_bob-inl.h +36 -0
- package/src/headers/26/node_bob.h +107 -0
- package/src/headers/26/node_buffer.h +92 -0
- package/src/headers/26/node_builtins.h +239 -0
- package/src/headers/26/node_concepts.h +46 -0
- package/src/headers/26/node_config_file.h +77 -0
- package/src/headers/26/node_constants.h +82 -0
- package/src/headers/26/node_context_data.h +171 -0
- package/src/headers/26/node_contextify.h +261 -0
- package/src/headers/26/node_crypto.h +66 -0
- package/src/headers/26/node_debug.h +30 -0
- package/src/headers/26/node_diagnostics_channel.h +121 -0
- package/src/headers/26/node_dir.h +52 -0
- package/src/headers/26/node_dotenv.h +45 -0
- package/src/headers/26/node_errors.h +378 -0
- package/src/headers/26/node_exit_code.h +54 -0
- package/src/headers/26/node_external_reference.h +171 -0
- package/src/headers/26/node_ffi.h +212 -0
- package/src/headers/26/node_file-inl.h +421 -0
- package/src/headers/26/node_file.h +561 -0
- package/src/headers/26/node_file_utils.h +39 -0
- package/src/headers/26/node_hash.h +212 -0
- package/src/headers/26/node_http2.h +1202 -0
- package/src/headers/26/node_http2_state.h +159 -0
- package/src/headers/26/node_http_common-inl.h +199 -0
- package/src/headers/26/node_http_common.h +535 -0
- package/src/headers/26/node_i18n.h +132 -0
- package/src/headers/26/node_internals.h +485 -0
- package/src/headers/26/node_locks.h +184 -0
- package/src/headers/26/node_main_instance.h +67 -0
- package/src/headers/26/node_mem-inl.h +113 -0
- package/src/headers/26/node_mem.h +45 -0
- package/src/headers/26/node_messaging.h +389 -0
- package/src/headers/26/node_metadata.h +172 -0
- package/src/headers/26/node_modules.h +102 -0
- package/src/headers/26/node_mutex.h +323 -0
- package/src/headers/26/node_object_wrap.h +145 -0
- package/src/headers/26/node_options-inl.h +524 -0
- package/src/headers/26/node_options.h +710 -0
- package/src/headers/26/node_perf.h +167 -0
- package/src/headers/26/node_perf_common.h +103 -0
- package/src/headers/26/node_platform.h +290 -0
- package/src/headers/26/node_process-inl.h +26 -0
- package/src/headers/26/node_process.h +112 -0
- package/src/headers/26/node_profiling.h +38 -0
- package/src/headers/26/node_realm-inl.h +156 -0
- package/src/headers/26/node_realm.h +236 -0
- package/src/headers/26/node_report.h +43 -0
- package/src/headers/26/node_revert.h +82 -0
- package/src/headers/26/node_root_certs.h +2937 -0
- package/src/headers/26/node_sea.h +101 -0
- package/src/headers/26/node_shadow_realm.h +46 -0
- package/src/headers/26/node_snapshot_builder.h +57 -0
- package/src/headers/26/node_snapshotable.h +167 -0
- package/src/headers/26/node_sockaddr-inl.h +248 -0
- package/src/headers/26/node_sockaddr.h +459 -0
- package/src/headers/26/node_sqlite.h +607 -0
- package/src/headers/26/node_stat_watcher.h +73 -0
- package/src/headers/26/node_task_runner.h +92 -0
- package/src/headers/26/node_threadsafe_cow-inl.h +54 -0
- package/src/headers/26/node_threadsafe_cow.h +105 -0
- package/src/headers/26/node_union_bytes.h +81 -0
- package/src/headers/26/node_url.h +101 -0
- package/src/headers/26/node_url_pattern.h +118 -0
- package/src/headers/26/node_v8.h +78 -0
- package/src/headers/26/node_v8_embedder.h +26 -0
- package/src/headers/26/node_v8_platform-inl.h +96 -0
- package/src/headers/26/node_version.h +116 -0
- package/src/headers/26/node_wasi.h +184 -0
- package/src/headers/26/node_wasm_web_api.h +55 -0
- package/src/headers/26/node_watchdog.h +164 -0
- package/src/headers/26/node_webstorage.h +62 -0
- package/src/headers/26/node_worker.h +162 -0
- package/src/headers/26/path.h +36 -0
- package/src/headers/26/permission/boolean_permission.h +50 -0
- package/src/headers/26/permission/fs_permission.h +192 -0
- package/src/headers/26/permission/permission.h +155 -0
- package/src/headers/26/permission/permission_base.h +79 -0
- package/src/headers/26/pipe_wrap.h +80 -0
- package/src/headers/26/quic/application.h +279 -0
- package/src/headers/26/quic/arena.h +373 -0
- package/src/headers/26/quic/bindingdata.h +440 -0
- package/src/headers/26/quic/cid.h +127 -0
- package/src/headers/26/quic/data.h +358 -0
- package/src/headers/26/quic/defs.h +417 -0
- package/src/headers/26/quic/endpoint.h +519 -0
- package/src/headers/26/quic/guard.h +5 -0
- package/src/headers/26/quic/http3.h +25 -0
- package/src/headers/26/quic/packet.h +166 -0
- package/src/headers/26/quic/preferredaddress.h +72 -0
- package/src/headers/26/quic/session.h +770 -0
- package/src/headers/26/quic/session_manager.h +107 -0
- package/src/headers/26/quic/sessionticket.h +110 -0
- package/src/headers/26/quic/streams.h +518 -0
- package/src/headers/26/quic/tlscontext.h +353 -0
- package/src/headers/26/quic/tokens.h +263 -0
- package/src/headers/26/quic/transportparams.h +184 -0
- package/src/headers/26/req_wrap-inl.h +175 -0
- package/src/headers/26/req_wrap.h +79 -0
- package/src/headers/26/spawn_sync.h +243 -0
- package/src/headers/26/stream_base-inl.h +170 -0
- package/src/headers/26/stream_base.h +487 -0
- package/src/headers/26/stream_pipe.h +76 -0
- package/src/headers/26/stream_wrap.h +137 -0
- package/src/headers/26/string_bytes.h +114 -0
- package/src/headers/26/string_decoder-inl.h +31 -0
- package/src/headers/26/string_decoder.h +49 -0
- package/src/headers/26/tcp_wrap.h +138 -0
- package/src/headers/26/threadpoolwork-inl.h +70 -0
- package/src/headers/26/timer_wrap-inl.h +32 -0
- package/src/headers/26/timer_wrap.h +87 -0
- package/src/headers/26/timers.h +70 -0
- package/src/headers/26/tracing/agent.h +125 -0
- package/src/headers/26/tracing/agent_legacy.h +146 -0
- package/src/headers/26/tracing/agent_perfetto.h +145 -0
- package/src/headers/26/tracing/node_trace_buffer.h +91 -0
- package/src/headers/26/tracing/node_trace_writer.h +79 -0
- package/src/headers/26/tracing/trace_event.h +27 -0
- package/src/headers/26/tracing/trace_event_helper.h +40 -0
- package/src/headers/26/tracing/trace_event_legacy.h +1113 -0
- package/src/headers/26/tracing/trace_event_legacy_inl.h +711 -0
- package/src/headers/26/tracing/trace_event_perfetto.h +47 -0
- package/src/headers/26/tracing/traced_value.h +167 -0
- package/src/headers/26/tty_wrap.h +65 -0
- package/src/headers/26/udp_wrap.h +231 -0
- package/src/headers/26/undici_version.h +6 -0
- package/src/headers/26/util-inl.h +788 -0
- package/src/headers/26/util.h +1207 -0
- package/src/headers/26/v8-cpp-heap-external.h +56 -0
- package/src/headers/26/v8-external-memory-accounter.h +60 -0
- package/src/headers/26/v8-fast-api-calls.h +784 -0
- package/src/headers/26/v8-inspector-protocol.h +13 -0
- package/src/headers/26/v8-inspector.h +458 -0
- package/src/headers/26/v8-metrics.h +273 -0
- package/src/headers/26/v8-trace-categories.h +28 -0
- package/src/headers/26/v8-unwinder-state.h +30 -0
- package/src/headers/26/v8-util.h +549 -0
- package/src/headers/26/v8-value-serializer-version.h +24 -0
- package/src/headers/26/v8-version-string.h +38 -0
- package/src/headers/26/v8-wasm-trap-handler-posix.h +31 -0
- package/src/headers/26/v8-wasm-trap-handler-win.h +28 -0
- package/src/headers/26/zlib_version.h +6 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
4
|
+
|
|
5
|
+
#include <memory_tracker-inl.h>
|
|
6
|
+
#include <util.h>
|
|
7
|
+
#include <cstddef>
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <new>
|
|
11
|
+
#include <utility>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace node::quic {
|
|
15
|
+
|
|
16
|
+
// ArenaPool<T> is a block-based arena allocator for fixed-size objects.
|
|
17
|
+
//
|
|
18
|
+
// Objects are allocated from contiguous memory blocks ("slabs"), reducing
|
|
19
|
+
// heap fragmentation and allocation overhead. Each block contains a fixed
|
|
20
|
+
// number of equally-sized slots. Free slots form an intrusive singly-linked
|
|
21
|
+
// list for O(1) acquire/release.
|
|
22
|
+
//
|
|
23
|
+
// Each slot can optionally hold extra bytes after T (e.g., a trailing data
|
|
24
|
+
// buffer). The extra_bytes parameter is set at pool construction time and
|
|
25
|
+
// applies uniformly to all slots. All slots are allocated at the same size
|
|
26
|
+
// regardless of how much of the extra space is actually used, to prevent
|
|
27
|
+
// fragmentation.
|
|
28
|
+
//
|
|
29
|
+
// ArenaPool<T>::Ptr is a move-only RAII smart pointer that releases slots
|
|
30
|
+
// back to the pool on destruction. Ptr::release() detaches ownership for
|
|
31
|
+
// handoff to external systems (e.g., libuv), after which the caller must
|
|
32
|
+
// eventually call ArenaPool<T>::Release(raw_ptr) to return the slot.
|
|
33
|
+
//
|
|
34
|
+
// The pool supports lazy GC: when the ratio of free slots to total slots
|
|
35
|
+
// exceeds a threshold, fully-unused blocks are reclaimed. At least one
|
|
36
|
+
// block is always retained.
|
|
37
|
+
template <typename T>
|
|
38
|
+
class ArenaPool final : public MemoryRetainer {
|
|
39
|
+
public:
|
|
40
|
+
class Ptr;
|
|
41
|
+
|
|
42
|
+
// extra_bytes: additional memory available after T in each slot.
|
|
43
|
+
// All slots are sized identically regardless of how
|
|
44
|
+
// much extra space is actually used.
|
|
45
|
+
// slots_per_block: number of T slots per allocation block.
|
|
46
|
+
explicit ArenaPool(size_t extra_bytes = 0,
|
|
47
|
+
size_t slots_per_block = kDefaultSlotsPerBlock);
|
|
48
|
+
~ArenaPool();
|
|
49
|
+
|
|
50
|
+
ArenaPool(const ArenaPool&) = delete;
|
|
51
|
+
ArenaPool& operator=(const ArenaPool&) = delete;
|
|
52
|
+
ArenaPool(ArenaPool&&) = delete;
|
|
53
|
+
ArenaPool& operator=(ArenaPool&&) = delete;
|
|
54
|
+
|
|
55
|
+
// Construct T in an acquired slot with forwarded args.
|
|
56
|
+
// Returns an empty Ptr only on allocation failure.
|
|
57
|
+
template <typename... Args>
|
|
58
|
+
[[nodiscard]] Ptr Acquire(Args&&... args);
|
|
59
|
+
|
|
60
|
+
// Construct T with (extra_data_ptr, extra_bytes, ...args).
|
|
61
|
+
// Use this for types whose constructor accepts a trailing data
|
|
62
|
+
// buffer as its first two parameters.
|
|
63
|
+
template <typename... Args>
|
|
64
|
+
[[nodiscard]] Ptr AcquireExtra(Args&&... args);
|
|
65
|
+
|
|
66
|
+
// Release a raw T* previously detached via Ptr::release().
|
|
67
|
+
// Calls ~T() and returns the slot to the pool's free list.
|
|
68
|
+
// Recovers the pool instance from T*'s slot metadata.
|
|
69
|
+
static void Release(T* obj);
|
|
70
|
+
|
|
71
|
+
// Attempt to reclaim fully-unused blocks. Called automatically
|
|
72
|
+
// from Release/ReleaseSlot when the pool is over-provisioned.
|
|
73
|
+
void MaybeGC();
|
|
74
|
+
|
|
75
|
+
// Free all unused blocks immediately, keeping at least one.
|
|
76
|
+
void Flush();
|
|
77
|
+
|
|
78
|
+
size_t extra_bytes() const { return extra_bytes_; }
|
|
79
|
+
size_t slot_size() const { return slot_size_; }
|
|
80
|
+
size_t total_slots() const { return total_slots_; }
|
|
81
|
+
size_t in_use_count() const { return in_use_count_; }
|
|
82
|
+
size_t block_count() const { return blocks_.size(); }
|
|
83
|
+
|
|
84
|
+
void MemoryInfo(MemoryTracker* tracker) const override;
|
|
85
|
+
SET_MEMORY_INFO_NAME(ArenaPool)
|
|
86
|
+
SET_SELF_SIZE(ArenaPool)
|
|
87
|
+
|
|
88
|
+
static constexpr size_t kDefaultSlotsPerBlock = 128;
|
|
89
|
+
|
|
90
|
+
private:
|
|
91
|
+
// -------------------------------------------------------------------
|
|
92
|
+
// Slot layout in memory:
|
|
93
|
+
//
|
|
94
|
+
// [0, kObjectOffset) SlotHeader
|
|
95
|
+
// [kObjectOffset, kObjectOffset+sizeof(T)) T object
|
|
96
|
+
// [kObjectOffset+sizeof(T), slot_size_) extra bytes + padding
|
|
97
|
+
//
|
|
98
|
+
// SlotHeader is only used when the slot is on the free list.
|
|
99
|
+
// When the slot is acquired, the T object occupies its storage
|
|
100
|
+
// and the header fields are not accessed.
|
|
101
|
+
// -------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
struct Block;
|
|
104
|
+
|
|
105
|
+
struct SlotHeader {
|
|
106
|
+
SlotHeader* next_free;
|
|
107
|
+
Block* block;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
struct Block {
|
|
111
|
+
std::unique_ptr<char[]> memory;
|
|
112
|
+
ArenaPool* pool;
|
|
113
|
+
size_t capacity;
|
|
114
|
+
size_t in_use_count;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
static constexpr size_t kObjectOffset =
|
|
118
|
+
(sizeof(SlotHeader) + alignof(T) - 1) & ~(alignof(T) - 1);
|
|
119
|
+
|
|
120
|
+
// Slot ↔ T* conversion using the compile-time offset.
|
|
121
|
+
static T* ObjectFromSlot(SlotHeader* slot) {
|
|
122
|
+
return reinterpret_cast<T*>(reinterpret_cast<char*>(slot) + kObjectOffset);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static SlotHeader* SlotFromObject(T* obj) {
|
|
126
|
+
return reinterpret_cast<SlotHeader*>(reinterpret_cast<char*>(obj) -
|
|
127
|
+
kObjectOffset);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static uint8_t* ExtraDataFromSlot(SlotHeader* slot) {
|
|
131
|
+
return reinterpret_cast<uint8_t*>(reinterpret_cast<char*>(slot) +
|
|
132
|
+
kObjectOffset + sizeof(T));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
SlotHeader* SlotAt(Block* block, size_t index) {
|
|
136
|
+
return reinterpret_cast<SlotHeader*>(block->memory.get() +
|
|
137
|
+
index * slot_size_);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
SlotHeader* AcquireSlot();
|
|
141
|
+
void ReleaseSlot(SlotHeader* slot);
|
|
142
|
+
bool Grow();
|
|
143
|
+
void FreeEmptyBlocks();
|
|
144
|
+
void RemoveBlockFromFreeList(Block* block);
|
|
145
|
+
|
|
146
|
+
const size_t extra_bytes_;
|
|
147
|
+
const size_t slots_per_block_;
|
|
148
|
+
const size_t slot_size_;
|
|
149
|
+
|
|
150
|
+
SlotHeader* free_list_ = nullptr;
|
|
151
|
+
std::vector<std::unique_ptr<Block>> blocks_;
|
|
152
|
+
size_t total_slots_ = 0;
|
|
153
|
+
size_t in_use_count_ = 0;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// =====================================================================
|
|
157
|
+
// ArenaPool<T>::Ptr — Move-only RAII smart pointer
|
|
158
|
+
// =====================================================================
|
|
159
|
+
|
|
160
|
+
template <typename T>
|
|
161
|
+
class ArenaPool<T>::Ptr final {
|
|
162
|
+
public:
|
|
163
|
+
Ptr() = default;
|
|
164
|
+
~Ptr() { reset(); }
|
|
165
|
+
|
|
166
|
+
Ptr(Ptr&& other) noexcept : slot_(other.slot_) { other.slot_ = nullptr; }
|
|
167
|
+
|
|
168
|
+
Ptr& operator=(Ptr&& other) noexcept {
|
|
169
|
+
if (this != &other) {
|
|
170
|
+
reset();
|
|
171
|
+
slot_ = other.slot_;
|
|
172
|
+
other.slot_ = nullptr;
|
|
173
|
+
}
|
|
174
|
+
return *this;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
Ptr(const Ptr&) = delete;
|
|
178
|
+
Ptr& operator=(const Ptr&) = delete;
|
|
179
|
+
|
|
180
|
+
T* get() const { return slot_ ? ObjectFromSlot(slot_) : nullptr; }
|
|
181
|
+
T* operator->() const {
|
|
182
|
+
DCHECK(slot_);
|
|
183
|
+
return ObjectFromSlot(slot_);
|
|
184
|
+
}
|
|
185
|
+
T& operator*() const {
|
|
186
|
+
DCHECK(slot_);
|
|
187
|
+
return *ObjectFromSlot(slot_);
|
|
188
|
+
}
|
|
189
|
+
explicit operator bool() const { return slot_ != nullptr; }
|
|
190
|
+
|
|
191
|
+
// Access the extra data region after T in the slot.
|
|
192
|
+
uint8_t* extra_data() const {
|
|
193
|
+
return slot_ ? ExtraDataFromSlot(slot_) : nullptr;
|
|
194
|
+
}
|
|
195
|
+
size_t extra_bytes() const {
|
|
196
|
+
return slot_ ? slot_->block->pool->extra_bytes_ : 0;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Detach ownership. The caller takes responsibility for eventually
|
|
200
|
+
// calling ArenaPool<T>::Release(ptr) to destruct T and return
|
|
201
|
+
// the slot to the pool.
|
|
202
|
+
[[nodiscard]] T* release() noexcept {
|
|
203
|
+
if (!slot_) return nullptr;
|
|
204
|
+
T* obj = ObjectFromSlot(slot_);
|
|
205
|
+
slot_ = nullptr;
|
|
206
|
+
return obj;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Destruct T and return the slot to the pool. Ptr becomes empty.
|
|
210
|
+
void reset() {
|
|
211
|
+
if (slot_) {
|
|
212
|
+
ObjectFromSlot(slot_)->~T();
|
|
213
|
+
slot_->block->pool->ReleaseSlot(slot_);
|
|
214
|
+
slot_ = nullptr;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private:
|
|
219
|
+
friend class ArenaPool<T>;
|
|
220
|
+
explicit Ptr(SlotHeader* slot) : slot_(slot) {}
|
|
221
|
+
SlotHeader* slot_ = nullptr;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// =====================================================================
|
|
225
|
+
// ArenaPool<T> implementation
|
|
226
|
+
// =====================================================================
|
|
227
|
+
|
|
228
|
+
template <typename T>
|
|
229
|
+
ArenaPool<T>::ArenaPool(size_t extra_bytes, size_t slots_per_block)
|
|
230
|
+
: extra_bytes_(extra_bytes),
|
|
231
|
+
slots_per_block_(slots_per_block),
|
|
232
|
+
slot_size_(((kObjectOffset + sizeof(T) + extra_bytes) +
|
|
233
|
+
alignof(std::max_align_t) - 1) &
|
|
234
|
+
~(alignof(std::max_align_t) - 1)) {
|
|
235
|
+
DCHECK_GT(slots_per_block, 0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
template <typename T>
|
|
239
|
+
ArenaPool<T>::~ArenaPool() {
|
|
240
|
+
DCHECK_EQ(in_use_count_, 0);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
template <typename T>
|
|
244
|
+
template <typename... Args>
|
|
245
|
+
typename ArenaPool<T>::Ptr ArenaPool<T>::Acquire(Args&&... args) {
|
|
246
|
+
SlotHeader* slot = AcquireSlot();
|
|
247
|
+
if (!slot) return Ptr();
|
|
248
|
+
T* obj = new (ObjectFromSlot(slot)) T(std::forward<Args>(args)...);
|
|
249
|
+
CHECK_EQ(obj, ObjectFromSlot(slot));
|
|
250
|
+
return Ptr(slot);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
template <typename T>
|
|
254
|
+
template <typename... Args>
|
|
255
|
+
typename ArenaPool<T>::Ptr ArenaPool<T>::AcquireExtra(Args&&... args) {
|
|
256
|
+
SlotHeader* slot = AcquireSlot();
|
|
257
|
+
if (!slot) return Ptr();
|
|
258
|
+
T* obj = new (ObjectFromSlot(slot))
|
|
259
|
+
T(ExtraDataFromSlot(slot), extra_bytes_, std::forward<Args>(args)...);
|
|
260
|
+
CHECK_EQ(obj, ObjectFromSlot(slot));
|
|
261
|
+
return Ptr(slot);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
template <typename T>
|
|
265
|
+
void ArenaPool<T>::Release(T* obj) {
|
|
266
|
+
DCHECK_NOT_NULL(obj);
|
|
267
|
+
SlotHeader* slot = SlotFromObject(obj);
|
|
268
|
+
DCHECK_NOT_NULL(slot->block);
|
|
269
|
+
DCHECK_NOT_NULL(slot->block->pool);
|
|
270
|
+
obj->~T();
|
|
271
|
+
slot->block->pool->ReleaseSlot(slot);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
template <typename T>
|
|
275
|
+
typename ArenaPool<T>::SlotHeader* ArenaPool<T>::AcquireSlot() {
|
|
276
|
+
if (!free_list_) {
|
|
277
|
+
if (!Grow()) return nullptr;
|
|
278
|
+
}
|
|
279
|
+
DCHECK_NOT_NULL(free_list_);
|
|
280
|
+
SlotHeader* slot = free_list_;
|
|
281
|
+
free_list_ = slot->next_free;
|
|
282
|
+
slot->next_free = nullptr;
|
|
283
|
+
slot->block->in_use_count++;
|
|
284
|
+
in_use_count_++;
|
|
285
|
+
return slot;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
template <typename T>
|
|
289
|
+
void ArenaPool<T>::ReleaseSlot(SlotHeader* slot) {
|
|
290
|
+
DCHECK_NOT_NULL(slot);
|
|
291
|
+
DCHECK_NOT_NULL(slot->block);
|
|
292
|
+
DCHECK_GT(slot->block->in_use_count, 0);
|
|
293
|
+
DCHECK_GT(in_use_count_, 0);
|
|
294
|
+
|
|
295
|
+
slot->block->in_use_count--;
|
|
296
|
+
in_use_count_--;
|
|
297
|
+
slot->next_free = free_list_;
|
|
298
|
+
free_list_ = slot;
|
|
299
|
+
|
|
300
|
+
MaybeGC();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
template <typename T>
|
|
304
|
+
bool ArenaPool<T>::Grow() {
|
|
305
|
+
auto block = std::make_unique<Block>();
|
|
306
|
+
block->pool = this;
|
|
307
|
+
block->capacity = slots_per_block_;
|
|
308
|
+
block->in_use_count = 0;
|
|
309
|
+
block->memory.reset(new char[slots_per_block_ * slot_size_]());
|
|
310
|
+
|
|
311
|
+
// Initialize slot headers and chain onto free list.
|
|
312
|
+
for (size_t i = 0; i < slots_per_block_; i++) {
|
|
313
|
+
SlotHeader* slot = SlotAt(block.get(), i);
|
|
314
|
+
slot->block = block.get();
|
|
315
|
+
slot->next_free = free_list_;
|
|
316
|
+
free_list_ = slot;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
total_slots_ += slots_per_block_;
|
|
320
|
+
blocks_.push_back(std::move(block));
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
template <typename T>
|
|
325
|
+
void ArenaPool<T>::MaybeGC() {
|
|
326
|
+
// Only GC when we have excess capacity: more than one block and
|
|
327
|
+
// less than half the slots are in use.
|
|
328
|
+
if (blocks_.size() <= 1 || in_use_count_ >= total_slots_ / 2) return;
|
|
329
|
+
FreeEmptyBlocks();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
template <typename T>
|
|
333
|
+
void ArenaPool<T>::Flush() {
|
|
334
|
+
FreeEmptyBlocks();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
template <typename T>
|
|
338
|
+
void ArenaPool<T>::FreeEmptyBlocks() {
|
|
339
|
+
for (auto it = blocks_.begin(); it != blocks_.end();) {
|
|
340
|
+
if ((*it)->in_use_count == 0 && blocks_.size() > 1) {
|
|
341
|
+
RemoveBlockFromFreeList(it->get());
|
|
342
|
+
total_slots_ -= (*it)->capacity;
|
|
343
|
+
it = blocks_.erase(it);
|
|
344
|
+
} else {
|
|
345
|
+
++it;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
template <typename T>
|
|
351
|
+
void ArenaPool<T>::RemoveBlockFromFreeList(Block* block) {
|
|
352
|
+
char* block_start = block->memory.get();
|
|
353
|
+
char* block_end = block_start + block->capacity * slot_size_;
|
|
354
|
+
|
|
355
|
+
SlotHeader** pp = &free_list_;
|
|
356
|
+
while (*pp) {
|
|
357
|
+
char* addr = reinterpret_cast<char*>(*pp);
|
|
358
|
+
if (addr >= block_start && addr < block_end) {
|
|
359
|
+
*pp = (*pp)->next_free;
|
|
360
|
+
} else {
|
|
361
|
+
pp = &(*pp)->next_free;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
template <typename T>
|
|
367
|
+
void ArenaPool<T>::MemoryInfo(MemoryTracker* tracker) const {
|
|
368
|
+
tracker->TrackFieldWithSize("blocks", total_slots_ * slot_size_);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
} // namespace node::quic
|
|
372
|
+
|
|
373
|
+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|