• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / edge-runtime / 13558472317

27 Feb 2025 03:45AM UTC coverage: 50.441% (-0.02%) from 50.464%
13558472317

push

github

web-flow
build(deps): bring down hickory-proto from 0.25.0-alpha.5 to 0.25.0-alpha.4 (#507)

17462 of 34619 relevant lines covered (50.44%)

1897.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.08
/ext/runtime/external_memory.rs
1
use std::ffi::c_void;
2
use std::sync::atomic::AtomicUsize;
3
use std::sync::atomic::Ordering;
4
use std::sync::Arc;
5
use std::sync::RwLock;
6

7
use deno_core::v8;
8
use deno_core::v8::UniqueRef;
9
use futures::task::AtomicWaker;
10

11
pub struct CustomAllocator {
12
  max: usize,
13
  count: AtomicUsize,
14
  waker: RwLock<Option<Arc<AtomicWaker>>>,
15
}
16

17
impl CustomAllocator {
18
  pub fn new(max: usize) -> Arc<Self> {
168✔
19
    Arc::new(Self {
168✔
20
      max,
168✔
21
      count: AtomicUsize::new(0),
168✔
22
      waker: RwLock::new(None),
168✔
23
    })
168✔
24
  }
168✔
25

26
  pub fn set_waker(&self, waker: Arc<AtomicWaker>) {
168✔
27
    _ = self.waker.try_write().unwrap().insert(waker);
168✔
28
  }
168✔
29

30
  pub fn into_v8_allocator(
168✔
31
    self: Arc<Self>,
168✔
32
  ) -> UniqueRef<deno_core::v8::Allocator> {
168✔
33
    let vtable: &'static v8::RustAllocatorVtable<CustomAllocator> =
168✔
34
      &v8::RustAllocatorVtable {
168✔
35
        allocate,
168✔
36
        allocate_uninitialized,
168✔
37
        free,
168✔
38
        reallocate,
168✔
39
        drop,
168✔
40
      };
168✔
41

168✔
42
    unsafe { v8::new_rust_allocator(Arc::into_raw(self), vtable) }
168✔
43
  }
168✔
44

45
  fn wake(&self) {
69,098✔
46
    if let Some(waker) = self.waker.try_read().ok().and_then(|it| it.clone()) {
69,098✔
47
      waker.wake();
69,098✔
48
    }
69,098✔
49
  }
69,098✔
50
}
51

52
#[allow(clippy::unnecessary_cast)]
53
unsafe extern "C" fn allocate(
28,341✔
54
  allocator: &CustomAllocator,
28,341✔
55
  n: usize,
28,341✔
56
) -> *mut c_void {
28,341✔
57
  allocator.count.fetch_add(n, Ordering::SeqCst);
28,341✔
58

28,341✔
59
  let count_loaded = allocator.count.load(Ordering::SeqCst);
28,341✔
60

28,341✔
61
  if count_loaded > allocator.max {
28,341✔
62
    return std::ptr::null::<*mut [u8]>() as *mut c_void;
12✔
63
  }
28,329✔
64

28,329✔
65
  allocator.wake();
28,329✔
66

28,329✔
67
  Box::into_raw(vec![0u8; n].into_boxed_slice()) as *mut [u8] as *mut c_void
28,329✔
68
}
28,341✔
69

70
#[allow(clippy::unnecessary_cast)]
71
#[allow(clippy::uninit_vec)]
72
unsafe extern "C" fn allocate_uninitialized(
6,257✔
73
  allocator: &CustomAllocator,
6,257✔
74
  n: usize,
6,257✔
75
) -> *mut c_void {
6,257✔
76
  allocator.count.fetch_add(n, Ordering::SeqCst);
6,257✔
77

6,257✔
78
  let count_loaded = allocator.count.load(Ordering::SeqCst);
6,257✔
79

6,257✔
80
  if count_loaded > allocator.max {
6,257✔
81
    return std::ptr::null::<*mut [u8]>() as *mut c_void;
×
82
  }
6,257✔
83

6,257✔
84
  let mut store = Vec::with_capacity(n);
6,257✔
85

6,257✔
86
  store.set_len(n);
6,257✔
87
  allocator.wake();
6,257✔
88

6,257✔
89
  Box::into_raw(store.into_boxed_slice()) as *mut [u8] as *mut c_void
6,257✔
90
}
6,257✔
91

92
unsafe extern "C" fn free(
34,512✔
93
  allocator: &CustomAllocator,
34,512✔
94
  data: *mut c_void,
34,512✔
95
  n: usize,
34,512✔
96
) {
34,512✔
97
  allocator.count.fetch_sub(n, Ordering::SeqCst);
34,512✔
98
  allocator.wake();
34,512✔
99

34,512✔
100
  let _ = Box::from_raw(std::slice::from_raw_parts_mut(data as *mut u8, n));
34,512✔
101
}
34,512✔
102

103
#[allow(clippy::unnecessary_cast)]
104
unsafe extern "C" fn reallocate(
×
105
  allocator: &CustomAllocator,
×
106
  prev: *mut c_void,
×
107
  oldlen: usize,
×
108
  newlen: usize,
×
109
) -> *mut c_void {
×
110
  allocator
×
111
    .count
×
112
    .fetch_add(newlen.wrapping_sub(oldlen), Ordering::SeqCst);
×
113

×
114
  let count_loaded = allocator.count.load(Ordering::SeqCst);
×
115

×
116
  if count_loaded > allocator.max {
×
117
    return std::ptr::null::<*mut [u8]>() as *mut c_void;
×
118
  }
×
119

×
120
  let old_store =
×
121
    Box::from_raw(std::slice::from_raw_parts_mut(prev as *mut u8, oldlen));
×
122
  let mut new_store = Vec::with_capacity(newlen);
×
123
  let copy_len = oldlen.min(newlen);
×
124

×
125
  new_store.extend_from_slice(&old_store[..copy_len]);
×
126
  new_store.resize(newlen, 0u8);
×
127
  allocator.wake();
×
128

×
129
  Box::into_raw(new_store.into_boxed_slice()) as *mut [u8] as *mut c_void
×
130
}
×
131

132
unsafe extern "C" fn drop(allocator: *const CustomAllocator) {
166✔
133
  Arc::from_raw(allocator);
166✔
134
}
166✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc