Skip to content

Commit

Permalink
flag sqlite mimalloc allocator (#1497)
Browse files Browse the repository at this point in the history
* flag sqlite mimalloc allocator

* fix potential segv when alloc return null
  • Loading branch information
MarinPostma authored Jun 24, 2024
1 parent e1583dd commit 8da0b63
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion libsql-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ where
static INIT: std::sync::Once = std::sync::Once::new();
let mut join_set = JoinSet::new();

setup_sqlite_alloc();
if std::env::var("LIBSQL_SQLITE_MIMALLOC").is_ok() {
setup_sqlite_alloc();
}

INIT.call_once(|| {
if let Ok(size) = std::env::var("LIBSQL_EXPERIMENTAL_PAGER") {
Expand Down Expand Up @@ -746,6 +748,11 @@ fn setup_sqlite_alloc() {
let size_total = size as usize + size_of::<usize>();
let layout = Layout::from_size_align(size_total, align_of::<usize>()).unwrap();
let ptr = GLOBAL.alloc(layout);

if ptr.is_null() {
return std::ptr::null_mut();
}

*(ptr as *mut usize) = size as usize;
ptr.offset(size_of::<usize>() as _) as *mut _
}
Expand All @@ -766,6 +773,11 @@ fn setup_sqlite_alloc() {
layout,
new_size as usize + size_of::<usize>(),
);

if ptr.is_null() {
return std::ptr::null_mut();
}

*(new_ptr as *mut usize) = new_size as usize;
new_ptr.offset(size_of::<usize>() as _) as *mut _
}
Expand Down

0 comments on commit 8da0b63

Please sign in to comment.