21static FILE *fp_tmalloc = NULL;
22static FILE *fp_trealloc = NULL;
23static FILE *fp_tfree = NULL;
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);
61 static uint64_t total_bytes = 0;
63 if (size_of_block <= 0)
69 if (!(ptr = calloc(size_of_block, 1))) {
70 printf(
"error: memory allocation failure--%lu bytes requested.\n",
72 printf(
"tmalloc() has allocated %lu bytes previously\n", total_bytes);
76 fprintf(fp_tmalloc,
"%lx %lu\n", (uint64_t)ptr, size_of_block);
79 total_bytes += size_of_block;
93void **
zarray_2d(uint64_t size, uint64_t n1, uint64_t n2) {
96 ptr0 = ptr1 = (
void **)
tmalloc((uint64_t)(
sizeof(*ptr0) * n1));
98 *ptr1++ = (
void *)
tmalloc((uint64_t)(size * n2));
118 void **array, uint64_t n1, uint64_t n2) {
123 if (!(array = (
void **)
trealloc((
void *)array,
124 (uint64_t)(
sizeof(*array) * n1))))
125 bomb(
"memory allocation failuire in resize_zarray_2d()", NULL);
128 while (n1-- != old_n1)
129 *--ptr = (
void *)
tmalloc(size * n2);
136 if (!(*ptr = (
void *)
trealloc((
void *)*ptr, (uint64_t)(size * n2))))
137 bomb(
"memory allocation failure in resize_zarray_2d()", NULL);
168 return (
tfree(ptr0));
181void *
trealloc(
void *old_ptr, uint64_t size_of_block) {
183 static uint64_t total_bytes = 0;
185 if (size_of_block <= 0)
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",
193 printf(
"trealloc() has reallocated %lu bytes previously\n", total_bytes);
197 fprintf(fp_trealloc,
"d:%lx\na:%lx %lu\n", (uint64_t)old_ptr,
198 (uint64_t)ptr, size_of_block);
201 total_bytes += size_of_block;
232 fprintf(fp_tfree,
"%lx\n", (uint64_t)ptr);
253void *
array_1d(uint64_t size, uint64_t lower_index, uint64_t upper_index) {
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);
275void **
array_2d(uint64_t size, uint64_t lower1, uint64_t upper1,
276 uint64_t lower2, uint64_t upper2)
279 register uint64_t i, n1, n2;
282 if (!(ptr =
tmalloc((uint64_t)
sizeof(*ptr) *(n1 = upper1 - lower1 + 1))))
283 bomb(
"unable to allocate array (array_2d)", NULL);
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;
292 return ((
void **)(ptr - lower1));
307 uint64_t upper_index) {
310 free((
char *)array + size * lower_index);
327int free_array_2d(
void **array, uint64_t size, uint64_t lower1, uint64_t upper1,
328 uint64_t lower2, uint64_t upper2)
337 n1 = upper1 - lower1 + 1;
339 for (i = 0; i < n1; i++) {
340 if ((ptr = (
char *)array[i] + size * lower2))
358void **
czarray_2d(
const uint64_t size,
const uint64_t n1,
const uint64_t n2) {
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);
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);
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.
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.
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 * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
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.
void zero_memory(void *mem, uint64_t n_bytes)
Sets a block of memory to zero.
int free_zarray_2d(void **array, uint64_t n1, uint64_t n2)
Frees a 2D array and its associated memory.
void ** resize_czarray_2d(void **data, uint64_t size, uint64_t n1, uint64_t n2)
Resizes a contiguous 2D array to new dimensions.
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
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 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.
int free_czarray_2d(void **array, uint64_t n1, uint64_t n2)
Frees a contiguous 2D array and its associated memory.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.