---
name: "vmem_malloc"
section: "3"
platform: "linux"
package: "libvmem-dev"
version: "1.8-2"
release: "Debian 11 (Bullseye)"
platforms: ["linux"]
url: "https://new.carta.tech/vmem_malloc"
---

# vmem\_malloc(3)

memory allocation related functions

## NAME

**vmem\_malloc**(), **vmem\_calloc**(), **vmem\_realloc**(), **vmem\_free**(), **vmem\_aligned\_alloc**(), **vmem\_strdup**(), **vmem\_wcsdup**(), **vmem\_malloc\_usable\_size**() - memory allocation related functions

## SYNOPSIS

-

  ```
  #include <libvmem.h>
  void *vmem_malloc(VMEM *vmp, size_t size);
  void vmem_free(VMEM *vmp, void *ptr);
  void *vmem_calloc(VMEM *vmp, size_t nmemb, size_t size);
  void *vmem_realloc(VMEM *vmp, void *ptr, size_t size);
  void *vmem_aligned_alloc(VMEM *vmp, size_t alignment, size_t size);
  char *vmem_strdup(VMEM *vmp, const char *s);
  wchar_t *vmem_wcsdup(VMEM *vmp, const wchar_t *s);
  size_t vmem_malloc_usable_size(VMEM *vmp, void *ptr);
  ```

## DESCRIPTION

This section describes the *malloc*-like API provided by [**libvmem**(7)](/libvmem/7.md). These functions provide the same semantics as their libc namesakes, but operate on the memory pools specified by their first arguments.

The **vmem\_malloc**() function provides the same semantics as [**malloc**(3)](/malloc/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. It allocates specified *size* bytes.

The **vmem\_free**() function provides the same semantics as [**free**(3)](/free/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. It frees the memory space pointed to by *ptr*, which must have been returned by a previous call to **vmem\_malloc**(), **vmem\_calloc**() or **vmem\_realloc**() for *the same* *pool of memory*. If *ptr* is NULL, no operation is performed.

The **vmem\_calloc**() function provides the same semantics as [**calloc**(3)](/calloc/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. It allocates memory for an array of *nmemb* elements of *size* bytes each. The memory is set to zero.

The **vmem\_realloc**() function provides the same semantics as [**realloc**(3)](/realloc/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. It changes the size of the memory block pointed to by *ptr* to *size* bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will *not* be initialized.

Unless *ptr* is NULL, it must have been returned by an earlier call to **vmem\_malloc**(), **vmem\_calloc**() or **vmem\_realloc**(). If *ptr* is NULL, then the call is equivalent to *vmem\_malloc(vmp, size)*, for all values of *size*; if *size* is equal to zero, and *ptr* is not NULL, then the call is equivalent to *vmem\_free(vmp, ptr)*.

The **vmem\_aligned\_alloc**() function provides the same semantics as [**aligned\_alloc**(3)](/aligned_alloc/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. It allocates *size* bytes from the memory pool. The memory address will be a multiple of *alignment*, which must be a power of two.

The **vmem\_strdup**() function provides the same semantics as [**strdup**(3)](/strdup/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. Memory for the new string is obtained with **vmem\_malloc**(), on the given memory pool, and can be freed with **vmem\_free**() on the same memory pool.

The **vmem\_wcsdup**() function provides the same semantics as [**wcsdup**(3)](/wcsdup/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system. Memory for the new string is obtained with **vmem\_malloc**(), on the given memory pool, and can be freed with **vmem\_free**() on the same memory pool.

The **vmem\_malloc\_usable\_size**() function provides the same semantics as [**malloc\_usable\_size**(3)](/malloc_usable_size/3.md), but operates on the memory pool *vmp* instead of the process heap supplied by the system.

## RETURN VALUE

On success, **vmem\_malloc**() returns a pointer to the allocated memory. If *size* is 0, then **vmem\_malloc**() returns either NULL, or a unique pointer value that can later be successfully passed to **vmem\_free**(). If **vmem\_malloc**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

The **vmem\_free**() function returns no value. Undefined behavior occurs if frees do not correspond to allocated memory from the same memory pool.

On success, **vmem\_calloc**() returns a pointer to the allocated memory. If *nmemb* or *size* is 0, then **vmem\_calloc**() returns either NULL, or a unique pointer value that can later be successfully passed to **vmem\_free**(). If **vmem\_calloc**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

On success, **vmem\_realloc**() returns a pointer to the allocated memory, which may be different from *ptr*. If the area pointed to was moved, a *vmem\_free(vmp, ptr)* is done. If **vmem\_realloc**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

On success, **vmem\_aligned\_alloc**() returns a pointer to the allocated memory. If **vmem\_aligned\_alloc**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

On success, **vmem\_strdup**() returns a pointer to a new string which is a duplicate of the string *s*. If **vmem\_strdup**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

On success, **vmem\_wcsdup**() returns a pointer to a new wide character string which is a duplicate of the wide character string *s*. If **vmem\_wcsdup**() is unable to satisfy the allocation request, it returns NULL and sets *errno* appropriately.

The **vmem\_malloc\_usable\_size**() function returns the number of usable bytes in the block of allocated memory pointed to by *ptr*, a pointer to a block of memory allocated by **vmem\_malloc**() or a related function. If *ptr* is NULL, 0 is returned.

## SEE ALSO

[**calloc**(3)](/calloc/3.md), [**free**(3)](/free/3.md), [**malloc**(3)](/malloc/3.md), [**malloc\_usable\_size**(3)](/malloc_usable_size/3.md), [**realloc**(3)](/realloc/3.md), [**strdup**(3)](/strdup/3.md), [**wcsdup**(3)](/wcsdup/3.md) **[libvmem(7)](/libvmem/7.md)** and **\<http://pmem.io\>**
