Headers for util_malloc.c.
More...
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "lib/cc/compat_compiler.h"
Go to the source code of this file.
|
#define | tor_free(p) |
|
#define | tor_malloc(size) tor_malloc_(size) |
|
#define | tor_malloc_zero(size) tor_malloc_zero_(size) |
|
#define | tor_calloc(nmemb, size) tor_calloc_(nmemb, size) |
|
#define | tor_realloc(ptr, size) tor_realloc_(ptr, size) |
|
#define | tor_reallocarray(ptr, sz1, sz2) tor_reallocarray_((ptr), (sz1), (sz2)) |
|
#define | tor_strdup(s) tor_strdup_(s) |
|
#define | tor_strndup(s, n) tor_strndup_(s, n) |
|
#define | tor_memdup(s, n) tor_memdup_(s, n) |
|
#define | tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n) |
|
#define | raw_malloc malloc |
|
#define | raw_realloc realloc |
|
#define | raw_free free |
|
#define | raw_strdup strdup |
|
#define | FREE_AND_NULL(typename, freefn, var) |
|
Headers for util_malloc.c.
Definition in file malloc.h.
◆ FREE_AND_NULL
#define FREE_AND_NULL |
( |
|
typename, |
|
|
|
freefn, |
|
|
|
var |
|
) |
| |
Value: do { \
\
typename **tmp__free__ptr ## freefn = &(var); \
freefn(*tmp__free__ptr ## freefn); \
(*tmp__free__ptr ## freefn) = NULL; \
} while (0)
Definition at line 84 of file malloc.h.
◆ raw_free
◆ raw_malloc
#define raw_malloc malloc |
◆ raw_realloc
#define raw_realloc realloc |
◆ raw_strdup
#define raw_strdup strdup |
◆ tor_calloc
#define tor_calloc |
( |
|
nmemb, |
|
|
|
size |
|
) |
| tor_calloc_(nmemb, size) |
◆ tor_free
Value: STMT_BEGIN \
raw_free(p); \
(p)=NULL; \
STMT_END
Release memory allocated by tor_malloc, tor_realloc, tor_strdup, etc. Unlike the free() function, the tor_free() macro sets the pointer value to NULL after freeing it.
This is a macro. If you need a function pointer to release memory from tor_malloc(), use tor_free_().
Note that this macro takes the address of the pointer it is going to free and clear. If that pointer is stored with a nonstandard alignment (eg because of a "packed" pragma) it is not correct to use tor_free().
Definition at line 56 of file malloc.h.
◆ tor_malloc
◆ tor_malloc_zero
◆ tor_memdup
◆ tor_memdup_nulterm
◆ tor_realloc
◆ tor_reallocarray
◆ tor_strdup
◆ tor_strndup
◆ tor_calloc_()
void * tor_calloc_ |
( |
size_t |
nmemb, |
|
|
size_t |
size |
|
) |
| |
Allocate a chunk of nmemb*size bytes of memory, fill the memory with zero bytes, and return a pointer to the result. Log and terminate the process on error. (Same as calloc(nmemb,size), but never returns NULL.) The second argument (size) should preferably be non-zero and a compile-time constant.
Definition at line 107 of file malloc.c.
◆ tor_free_()
void tor_free_ |
( |
void * |
mem | ) |
|
◆ tor_malloc_()
void * tor_malloc_ |
( |
size_t |
size | ) |
|
Allocate a chunk of size bytes of memory, and return a pointer to result. On error, log and terminate the process. (Same as malloc(size), but never returns NULL.)
Definition at line 32 of file malloc.c.
Referenced by tor_malloc_zero_(), and tor_run_main().
◆ tor_malloc_zero_()
void * tor_malloc_zero_ |
( |
size_t |
size | ) |
|
Allocate a chunk of size bytes of memory, fill the memory with zero bytes, and return a pointer to the result. Log and terminate the process on error. (Same as calloc(size,1), but never returns NULL.)
Definition at line 63 of file malloc.c.
◆ tor_memdup_()
void * tor_memdup_ |
( |
const void * |
mem, |
|
|
size_t |
len |
|
) |
| |
Allocate a chunk of len bytes, with the same contents as the len bytes starting at mem.
Definition at line 200 of file malloc.c.
◆ tor_memdup_nulterm_()
void * tor_memdup_nulterm_ |
( |
const void * |
mem, |
|
|
size_t |
len |
|
) |
| |
As tor_memdup(), but add an extra 0 byte at the end of the resulting memory.
Definition at line 213 of file malloc.c.
◆ tor_realloc_()
void * tor_realloc_ |
( |
void * |
ptr, |
|
|
size_t |
size |
|
) |
| |
Change the size of the memory block pointed to by ptr to size bytes long; return the new memory block. On error, log and terminate. (Like realloc(ptr,size), but never returns NULL.)
Definition at line 118 of file malloc.c.
Referenced by tor_run_main().
◆ tor_reallocarray_()
void * tor_reallocarray_ |
( |
void * |
ptr, |
|
|
size_t |
sz1, |
|
|
size_t |
sz2 |
|
) |
| |
Try to realloc ptr so that it takes up sz1 * sz2 bytes. Check for overflow. Unlike other allocation functions, return NULL on overflow.
Definition at line 146 of file malloc.c.
◆ tor_strdup_()
char * tor_strdup_ |
( |
const char * |
s | ) |
|
Return a newly allocated copy of the NUL-terminated string s. On error, log and terminate. (Like strdup(s), but never returns NULL.)
Definition at line 160 of file malloc.c.
◆ tor_strndup_()
char * tor_strndup_ |
( |
const char * |
s, |
|
|
size_t |
n |
|
) |
| |
Allocate and return a new string containing the first n characters of s. If s is longer than n characters, only the first n are copied. The result is always NUL-terminated. (Like strndup(s,n), but never returns NULL.)
Definition at line 182 of file malloc.c.