Skip to content
Discussion options

You must be logged in to vote

tokio always use the allocator specified by the #[global_allocator]

I tried a very simple toy profiler, which correctly reports the memory usage.

#![allow(unused_variables)]

use tokio::task::JoinSet;
use std::alloc::{GlobalAlloc, System, Layout};
use std::sync::atomic::{AtomicUsize, Ordering::*};

static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
static DEALLOCATED: AtomicUsize = AtomicUsize::new(0);

struct MyAllocator;

#[global_allocator]
static GLOBAL: MyAllocator = MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        ALLOCATED.fetch_add(layout.size(), Relaxed);
        unsafe { System.alloc(layout) }
    }

    un…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@spoutn1k
Comment options

Answer selected by spoutn1k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants