Crates.io | packed_ptr |
lib.rs | packed_ptr |
version | 0.1.2 |
source | src |
created_at | 2023-12-02 02:47:52.767195 |
updated_at | 2024-02-24 18:02:38.617615 |
description | A configurable & generic tagged pointer implementation in Rust |
homepage | |
repository | https://github.com/ansg191/packed_ptr |
max_upload_size | |
id | 1055837 |
size | 57,549 |
A configurable & generic tagged pointer implementation in Rust.
A Tagged Pointer is a technique to store a value and a tag (some data) in a single pointer. This implementation allows you to use both the LSB & MSB for the tag, configured using a generic parameter.
use packed_ptr::PackedPtr;
use packed_ptr::config::AlignOnly;
let data = 0xdeadbeefu32;
let ptr = PackedPtr::new(&data, 1, AlignOnly).unwrap();
assert_eq!(data, unsafe { *ptr.ptr() });
assert_eq!(1, ptr.data());
On x86_64
, using level 4 paging, the 16 most significant bits are unused.
Therefore, we can pack 18 bits of data into a *const u32
.
use packed_ptr::PackedPtr;
use packed_ptr::config::Level4Paging;
let data = 0xdeadbeefu32;
let ptr = PackedPtr::new(&data, (1 << 18) - 1, Level4Paging).unwrap();
assert_eq!(data, unsafe { *ptr.ptr() });
assert_eq!((1 << 18) - 1, ptr.data());
packed_ptr
provides a type safe interface for storing & retrieving data from a pointer.
The data must implement Packable
, and it can be packed into a pointer using TypedPackedPtr
.
use packed_ptr::TypedPackedPtr;
use packed_ptr::config::AlignOnly;
let data = 0xdeadbeefu32;
let packed = (true, false);
let ptr = TypedPackedPtr::new(&data, packed, AlignOnly).unwrap();
assert_eq!(data, unsafe { *ptr.ptr() });
assert_eq!(packed, ptr.data());
packed_ptr
also provides a type safe interface for storing & retrieving packed data from a reference.
use packed_ptr::PackedRef;
use packed_ptr::config::AlignOnly;
let data = 0xdeadbeefu32;
let packed = (true, false);
let ref1 = PackedRef::new(&data, packed, AlignOnly).unwrap();
assert_eq!(data, *ref1);
assert_eq!(packed, ref1.data());