Preserve errno across free, free_sized, and free_aligned_sized

This commit is contained in:
Slobodan Predolac 2026-04-29 15:06:27 -07:00
parent 4cc497a4a7
commit 3a77966168
2 changed files with 4 additions and 6 deletions

View file

@ -1035,6 +1035,7 @@ void
free_default(void *ptr) {
UTRACE(ptr, 0, 0);
if (likely(ptr != NULL)) {
int saved_errno = get_errno();
/*
* We avoid setting up tsd fully (e.g. tcache, arena binding)
* based on only free() calls -- other activities trigger the
@ -1061,6 +1062,7 @@ free_default(void *ptr) {
}
check_entry_exit_locking(tsd_tsdn(tsd));
set_errno(saved_errno);
}
}
@ -1896,6 +1898,7 @@ inallocx(tsdn_t *tsdn, size_t size, int flags) {
JEMALLOC_NOINLINE void
sdallocx_default(void *ptr, size_t size, int flags) {
int saved_errno = get_errno();
assert(ptr != NULL);
assert(malloc_initialized() || malloc_is_initializer());
@ -1918,6 +1921,7 @@ sdallocx_default(void *ptr, size_t size, int flags) {
isfree(tsd, ptr, usize, tcache, true);
}
check_entry_exit_locking(tsd_tsdn(tsd));
set_errno(saved_errno);
}
JEMALLOC_EXPORT void JEMALLOC_NOTHROW

View file

@ -662,11 +662,6 @@ pages_purge_process_madvise_impl(
return true;
}
/*
* TODO: remove this save/restore of errno after supporting errno
* preservation for free() call properly.
*/
int saved_errno = get_errno();
size_t purged_bytes = (size_t)syscall(JE_SYS_PROCESS_MADVISE_NR,
PIDFD_SELF, (struct iovec *)vec, vec_len, MADV_DONTNEED, 0);
if (purged_bytes == (size_t)-1) {
@ -676,7 +671,6 @@ pages_purge_process_madvise_impl(
atomic_store_b(
&process_madvise_gate, false, ATOMIC_RELAXED);
}
set_errno(saved_errno);
}
return purged_bytes != total_bytes;