| mi-malloc 1.8/2.1 |

Loading...
Searching...
No Matches
Functions
Basic Allocation

The basic allocation interface. More...

## Functions
void | mi_free (void *p)
| Free previously allocated memory.
void * | mi_malloc (size_t size)
| Allocate size bytes.
void * | mi_zalloc (size_t size)
| Allocate zero-initialized `size` bytes.
void * | mi_calloc (size_t count, size_t size)
| Allocate zero-initialized count elements of size bytes.
void * | mi_realloc (void *p, size_t newsize)
| Re-allocate memory to newsize bytes.
void * | mi_expand (void *p, size_t newsize)
| Try to re-allocate memory to newsize bytes in place.
void * | mi_mallocn (size_t count, size_t size)
| Allocate count elements of size bytes.
void * | mi_reallocn (void *p, size_t count, size_t size)
| Re-allocate memory to count elements of size bytes.
void * | mi_reallocf (void *p, size_t newsize)
| Re-allocate memory to newsize bytes,.
char * | mi_strdup (const char *s)
| Allocate and duplicate a string.
char * | mi_strndup (const char *s, size_t n)
| Allocate and duplicate a string up to n bytes.
char * | mi_realpath (const char *fname, char *resolved_name)
| Resolve a file path name.
size_t | mi_usable_size (void *p)
| Return the available bytes in a memory block.
size_t | mi_good_size (size_t size)
| Return the probable allocation block size for a given required size.

## Detailed Description

The basic allocation interface.

## Function Documentation

## ◆ mi_calloc()

void * mi_calloc | ( | size_t | count,
|  | size_t | size )

Allocate zero-initialized count elements of size bytes.

Parameters

  count | number of elements.
  size | size of each element.

Returns
  pointer to the allocated memory of size*count bytes, or NULL if either out of memory or when `count*size` overflows.

Returns a unique pointer if called with either size or count of 0.

See also
  mi_zalloc()

## ◆ mi_expand()

void * mi_expand | ( | void * | p,
|  | size_t | newsize )

Try to re-allocate memory to newsize bytes in place.

Parameters

  p | pointer to previously allocated memory (or NULL).
  newsize | the new required size in bytes.

Returns
  pointer to the re-allocated memory of newsize bytes (always equal to p), or NULL if either out of memory or if the memory could not be expanded in place. If NULL is returned, the pointer p is not freed. Otherwise the original pointer is returned as the reallocated result since it fits in-place with the new size. If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.

## ◆ mi_free()

void mi_free | ( | void * | p | ) |

Free previously allocated memory.

The pointer `p` must have been allocated before (or be NULL).

Parameters

  p | pointer to free, or NULL.

## ◆ mi_good_size()

size_t mi_good_size | ( | size_t | size | ) |

Return the probable allocation block size for a given required size.

Parameters

  size | The minimal required size in bytes.

Returns
  the size `n` that will be allocated, where `n >= size`.

Generally, `mi_usable_size(mi_malloc(size)) == mi_good_size(size)`. This function can be used to reduce internal wasted space when allocating buffers for example.

See also
  mi_usable_size()

## ◆ mi_malloc()

void * mi_malloc | ( | size_t | size | ) |

Allocate size bytes.

Parameters

  size | number of bytes to allocate.

Returns
  pointer to the allocated memory or NULL if out of memory. Returns a unique pointer if called with size 0.

## ◆ mi_mallocn()

void * mi_mallocn | ( | size_t | count,
|  | size_t | size )

Allocate count elements of size bytes.

Parameters

  count | The number of elements.
  size | The size of each element.

Returns
  A pointer to a block of count * size bytes, or NULL if out of memory or if count * size overflows.

If there is no overflow, it behaves exactly like `mi_malloc(count*size)`.

See also
  mi_calloc()
  mi_zallocn()

## ◆ mi_realloc()

void * mi_realloc | ( | void * | p,
|  | size_t | newsize )

Re-allocate memory to newsize bytes.

Parameters

  p | pointer to previously allocated memory (or NULL).
  newsize | the new required size in bytes.

Returns
  pointer to the re-allocated memory of newsize bytes, or NULL if out of memory. If NULL is returned, the pointer p is not freed. Otherwise the original pointer is either freed or returned as the reallocated result (in case it fits in-place with the new size). If the pointer p is NULL, it behaves as mi_malloc(newsize). If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.

See also
  mi_reallocf()

## ◆ mi_reallocf()

void * mi_reallocf | ( | void * | p,
|  | size_t | newsize )

Re-allocate memory to newsize bytes,.

Parameters

  p | pointer to previously allocated memory (or NULL).
  newsize | the new required size in bytes.

Returns
  pointer to the re-allocated memory of newsize bytes, or NULL if out of memory.

In contrast to mi_realloc(), if NULL is returned, the original pointer p is freed (if it was not NULL itself). Otherwise the original pointer is either freed or returned as the reallocated result (in case it fits in-place with the new size). If the pointer p is NULL, it behaves as mi_malloc(newsize). If newsize is larger than the original size allocated for p, the bytes after size are uninitialized.

See also
  reallocf (on BSD)

## ◆ mi_reallocn()

void * mi_reallocn | ( | void * | p,
|  | size_t | count,
|  | size_t | size )

Re-allocate memory to count elements of size bytes.

Parameters

  p | Pointer to a previously allocated block (or NULL).
  count | The number of elements.
  size | The size of each element.

Returns
  A pointer to a re-allocated block of count * size bytes, or NULL if out of memory or if count * size overflows.

If there is no overflow, it behaves exactly like `mi_realloc(p,count*size)`.

See also
  reallocarray() (on BSD)

## ◆ mi_realpath()

char * mi_realpath | ( | const char * | fname,
|  | char * | resolved_name )

Resolve a file path name.

Parameters

  fname | File name.
  resolved_name | Should be NULL (but can also point to a buffer of at least PATH_MAX bytes).

Returns
  If successful a pointer to the resolved absolute file name, or NULL on failure (with errno set to the error code).

If resolved_name was NULL, the returned result should be freed with mi_free().

Replacement for the standard realpath() such that mi_free() can be used on the returned result (if resolved_name was NULL).

## ◆ mi_strdup()

char * mi_strdup | ( | const char * | s | ) |

Allocate and duplicate a string.

Parameters

  s | string to duplicate (or NULL).

Returns
  a pointer to newly allocated memory initialized to string s, or NULL if either out of memory or if s is NULL.

Replacement for the standard strdup() such that mi_free() can be used on the returned result.

## ◆ mi_strndup()

char * mi_strndup | ( | const char * | s,
|  | size_t | n )

Allocate and duplicate a string up to n bytes.

Parameters

  s | string to duplicate (or NULL).
  n | maximum number of bytes to copy (excluding the terminating zero).

Returns
  a pointer to newly allocated memory initialized to string s up to the first n bytes (and always zero terminated), or NULL if either out of memory or if s is NULL.

Replacement for the standard strndup() such that mi_free() can be used on the returned result.

## ◆ mi_usable_size()

size_t mi_usable_size | ( | void * | p | ) |

Return the available bytes in a memory block.

Parameters

  p | Pointer to previously allocated memory (or NULL)

Returns
  Returns the available bytes in the memory block, or 0 if p was NULL.

The returned size can be used to call mi_expand successfully. The returned size is always at least equal to the allocated size of p.

See also
  _msize (Windows)
  malloc_usable_size (Linux)
  mi_good_size()

## ◆ mi_zalloc()

void * mi_zalloc | ( | size_t | size | ) |

Allocate zero-initialized `size` bytes.

Parameters

  size | The size in bytes.

Returns
  Pointer to newly allocated zero initialized memory, or NULL if out of memory.