.\" Automatically generated by Pandoc 1.16.0.2
.\"
.TH "PMEMCTO_MALLOC" "3" "2017-12-22" "PMDK - libpmemcto API version 1.0" "PMDK Programmer's Manual"
.hy
.\" Copyright 2014-2017, Intel Corporation
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" * Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\"
.\" * Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" * Neither the name of the copyright holder nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
.\" OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
.\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.SH NAME
.PP
pmemcto_malloc, pmemcto_free, pmemcto_calloc, pmemcto_realloc \-\-
allocate and free persistent memory
.SH SYNOPSIS
.IP
.nf
\f[C]
#include\ <libpmemcto.h>
void\ *pmemcto_malloc(PMEMctopool\ *pcp,\ size_t\ size);
void\ pmemcto_free(PMEMctopool\ *pcp,\ void\ *ptr);
void\ *pmemcto_calloc(PMEMctopool\ *pcp,\ size_t\ nmemb,\ size_t\ size);
void\ *pmemcto_realloc(PMEMctopool\ *pcp,\ void\ *ptr,\ size_t\ size);
\f[]
.fi
.SH DESCRIPTION
.PP
The \f[B]pmemcto_malloc\f[]() function provides the same semantics as
\f[B]malloc\f[](3), but operates on the memory pool \f[I]pcp\f[] instead
of the process heap supplied by the system.
It allocates \f[I]size\f[] bytes and returns a pointer to the allocated
memory.
\f[I]The memory is not initialized\f[].
If \f[I]size\f[] is 0, then \f[B]pmemcto_malloc\f[]() returns either
NULL, or a unique pointer value that can later be successfully passed to
\f[B]pmemcto_free\f[]().
.PP
The \f[B]pmemcto_free\f[]() function provides the same semantics as
\f[B]free\f[](3), but operates on the memory pool \f[I]pcp\f[] instead
of the process heap supplied by the system.
It frees the memory space pointed to by \f[I]ptr\f[], which must have
been returned by a previous call to \f[B]pmemcto_malloc\f[](),
\f[B]pmemcto_calloc\f[]() or \f[B]pmemcto_realloc\f[]() for \f[I]the
same pool of memory\f[].
Undefined behavior occurs if frees do not correspond to allocated memory
from the same memory pool.
If \f[I]ptr\f[] is NULL, no operation is performed.
.PP
The \f[B]pmemcto_calloc\f[]() function provides the same semantics as
\f[B]calloc\f[](3), but operates on the memory pool \f[I]pcp\f[] instead
of the process heap supplied by the system.
It allocates memory for an array of \f[I]nmemb\f[] elements of
\f[I]size\f[] bytes each and returns a pointer to the allocated memory.
The memory is set to zero.
If \f[I]nmemb\f[] or \f[I]size\f[] is 0, then \f[B]pmemcto_calloc\f[]()
returns either NULL, or a unique pointer value that can later be
successfully passed to \f[B]pmemcto_free\f[]().
.PP
The \f[B]pmemcto_realloc\f[]() function provides the same semantics as
\f[B]realloc\f[](3), but operates on the memory pool \f[I]pcp\f[]
instead of the process heap supplied by the system.
It changes the size of the memory block pointed to by \f[I]ptr\f[] to
\f[I]size\f[] 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
\f[I]not\f[] be initialized.
If \f[I]ptr\f[] is NULL, then the call is equivalent to
\f[I]pmemcto_malloc(pcp, size)\f[], for all values of \f[I]size\f[]; if
\f[I]size\f[] is equal to zero and \f[I]ptr\f[] is not NULL, then the
call is equivalent to \f[I]pmemcto_free(pcp, ptr)\f[].
Unless \f[I]ptr\f[] is NULL, it must have been returned by an earlier
call to \f[B]pmemcto_malloc\f[](), \f[B]pmemcto_calloc\f[]() or
\f[B]pmemcto_realloc\f[]().
If the area pointed to was moved, a \f[I]pmemcto_free(pcp, ptr)\f[] is
done.
.SH RETURN VALUE
.PP
On success, \f[B]pmemcto_malloc\f[]() and \f[B]pmemcto_calloc\f[]()
functions return a pointer to the allocated memory.
If the allocation request cannot be satisfied, a NULL pointer is
returned and \f[I]errno\f[] is set appropriately.
.PP
The \f[B]pmemcto_free\f[]() function returns no value.
.PP
On success, \f[B]pmemcto_realloc\f[]() function returns a pointer to the
newly allocated memory, or NULL if it is unable to satisfy the
allocation request.
If \f[I]size\f[] was equal to 0, either NULL or a pointer suitable to be
passed to \f[B]pmemcto_free\f[]() is returned.
If \f[B]pmemcto_realloc\f[]() fails the original block is left
untouched; it is not freed or moved.
.SH ERRORS
.PP
\f[B]ENOMEM\f[] Insufficient memory available to satisfy allocation
request.
.SH NOTES
.PP
Unlike the normal \f[B]malloc\f[](), which asks the system for
additional memory when it runs out, \f[B]libpmemcto\f[](7) allocates the
size it is told to and never attempts to grow or shrink that memory
pool.
.SH SEE ALSO
.PP
\f[B]malloc\f[](3), \f[B]jemalloc\f[](3), \f[B]libpmemcto\f[](7) and
\f[B]<http://pmem.io>\f[]