Skip to content
Merged
73 changes: 1 addition & 72 deletions src/bun_alloc/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,84 +31,13 @@ pub(crate) fn default_allocator_free(_: *mut c_void, buf: &mut [u8], _: Alignmen
unsafe { default_alloc::free(buf.as_mut_ptr().cast()) }
}

pub(crate) struct MimallocAllocator;

impl MimallocAllocator {
fn aligned_alloc(len: usize, alignment: Alignment) -> *mut u8 {
let ptr: *mut c_void = default_alloc::malloc_aligned(len, alignment.to_byte_units());

#[cfg(debug_assertions)]
{
if !ptr.is_null() {
// SAFETY: ptr is non-null and was just returned by the default allocator
let usable = unsafe { default_alloc::usable_size(ptr) };
if usable < len && !ptr.is_null() {
panic!(
"default allocator: allocated size is too small: {} < {}",
usable, len
);
}
}
}

ptr.cast::<u8>()
}

fn alloc_with_default_allocator(
_: *mut c_void,
len: usize,
alignment: Alignment,
_: usize,
) -> *mut u8 {
Self::aligned_alloc(len, alignment)
}

pub(crate) fn resize_with_default_allocator(
_: *mut c_void,
buf: &mut [u8],
_: Alignment,
new_len: usize,
_: usize,
) -> bool {
if cfg!(bun_asan) {
return false;
}
// SAFETY: buf.ptr was allocated by mimalloc (non-ASAN ⇒ default = mimalloc)
unsafe { !mimalloc::mi_expand(buf.as_mut_ptr().cast(), new_len).is_null() }
}

pub(crate) fn remap_with_default_allocator(
_: *mut c_void,
buf: &mut [u8],
alignment: Alignment,
new_len: usize,
_: usize,
) -> *mut u8 {
// SAFETY: buf.ptr was allocated by the default allocator with this alignment
unsafe {
default_alloc::realloc_aligned(
buf.as_mut_ptr().cast(),
new_len,
alignment.to_byte_units(),
)
.cast::<u8>()
}
}

const FREE_WITH_DEFAULT_ALLOCATOR: fn(*mut c_void, &mut [u8], Alignment, usize) =
default_allocator_free;
}

pub static C_ALLOCATOR: StdAllocator = StdAllocator {
// This ptr can be anything. But since it's not nullable, we should set it to something.
ptr: memory_allocator_tags::DEFAULT_ALLOCATOR_TAG_PTR,
vtable: C_ALLOCATOR_VTABLE,
};
static C_ALLOCATOR_VTABLE: &AllocatorVTable = &AllocatorVTable {
alloc: MimallocAllocator::alloc_with_default_allocator,
resize: MimallocAllocator::resize_with_default_allocator,
remap: MimallocAllocator::remap_with_default_allocator,
free: MimallocAllocator::FREE_WITH_DEFAULT_ALLOCATOR,
free: default_allocator_free,
};

mod memory_allocator_tags {
Expand Down
105 changes: 2 additions & 103 deletions src/bun_alloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ pub mod c_thunks;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Alignment(pub u8); // log2 of byte alignment
impl Alignment {
#[inline]
pub(crate) const fn to_byte_units(self) -> usize {
1usize << self.0
}
#[inline]
pub(crate) const fn from_byte_units(b: usize) -> Self {
Self(b.trailing_zeros() as u8)
Expand Down Expand Up @@ -62,48 +58,14 @@ pub(crate) const MAX_ALIGN_T: usize = 16;
pub(crate) const MAX_ALIGN_T: usize = core::mem::align_of::<libc::max_align_t>();

pub struct AllocatorVTable {
pub alloc: unsafe fn(*mut core::ffi::c_void, usize, Alignment, usize) -> *mut u8,
pub resize: unsafe fn(*mut core::ffi::c_void, &mut [u8], Alignment, usize, usize) -> bool,
pub remap: unsafe fn(*mut core::ffi::c_void, &mut [u8], Alignment, usize, usize) -> *mut u8,
pub free: unsafe fn(*mut core::ffi::c_void, &mut [u8], Alignment, usize),
}
impl AllocatorVTable {
/// `alloc` impl that always fails. For vtables that only ever `free` an
/// externally-produced buffer (mmap region, plugin-owned memory, refcounted
/// foreign string) and never allocate or grow it.
pub(crate) const NO_ALLOC: unsafe fn(
*mut core::ffi::c_void,
usize,
Alignment,
usize,
) -> *mut u8 = |_, _, _, _| core::ptr::null_mut();
pub(crate) const NO_RESIZE: unsafe fn(
*mut core::ffi::c_void,
&mut [u8],
Alignment,
usize,
usize,
) -> bool = |_, _, _, _, _| false;
pub(crate) const NO_REMAP: unsafe fn(
*mut core::ffi::c_void,
&mut [u8],
Alignment,
usize,
usize,
) -> *mut u8 = |_, _, _, _, _| core::ptr::null_mut();

/// Build a "free-only" vtable: `alloc`/`resize`/`remap` all no-op/fail and
/// only `free` is meaningful. Each call site still gets its own `static`
/// (vtable address is an identity tag for `is_instance`).
/// Each call site keeps its own `static`: the vtable address is the `is_instance` tag.
pub const fn free_only(
free: unsafe fn(*mut core::ffi::c_void, &mut [u8], Alignment, usize),
) -> Self {
Self {
alloc: Self::NO_ALLOC,
resize: Self::NO_RESIZE,
remap: Self::NO_REMAP,
free,
}
Self { free }
}
}

Expand Down Expand Up @@ -289,69 +251,6 @@ pub mod default_alloc {
#[cfg(not(any(all(bun_asan, target_os = "linux"), all(bun_asan, target_os = "macos"))))]
return unsafe { crate::mimalloc::mi_usable_size(ptr) };
}

// The aligned variants are `#[cfg]`-split (not `if cfg!()`) because the
// posix_memalign/malloc_usable_size symbols don't exist on Windows.

#[cfg(not(bun_asan))]
#[inline]
pub(crate) fn malloc_aligned(size: usize, align: usize) -> *mut c_void {
crate::mimalloc::mi_malloc_auto_align(size, align)
}

#[cfg(bun_asan)]
#[inline]
pub(crate) fn malloc_aligned(size: usize, align: usize) -> *mut c_void {
if align <= crate::MAX_ALIGN_T {
return unsafe { libc::malloc(size) };
}
let mut p: *mut c_void = core::ptr::null_mut();
let align = align.max(core::mem::size_of::<*mut c_void>());
if unsafe { libc::posix_memalign(&mut p, align, size) } != 0 {
return core::ptr::null_mut();
}
p
}

/// # Safety
/// `ptr` must be null or a live allocation from the default allocator with the given `align`.
#[cfg(not(bun_asan))]
#[inline]
pub(crate) unsafe fn realloc_aligned(
ptr: *mut c_void,
new_size: usize,
align: usize,
) -> *mut c_void {
// SAFETY: caller guarantees `ptr` is null or a live mimalloc allocation
// with alignment `align`.
unsafe { crate::mimalloc::mi_realloc_aligned(ptr, new_size, align) }
}

/// # Safety
/// `ptr` must be null or a live allocation from the default allocator with the given `align`.
#[cfg(bun_asan)]
#[inline]
pub(crate) unsafe fn realloc_aligned(
ptr: *mut c_void,
new_size: usize,
align: usize,
) -> *mut c_void {
if align <= crate::MAX_ALIGN_T {
return unsafe { libc::realloc(ptr, new_size) };
}
let new_ptr = malloc_aligned(new_size, align);
if new_ptr.is_null() {
return core::ptr::null_mut();
}
if !ptr.is_null() {
unsafe {
let copy = usable_size(ptr).min(new_size);
core::ptr::copy_nonoverlapping(ptr.cast::<u8>(), new_ptr.cast::<u8>(), copy);
libc::free(ptr);
}
}
new_ptr
}
}

pub use max_heap_allocator::MaxHeapAllocator;
Expand Down
14 changes: 0 additions & 14 deletions src/bun_core/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ pub const STORE_FILE_DESCRIPTORS: bool = !env::IS_BROWSER;

pub const TRACING: bool = true;

pub const ENABLE_ENTRY_CACHE: bool = true;

// TODO: remove this flag, it should use bun.Output.scoped
pub const VERBOSE_FS: bool = false;

pub const WATCH_DIRECTORIES: bool = true;

// This feature flag exists so when you have defines inside package.json, you can use single quotes in nested strings.
pub const ALLOW_JSON_SINGLE_QUOTES: bool = true;

pub const DISABLE_COMPRESSION_IN_HTTP_CLIENT: bool = false;

pub const ENABLE_KEEPALIVE: bool = true;
Expand All @@ -28,12 +20,6 @@ pub const ATOMIC_FILE_WATCHER: bool = env::IS_LINUX;

pub const HTTP_BUFFER_POOLING: bool = true;

/// There is, what I think is, a bug in getaddrinfo()
/// on macOS that specifically impacts localhost and not
/// other ipv4 hosts. This is a workaround for that.
/// "localhost" fails to connect.
pub const HARDCODE_LOCALHOST_TO_127_0_0_1: bool = false;

/// React will issue warnings in development if there are multiple children
/// without keys and "jsxs" is not used.
/// https://github.com/oven-sh/bun/issues/10733
Expand Down
28 changes: 0 additions & 28 deletions src/bun_core/string/immutable/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,11 @@ pub fn copy_latin1_into_ascii(dest: &mut [u8], src: &[u8]) {
pub enum BOM {
Utf8,
Utf16Le,
Utf16Be,
Utf32Le,
Utf32Be,
}

impl BOM {
pub const UTF8_BYTES: [u8; 3] = [0xef, 0xbb, 0xbf];
pub(crate) const UTF16_LE_BYTES: [u8; 2] = [0xff, 0xfe];
pub(crate) const UTF16_BE_BYTES: [u8; 2] = [0xfe, 0xff];
pub(crate) const UTF32_LE_BYTES: [u8; 4] = [0xff, 0xfe, 0x00, 0x00];
pub(crate) const UTF32_BE_BYTES: [u8; 4] = [0x00, 0x00, 0xfe, 0xff];

pub fn detect(bytes: &[u8]) -> Option<BOM> {
if bytes.len() < 3 {
Expand All @@ -555,12 +549,8 @@ impl BOM {
return Some(BOM::Utf8);
}
if eql_ignore_len(bytes, &Self::UTF16_LE_BYTES) {
// if (bytes.len > 4 and eqlComptimeIgnoreLen(bytes[2..], utf32_le_bytes[2..]))
// return .utf32_le;
return Some(BOM::Utf16Le);
}
// if (eqlComptimeIgnoreLen(bytes, utf16_be_bytes)) return .utf16_be;
// if (bytes.len > 4 and eqlComptimeIgnoreLen(bytes, utf32_le_bytes)) return .utf32_le;
None
}

Expand All @@ -576,9 +566,6 @@ impl BOM {
match self {
BOM::Utf8 => &Self::UTF8_BYTES,
BOM::Utf16Le => &Self::UTF16_LE_BYTES,
BOM::Utf16Be => &Self::UTF16_BE_BYTES,
BOM::Utf32Le => &Self::UTF32_LE_BYTES,
BOM::Utf32Be => &Self::UTF32_BE_BYTES,
}
}

Expand All @@ -590,9 +577,6 @@ impl BOM {
match self {
BOM::Utf8 => "utf8",
BOM::Utf16Le => "utf16_le",
BOM::Utf16Be => "utf16_be",
BOM::Utf32Le => "utf32_le",
BOM::Utf32Be => "utf32_be",
}
}

Expand All @@ -613,11 +597,6 @@ impl BOM {
drop(bytes);
out
}
_ => {
// TODO: this needs to re-encode, for now we just remove the BOM
crate::vec::drain_front(&mut bytes, self.get_header().len());
bytes
}
}
}

Expand Down Expand Up @@ -645,13 +624,6 @@ impl BOM {
// "always points to the base of the input" doc comment.
&list[..]
}
_ => {
// TODO: this needs to re-encode, for now we just remove the BOM
let n = self.get_header().len();
let len = list.len();
list.copy_within(n.., 0);
&list[..len - n]
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/bundler/AstBuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,6 @@ impl<'a, 'bump> AstBuilder<'a, 'bump> {
flags: Default::default(),
target,
top_level_await_keyword: Range::NONE,
// .nested_scope_slot_counts = if (p.options.features.minify_identifiers)
// renamer.assignNestedScopeSlots(p.arena, p.scopes.items[0], p.symbols.items)
// else
// js_ast.SlotCounts{},
nested_scope_slot_counts: Default::default(),
hashbang: b"".into(),
css: None,
Expand Down
3 changes: 0 additions & 3 deletions src/bundler/LinkerGraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub mod js_meta {
pub top_level_symbol_to_parts_overlay: TopLevelSymbolToParts,
pub cjs_export_copies: CjsExportCopies,
pub wrapper_part_index: Index,
pub entry_point_part_index: Index,
pub flags: Flags,
}

Expand All @@ -142,7 +141,6 @@ pub mod js_meta {
top_level_symbol_to_parts_overlay: TopLevelSymbolToParts::default(),
cjs_export_copies: AstAlloc::vec(),
wrapper_part_index: Index::default(),
entry_point_part_index: Index::default(),
flags: Flags::default(),
}
}
Expand All @@ -158,7 +156,6 @@ pub mod js_meta {
top_level_symbol_to_parts_overlay: TopLevelSymbolToParts,
cjs_export_copies: CjsExportCopies,
wrapper_part_index: Index,
entry_point_part_index: Index,
flags: Flags,
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/bundler/ParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ pub struct ParseTask {
// Used for splitting up the work between the io and parse steps.
pub(crate) stage: ParseTaskStage,

pub(crate) tree_shaking: bool,
pub(crate) known_target: options::Target,
pub(crate) module_type: options::ModuleType,
pub(crate) emit_decorator_metadata: bool,
Expand Down Expand Up @@ -289,7 +288,6 @@ impl ParseTask {
callback: io_task_callback,
},
stage: ParseTaskStage::NeedsSourceCode,
tree_shaking: false,
is_entry_point: false,
}
}
Expand Down Expand Up @@ -323,7 +321,6 @@ impl Default for ParseTask {
callback: io_task_callback,
},
stage: ParseTaskStage::NeedsSourceCode,
tree_shaking: false,
known_target: options::Target::default(),
module_type: options::ModuleType::Unknown,
emit_decorator_metadata: false,
Expand Down Expand Up @@ -565,7 +562,6 @@ pub mod parse_worker {
callback: io_task_callback,
},
stage: ParseTaskStage::NeedsSourceCode,
tree_shaking: false,
module_type: options::ModuleType::Unknown,
emit_decorator_metadata: false,
experimental_decorators: false,
Expand Down
Loading
Loading