Mescal
|
Macros and functions to help memory allocation. More...
Go to the source code of this file.
Macros | |
#define | MALLOC(p, num_objects) |
Macro for allocating memory using the malloc() function. | |
#define | CALLOC(p, num_objects) |
Macro for allocating memory using the calloc() function. | |
#define | REALLOC(p, num_objects) |
Macro for reallocating memory using the realloc() function. | |
#define | CHECK_NULL(n, ...) |
Macro to test if one or more pointers are NULL. | |
Functions | |
void | check_null (const char *function, char *file, int line, int n,...) |
Do not use this function directly; instead, use the macro CHECK_NULL(), which includes the error's location in the source code in the message. | |
void | multiple_free (void *p,...) |
Function to free memory using the free() function. | |
Macros and functions to help memory allocation.
The macros MALLOC(), CALLOC() and REALLOC() from this file make it possible to allocate dynamic memory. If they fail, they print an error message on the standard error, indicating the function, file and line where the allocation failed, and they then quit the program.
The function multiple_free() is used to free several pointers at once.
NULL
, and all must have been dynamically allocated before the call.The macro CHECK_NULL() checks whether one or several pointers are non-NULL
.
The function check_null() shouldn't be used directly, it is used by the macro CHECK_NULL().
#define CALLOC | ( | p, | |
num_objects ) |
Macro for allocating memory using the calloc() function.
p | Pointer to be allocated. |
num_objects | Number of objects to allocate. |
Allocates memory, initialized at 0, to store num_objects
, * each of type *p
.
Example of use:
If allocation fails, displays an error message and quits the program.
p
must have been declared, but must not point to an already dynamically allocated area (otherwise a memory leak will occur). If p
already points to a dynamically allocated area, use REALLOC.#define CHECK_NULL | ( | n, | |
... ) |
Macro to test if one or more pointers are NULL.
n | Number of pointers to test. |
... | Pointers to test, alternating with strings. |
There must be one pointer to test for each string, and vice versa.
The string following each pointer allows customizing the error message.
Example of use:
Here, 3 is the number of pointers to check, and p1
, p2
, and p3
are the pointers. If p1
and p3
are NULL, the macro will display an error message in the following form:
❌ [CRITICAL] Name1 is NULL!
❌ [CRITICAL] Name3 is NULL!
#define MALLOC | ( | p, | |
num_objects ) |
Macro for allocating memory using the malloc() function.
p | Pointer to be allocated. |
num_objects | Number of objects to allocate. |
Allocates memory to store num_objects
, each of type *p
.
Example of use:
If allocation fails, displays an error message and quits the program.
p
pointer must have been declared, but must not point to a zone that has already been dynamically allocated (otherwise a memory leak will occur). If p
already points to a dynamically allocated zone, use REALLOC.#define REALLOC | ( | p, | |
num_objects ) |
Macro for reallocating memory using the realloc() function.
p | Pointer to be allocated. |
num_objects | New number of objects to allocate. |
Reallocates memory pointed to by a pointer p
, whose value must have been previously returned by a dynamic allocation function (malloc
, calloc
, realloc
or one of the macros in this file).
If allocation fails, displays an error message and quits the program.
Example of use:
If allocation fails, displays an error message and quits the program.
p
pointer must have been declared and point to an already dynamically allocated zone. void check_null | ( | const char * | function, |
char * | file, | ||
int | line, | ||
int | n, | ||
... ) |
Do not use this function directly; instead, use the macro CHECK_NULL(), which includes the error's location in the source code in the message.
void multiple_free | ( | void * | p, |
... ) |
Function to free memory using the free() function.
Calls free on a variable number of arguments. The last argument must be NULL
.
Example of use:
p2
is NULL, p3
will not be freed, which can cause a memory leak.