Skip to content

emi2k01/okaoka

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example

use jemallocator::Jemalloc;
use std::alloc::System;

/// Allocator that doesn't deallocate
struct LeakingAllocator;

unsafe impl std::alloc::GlobalAlloc for LeakingAllocator {
    unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
        Jemalloc.alloc(layout)
    }

    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: std::alloc::Layout) {}
}

okaoka::set_multi_global_allocator! {
    GlobalAllocator, // Name of our allocator facade
    AllocatorTag, // Name of our allocator tag enum
    System => System,
    Jemalloc => Jemalloc,
    Leaking => LeakingAllocator,
}

fn main() {
    let mut x = Box::new(10); // Allocated with the default allocator
    dbg!(&*x as *const i32);
    GlobalAllocator::with(AllocatorTag::Leaking, || {
        x = Box::new(20); // Allocated with the leaking allocator
    }); // Previous allocator restored
    dbg!(&*x as *const i32);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages