9.0
general documentation
Loading...
Searching...
No Matches
cs_mem.cpp File Reference
#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"
Include dependency graph for cs_mem.cpp:

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_tcs_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)

Detailed Description

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.

Function Documentation

◆ cs_mem_end()

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.

◆ cs_mem_error_handler_get()

bft_error_handler_t * cs_mem_error_handler_get ( void )

Returns the error handler associated with the cs_mem_...() functions.

Returns
pointer to the error handler function.

◆ cs_mem_error_handler_set()

void cs_mem_error_handler_set ( bft_error_handler_t * handler)

◆ cs_mem_free()

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.

Parameters
[in]ptrpointer to previous memory location (if nullptr, cs_alloc() called).
[in]var_nameallocated variable name string
[in]file_namename of calling source file
[in]line_numline number in calling source file
Returns
null pointer.

◆ cs_mem_have_memalign()

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.

Returns
1 if memory aligned allocation is possible, 0 otherwise.

◆ cs_mem_init()

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.

Parameters
log_file_namename of optional log_file (if nullptr, no log).

◆ cs_mem_initialized()

int cs_mem_initialized ( void )

Indicates if cs_mem_...() functions are initialized.

Returns
1 if cs_mem_init has been called, 0 otherwise.

◆ cs_mem_malloc()

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.

Parameters
[in]ninumber of elements.
[in]sizeelement size.
[in]var_nameallocated variable name string.
[in]file_namename of calling source file.
[in]line_numline number in calling source file.
Returns
pointer to allocated memory.

◆ cs_mem_memalign()

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.

Parameters
[in]alignmentalignment.
[in]ninumber of elements.
[in]sizeelement size.
[in]var_nameallocated variable name string.
[in]file_namename of calling source file.
[in]line_numline number in calling source file.
Returns
pointer to allocated memory.

◆ cs_mem_realloc()

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.

Parameters
[in]ptrpointer to previous memory location (if nullptr, cs_alloc() called).
[in]ninumber of elements.
[in]sizeelement size.
[in]var_nameallocated variable name string.
[in]file_namename of calling source file.
[in]line_numline number in calling source file.
Returns
pointer to reallocated memory.

◆ cs_mem_size_current()

size_t cs_mem_size_current ( void )

Return current theoretical dynamic memory allocated.

Returns
current memory handled through cs_mem_...() (in kB).

◆ cs_mem_size_max()

size_t cs_mem_size_max ( void )

Return maximum theoretical dynamic memory allocated.

Returns
maximum memory handled through cs_mem_...() (in kB).

◆ cs_mem_stats()

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.

Parameters
[out]alloc_curcurrent allocation size, or nullptr
[out]alloc_maxmax allocation size, or nullptr
[out]n_allocstotal number of allocations, or nullptr
[out]n_reallocstotal number of reallocations, or nullptr
[out]n_freestotal number of frees, or nullptr
[out]n_currenttotal number of current allocations, or nullptr
Returns
1 if stats are available, O otherwise.