~mht/cmr

247633051a5130ed7cfc44ea7dff5885ac32429b — Martin Hafskjold Thoresen 3 years ago ca4e418
It turns out that `free` also does internal locking

Who knew :)
1 files changed, 13 insertions(+), 5 deletions(-)

M src/alloc.rs
M src/alloc.rs => src/alloc.rs +13 -5
@@ 348,9 348,7 @@ pub(crate) struct WrappedAllocator;
unsafe impl GlobalAlloc for WrappedAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        if let Some(b) = SOME_LOCK.start_alloc() {
            ::std::sync::atomic::compiler_fence(SeqCst);
            let ret = Jemalloc.alloc(layout);
            ::std::sync::atomic::compiler_fence(SeqCst);
            SOME_LOCK.end_alloc(b);
            return ret;
        }


@@ 358,6 356,11 @@ unsafe impl GlobalAlloc for WrappedAllocator {
    }

    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
        if let Some(b) = SOME_LOCK.start_alloc() {
            let ret = jemalloc_free_hack::free(ptr as usize);
            SOME_LOCK.end_alloc(b);
            return ret;
        }
        jemalloc_free_hack::free(ptr as usize);
    }
}


@@ 368,10 371,9 @@ pub(crate) struct WrappedSystemAllocator;

unsafe impl GlobalAlloc for WrappedSystemAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        // NOTE: This `layout` is bogus, since we don't keep track of the layout.
        if let Some(b) = SOME_LOCK.start_alloc() {
            ::std::sync::atomic::compiler_fence(SeqCst);
            let ret = System.alloc(layout);
            ::std::sync::atomic::compiler_fence(SeqCst);
            SOME_LOCK.end_alloc(b);
            return ret;
        }


@@ 379,6 381,12 @@ unsafe impl GlobalAlloc for WrappedSystemAllocator {
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout);
        // NOTE: This `layout` is bogus, since we don't keep track of the layout.
        if let Some(b) = SOME_LOCK.start_alloc() {
            let ret = System.dealloc(ptr, layout);
            SOME_LOCK.end_alloc(b);
            return ret;
        }
        System.dealloc(ptr, layout)
    }
}