mi-malloc 3.7/2.5/1.15
Loading...
Searching...
No Matches
Extended Functions

Internal functionality. More...

Typedefs

typedef void mi_deferred_free_fun(bool force, unsigned long long heartbeat, void *arg)
 Type of deferred free functions.
typedef void mi_output_fun(const char *msg, void *arg)
 Type of output functions.
typedef void mi_error_fun(int err, void *arg)
 Type of error callback functions.

Functions

int mi_version (void)
 Return the mimalloc version.
void mi_collect (bool force)
 Eagerly free memory.
void mi_collect_reduce (size_t target_thread_owned)
void mi_process_init (void)
 Initialize mimalloc for the process. Normally called automatically; repeated calls have no effect.
void mi_process_done (void)
 Perform process-exit cleanup.
void mi_thread_set_in_threadpool (void)
 v3: Communicate that a thread is in a threadpool.
bool mi_is_redirected ()
 Is the C runtime malloc API redirected?
void mi_thread_init (void)
 Initialize mimalloc on a thread.
void mi_thread_done (void)
 Uninitialize mimalloc on a thread.
void mi_register_deferred_free (mi_deferred_free_fun *deferred_free, void *arg)
 Register a deferred free function.
void mi_register_output (mi_output_fun *out, void *arg)
 Register an output function.
void mi_register_error (mi_error_fun *errfun, void *arg)
 Register an error callback function.

Detailed Description

Internal functionality.

Typedef Documentation

◆ mi_deferred_free_fun

typedef void mi_deferred_free_fun(bool force, unsigned long long heartbeat, void *arg)

Type of deferred free functions.

Parameters
forceIf true all outstanding items should be freed.
heartbeatA monotonically increasing count.
argArgument that was passed at registration to hold extra state.
See also
mi_register_deferred_free

◆ mi_error_fun

typedef void mi_error_fun(int err, void *arg)

Type of error callback functions.

Parameters
errError code (see mi_register_error() for a complete list).
argArgument that was passed at registration to hold extra state.
See also
mi_register_error()

◆ mi_output_fun

typedef void mi_output_fun(const char *msg, void *arg)

Type of output functions.

Parameters
msgMessage to output.
argArgument that was passed at registration to hold extra state.
See also
mi_register_output()

Function Documentation

◆ mi_collect()

void mi_collect ( bool force)

Eagerly free memory.

Parameters
forceIf true, aggressively return memory to the OS (can be expensive!)

Regular code should not have to call this function. It can be beneficial in very narrow circumstances; in particular, when a long running thread allocates a lot of blocks that are freed by other threads it may improve resource usage by calling this every once in a while.

◆ mi_collect_reduce()

void mi_collect_reduce ( size_t target_thread_owned)
Deprecated
Historical collection API, declared but not implemented in v3. Use mi_collect() instead.

◆ mi_is_redirected()

bool mi_is_redirected ( )

Is the C runtime malloc API redirected?

Returns
true if all malloc API calls are redirected to mimalloc.

Currently only used on Windows.

◆ mi_process_done()

void mi_process_done ( void )

Perform process-exit cleanup.

Normally called automatically on process exit or library unload. Not intended for use while the program continues allocating.

◆ mi_process_init()

void mi_process_init ( void )

Initialize mimalloc for the process. Normally called automatically; repeated calls have no effect.

◆ mi_register_deferred_free()

void mi_register_deferred_free ( mi_deferred_free_fun * deferred_free,
void * arg )

Register a deferred free function.

Parameters
deferred_freeAddress of a deferred free-ing function or NULL to unregister.
argArgument that will be passed on to the deferred free function.

Some runtime systems use deferred free-ing, for example when using reference counting to limit the worst case free time. Such systems can register (re-entrant) deferred free function to free more memory on demand. When the force parameter is true all possible memory should be freed. The per-thread heartbeat parameter is monotonically increasing and guaranteed to be deterministic if the program allocates deterministically. The deferred_free function is guaranteed to be called deterministically after some number of allocations (regardless of freeing or available free memory). At most one deferred_free function can be active. The function is always called with the argument it was registered with, but after re-registration other threads may still call the previous function for a short while.

The deferred free, output, and error functions can allocate and free, but nested callbacks are limited (to 8) and exceeding this is a fatal error. These functions cannot call mi_thread_done(), or delete, destroy, or release a heap or theap in use by the current thread (in which case an EINVAL error is raised), as the operation that invoked the callback may still be using it.

◆ mi_register_error()

void mi_register_error ( mi_error_fun * errfun,
void * arg )

Register an error callback function.

Parameters
errfunThe error function that is called on an error (use NULL for default)
argExtra argument that will be passed on to the error function.

The errfun function is called on an error in mimalloc after emitting an error message (through the output function). It as always legal to just return from the errfun function in which case allocation functions generally return NULL or ignore the condition. An EFAULT error message is always shown, and when compiled in secure mode, mimalloc always calls abort() after an EFAULT error (even if the error function returns). The possible error codes are:

  • EAGAIN: Double free was detected (only in debug and secure mode).
  • EFAULT: Corrupted free list or meta-data was detected (only in debug and secure mode).
  • ENOMEM: Not enough memory available to satisfy the request.
  • EOVERFLOW: Too large a request, for example in mi_calloc(), the count and size parameters are too large.
  • EINVAL: Trying to free or re-allocate an invalid pointer.

Independent of the error code passed to errfun, allocation functions that return NULL set errno as required by POSIX: ENOMEM, or EINVAL for an invalid alignment. Other errors (like freeing an invalid pointer) do not modify errno.

The function is always called with the argument it was registered with, but after re-registration other threads may still call the previous function for a short while. See mi_register_deferred_free() for the restrictions on callbacks.

◆ mi_register_output()

void mi_register_output ( mi_output_fun * out,
void * arg )

Register an output function.

Parameters
outThe output function, use NULL to output to stderr.
argArgument that will be passed on to the output function.

The out function is called to output any information from mimalloc, like verbose or warning messages. The function is always called with the argument it was registered with, but after re-registration other threads may still call the previous function for a short while. See mi_register_deferred_free() for the restrictions on callbacks.

◆ mi_thread_done()

void mi_thread_done ( void )

Uninitialize mimalloc on a thread.

Should not be used as on most systems (pthreads, windows) this is done automatically. Ensures that any memory that is not freed yet (but will be freed by other threads in the future) is properly handled. Afterwards, the thread is not implicitly re-initialized: allocations (for example in thread-local destructors) use a shared fallback heap until mi_thread_init() is called explicitly. Cannot be called from within a deferred free, output, or error function, or from within a mi_heap_visit_blocks() visitor (in which case an EINVAL error is raised).

◆ mi_thread_init()

void mi_thread_init ( void )

Initialize mimalloc on a thread.

Should not be used as on most systems (pthreads, windows) this is done automatically.

◆ mi_thread_set_in_threadpool()

void mi_thread_set_in_threadpool ( void )

v3: Communicate that a thread is in a threadpool.

This is done automatically for threads in the Windows threadpool, but if using a custom threadpool it is good to call this on worker threads. Internally, mimalloc uses different locality heuristics for worker threads to try to reduce non-local accesses.

◆ mi_version()

int mi_version ( void )

Return the mimalloc version.

Returns
The version. For v1,v2 the version is 100*major + 10*minor + patch (for example 227 for v2.2.7). For v3, it is 1000*major + 100*minor + path (for example, 3207 for v3.2.7).