mi-malloc: Heaps

| mi-malloc 1.8/2.1 |

Loading...
Searching...
No Matches
Typedefs | Functions
Heaps

First-class heaps. More...

## Typedefs
typedef struct mi_heap_s | mi_heap_t
| Type of first-class heaps.

## Functions
mi_heap_t * | mi_heap_new ()
| Create a new heap that can be used for allocation.
void | mi_heap_delete (mi_heap_t *heap)
| Delete a previously allocated heap.
void | mi_heap_destroy (mi_heap_t *heap)
| Destroy a heap, freeing all its still allocated blocks.
mi_heap_t * | mi_heap_set_default (mi_heap_t *heap)
| v1,v2: Set the default heap to use in the current thread for mi_malloc() et al.
mi_heap_t * | mi_heap_get_default ()
| v1,v2: Get the default heap that is used for mi_malloc() et al.
mi_heap_t * | mi_heap_get_backing ()
| v1,v2: Get the backing heap.
void | mi_heap_set_numa_affinity (mi_heap_t *heap, int numa_node)
| v3: set NUMA affinity for a heap.
mi_heap_t * | mi_heap_main (void)
| v3: the main heap (of the current sub-process)
mi_heap_t * | mi_heap_of (const void *p)
| v3: return the heap that contains the given pointer.
bool | mi_heap_contains (const mi_heap_t *heap, const void *p)
| v3: does a heap contain a specific pointer?
bool | mi_any_heap_contains (const void *p)
| v3: is a pointer pointing into mimalloc managed memory?
bool | mi_is_in_heap_region (const void *p)
| v1,v2: Is a pointer part of our heap?
bool | mi_check_owned (const void *p)
| v1,v2: Check if any pointer is part of the default heap of this thread.
bool | mi_heap_contains_block (mi_heap_t *heap, const void *p)
| v1,v2: Does a heap contain a pointer to a previously allocated block?
bool | mi_heap_check_owned (mi_heap_t *heap, const void *p)
| v1,v2: Check safely if any pointer is part of a heap.
void | mi_heap_collect (mi_heap_t *heap, bool force)
| Release outstanding resources in a specific heap.
void * | mi_heap_malloc (mi_heap_t *heap, size_t size)
| Allocate in a specific heap.
void * | mi_heap_malloc_small (mi_heap_t *heap, size_t size)
| Allocate a small object in a specific heap.
void * | mi_heap_zalloc (mi_heap_t *heap, size_t size)
| Allocate zero-initialized in a specific heap.
void * | mi_heap_zalloc_small (mi_heap_t *heap, size_t size)
| Allocate a small object in a specific heap.
void * | mi_heap_calloc (mi_heap_t *heap, size_t count, size_t size)
| Allocate count zero-initialized elements in a specific heap.
void * | mi_heap_mallocn (mi_heap_t *heap, size_t count, size_t size)
| Allocate count elements in a specific heap.
char * | mi_heap_strdup (mi_heap_t *heap, const char *s)
| Duplicate a string in a specific heap.
char * | mi_heap_strndup (mi_heap_t *heap, const char *s, size_t n)
| Duplicate a string of at most length n in a specific heap.
char * | mi_heap_realpath (mi_heap_t *heap, const char *fname, char *resolved_name)
| Resolve a file path name using a specific heap to allocate the result.
void * | mi_heap_realloc (mi_heap_t *heap, void *p, size_t newsize)
void * | mi_heap_reallocn (mi_heap_t *heap, void *p, size_t count, size_t size)
void * | mi_heap_reallocf (mi_heap_t *heap, void *p, size_t newsize)
void * | mi_heap_malloc_aligned (mi_heap_t *heap, size_t size, size_t alignment)
void * | mi_heap_malloc_aligned_at (mi_heap_t *heap, size_t size, size_t alignment, size_t offset)
void * | mi_heap_zalloc_aligned (mi_heap_t *heap, size_t size, size_t alignment)
void * | mi_heap_zalloc_aligned_at (mi_heap_t *heap, size_t size, size_t alignment, size_t offset)
void * | mi_heap_calloc_aligned (mi_heap_t *heap, size_t count, size_t size, size_t alignment)
void * | mi_heap_calloc_aligned_at (mi_heap_t *heap, size_t count, size_t size, size_t alignment, size_t offset)
void * | mi_heap_realloc_aligned (mi_heap_t *heap, void *p, size_t newsize, size_t alignment)
void * | mi_heap_realloc_aligned_at (mi_heap_t *heap, void *p, size_t newsize, size_t alignment, size_t offset)

## Detailed Description

First-class heaps.

Heaps allow allocations to be grouped, and for example be destroyed in one go. Heaps can also be associated with a specific allocation arena (`mi_heap_new_in_arena()`).

v1,v2: heaps are only semi-first-class and one can only use heap allocation functions from the thread that created the heap. (of course, mi_free() can always be used from any thread to free objects from any heap).

v3: heaps are fully first-class and can be used to allocate efficiently from from any thread. In v3, the old v1/v2 heaps still exist but are now called theaps (mi_theap_t()) for thread-local heaps. A v3 heap creates internally such theaps on demand to efficiently allocate without needing taking locks for example.

## Typedef Documentation

## ◆ mi_heap_t

typedef struct mi_heap_s mi_heap_t

Type of first-class heaps.

v1,v2: in mimalloc v1 and v2, a heap can only be used for allocation in the thread that created this heap!

## Function Documentation

## ◆ mi_any_heap_contains()

bool mi_any_heap_contains | ( | const void * | p | ) |

v3: is a pointer pointing into mimalloc managed memory?

Parameters

  p | Any pointer – not required to be previously allocated by mimalloc. This is a constant time efficient function.

## ◆ mi_check_owned()

bool mi_check_owned | ( | const void * | p | ) |

v1,v2: Check if any pointer is part of the default heap of this thread.

Parameters

  p | Any pointer – not required to be previously allocated by us.

Returns
  true if p points to a block in default heap of this thread. Note: expensive function, linear in the pages in the heap.

See also
  mi_any_heap_contains() for v3

## ◆ mi_heap_calloc()

void * mi_heap_calloc | ( | mi_heap_t * | heap,
|  | size_t | count,
|  | size_t | size )

Allocate count zero-initialized elements in a specific heap.

See also
  mi_calloc()

## ◆ mi_heap_calloc_aligned()

void * mi_heap_calloc_aligned | ( | mi_heap_t * | heap,
|  | size_t | count,
|  | size_t | size,
|  | size_t | alignment )

## ◆ mi_heap_calloc_aligned_at()

void * mi_heap_calloc_aligned_at | ( | mi_heap_t * | heap,
|  | size_t | count,
|  | size_t | size,
|  | size_t | alignment,
|  | size_t | offset )

## ◆ mi_heap_check_owned()

bool mi_heap_check_owned | ( | mi_heap_t * | heap,
|  | const void * | p )

v1,v2: Check safely if any pointer is part of a heap.

Parameters

  heap | The heap.
  p | Any pointer – not required to be previously allocated by us.

Returns
  true if p points to a block in heap. Note: expensive function, linear in the pages in the heap.

See also
  mi_heap_contains_block(), mi_heap_contains() for v3

## ◆ mi_heap_collect()

void mi_heap_collect | ( | mi_heap_t * | heap,
|  | bool | force )

Release outstanding resources in a specific heap.

## ◆ mi_heap_contains()

bool mi_heap_contains | ( | const mi_heap_t * | heap,
|  | const void * | p )

v3: does a heap contain a specific pointer?

Parameters

  heap | The heap to query.
  p | Any pointer – not required to be previously allocated by mimalloc. This is a constant time efficient function.

## ◆ mi_heap_contains_block()

bool mi_heap_contains_block | ( | mi_heap_t * | heap,
|  | const void * | p )

v1,v2: Does a heap contain a pointer to a previously allocated block?

Parameters

  heap | The heap.
  p | Pointer to a previously allocated block (in any heap)– cannot be some random pointer!

Returns
  true if the block pointed to by p is in the heap.

See also
  mi_heap_owned(), mi_heap_contains() for v3

## ◆ mi_heap_delete()

void mi_heap_delete | ( | mi_heap_t * | heap | ) |

Delete a previously allocated heap.

This will release internal resources but not free still allocated blocks in this heap (and should be `mi_free`'d later on as usual).

Note: trying to delete the main heap of a subprocess is ignored.

## ◆ mi_heap_destroy()

void mi_heap_destroy | ( | mi_heap_t * | heap | ) |

Destroy a heap, freeing all its still allocated blocks.

Use with care as this will free all blocks still allocated in the heap. However, this can be an efficient way to free all heap memory in one go.

Note: trying to destroy the main heap of a subprocess is ignored.

## ◆ mi_heap_get_backing()

mi_heap_t * mi_heap_get_backing | ( |  | ) |

v1,v2: Get the backing heap.

The backing heap is the initial default heap for a thread and always available for allocations. It cannot be destroyed or deleted except by exiting the thread.

See also
  mi_heap_main() for v3

## ◆ mi_heap_get_default()

mi_heap_t * mi_heap_get_default | ( |  | ) |

v1,v2: Get the default heap that is used for mi_malloc() et al.

(for the current thread).

Returns
  The current default heap.

See also
  mi_theap_get_default() for v3

## ◆ mi_heap_main()

mi_heap_t * mi_heap_main | ( | void |  | ) |

v3: the main heap (of the current sub-process)

Returns
  a pointer to the main heap Every (sub)process has a main heap that cannot be deleted or destroyed until (sub)process exit. If other heaps are deleted their live objects are migrated to the main heap.

## ◆ mi_heap_malloc()

void * mi_heap_malloc | ( | mi_heap_t * | heap,
|  | size_t | size )

Allocate in a specific heap.

See also
  mi_malloc()

## ◆ mi_heap_malloc_aligned()

void * mi_heap_malloc_aligned | ( | mi_heap_t * | heap,
|  | size_t | size,
|  | size_t | alignment )

## ◆ mi_heap_malloc_aligned_at()

void * mi_heap_malloc_aligned_at | ( | mi_heap_t * | heap,
|  | size_t | size,
|  | size_t | alignment,
|  | size_t | offset )

## ◆ mi_heap_malloc_small()

void * mi_heap_malloc_small | ( | mi_heap_t * | heap,
|  | size_t | size )

Allocate a small object in a specific heap.

size must be smaller or equal to MI_SMALL_SIZE_MAX().

See also
  mi_malloc_small()

## ◆ mi_heap_mallocn()

void * mi_heap_mallocn | ( | mi_heap_t * | heap,
|  | size_t | count,
|  | size_t | size )

Allocate count elements in a specific heap.

See also
  mi_mallocn()

## ◆ mi_heap_new()

mi_heap_t * mi_heap_new | ( |  | ) |

Create a new heap that can be used for allocation.

## ◆ mi_heap_of()

mi_heap_t * mi_heap_of | ( | const void * | p | ) |

v3: return the heap that contains the given pointer.

Parameters

  p | Any pointer – not required to be previously allocated by mimalloc.

Returns
  the heap that contains the given pointer, or `NULL` if the pointer does not point into mimalloc allocated memory. This is a constant time efficient function.

## ◆ mi_heap_realloc()

void * mi_heap_realloc | ( | mi_heap_t * | heap,
|  | void * | p,
|  | size_t | newsize )

## ◆ mi_heap_realloc_aligned()

void * mi_heap_realloc_aligned | ( | mi_heap_t * | heap,
|  | void * | p,
|  | size_t | newsize,
|  | size_t | alignment )

## ◆ mi_heap_realloc_aligned_at()

void * mi_heap_realloc_aligned_at | ( | mi_heap_t * | heap,
|  | void * | p,
|  | size_t | newsize,
|  | size_t | alignment,
|  | size_t | offset )

## ◆ mi_heap_reallocf()

void * mi_heap_reallocf | ( | mi_heap_t * | heap,
|  | void * | p,
|  | size_t | newsize )

## ◆ mi_heap_reallocn()

void * mi_heap_reallocn | ( | mi_heap_t * | heap,
|  | void * | p,
|  | size_t | count,
|  | size_t | size )

## ◆ mi_heap_realpath()

char * mi_heap_realpath | ( | mi_heap_t * | heap,
|  | const char * | fname,
|  | char * | resolved_name )

Resolve a file path name using a specific heap to allocate the result.

See also
  mi_realpath()

## ◆ mi_heap_set_default()

mi_heap_t * mi_heap_set_default | ( | mi_heap_t * | heap | ) |

v1,v2: Set the default heap to use in the current thread for mi_malloc() et al.

Parameters

  heap | The new default heap.

Returns
  The previous default heap.

See also
  mi_theap_set_default() for v3

## ◆ mi_heap_set_numa_affinity()

void mi_heap_set_numa_affinity | ( | mi_heap_t * | heap,
|  | int | numa_node )

v3: set NUMA affinity for a heap.

Parameters

  heap | the heap which should be associated with a specific numa node.
  numa_node | the numa node to associate to (`>=0`)

## ◆ mi_heap_strdup()

char * mi_heap_strdup | ( | mi_heap_t * | heap,
|  | const char * | s )

Duplicate a string in a specific heap.

See also
  mi_strdup()

## ◆ mi_heap_strndup()

char * mi_heap_strndup | ( | mi_heap_t * | heap,
|  | const char * | s,
|  | size_t | n )

Duplicate a string of at most length n in a specific heap.

See also
  mi_strndup()

## ◆ mi_heap_zalloc()

void * mi_heap_zalloc | ( | mi_heap_t * | heap,
|  | size_t | size )

Allocate zero-initialized in a specific heap.

See also
  mi_zalloc()

## ◆ mi_heap_zalloc_aligned()

void * mi_heap_zalloc_aligned | ( | mi_heap_t * | heap,
|  | size_t | size,
|  | size_t | alignment )

## ◆ mi_heap_zalloc_aligned_at()

void * mi_heap_zalloc_aligned_at | ( | mi_heap_t * | heap,
|  | size_t | size,
|  | size_t | alignment,
|  | size_t | offset )

## ◆ mi_heap_zalloc_small()

void * mi_heap_zalloc_small | ( | mi_heap_t * | heap,
|  | size_t | size )

Allocate a small object in a specific heap.

size must be smaller or equal to MI_SMALL_SIZE_MAX().

See also
  mi_zalloc_small()

## ◆ mi_is_in_heap_region()

bool mi_is_in_heap_region | ( | const void * | p | ) |

v1,v2: Is a pointer part of our heap?

Parameters

  p | The pointer to check.

Returns
  true if this is a pointer into our heap. This function is relatively fast.

See also
  mi_any_heap_contains() for v3.