use super::*;
fn registry() -> &'static sync::RwLock<HashMap<usize, InlineStartFn>> {
INLINE_HOOK_REGISTRY.get_or_init(|| sync::RwLock::new(HashMap::new()))
}
pub(crate) fn register_inline_hook(key: usize, start: InlineStartFn) {
if let Ok(mut map) = registry().write() {
map.insert(key, start);
}
}
pub(crate) fn unregister_inline_hook(key: usize) {
if key == 0 {
return;
}
if let Ok(mut map) = registry().write() {
map.remove(&key);
}
}
pub(crate) fn lookup_inline_hook(handler: &ServerHookHandler) -> CompiledHook {
let key: usize = Arc::as_ptr(handler) as *const () as usize;
if let Ok(map) = registry().read()
&& let Some(&init) = map.get(&key)
{
return CompiledHook::Inline(init);
}
CompiledHook::Dynamic(Arc::clone(handler))
}
async fn execute_server_hook<R>(stream_addr: usize, ctx_addr: usize) -> Status
where
R: ServerHook,
{
let stream: &mut Stream = stream_addr.into();
let ctx: &mut Context = ctx_addr.into();
R::new(stream, ctx).await.handle(stream, ctx).await
}
unsafe fn poll_inline_future<F>(ptr: *mut u8, cx: &mut task::Context<'_>) -> Poll<Status>
where
F: Future<Output = Status> + Send,
{
let future: Pin<&mut F> = unsafe { Pin::new_unchecked(&mut *ptr.cast::<F>()) };
future.poll(cx)
}
unsafe fn drop_inline_future<F>(ptr: *mut u8)
where
F: Future<Output = Status> + Send,
{
unsafe {
ptr::drop_in_place(ptr.cast::<F>());
}
}
#[inline(always)]
fn start_inline_future<F>(
future: F,
slot: *mut u8,
slot_len: usize,
cx: &mut task::Context<'_>,
) -> InlineStartOutcome
where
F: Future<Output = Status> + Send + 'static,
{
if mem::size_of::<F>() <= slot_len && mem::align_of::<F>() <= mem::align_of::<usize>() {
let dst: *mut F = slot.cast::<F>();
unsafe {
ptr::write(dst, future);
}
let guard: InlineDropGuard<F> = InlineDropGuard { ptr: dst };
let pinned: Pin<&mut F> = unsafe { Pin::new_unchecked(&mut *dst) };
match pinned.poll(cx) {
Poll::Ready(status) => {
drop(guard);
InlineStartOutcome::Ready(status)
}
Poll::Pending => {
mem::forget(guard);
InlineStartOutcome::PendingInline {
poll_fn: poll_inline_future::<F>,
drop_fn: drop_inline_future::<F>,
}
}
}
} else {
let mut boxed: FutureBox<Status> = Box::pin(future);
match boxed.as_mut().poll(cx) {
Poll::Ready(status) => InlineStartOutcome::Ready(status),
Poll::Pending => InlineStartOutcome::PendingBoxed(boxed),
}
}
}
pub(crate) fn inline_start_hook<R>(
stream_addr: usize,
ctx_addr: usize,
slot: *mut u8,
slot_len: usize,
cx: &mut task::Context<'_>,
) -> InlineStartOutcome
where
R: ServerHook,
{
start_inline_future(
execute_server_hook::<R>(stream_addr, ctx_addr),
slot,
slot_len,
cx,
)
}