Skip to main content

use_global_allocator

Function use_global_allocator 

Source
pub unsafe fn use_global_allocator() -> bool
Expand description

Routes OpenSSL memory allocations to the Rust global allocator.

OpenSSL allocates with the C runtime malloc by default. After this call it allocates through std::alloc, so an application that registers a #[global_allocator] (for example mimalloc) uses it for TLS buffers and handshake state as well.

The setting is process wide and OpenSSL accepts it only before its first allocation, call this at the start of main before any other OpenSSL use. Returns false if OpenSSL has already allocated memory, another component has installed OpenSSL memory hooks, or the linked library does not support custom allocators (OpenSSL older than 1.1.0, LibreSSL, BoringSSL, aws-lc). A failed call leaves the existing allocator unchanged.

Returns true when the hooks were installed, or were already installed by an earlier call. This affects all OpenSSL users in the process, not just connections created by this crate.

ยงSafety

OpenSSL keeps its allocator hooks in unsynchronized globals. No other thread may use OpenSSL or replace its memory hooks while this function runs, and no other component may replace the hooks afterwards, otherwise a block may be released by an allocator that did not allocate it.

OpenSSL 3.5 and later on Unix allocate aligned blocks without closing the window for replacing the hooks. Such a block must not be live unless OpenSSL has also made a regular allocation, otherwise it would be released through these hooks. Library initialization makes regular allocations, calling this before any other OpenSSL use excludes the case.