SDDSlib
|
Implementation of dynamic 2D arrays and memory management functions. More...
#include "mdb.h"
Go to the source code of this file.
Functions | |
void | keep_alloc_record (char *filename) |
Keeps a record of memory allocations by opening tracking files. | |
void * | tmalloc (uint64_t size_of_block) |
Allocates a memory block of the specified size with zero initialization. | |
void ** | zarray_2d (uint64_t size, uint64_t n1, uint64_t n2) |
Allocates a 2D array with specified dimensions. | |
void ** | resize_zarray_2d (uint64_t size, uint64_t old_n1, uint64_t old_n2, void **array, uint64_t n1, uint64_t n2) |
Resizes an existing 2D array to new dimensions. | |
int | free_zarray_2d (void **array, uint64_t n1, uint64_t n2) |
Frees a 2D array and its associated memory. | |
void * | trealloc (void *old_ptr, uint64_t size_of_block) |
Reallocates a memory block to a new size. | |
void | zero_memory (void *mem, uint64_t n_bytes) |
Sets a block of memory to zero. | |
int | tfree (void *ptr) |
Frees a memory block and records the deallocation if tracking is enabled. | |
void * | array_1d (uint64_t size, uint64_t lower_index, uint64_t upper_index) |
Allocates a 1D array with specified lower and upper indices. | |
void ** | array_2d (uint64_t size, uint64_t lower1, uint64_t upper1, uint64_t lower2, uint64_t upper2) |
Allocates a 2D array with specified lower and upper indices for both dimensions. | |
int | free_array_1d (void *array, uint64_t size, uint64_t lower_index, uint64_t upper_index) |
Frees a 1D array that was previously allocated. | |
int | free_array_2d (void **array, uint64_t size, uint64_t lower1, uint64_t upper1, uint64_t lower2, uint64_t upper2) |
Frees a 2D array and its associated memory. | |
void ** | czarray_2d (const uint64_t size, const uint64_t n1, const uint64_t n2) |
Allocates a contiguous 2D array with zero-based indexing. | |
void ** | resize_czarray_2d (void **data, uint64_t size, uint64_t n1, uint64_t n2) |
Resizes a contiguous 2D array to new dimensions. | |
int | free_czarray_2d (void **array, uint64_t n1, uint64_t n2) |
Frees a contiguous 2D array and its associated memory. | |
Variables | |
static FILE * | fp_tmalloc = NULL |
static FILE * | fp_trealloc = NULL |
static FILE * | fp_tfree = NULL |
Implementation of dynamic 2D arrays and memory management functions.
This file contains functions for allocating, resizing, and freeing 2D arrays, as well as custom memory allocation functions with tracking capabilities.
Definition in file array.c.
void * array_1d | ( | uint64_t | size, |
uint64_t | lower_index, | ||
uint64_t | upper_index ) |
Allocates a 1D array with specified lower and upper indices.
Allocates memory for a 1D array and adjusts the pointer based on the lower index to allow negative indexing if necessary.
size | The size of each element in the array. |
lower_index | The lower index of the array. |
upper_index | The upper index of the array. |
Definition at line 253 of file array.c.
void ** array_2d | ( | uint64_t | size, |
uint64_t | lower1, | ||
uint64_t | upper1, | ||
uint64_t | lower2, | ||
uint64_t | upper2 ) |
Allocates a 2D array with specified lower and upper indices for both dimensions.
Allocates memory for a 2D array of pointers, where each row is a 1D array. Adjusts pointers based on lower indices to allow for flexible indexing ranges.
size | The size of each element in the array. |
lower1 | The lower index for the first dimension (rows). |
upper1 | The upper index for the first dimension (rows). |
lower2 | The lower index for the second dimension (columns). |
upper2 | The upper index for the second dimension (columns). |
Definition at line 275 of file array.c.
void ** czarray_2d | ( | const uint64_t | size, |
const uint64_t | n1, | ||
const uint64_t | n2 ) |
Allocates a contiguous 2D array with zero-based indexing.
Allocates a single contiguous block of memory for a 2D array and sets up row pointers accordingly.
size | The size of each element in the array. |
n1 | The number of rows. |
n2 | The number of columns. |
int free_array_1d | ( | void * | array, |
uint64_t | size, | ||
uint64_t | lower_index, | ||
uint64_t | upper_index ) |
Frees a 1D array that was previously allocated.
Adjusts the pointer based on the lower index and frees the allocated memory.
array | Pointer to the 1D array to free. |
size | The size of each element in the array. |
lower_index | The lower index of the array. |
upper_index | The upper index of the array. |
Definition at line 306 of file array.c.
int free_array_2d | ( | void ** | array, |
uint64_t | size, | ||
uint64_t | lower1, | ||
uint64_t | upper1, | ||
uint64_t | lower2, | ||
uint64_t | upper2 ) |
Frees a 2D array and its associated memory.
Adjusts the pointer based on the lower indices and frees each row followed by the array of pointers.
array | Pointer to the 2D array to free. |
size | The size of each element in the array. |
lower1 | The lower index for the first dimension (rows). |
upper1 | The upper index for the first dimension (rows). |
lower2 | The lower index for the second dimension (columns). |
upper2 | The upper index for the second dimension (columns). |
Definition at line 327 of file array.c.
int free_czarray_2d | ( | void ** | array, |
uint64_t | n1, | ||
uint64_t | n2 ) |
Frees a contiguous 2D array and its associated memory.
Frees the contiguous memory block holding the array elements and the array of row pointers.
array | Pointer to the contiguous 2D array to free. |
n1 | The number of rows in the array. |
n2 | The number of columns in the array. |
Definition at line 405 of file array.c.
int free_zarray_2d | ( | void ** | array, |
uint64_t | n1, | ||
uint64_t | n2 ) |
Frees a 2D array and its associated memory.
Frees each row of the 2D array and then frees the array of pointers itself.
array | Pointer to the 2D array to free. |
n1 | The number of rows in the array. |
n2 | The number of columns in the array. |
Definition at line 155 of file array.c.
void keep_alloc_record | ( | char * | filename | ) |
Keeps a record of memory allocations by opening tracking files.
Opens tracking files for memory allocation, reallocation, and freeing based on the provided filename. If tracking files are already open, they are closed before reopening.
filename | The base name for the tracking files. |
Definition at line 33 of file array.c.
void ** resize_czarray_2d | ( | void ** | data, |
uint64_t | size, | ||
uint64_t | n1, | ||
uint64_t | n2 ) |
Resizes a contiguous 2D array to new dimensions.
Resizes both the array of row pointers and the contiguous memory block holding the array elements.
data | Pointer to the original contiguous 2D array. |
size | The size of each element in the array. |
n1 | The new number of rows. |
n2 | The new number of columns. |
Definition at line 381 of file array.c.
void ** resize_zarray_2d | ( | uint64_t | size, |
uint64_t | old_n1, | ||
uint64_t | old_n2, | ||
void ** | array, | ||
uint64_t | n1, | ||
uint64_t | n2 ) |
Resizes an existing 2D array to new dimensions.
Resizes the array of pointers if the number of rows (n1
) increases. Additionally, resizes each row to accommodate more columns (n2
) if needed. If resizing fails, the function aborts the program.
size | The size of each element in the array. |
old_n1 | The original number of rows. |
old_n2 | The original number of columns. |
array | Pointer to the original 2D array. |
n1 | The new number of rows. |
n2 | The new number of columns. |
Definition at line 117 of file array.c.
int tfree | ( | void * | ptr | ) |
Frees a memory block and records the deallocation if tracking is enabled.
Frees the specified memory block and logs the deallocation if tracking is active.
ptr | Pointer to the memory block to free. |
Definition at line 230 of file array.c.
void * tmalloc | ( | uint64_t | size_of_block | ) |
Allocates a memory block of the specified size with zero initialization.
Uses calloc
to allocate memory and initializes it to zero. Tracks the allocation if tracking is enabled. If the allocation fails, the function prints an error message and aborts the program.
size_of_block | The size of the memory block to allocate in bytes. |
Definition at line 59 of file array.c.
void * trealloc | ( | void * | old_ptr, |
uint64_t | size_of_block ) |
Reallocates a memory block to a new size.
Uses realloc
to resize the memory block. Tracks the reallocation if tracking is enabled. If the reallocation fails, the function prints an error message and aborts the program.
old_ptr | Pointer to the original memory block. |
size_of_block | The new size for the memory block in bytes. |
Definition at line 181 of file array.c.
void ** zarray_2d | ( | uint64_t | size, |
uint64_t | n1, | ||
uint64_t | n2 ) |
Allocates a 2D array with specified dimensions.
Allocates memory for a 2D array where each row is a contiguous block of memory. Initializes each row to zero.
size | The size of each element in the array. |
n1 | The number of rows. |
n2 | The number of columns. |
void zero_memory | ( | void * | mem, |
uint64_t | n_bytes ) |
Sets a block of memory to zero.
Iterates through the specified memory block and sets each byte to zero.
mem | Pointer to the memory block. |
n_bytes | The number of bytes to set to zero. |
Definition at line 213 of file array.c.