惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
量子位
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
V
Visual Studio Blog
博客园 - 聂微东
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release Feedback about post in medium
Arc::increment_strong_count design question (cross-post)
@geebee22 Ge · 2026-04-22 · via The Rust Programming Language Forum - Latest topics

1

Cross-post: This is a cross-post from internals forum. Since it got no replies for a week I think it is safe to cross-post it here, as there will be no discussion overlap.


I was reading docs of Arc::increment_strong_count and in the safety section I found requirement that raw pointer passed to the function must point to valid allocation created by global allocator.

The pointer must have been obtained through Arc::into_raw and must satisfy the same layout requirements specified in Arc::from_raw_in. The associated Arc instance must be valid (i.e. the strong count must be at least 1) for the duration of this method, and ptr must point to a block of memory allocated by the global allocator.

This requirement surprised me, so I looked into implementation and after going though Arc::increment_strong_count_in layer I found that the implementation basically just creates Arc from raw pointer and clones it:

// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
let arc = unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) };
// Now increase refcount, but don't drop new refcount either
let _arc_clone: mem::ManuallyDrop<_> = arc.clone();

Why does the implementation of Arc::increment_strong_count simply not walk pointers and increment strong count directly? Requiring alloc to be passed into Arc::increment_strong_count seams overly constraining, since at no point the allocator is used at all.

I understand why Arc::decrement_strong_count needs access to the allocator, as it needs to drop inner T and ArcInner when strong and weak counters go to 0. Was Arc::increment_strong_count designed in such way to keep symmetry with decrementing part? Or is there some safety invariant that I do not see, that would be violated if Arc::increment_strong_count would call fetch_add on the strong counter directly?

2

I saw your post, and guessed someone better qualified to comment than myself would say something.

But FWIW I think you are right. I may well be wrong though, I haven't implemented Arc yet and thought about it in any detail. I don't have plans to implement it either, as I don't think Arc allocations are typically frequent.

Also, on unstable nightly, the memory could have come from any allocator, not necessarily Global. But that is perhaps some kind of nit-pick.