summaryrefslogtreecommitdiff
path: root/src/gallium/frontends/rusticl/mesa/util/disk_cache.rs
blob: 55f7dbd60b8b0e28f8cd882ba66c5c7b6926151c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use mesa_rust_gen::*;

use std::ffi::CString;
use std::ops::Deref;
use std::ptr;
use std::ptr::NonNull;
use std::slice;

pub struct DiskCacheBorrowed {
    cache: NonNull<disk_cache>,
}

pub struct DiskCache {
    inner: DiskCacheBorrowed,
}

// disk_cache is thread safe
unsafe impl Sync for DiskCacheBorrowed {}

impl DiskCacheBorrowed {
    pub fn from_ptr(cache: *mut disk_cache) -> Option<Self> {
        NonNull::new(cache).map(|c| Self { cache: c })
    }

    pub fn put(&self, data: &[u8], key: &mut cache_key) {
        unsafe {
            disk_cache_put(
                self.cache.as_ptr(),
                key,
                data.as_ptr().cast(),
                data.len(),
                ptr::null_mut(),
            );
        }
    }

    pub fn get(&self, key: &mut cache_key) -> Option<DiskCacheEntry> {
        let mut size = 0;

        unsafe {
            let data = disk_cache_get(self.cache.as_ptr(), key, &mut size);
            if data.is_null() {
                None
            } else {
                Some(DiskCacheEntry {
                    data: slice::from_raw_parts_mut(data.cast(), size),
                })
            }
        }
    }

    pub fn gen_key(&self, data: &[u8]) -> cache_key {
        let mut key = cache_key::default();

        unsafe {
            disk_cache_compute_key(
                self.cache.as_ptr(),
                data.as_ptr().cast(),
                data.len(),
                &mut key,
            );
        }

        key
    }

    pub fn as_ptr(s: &Option<Self>) -> *mut disk_cache {
        if let Some(s) = s {
            s.cache.as_ptr()
        } else {
            ptr::null_mut()
        }
    }
}

impl DiskCache {
    pub fn new(name: &str, driver_id: &str, flags: u64) -> Option<Self> {
        let c_name = CString::new(name).unwrap();
        let c_id = CString::new(driver_id).unwrap();
        let cache = unsafe { disk_cache_create(c_name.as_ptr(), c_id.as_ptr(), flags) };

        DiskCacheBorrowed::from_ptr(cache).map(|c| Self { inner: c })
    }
}

impl Deref for DiskCache {
    type Target = DiskCacheBorrowed;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl Drop for DiskCache {
    fn drop(&mut self) {
        unsafe {
            disk_cache_destroy(self.cache.as_ptr());
        }
    }
}

pub struct DiskCacheEntry<'a> {
    data: &'a mut [u8],
}

impl<'a> Deref for DiskCacheEntry<'a> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.data
    }
}

impl<'a> Drop for DiskCacheEntry<'a> {
    fn drop(&mut self) {
        unsafe {
            free(self.data.as_mut_ptr().cast());
        }
    }
}