Struct memflow::mem::memory_view::cached_view::CachedViewBuilder
source · pub struct CachedViewBuilder<T, Q> { /* private fields */ }
Implementations§
source§impl<T: MemoryView> CachedViewBuilder<T, DefaultCacheValidator>
impl<T: MemoryView> CachedViewBuilder<T, DefaultCacheValidator>
sourcepub fn new(mem: T) -> Self
pub fn new(mem: T) -> Self
Creates a new CachedView
builder.
The memory object is mandatory as the CachedView
struct wraps around it.
This type of cache also is required to know the exact page size of the target system.
This can either be set directly via the page_size()
method or via the arch()
method.
If no page size has been set this builder will fail to build the CachedView
.
Without further adjustments this function creates a cache that is 2 megabytes in size and caches pages that contain pagetable entries as well as read-only pages.
It is also possible to either let the CachedView
object own or just borrow the underlying memory object.
Examples
Moves ownership of a mem object and retrieves it back:
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_mem = CachedView::builder(virt_mem)
.arch(x64::ARCH)
.build()
.unwrap();
let addr = virt_base; // some arbitrary address
cached_mem.write(addr, &MAGIC_VALUE).unwrap();
let value: u64 = cached_mem.read(addr).unwrap();
assert_eq!(value, MAGIC_VALUE);
Borrowing a mem object:
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
fn build<T: MemoryView>(mem: Fwd<&mut T>)
-> impl MemoryView + '_ {
CachedView::builder(mem)
.arch(x64::ARCH)
.build()
.unwrap()
}
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_view = build(virt_mem.forward_mut());
let read = cached_view.read::<u32>(0.into()).unwrap();
source§impl<T: MemoryView, Q: CacheValidator> CachedViewBuilder<T, Q>
impl<T: MemoryView, Q: CacheValidator> CachedViewBuilder<T, Q>
sourcepub fn build<'a>(self) -> Result<CachedView<'a, T, Q>>
pub fn build<'a>(self) -> Result<CachedView<'a, T, Q>>
Builds the CachedView
object or returns an error if the page size is not set.
sourcepub fn validator<QN: CacheValidator>(
self,
validator: QN
) -> CachedViewBuilder<T, QN>
pub fn validator<QN: CacheValidator>( self, validator: QN ) -> CachedViewBuilder<T, QN>
Sets a custom validator for the cache.
If this function is not called it will default to a DefaultCacheValidator
.
The default validator for std builds is the [TimedCacheValidator
].
The default validator for no_std builds is the [CountCacheValidator
].
The default setting is DefaultCacheValidator::default()
.
Examples:
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
use std::time::Duration;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_mem = CachedView::builder(virt_mem)
.arch(x64::ARCH)
.validator(DefaultCacheValidator::new(Duration::from_millis(2000).into()))
.build()
.unwrap();
let addr = virt_base; // some arbitrary address
cached_mem.write(addr, &MAGIC_VALUE).unwrap();
let value: u64 = cached_mem.read(addr).unwrap();
assert_eq!(value, MAGIC_VALUE);
sourcepub fn page_size(self, page_size: usize) -> Self
pub fn page_size(self, page_size: usize) -> Self
Changes the page size of the cache.
The cache has to know the exact page size of the target system internally to give reasonable performance.
The page size can be either set directly via this function or it can be fetched from the Architecture
via the arch()
method of the builder.
If the page size is not set the builder will fail.
Examples
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_mem = CachedView::builder(virt_mem)
.page_size(size::kb(4))
.build()
.unwrap();
let addr = virt_base; // some arbitrary address
cached_mem.write(addr, &MAGIC_VALUE).unwrap();
let value: u64 = cached_mem.read(addr).unwrap();
assert_eq!(value, MAGIC_VALUE);
sourcepub fn arch(self, arch: impl Into<ArchitectureObj>) -> Self
pub fn arch(self, arch: impl Into<ArchitectureObj>) -> Self
Retrieves the page size for this cache from the given Architecture
.
The cache has to know the exact page size of the target system internally to give reasonable performance.
The page size can be either fetched from the Architecture
via this method or it can be set directly
via the page_size()
method of the builder.
If the page size is not set the builder will fail.
Examples
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_mem = CachedView::builder(virt_mem)
.arch(x64::ARCH)
.build()
.unwrap();
let addr = virt_base; // some arbitrary address
cached_mem.write(addr, &MAGIC_VALUE).unwrap();
let value: u64 = cached_mem.read(addr).unwrap();
assert_eq!(value, MAGIC_VALUE);
sourcepub fn cache_size(self, cache_size: usize) -> Self
pub fn cache_size(self, cache_size: usize) -> Self
Sets the total amount of cache to be used.
This is the total amount of cache (in bytes) this page cache will allocate. Ideally you’d want to keep this value low enough so that most of the cache stays in the lower level caches of your cpu.
The default setting is 2 megabytes.
This setting can drastically impact the performance of the cache.
Examples:
use memflow::prelude::v1::*;
use memflow::dummy::DummyMemory;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cached_mem = CachedView::builder(virt_mem)
.arch(x64::ARCH)
.cache_size(size::mb(2))
.build()
.unwrap();
let addr = virt_base; // some arbitrary address
cached_mem.write(addr, &MAGIC_VALUE).unwrap();
let value: u64 = cached_mem.read(addr).unwrap();
assert_eq!(value, MAGIC_VALUE);
Auto Trait Implementations§
impl<T, Q> RefUnwindSafe for CachedViewBuilder<T, Q>where
Q: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, Q> Send for CachedViewBuilder<T, Q>
impl<T, Q> Sync for CachedViewBuilder<T, Q>
impl<T, Q> Unpin for CachedViewBuilder<T, Q>
impl<T, Q> UnwindSafe for CachedViewBuilder<T, Q>where
Q: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> GetWithMetadata for T
impl<T> GetWithMetadata for T
§type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
WithMetadata_<Self, Self>
§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read more§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read more§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read more§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read more§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read more§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read more§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
§fn piped_ref<'a, F, U>(&'a self, f: F) -> U
fn piped_ref<'a, F, U>(&'a self, f: F) -> U
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read more§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more