#include "base/cs_defs.h"
#include <map>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bft/bft_error.h"
#include "bft/bft_mem_usage.h"
#include "bft/bft_printf.h"
#include "base/cs_mem.h"
Functions | |
void | cs_mem_init (const char *log_file_name) |
Initialize memory handling. | |
void | cs_mem_end (void) |
End memory handling. | |
int | cs_mem_initialized (void) |
Indicates if cs_mem_...() functions are initialized. | |
void * | cs_mem_malloc (size_t ni, size_t size, const char *var_name, const char *file_name, int line_num) |
Allocate memory for ni elements of size bytes. | |
void * | cs_mem_realloc (void *ptr, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num) |
Reallocate memory for ni elements of size bytes. | |
void * | cs_mem_free (void *ptr, const char *var_name, const char *file_name, int line_num) |
Free allocated memory. | |
void * | cs_mem_memalign (size_t alignment, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num) |
Allocate aligned memory for ni elements of size bytes. | |
size_t | cs_mem_size_current (void) |
Return current theoretical dynamic memory allocated. | |
size_t | cs_mem_size_max (void) |
Return maximum theoretical dynamic memory allocated. | |
int | cs_mem_stats (uint64_t *alloc_cur, uint64_t *alloc_max, uint64_t *n_allocs, uint64_t *n_reallocs, uint64_t *n_frees, uint64_t *n_current) |
Return current theoretical dynamic memory allocated. | |
int | cs_mem_have_memalign (void) |
Indicate if a memory aligned allocation variant is available. | |
bft_error_handler_t * | cs_mem_error_handler_get (void) |
Returns the error handler associated with the cs_mem_...() functions. | |
void | cs_mem_error_handler_set (bft_error_handler_t *handler) |
Base memory allocation wrappers with optional tracing.
The memory managment function provided here provide optional logging, and tracking of non-freed pointers.
Since in most of the intended applications, failure to allocate memory is considered fatal, failed allocations from these functions are considedered as errors, which are fatal by default but can be handled differently if an appropriate error handler is defined. So additional checking of the return values in the calling code is not needed.
The functions provided here are otherwise based on the matching C library functions.
void cs_mem_end | ( | void | ) |
End memory handling.
This function should be called after all other cs_mem_...() functions. In case of memory allocation logging, it writes final information to the log file and closes is.
bft_error_handler_t * cs_mem_error_handler_get | ( | void | ) |
Returns the error handler associated with the cs_mem_...() functions.
void cs_mem_error_handler_set | ( | bft_error_handler_t * | handler | ) |
void * cs_mem_free | ( | void * | ptr, |
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num ) |
Free allocated memory.
This function calls free(), but adds tracing capabilities, and automatically calls the bft_error() errorhandler if it fails to free the corresponding memory. In case of a null pointer argument, the function simply returns.
[in] | ptr | pointer to previous memory location (if nullptr, cs_alloc() called). |
[in] | var_name | allocated variable name string |
[in] | file_name | name of calling source file |
[in] | line_num | line number in calling source file |
int cs_mem_have_memalign | ( | void | ) |
Indicate if a memory aligned allocation variant is available.
If no such function is available, cs_mem_memalign() will always fail.
void cs_mem_init | ( | const char * | log_file_name | ) |
Initialize memory handling.
This function should be called before any other cs_mem_...() function. To activate memory allocation logging, a logfile name should be given as an argument. The resulting file will be a regular, local file. If this file cannot be opened for some reason, logging is silently de-activated.
If the log file name argument is non-null but is an empty string, memory management be tracked, but not logged in detail, so only statistics will be available.
log_file_name | name of optional log_file (if nullptr, no log). |
int cs_mem_initialized | ( | void | ) |
Indicates if cs_mem_...() functions are initialized.
void * cs_mem_malloc | ( | size_t | ni, |
size_t | size, | ||
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num ) |
Allocate memory for ni elements of size bytes.
This function calls malloc(), but adds tracing capabilities, and automatically calls the bft_error() errorhandler if it fails to allocate the required memory.
Allocation couting and logging to trace file will be done if both required by the cs_mem_init options and if file_name != nullptr. If required but file_name == nullptr, it must be handled by the caller, using cs_mem_log_mem_op.
[in] | ni | number of elements. |
[in] | size | element size. |
[in] | var_name | allocated variable name string. |
[in] | file_name | name of calling source file. |
[in] | line_num | line number in calling source file. |
void * cs_mem_memalign | ( | size_t | alignment, |
size_t | ni, | ||
size_t | size, | ||
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num ) |
Allocate aligned memory for ni elements of size bytes.
This function calls posix_memalign() if available, but adds tracing capabilities, and automatically calls the bft_error() errorhandler if it fails to allocate the required memory.
The associated function cs_mem_have_memalign() indicates if this type of allocation may be used on this system.
[in] | alignment | alignment. |
[in] | ni | number of elements. |
[in] | size | element size. |
[in] | var_name | allocated variable name string. |
[in] | file_name | name of calling source file. |
[in] | line_num | line number in calling source file. |
void * cs_mem_realloc | ( | void * | ptr, |
size_t | ni, | ||
size_t | size, | ||
const char * | var_name, | ||
const char * | file_name, | ||
int | line_num ) |
Reallocate memory for ni elements of size bytes.
This function calls realloc(), but adds tracing capabilities, and automatically calls the bft_error() errorhandler if it fails to allocate the required memory.
[in] | ptr | pointer to previous memory location (if nullptr, cs_alloc() called). |
[in] | ni | number of elements. |
[in] | size | element size. |
[in] | var_name | allocated variable name string. |
[in] | file_name | name of calling source file. |
[in] | line_num | line number in calling source file. |
size_t cs_mem_size_current | ( | void | ) |
Return current theoretical dynamic memory allocated.
size_t cs_mem_size_max | ( | void | ) |
Return maximum theoretical dynamic memory allocated.
int cs_mem_stats | ( | uint64_t * | alloc_cur, |
uint64_t * | alloc_max, | ||
uint64_t * | n_allocs, | ||
uint64_t * | n_reallocs, | ||
uint64_t * | n_frees, | ||
uint64_t * | n_current ) |
Return current theoretical dynamic memory allocated.
Return memory allocation stats, if available.
Availability of statistics depends on the cs_mem_init options.
[out] | alloc_cur | current allocation size, or nullptr |
[out] | alloc_max | max allocation size, or nullptr |
[out] | n_allocs | total number of allocations, or nullptr |
[out] | n_reallocs | total number of reallocations, or nullptr |
[out] | n_frees | total number of frees, or nullptr |
[out] | n_current | total number of current allocations, or nullptr |