SDDSlib
Loading...
Searching...
No Matches
array.c File Reference

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
 

Detailed Description

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.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday, H. Shang

Definition in file array.c.

Function Documentation

◆ array_1d()

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.

Parameters
sizeThe size of each element in the array.
lower_indexThe lower index of the array.
upper_indexThe upper index of the array.
Returns
Pointer to the allocated 1D array.

Definition at line 253 of file array.c.

253 {
254 char *ptr;
255
256 if (!(ptr = tmalloc((uint64_t)size * (upper_index - lower_index + 1))))
257 bomb("unable to allocate array (array_1d)", NULL);
258 ptr -= lower_index * size;
259 return ((void *)ptr);
260}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26

◆ array_2d()

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.

Parameters
sizeThe size of each element in the array.
lower1The lower index for the first dimension (rows).
upper1The upper index for the first dimension (rows).
lower2The lower index for the second dimension (columns).
upper2The upper index for the second dimension (columns).
Returns
Pointer to the allocated 2D array.

Definition at line 275 of file array.c.

278{
279 register uint64_t i, n1, n2;
280 char **ptr;
281
282 if (!(ptr = tmalloc((uint64_t)sizeof(*ptr) *(n1 = upper1 - lower1 + 1))))
283 bomb("unable to allocate array (array_2d)", NULL);
284
285 n2 = upper2 - lower2 + 1;
286 for (i = 0; i < n1; i++) {
287 if (!(ptr[i] = tmalloc((uint64_t)size * n2)))
288 bomb("unable to allocate array (array_2d)", NULL);
289 ptr[i] -= lower2 * size;
290 }
291
292 return ((void **)(ptr - lower1));
293}

◆ czarray_2d()

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.

Parameters
sizeThe size of each element in the array.
n1The number of rows.
n2The number of columns.
Returns
Pointer to the allocated contiguous 2D array.

Definition at line 358 of file array.c.

358 {
359 char **ptr0;
360 char *buffer;
361 uint64_t i;
362
363 ptr0 = (char **)tmalloc((uint64_t)(sizeof(*ptr0) * n1));
364 buffer = (char *)tmalloc((uint64_t)(sizeof(*buffer) * size * n1 * n2));
365 for (i = 0; i < n1; i++)
366 ptr0[i] = buffer + i * size * n2;
367 return ((void **)ptr0);
368}

◆ free_array_1d()

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.

Parameters
arrayPointer to the 1D array to free.
sizeThe size of each element in the array.
lower_indexThe lower index of the array.
upper_indexThe upper index of the array.
Returns
Status of the free operation (1 if successful, 0 otherwise).

Definition at line 306 of file array.c.

307 {
308 if (!array)
309 return (0);
310 free((char *)array + size * lower_index);
311 return (1);
312}

◆ free_array_2d()

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.

Parameters
arrayPointer to the 2D array to free.
sizeThe size of each element in the array.
lower1The lower index for the first dimension (rows).
upper1The upper index for the first dimension (rows).
lower2The lower index for the second dimension (columns).
upper2The upper index for the second dimension (columns).
Returns
Status of the free operation (1 if successful, 0 otherwise).

Definition at line 327 of file array.c.

330{
331 uint64_t i, n1;
332 char *ptr;
333
334 if (!array)
335 return (0);
336
337 n1 = upper1 - lower1 + 1;
338 array += lower1;
339 for (i = 0; i < n1; i++) {
340 if ((ptr = (char *)array[i] + size * lower2))
341 free(ptr);
342 }
343
344 free(array);
345 return (1);
346}

◆ free_czarray_2d()

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.

Parameters
arrayPointer to the contiguous 2D array to free.
n1The number of rows in the array.
n2The number of columns in the array.
Returns
Status of the free operation (always returns 0).

Definition at line 405 of file array.c.

405 {
406 free(*array);
407 free(array);
408 return 0;
409}

◆ free_zarray_2d()

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.

Parameters
arrayPointer to the 2D array to free.
n1The number of rows in the array.
n2The number of columns in the array.
Returns
Status of the free operation (1 if successful, 0 otherwise).

Definition at line 155 of file array.c.

155 {
156 void *ptr0;
157
158 if (!(ptr0 = array))
159 return (0);
160 while (n1--) {
161 if (*array) {
162 tfree(*array);
163 *array = NULL;
164 } else
165 return (0);
166 array++;
167 }
168 return (tfree(ptr0));
169}
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
Definition array.c:230

◆ keep_alloc_record()

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.

Parameters
filenameThe base name for the tracking files.

Definition at line 33 of file array.c.

33 {
34 char s[100];
35
36 if (fp_tmalloc)
37 free(fp_tmalloc);
38 if (fp_trealloc)
39 free(fp_trealloc);
40 if (fp_tfree)
41 free(fp_tfree);
42 sprintf(s, "%s.tmalloc", filename);
43 fp_tmalloc = fopen_e(s, "w", 0);
44 sprintf(s, "%s.trealloc", filename);
45 fp_trealloc = fopen_e(s, "w", 0);
46 sprintf(s, "%s.tfree", filename);
47 fp_tfree = fopen_e(s, "w", 0);
48}
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
Definition fopen_e.c:30

◆ resize_czarray_2d()

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.

Parameters
dataPointer to the original contiguous 2D array.
sizeThe size of each element in the array.
n1The new number of rows.
n2The new number of columns.
Returns
Pointer to the resized contiguous 2D array.

Definition at line 381 of file array.c.

381 {
382 char **ptr0;
383 char *buffer;
384 uint64_t i;
385
386 if (!data)
387 return czarray_2d(size, n1, n2);
388 buffer = (char *)trealloc(*data, (uint64_t)(sizeof(char) * size * n1 * n2));
389 ptr0 = (char **)trealloc(data, (uint64_t)(sizeof(char *) * n1));
390 for (i = 0; i < n1; i++)
391 ptr0[i] = buffer + i * size * n2;
392 return ((void **)ptr0);
393}
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
void ** czarray_2d(const uint64_t size, const uint64_t n1, const uint64_t n2)
Allocates a contiguous 2D array with zero-based indexing.
Definition array.c:358

◆ resize_zarray_2d()

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.

Parameters
sizeThe size of each element in the array.
old_n1The original number of rows.
old_n2The original number of columns.
arrayPointer to the original 2D array.
n1The new number of rows.
n2The new number of columns.
Returns
Pointer to the resized 2D array.

Definition at line 117 of file array.c.

118 {
119 void **ptr;
120
121 if (n1 > old_n1) {
122 /* increase length of array of pointers */
123 if (!(array = (void **)trealloc((void *)array,
124 (uint64_t)(sizeof(*array) * n1))))
125 bomb("memory allocation failuire in resize_zarray_2d()", NULL);
126 /* allocate memory for new pointed-to objects */
127 ptr = array + n1;
128 while (n1-- != old_n1)
129 *--ptr = (void *)tmalloc(size * n2);
130 }
131
132 if (n2 > old_n2) {
133 /* increase size of old pointed-to objects */
134 ptr = array;
135 while (old_n1--) {
136 if (!(*ptr = (void *)trealloc((void *)*ptr, (uint64_t)(size * n2))))
137 bomb("memory allocation failure in resize_zarray_2d()", NULL);
138 ptr++;
139 }
140 }
141
142 return (array);
143}

◆ tfree()

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.

Parameters
ptrPointer to the memory block to free.
Returns
Status of the free operation (1 if successful, 0 otherwise).

Definition at line 230 of file array.c.

230 {
231 if (fp_tfree) {
232 fprintf(fp_tfree, "%lx\n", (uint64_t)ptr);
233 fflush(fp_tfree);
234 }
235 if (ptr) {
236 free(ptr);
237 return (1);
238 }
239 return (0);
240}

◆ tmalloc()

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.

Parameters
size_of_blockThe size of the memory block to allocate in bytes.
Returns
Pointer to the allocated memory block.

Definition at line 59 of file array.c.

59 {
60 void *ptr;
61 static uint64_t total_bytes = 0;
62
63 if (size_of_block <= 0)
64 size_of_block = 4;
65
66 /* even though the function is tMalloc, I use calloc to get memory filled
67 * with zeros
68 */
69 if (!(ptr = calloc(size_of_block, 1))) {
70 printf("error: memory allocation failure--%lu bytes requested.\n",
71 size_of_block);
72 printf("tmalloc() has allocated %lu bytes previously\n", total_bytes);
73 abort();
74 }
75 if (fp_tmalloc) {
76 fprintf(fp_tmalloc, "%lx %lu\n", (uint64_t)ptr, size_of_block);
77 fflush(fp_tmalloc);
78 }
79 total_bytes += size_of_block;
80 return (ptr);
81}

◆ trealloc()

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.

Parameters
old_ptrPointer to the original memory block.
size_of_blockThe new size for the memory block in bytes.
Returns
Pointer to the reallocated memory block.

Definition at line 181 of file array.c.

181 {
182 void *ptr;
183 static uint64_t total_bytes = 0;
184
185 if (size_of_block <= 0)
186 size_of_block = 4;
187
188 if (!old_ptr)
189 return (tmalloc(size_of_block));
190 if (!(ptr = realloc((void *)old_ptr, (uint64_t)(size_of_block)))) {
191 printf("error: memory reallocation failure--%lu bytes requested.\n",
192 size_of_block);
193 printf("trealloc() has reallocated %lu bytes previously\n", total_bytes);
194 abort();
195 }
196 if (fp_trealloc) {
197 fprintf(fp_trealloc, "d:%lx\na:%lx %lu\n", (uint64_t)old_ptr,
198 (uint64_t)ptr, size_of_block);
199 fflush(fp_trealloc);
200 }
201 total_bytes += size_of_block;
202 return (ptr);
203}

◆ zarray_2d()

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.

Parameters
sizeThe size of each element in the array.
n1The number of rows.
n2The number of columns.
Returns
Pointer to the allocated 2D array.

Definition at line 93 of file array.c.

93 {
94 void **ptr1, **ptr0;
95
96 ptr0 = ptr1 = (void **)tmalloc((uint64_t)(sizeof(*ptr0) * n1));
97 while (n1--)
98 *ptr1++ = (void *)tmalloc((uint64_t)(size * n2));
99 return (ptr0);
100}

◆ zero_memory()

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.

Parameters
memPointer to the memory block.
n_bytesThe number of bytes to set to zero.

Definition at line 213 of file array.c.

213 {
214 char *cmem;
215
216 if (!(cmem = mem))
217 return;
218 while (n_bytes--)
219 *cmem++ = 0;
220}

Variable Documentation

◆ fp_tfree

FILE* fp_tfree = NULL
static

Definition at line 23 of file array.c.

◆ fp_tmalloc

FILE* fp_tmalloc = NULL
static

Definition at line 21 of file array.c.

◆ fp_trealloc

FILE* fp_trealloc = NULL
static

Definition at line 22 of file array.c.