Search code examples
pointersrustgarbage-collectionraw-pointercrossbeam

What does crossbeam_epoch::Shared::as_raw mean by "Converts the pointer to a raw pointer (without the tag)"?


Can someone translate this into something that makes sense for me:

Converts the pointer to a raw pointer (without the tag).

  1. What is the difference between a pointer and a raw pointer?

    The Stack Overflow raw-pointer tag says neither "smart" nor "shared" which again is mystifying.

  2. What are Crossbeam's Shared::as_raw's "tags" all about?


Solution

    • crossbeam_epoch::Shared is a smart pointer. That is, a pointer plus extra stuff. In C++ or Rust, smart pointer is the term used for a pointer wrapper which adds any of the following:

      • Ownership information
      • Lifetime information
      • Packing extra data in unused bits
      • Copy-on-write behavior
      • Reference counting

      In that context, a raw pointer is just the wrapped pointer, without all the extra stuff.

    • crossbeam_epoch::Shared fits (among others) in the “Packing extra data in unused bits” category above. Most data in modern computers is aligned, that is, addresses are a multiple of some power of two. This means that all low bits of the addresses are always 0. One can use that fact to store a few extra bits of information in a pointer.

      This extra data is called tag by this particular library, however that term isn't as common as raw pointer.