Download next.js/turbopack/crates/turbo-tasks-malloc/src/lib.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbo-tasks-malloc/src/lib.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbo-tasks-malloc/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbo-tasks-malloc/src/lib.rs
3.43 kB
| mod counter; | |
| use std::{ | |
| alloc::{GlobalAlloc, Layout}, | |
| marker::PhantomData, | |
| }; | |
| use self::counter::{add, flush, get, remove, update}; | |
| pub struct AllocationInfo { | |
| pub allocations: usize, | |
| pub deallocations: usize, | |
| pub allocation_count: usize, | |
| pub deallocation_count: usize, | |
| } | |
| impl AllocationInfo { | |
| pub fn is_empty(&self) -> bool { | |
| self.allocations == 0 | |
| && self.deallocations == 0 | |
| && self.allocation_count == 0 | |
| && self.deallocation_count == 0 | |
| } | |
| } | |
| pub struct AllocationCounters { | |
| pub allocations: usize, | |
| pub deallocations: usize, | |
| pub allocation_count: usize, | |
| pub deallocation_count: usize, | |
| _not_send: PhantomData<*mut ()>, | |
| } | |
| impl AllocationCounters { | |
| const fn new() -> Self { | |
| Self { | |
| allocation_count: 0, | |
| deallocation_count: 0, | |
| allocations: 0, | |
| deallocations: 0, | |
| _not_send: PhantomData {}, | |
| } | |
| } | |
| pub fn until_now(&self) -> AllocationInfo { | |
| let new = TurboMalloc::allocation_counters(); | |
| AllocationInfo { | |
| allocations: new.allocations - self.allocations, | |
| deallocations: new.deallocations - self.deallocations, | |
| allocation_count: new.allocation_count - self.allocation_count, | |
| deallocation_count: new.deallocation_count - self.deallocation_count, | |
| } | |
| } | |
| } | |
| /// Turbo's preferred global allocator. This is a new type instead of a type | |
| /// alias because you can't use type aliases to instantiate unit types (E0423). | |
| pub struct TurboMalloc; | |
| impl TurboMalloc { | |
| // Returns the current amount of memory | |
| pub fn memory_usage() -> usize { | |
| get() | |
| } | |
| pub fn thread_stop() { | |
| flush(); | |
| } | |
| pub fn allocation_counters() -> AllocationCounters { | |
| self::counter::allocation_counters() | |
| } | |
| pub fn reset_allocation_counters(start: AllocationCounters) { | |
| self::counter::reset_allocation_counters(start); | |
| } | |
| } | |
| /// Get the allocator for this platform that we should wrap with TurboMalloc. | |
| fn base_alloc() -> &'static impl GlobalAlloc { | |
| return &mimalloc::MiMalloc; | |
| return &std::alloc::System; | |
| } | |
| unsafe impl GlobalAlloc for TurboMalloc { | |
| unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | |
| let ret = unsafe { base_alloc().alloc(layout) }; | |
| if !ret.is_null() { | |
| add(layout.size()); | |
| } | |
| ret | |
| } | |
| unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | |
| unsafe { base_alloc().dealloc(ptr, layout) }; | |
| remove(layout.size()); | |
| } | |
| unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | |
| let ret = unsafe { base_alloc().alloc_zeroed(layout) }; | |
| if !ret.is_null() { | |
| add(layout.size()); | |
| } | |
| ret | |
| } | |
| unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | |
| let ret = unsafe { base_alloc().realloc(ptr, layout, new_size) }; | |
| if !ret.is_null() { | |
| let old_size = layout.size(); | |
| update(old_size, new_size); | |
| } | |
| ret | |
| } | |
| } | |