Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jemalloc): fix not linux and macos jemalloc fallback to std #9786

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/common/base/src/mem_allocator/jemalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#[derive(Debug, Clone, Copy, Default)]
pub struct JEAllocator;

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod linux_or_macos {
#[cfg(target_os = "linux")]
pub mod linux {
use std::alloc::AllocError;
use std::alloc::Allocator;
use std::alloc::Layout;
Expand Down Expand Up @@ -212,3 +212,59 @@ pub mod linux_or_macos {
}
}
}

/// Other target fallback to std allocator.
#[cfg(not(target_os = "linux"))]
pub mod not_linux {
use std::alloc::AllocError;
use std::alloc::Allocator;
use std::alloc::Layout;
use std::ptr::NonNull;

use super::JEAllocator;
use crate::mem_allocator::StdAllocator;

unsafe impl Allocator for JEAllocator {
#[inline(always)]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
StdAllocator.allocate(layout)
}

#[inline(always)]
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
StdAllocator.allocate_zeroed(layout)
}

#[inline(always)]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
StdAllocator.deallocate(ptr, layout)
}

unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
StdAllocator.grow(ptr, old_layout, new_layout)
}

unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
StdAllocator.grow_zeroed(ptr, old_layout, new_layout)
}

unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
StdAllocator.shrink(ptr, old_layout, new_layout)
}
}
}