Compare commits

...

70 Commits

Author SHA1 Message Date
Kai Tamkun
35ed97df45 Windows... 2025-08-14 16:09:07 -07:00
Kai Tamkun
9e0b1bc337 More fixes 2025-08-14 15:18:36 -07:00
Kai Tamkun
83704566e5 Merge branch 'main' into kai/memory-usage 2025-08-14 14:30:49 -07:00
Kai Tamkun
e19dc02220 Add USE_MIMALLOC option 2025-08-14 14:28:27 -07:00
Kai Tamkun
aea0a2daad Uint8Array allocator usage fixes 2025-08-14 13:35:13 -07:00
Kai Tamkun
886164558e Enable mimalloc even on ASAN 2025-08-13 19:28:18 -07:00
Kai Tamkun
de975a9689 Fix zlib allocator usage 2025-08-13 18:59:17 -07:00
Kai Tamkun
9616c88583 Merge branch 'jarred/mimalloc-upgrade-v3' into kai/memory-usage 2025-08-13 18:19:06 -07:00
Kai Tamkun
65714b0127 Undo harness changes 2025-08-06 15:02:23 -07:00
Kai Tamkun
ad49ebf702 Disable heap breakdown when ASAN is enabled 2025-08-06 14:52:10 -07:00
autofix-ci[bot]
700942390a [autofix.ci] apply automated fixes 2025-08-06 21:19:41 +00:00
Kai Tamkun
658406129a Don't use mimalloc with ASAN 2025-08-06 14:16:21 -07:00
Kai Tamkun
86aac268c2 Merge branch 'main' into kai/memory-usage 2025-08-05 13:21:02 -07:00
Kai Tamkun
fd0a783da0 Try to ignore fetch errors in dev-server-ssr-100.test.ts 2025-07-30 19:22:11 -07:00
Kai Tamkun
0fe3ba3a45 oops 2025-07-30 16:19:02 -07:00
Kai Tamkun
82a32e632a Weaken NodeVMScriptFetcher::m_owner 2025-07-30 16:14:22 -07:00
Kai Tamkun
8aaac791ff Move RSS/committed reporting to the test harness 2025-07-30 15:47:44 -07:00
Kai Tamkun
2cdc8abe12 (test) Report final RSS/committed in scripts/runner.node.mjs 2025-07-30 15:39:44 -07:00
Kai Tamkun
ba9a49e3d6 Fix a null access 2025-07-28 15:59:47 -07:00
Kai Tamkun
5772f5daa5 Actually allow AllocationScope to be disabled 2025-07-28 15:59:32 -07:00
Kai Tamkun
a687d7553a ok. 2025-07-28 15:12:45 -07:00
Kai Tamkun
8a4d76b43d Merge branch 'jarred/mimalloc-upgrade-v3' of github.com:oven-sh/bun into jarred/mimalloc-upgrade-v3 2025-07-28 15:07:15 -07:00
Kai Tamkun
1768fd7ec7 oops 2025-07-28 15:06:45 -07:00
autofix-ci[bot]
269d078862 [autofix.ci] apply automated fixes 2025-07-28 22:05:14 +00:00
Kai Tamkun
0ecb121199 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-07-28 14:56:50 -07:00
Kai Tamkun
5fa4c6766c Less deiniting 2025-07-25 19:21:12 -07:00
Kai Tamkun
87160c88ef ban-words fix 2025-07-25 18:40:34 -07:00
Kai Tamkun
f61209114c Remove printer.deinit 2025-07-25 18:34:33 -07:00
Kai Tamkun
b63bb99638 Sort out some allocator mismatches in the dev server 2025-07-24 20:19:43 -07:00
Kai Tamkun
51578702b2 Fix some leaks 2025-07-23 17:55:17 -07:00
Kai Tamkun
5322bd491c Use default_allocator for quoted_source_contents 2025-07-22 17:57:32 -07:00
Kai Tamkun
17bf13e72e Some work on freeing memory in the bundler 2025-07-22 16:56:13 -07:00
Kai Tamkun
7a46e4c000 Can't destroy an optional pointer 2025-07-21 13:58:44 -07:00
Kai Tamkun
a73bbcd150 Make TCPSocket/TLSSocket.handlers optional (please don't free undefined) 2025-07-21 13:56:39 -07:00
Kai Tamkun
71d9084252 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-07-21 12:49:08 -07:00
Kai Tamkun
3bfa2b2d06 Fix body-leak-test-fixture.ts 2025-07-17 17:00:15 -07:00
Kai Tamkun
50c0472f40 Even less arena usage 2025-07-17 12:22:44 -07:00
Kai Tamkun
b7d8a41229 mimalloc added another initializer or something 2025-07-17 12:06:12 -07:00
Kai Tamkun
a8afab0a35 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-07-16 21:11:38 -07:00
Kai Tamkun
84f95f7072 Fix mimalloc lib filename on Windows 2025-07-16 19:15:27 -07:00
Kai Tamkun
cfe33ce99d Less arena usage 2025-07-16 19:10:12 -07:00
Kai Tamkun
d321cac3b2 Another WTFTimer.cancel fix 2025-07-16 18:27:29 -07:00
Kai Tamkun
6a0eb3890f More careful WTFTimer.cancel 2025-07-16 16:14:15 -07:00
Kai Tamkun
48e1b43e59 See what happens if we don't use arenas in HTTPThread 2025-07-15 20:13:51 -07:00
Kai Tamkun
a05509f024 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-07-14 20:51:40 -07:00
Kai Tamkun
af15c690cc Possible 09748 fix 2025-07-14 20:47:02 -07:00
Jarred Sumner
a7640f3272 Update BuildMimalloc.cmake 2025-06-17 21:49:39 -07:00
Jarred Sumner
d022cea0bf Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-06-17 21:49:27 -07:00
Jarred Sumner
8bd438611e Update BuildMimalloc.cmake 2025-06-06 23:54:59 -07:00
Jarred Sumner
9038282484 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-06-06 23:54:40 -07:00
Jarred-Sumner
e01b794275 bun run zig-format 2025-05-28 02:11:16 +00:00
Jarred Sumner
04f4be3f2a Update BuildMimalloc.cmake 2025-05-27 19:09:06 -07:00
Jarred Sumner
e12da98158 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-05-27 19:06:00 -07:00
Devin AI
9724d14817 Fix: Uncomment mimalloc static object workaround to resolve linking failure
Co-Authored-By: Jarred Sumner <jarred@jarredsumner.com>
2025-05-15 03:05:50 +00:00
Jarred Sumner
a0f66c93fc Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-05-14 19:05:41 -07:00
Jarred Sumner
c2e4ce5824 Bump 2025-04-11 22:21:44 -07:00
Jarred Sumner
38f63b73a6 Bump 2025-04-11 22:18:22 -07:00
Jarred Sumner
b2d4f4af3f Bump 2025-04-11 22:13:52 -07:00
Jarred Sumner
ce1addf292 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-04-11 22:12:41 -07:00
Jarred Sumner
187a44779e some tweaks 2025-03-29 18:34:14 -07:00
Jarred Sumner
b27878cb51 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-03-29 05:56:26 -07:00
Jarred Sumner
fe6f462cdb actually lets downgrade mimalloc 2025-03-29 05:55:42 -07:00
Jarred Sumner
0860131df4 Update BuildMimalloc.cmake 2025-03-29 02:28:43 -07:00
Jarred Sumner
d42a9dce6c Bump 2025-03-28 20:41:55 -07:00
Jarred Sumner
86cce7458a Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-03-28 20:39:44 -07:00
Jarred Sumner
67d2192f26 Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-02-28 19:13:00 -08:00
Jarred Sumner
22e5078528 Update BuildMimalloc.cmake 2025-02-23 22:22:34 -08:00
Jarred Sumner
2fba712a7d Merge branch 'main' into jarred/mimalloc-upgrade-v3 2025-02-23 22:21:54 -08:00
Jarred Sumner
ffe34a9a86 Set threadpool flag 2025-02-16 00:44:46 -08:00
Jarred Sumner
6145469875 Try mimalloc v3 2025-02-16 00:17:38 -08:00
22 changed files with 136 additions and 55 deletions

View File

@@ -315,3 +315,10 @@ if(NOT WIN32 AND NOT APPLE)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
endif()
if(USE_MIMALLOC)
register_compiler_flags(
DESCRIPTION "Use mimalloc"
-DUSE_MIMALLOC=1
)
endif()

View File

@@ -184,3 +184,5 @@ optionx(USE_WEBKIT_ICU BOOL "Use the ICU libraries from WebKit" DEFAULT ${DEFAUL
optionx(ERROR_LIMIT STRING "Maximum number of errors to show when compiling C++ code" DEFAULT "100")
list(APPEND CMAKE_ARGS -DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
optionx(USE_MIMALLOC BOOL "Whether mimalloc should be used" DEFAULT ON)

View File

@@ -4,7 +4,7 @@ register_repository(
REPOSITORY
oven-sh/mimalloc
COMMIT
1beadf9651a7bfdec6b5367c380ecc3fe1c40d1a
c1f17cd2538417620f60bff70bffe7e68d332aec
)
set(MIMALLOC_CMAKE_ARGS
@@ -14,7 +14,7 @@ set(MIMALLOC_CMAKE_ARGS
-DMI_BUILD_TESTS=OFF
-DMI_USE_CXX=ON
-DMI_SKIP_COLLECT_ON_EXIT=ON
# ```
# mimalloc_allow_large_os_pages=0 BUN_PORT=3004 mem bun http-hello.js
# Started development server: http://localhost:3004
@@ -51,7 +51,7 @@ if(ENABLE_ASAN)
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_DEBUG_UBSAN=ON)
elseif(APPLE OR LINUX)
if(APPLE)
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OVERRIDE=OFF)
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OVERRIDE=OFF)
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OSX_ZONE=OFF)
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OSX_INTERPOSE=OFF)
else()
@@ -77,9 +77,9 @@ endif()
if(WIN32)
if(DEBUG)
set(MIMALLOC_LIBRARY mimalloc-static-debug)
set(MIMALLOC_LIBRARY mimalloc-debug)
else()
set(MIMALLOC_LIBRARY mimalloc-static)
set(MIMALLOC_LIBRARY mimalloc)
endif()
elseif(DEBUG)
if (ENABLE_ASAN)

View File

@@ -117,7 +117,7 @@ fn alignedAlloc(self: Self, len: usize, alignment: Alignment) ?[*]u8 {
const ptr: ?*anyopaque = if (mimalloc.mustUseAlignedAlloc(alignment))
mimalloc.mi_heap_malloc_aligned(heap, len, alignment.toByteUnits())
else
mimalloc.mi_heap_malloc(heap, len);
mimalloc.mi_heap_malloc_aligned_at(heap, len, alignment.toByteUnits(), 0);
if (comptime bun.Environment.isDebug) {
const usable = mimalloc.mi_malloc_usable_size(ptr);

View File

@@ -33,11 +33,9 @@ const MimallocAllocator = struct {
mimalloc.mi_malloc(len);
if (comptime Environment.isDebug) {
if (ptr != null) {
const usable = mimalloc.mi_malloc_usable_size(ptr);
if (usable < len and ptr != null) {
std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len });
}
const usable = mimalloc.mi_malloc_usable_size(ptr);
if (usable < len) {
std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len });
}
}

View File

@@ -97,7 +97,7 @@ pub fn fromBlob(
if (mime_type.category.isTextLike()) {
var output = MutableString.initEmpty(allocator);
try JSPrinter.quoteForJSON(bytes, &output, true);
var list = output.toOwnedSlice();
var list = output.slice();
// remove the quotes
if (list.len > 0) {
list = list[1 .. list.len - 1];

View File

@@ -12,12 +12,6 @@ pub fn load() void {
boring.SSL_load_error_strings();
boring.ERR_load_BIO_strings();
boring.OpenSSL_add_all_algorithms();
if (!builtin.is_test) {
std.mem.doNotOptimizeAway(&OPENSSL_memory_alloc);
std.mem.doNotOptimizeAway(&OPENSSL_memory_get_size);
std.mem.doNotOptimizeAway(&OPENSSL_memory_free);
}
}
var ctx_store: ?*boring.SSL_CTX = null;
@@ -58,19 +52,29 @@ pub fn initClient() *boring.SSL {
// into the process, including pthreads locks. Failing to meet these constraints
// may result in deadlocks, crashes, or memory corruption.
export fn OPENSSL_memory_alloc(size: usize) ?*anyopaque {
return bun.mimalloc.mi_malloc(size);
}
comptime {
if (bun.use_mimalloc) {
@export(&struct {
pub fn alloc(size: usize) callconv(.C) ?*anyopaque {
return bun.mimalloc.mi_malloc(size);
}
}.alloc, .{ .name = "OPENSSL_memory_alloc" });
// BoringSSL always expects memory to be zero'd
export fn OPENSSL_memory_free(ptr: *anyopaque) void {
const len = bun.mimalloc.mi_usable_size(ptr);
@memset(@as([*]u8, @ptrCast(ptr))[0..len], 0);
bun.mimalloc.mi_free(ptr);
}
// BoringSSL always expects memory to be zero'd
@export(&struct {
pub fn free(ptr: *anyopaque) callconv(.C) void {
const len = bun.mimalloc.mi_usable_size(ptr);
@memset(@as([*]u8, @ptrCast(ptr))[0..len], 0);
bun.mimalloc.mi_free(ptr);
}
}.free, .{ .name = "OPENSSL_memory_free" });
export fn OPENSSL_memory_get_size(ptr: ?*const anyopaque) usize {
return bun.mimalloc.mi_usable_size(ptr);
@export(&struct {
pub fn get_size(ptr: ?*const anyopaque) callconv(.C) usize {
return bun.mimalloc.mi_usable_size(ptr);
}
}.get_size, .{ .name = "OPENSSL_memory_get_size" });
}
}
const INET6_ADDRSTRLEN = if (bun.Environment.isWindows) 65 else 46;
@@ -221,7 +225,6 @@ pub fn ERR_toJS(globalThis: *jsc.JSGlobalObject, err_code: u32) jsc.JSValue {
const X509 = @import("./bun.js/api/bun/x509.zig");
const boring = @import("./deps/boringssl.translated.zig");
const builtin = @import("builtin");
const c_ares = @import("./deps/c_ares.zig");
const std = @import("std");

View File

@@ -19,7 +19,11 @@ pub const BrotliAllocator = struct {
return;
}
mimalloc.mi_free(data);
if (comptime bun.use_mimalloc) {
mimalloc.mi_free(data);
} else {
std.c.free(data);
}
}
};

View File

@@ -440,7 +440,11 @@ pub const MatchedRoute = struct {
}
if (this.needs_deinit) {
if (this.route.pathname.len > 0 and bun.mimalloc.mi_is_in_heap_region(this.route.pathname.ptr)) {
bun.mimalloc.mi_free(@constCast(this.route.pathname.ptr));
if (comptime bun.use_mimalloc) {
bun.mimalloc.mi_free(@constCast(this.route.pathname.ptr));
} else {
std.c.free(@constCast(this.route.pathname.ptr));
}
}
this.params_list_holder.deinit(bun.default_allocator);

View File

@@ -714,7 +714,11 @@ extern "C" BunString BunString__createExternalGloballyAllocatedLatin1(
{
ASSERT(length > 0);
Ref<WTF::ExternalStringImpl> impl = WTF::ExternalStringImpl::create({ bytes, length }, nullptr, [](void*, void* ptr, size_t) {
#ifdef USE_MIMALLOC
mi_free(ptr);
#else
free(ptr);
#endif
});
return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } };
}
@@ -725,7 +729,11 @@ extern "C" BunString BunString__createExternalGloballyAllocatedUTF16(
{
ASSERT(length > 0);
Ref<WTF::ExternalStringImpl> impl = WTF::ExternalStringImpl::create({ bytes, length }, nullptr, [](void*, void* ptr, size_t) {
#ifdef USE_MIMALLOC
mi_free(ptr);
#else
free(ptr);
#endif
});
return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } };
}

View File

@@ -1061,7 +1061,7 @@ JSC_DEFINE_HOST_FUNCTION(vmModuleRunInNewContext, (JSGlobalObject * globalObject
JSValue globalObjectDynamicImportCallback;
if (auto encodedException = getNodeVMContextOptions(globalObject, vm, scope, contextOptionsArg, contextOptions, "contextCodeGeneration", &globalObjectDynamicImportCallback)) {
if (auto encodedException = getNodeVMContextOptions(globalObject, vm, scope, contextOptionsArg, contextOptions, "contextCodeGeneration"_s, &globalObjectDynamicImportCallback)) {
return *encodedException;
}
@@ -1085,7 +1085,7 @@ JSC_DEFINE_HOST_FUNCTION(vmModuleRunInNewContext, (JSGlobalObject * globalObject
RETURN_IF_EXCEPTION(scope, {});
}
RefPtr fetcher(NodeVMScriptFetcher::create(vm, scriptDynamicImportCallback, jsUndefined()));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, scriptDynamicImportCallback, nullptr));
SourceCode sourceCode(
JSC::StringSourceProvider::create(
@@ -1135,7 +1135,7 @@ JSC_DEFINE_HOST_FUNCTION(vmModuleRunInThisContext, (JSGlobalObject * globalObjec
RETURN_IF_EXCEPTION(throwScope, encodedJSUndefined());
}
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, jsUndefined()));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, nullptr));
SourceCode source(
JSC::StringSourceProvider::create(sourceString, JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(options.filename), *fetcher), options.filename, JSC::SourceTaintedOrigin::Untainted, TextPosition(options.lineOffset, options.columnOffset)),
@@ -1212,7 +1212,7 @@ JSC_DEFINE_HOST_FUNCTION(vmModuleCompileFunction, (JSGlobalObject * globalObject
// Add the function body
constructFunctionArgs.append(jsString(vm, sourceString));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, jsUndefined()));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, nullptr));
// Create the source origin
SourceOrigin sourceOrigin { WTF::URL::fileURLWithFileSystemPath(options.filename), *fetcher };
@@ -1290,7 +1290,7 @@ JSC_DEFINE_HOST_FUNCTION(vmModule_createContext, (JSGlobalObject * globalObject,
JSValue importer;
if (auto encodedException = getNodeVMContextOptions(globalObject, vm, scope, optionsArg, contextOptions, "codeGeneration", &importer)) {
if (auto encodedException = getNodeVMContextOptions(globalObject, vm, scope, optionsArg, contextOptions, "codeGeneration"_s, &importer)) {
return *encodedException;
}

View File

@@ -126,7 +126,7 @@ constructScript(JSGlobalObject* globalObject, CallFrame* callFrame, JSValue newT
RETURN_IF_EXCEPTION(scope, {});
}
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, jsUndefined()));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, importer, nullptr));
SourceCode source = makeSource(sourceString, JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(options.filename), *fetcher), JSC::SourceTaintedOrigin::Untainted, options.filename, TextPosition(options.lineOffset, options.columnOffset));
RETURN_IF_EXCEPTION(scope, {});

View File

@@ -7,17 +7,19 @@
namespace Bun {
class NodeVMScript;
// The presence of this class in a JSFunction's sourceOrigin indicates that the function was compiled by Bun's node:vm implementation.
class NodeVMScriptFetcher : public JSC::ScriptFetcher {
public:
static Ref<NodeVMScriptFetcher> create(JSC::VM& vm, JSC::JSValue dynamicImportCallback, JSC::JSValue owner) { return adoptRef(*new NodeVMScriptFetcher(vm, dynamicImportCallback, owner)); }
static Ref<NodeVMScriptFetcher> create(JSC::VM& vm, JSC::JSValue dynamicImportCallback, JSCell* owner) { return adoptRef(*new NodeVMScriptFetcher(vm, dynamicImportCallback, owner)); }
Type fetcherType() const final { return Type::NodeVM; }
JSC::JSValue dynamicImportCallback() const { return m_dynamicImportCallback.get(); }
JSC::JSValue owner() const { return m_owner.get(); }
void owner(JSC::VM& vm, JSC::JSValue value) { m_owner.set(vm, value); }
JSCell* owner() const { return m_owner.get(); }
void owner(JSC::VM& vm, JSCell* value) { m_owner.set(vm, value); }
bool isUsingDefaultLoader() const { return m_isUsingDefaultLoader; }
auto temporarilyUseDefaultLoader()
@@ -30,10 +32,10 @@ public:
private:
JSC::Strong<JSC::Unknown> m_dynamicImportCallback;
JSC::Strong<JSC::Unknown> m_owner;
JSC::Weak<JSCell> m_owner;
bool m_isUsingDefaultLoader = false;
NodeVMScriptFetcher(JSC::VM& vm, JSC::JSValue dynamicImportCallback, JSC::JSValue owner)
NodeVMScriptFetcher(JSC::VM& vm, JSC::JSValue dynamicImportCallback, JSCell* owner)
: m_dynamicImportCallback(vm, dynamicImportCallback)
, m_owner(vm, owner)
{

View File

@@ -89,7 +89,7 @@ NodeVMSourceTextModule* NodeVMSourceTextModule::create(VM& vm, JSGlobalObject* g
uint32_t columnOffset = columnOffsetValue.toUInt32(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
RefPtr fetcher(NodeVMScriptFetcher::create(vm, dynamicImportCallback, moduleWrapper));
RefPtr fetcher(NodeVMScriptFetcher::create(vm, dynamicImportCallback, moduleWrapper.isUndefinedOrNull() ? nullptr : moduleWrapper.asCell()));
RETURN_IF_EXCEPTION(scope, nullptr);
SourceOrigin sourceOrigin { {}, *fetcher };

View File

@@ -12,7 +12,11 @@ extern "C" JSC::EncodedJSValue JSUint8Array__fromDefaultAllocator(JSC::JSGlobalO
if (length > 0) [[likely]] {
auto buffer = ArrayBuffer::createFromBytes({ ptr, length }, createSharedTask<void(void*)>([](void* p) {
#ifdef USE_MIMALLOC
mi_free(p);
#else
free(p);
#endif
}));
uint8Array = JSC::JSUint8Array::create(lexicalGlobalObject, lexicalGlobalObject->typedArrayStructureWithTypedArrayType<JSC::TypeUint8>(), WTFMove(buffer), 0, length);
@@ -30,7 +34,11 @@ extern "C" JSC::EncodedJSValue JSArrayBuffer__fromDefaultAllocator(JSC::JSGlobal
if (length > 0) [[likely]] {
buffer = ArrayBuffer::createFromBytes({ ptr, length }, createSharedTask<void(void*)>([](void* p) {
#ifdef USE_MIMALLOC
mi_free(p);
#else
free(p);
#endif
}));
} else {
buffer = ArrayBuffer::create(0, 1);

View File

@@ -94,7 +94,11 @@ Ref<SourceProvider> SourceProvider::create(
const auto getProvider = [&]() -> Ref<SourceProvider> {
if (resolvedSource.bytecode_cache != nullptr) {
const auto destructorPtr = [](const void* ptr) {
#ifdef USE_MIMALLOC
mi_free(const_cast<void*>(ptr));
#else
free(const_cast<void*>(ptr));
#endif
};
const auto destructorNoOp = [](const void* ptr) {
// no-op, for bun build --compile.

View File

@@ -743,7 +743,7 @@ pub const ZigString = extern struct {
}
inline fn assertGlobal(this: *const ZigString) void {
if (comptime bun.Environment.allow_assert) {
if (comptime bun.Environment.allow_assert and bun.use_mimalloc) {
bun.assert(this.len == 0 or
bun.mimalloc.mi_is_in_heap_region(untagged(this._unsafe_ptr_do_not_use)) or
bun.mimalloc.mi_check_owned(untagged(this._unsafe_ptr_do_not_use)));
@@ -852,7 +852,7 @@ pub const StringPointer = struct {
export fn ZigString__free(raw: [*]const u8, len: usize, allocator_: ?*anyopaque) void {
var allocator: std.mem.Allocator = @as(*std.mem.Allocator, @ptrCast(@alignCast(allocator_ orelse return))).*;
var ptr = ZigString.init(raw[0..len]).slice().ptr;
if (comptime Environment.allow_assert) {
if (comptime Environment.allow_assert and bun.use_mimalloc) {
bun.assert(Mimalloc.mi_is_in_heap_region(ptr));
}
const str = ptr[0..len];
@@ -862,11 +862,15 @@ export fn ZigString__free(raw: [*]const u8, len: usize, allocator_: ?*anyopaque)
export fn ZigString__freeGlobal(ptr: [*]const u8, len: usize) void {
const untagged = @as(*anyopaque, @ptrFromInt(@intFromPtr(ZigString.init(ptr[0..len]).slice().ptr)));
if (comptime Environment.allow_assert) {
bun.assert(Mimalloc.mi_is_in_heap_region(ptr));
if (comptime bun.use_mimalloc) {
if (comptime Environment.allow_assert) {
bun.assert(Mimalloc.mi_is_in_heap_region(ptr));
}
// we must untag the string pointer
Mimalloc.mi_free(untagged);
} else {
std.c.free(untagged);
}
// we must untag the string pointer
Mimalloc.mi_free(untagged);
}
const string = []const u8;

View File

@@ -628,7 +628,11 @@ pub export fn MarkedArrayBuffer_deallocator(bytes_: *anyopaque, _: *anyopaque) v
// mimalloc.mi_heap_check_owned(jsc.VirtualMachine.get().arena.heap.?, bytes_));
// }
mimalloc.mi_free(bytes_);
if (comptime bun.use_mimalloc) {
mimalloc.mi_free(bytes_);
} else {
std.c.free(bytes_);
}
}
pub export fn BlobArrayBuffer_deallocator(_: *anyopaque, blob: *anyopaque) void {

View File

@@ -44,6 +44,15 @@ pub fn init(allocator: std.mem.Allocator, file_count: usize) !LinkerGraph {
};
}
pub fn deinit(this: *LinkerGraph) void {
for (this.files.items(.quoted_source_contents)) |maybe_contents| {
if (maybe_contents) |contents| {
bun.default_allocator.free(contents);
}
}
this.files.clearAndFree(bun.default_allocator);
}
pub fn runtimeFunction(this: *const LinkerGraph, name: string) Ref {
return this.ast.items(.named_exports)[Index.runtime.value].get(name).?.ref;
}

View File

@@ -1,6 +1,6 @@
const vm_size_t = usize;
pub const enabled = Environment.allow_assert and Environment.isMac;
pub const enabled = Environment.allow_assert and Environment.isMac and !Environment.enable_asan;
fn heapLabel(comptime T: type) [:0]const u8 {
const base_name = if (@hasDecl(T, "heap_label"))

View File

@@ -1683,6 +1683,10 @@ pub const Chunk = struct {
return .{ .ctx = Type.init(allocator, prepend_count) };
}
pub inline fn deinit(this: *Format) void {
this.ctx.deinit();
}
pub inline fn appendLineSeparator(this: *Format) anyerror!void {
try this.ctx.appendLineSeparator();
}
@@ -1729,6 +1733,10 @@ pub const Chunk = struct {
return map;
}
pub fn deinit(this: *VLQSourceMap) void {
this.data.deinit();
}
pub fn appendLineSeparator(this: *VLQSourceMap) anyerror!void {
try this.data.appendChar(';');
}

View File

@@ -136,7 +136,11 @@ pub fn NewZlibReader(comptime Writer: type, comptime buffer_size: usize) type {
}
pub fn free(_: *anyopaque, data: *anyopaque) callconv(.C) void {
mimalloc.mi_free(data);
if (comptime bun.use_mimalloc) {
mimalloc.mi_free(data);
} else {
std.c.free(data);
}
}
pub fn deinit(this: *ZlibReader) void {
@@ -300,6 +304,10 @@ const ZlibAllocator = struct {
return zone.malloc_zone_calloc(items, len) orelse bun.outOfMemory();
}
if (comptime !bun.use_mimalloc) {
return std.c.calloc(items, len) orelse bun.outOfMemory();
}
return mimalloc.mi_calloc(items, len) orelse bun.outOfMemory();
}
@@ -310,7 +318,11 @@ const ZlibAllocator = struct {
return;
}
mimalloc.mi_free(data);
if (comptime bun.use_mimalloc) {
mimalloc.mi_free(data);
} else {
std.c.free(data);
}
}
};
@@ -757,7 +769,11 @@ pub const ZlibCompressorArrayList = struct {
}
pub fn free(_: *anyopaque, data: *anyopaque) callconv(.C) void {
mimalloc.mi_free(data);
if (comptime bun.use_mimalloc) {
mimalloc.mi_free(data);
} else {
std.c.free(data);
}
}
pub fn deinit(this: *ZlibCompressor) void {