packed_ptr

Crates.iopacked_ptr
lib.rspacked_ptr
version0.1.2
sourcesrc
created_at2023-12-02 02:47:52.767195
updated_at2024-02-24 18:02:38.617615
descriptionA configurable & generic tagged pointer implementation in Rust
homepage
repositoryhttps://github.com/ansg191/packed_ptr
max_upload_size
id1055837
size57,549
Anshul Gupta (ansg191)

documentation

https://docs.rs/packed_ptr

README

Packed Pointers

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.

Usage

Portable Usage

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());

Platform Specific Usage

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());

Type Safe 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());

References

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());
Commit count: 17

cargo fmt