Basic Allocation

The basic allocation interface. More...

## Functions

void | mi_free (void *p)
| Free previously allocated memory. More...
void * | mi_malloc (size_t size)
| Allocate size bytes. More...
void * | mi_zalloc (size_t size)
| Allocate zero-initialized `size` bytes. More...
void * | mi_calloc (size_t count, size_t size)
| Allocate zero-initialized count elements of size bytes. More...
void * | mi_realloc (void *p, size_t newsize)
| Re-allocate memory to newsize bytes. More...
void * | mi_recalloc (void *p, size_t count, size_t size)
| Re-allocate memory to count elements of size bytes, with extra memory initialized to zero. More...
void * | mi_expand (void *p, size_t newsize)
| Try to re-allocate memory to newsize bytes in place. More...
void * | mi_mallocn (size_t count, size_t size)
| Allocate count elements of size bytes. More...
void * | mi_reallocn (void *p, size_t count, size_t size)
| Re-allocate memory to count elements of size bytes. More...
void * | mi_reallocf (void *p, size_t newsize)
| Re-allocate memory to newsize bytes,. More...
char * | mi_strdup (const char *s)
| Allocate and duplicate a string. More...
char * | mi_strndup (const char *s, size_t n)
| Allocate and duplicate a string up to n bytes. More...
char * | mi_realpath (const char *fname, char *resolved_name)
| Resolve a file path name. More...

## 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_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(p,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.

## 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_recalloc()

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

Re-allocate memory to count elements of size bytes, with extra memory initialized to zero.

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_rezalloc(p,count*size)`.

See also
  mi_reallocn()
  recallocarray() (on BSD).

## 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_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.