TSmalloc¶
Traffic Server memory allocation API.
Synopsis¶
#include <ts/ts.h>
-
char *TSstrdup(const char *str)¶
-
void TSfree(void *ptr)¶
Description¶
Traffic Server provides a number of routines for allocating and freeing
memory. These routines correspond to similar routines in the C library.
For example, TSrealloc()
behaves like the C library routine realloc
.
There are two reasons to use the routines provided by Traffic Server. The
first is portability. The Traffic Server API routines behave the same on
all of Traffic Servers supported platforms. For example, realloc
does
not accept an argument of nullptr
on some platforms. The second reason is
that the Traffic Server routines actually track the memory allocations by
file and line number. This tracking is very efficient, is always turned
on, and is useful for tracking down memory leaks.
TSmalloc()
returns a pointer to size bytes of memory allocated from the
heap. Traffic Server uses TSmalloc()
internally for memory allocations.
Always use TSfree()
to release memory allocated by TSmalloc()
; do not use
free
.
TSRalloc() returns a pointer, of type T *
,
to allocated memory with enough bytes to hold an array of count
(default value
of :code;`1`) instances of T
. The memory is “raw”, no constructor of T
is called for the array elements. This function in turn calls TSmalloc()
, so the
memory it allocates should be released by calling TSfree()
.
TSstrdup()
returns a pointer to a new string that is a duplicate of the
string pointed to by str. The memory for the new string is allocated using
TSmalloc()
and should be freed by a call to TSfree()
.
TSstrndup()
returns a pointer to a new string that is a duplicate of the
string pointed to by str but is at most size bytes long. The new
string will be NUL-terminated. This API is very useful for transforming non
NUL-terminated string values returned by APIs such as
TSMimeHdrFieldValueStringGet()
into NUL-terminated string values. The
memory for the new string is allocated using TSmalloc()
and should be
freed by a call to TSfree()
.
TSstrlcpy()
copies up to size - 1 characters from the NUL-terminated
string src to dst, NUL-terminating the result.
TSstrlcat()
appends the NUL-terminated string src to the end of dst. It
will append at most size - strlen(dst)
- 1 bytes, NUL-terminating the
result.
TSfree()
releases the memory allocated by TSmalloc()
or TSrealloc()
. If
ptr is nullptr
, TSfree()
does no operation.
Use of these functions should be avoided in new code. For dynamic memory allocation,
prefer in general to use C++ Standard Library containers and smart pointers. Use of C++
new
and delete
operators is the next best option.
See also¶
TSAPI(3ts)