20#include "mdb_thread.h"
22static FILE *fp_tmalloc = NULL;
23static FILE *fp_trealloc = NULL;
24static FILE *fp_tfree = NULL;
25static uint64_t tmalloc_total_bytes = 0;
26static uint64_t trealloc_total_bytes = 0;
27static MDB_THREAD_LOCK alloc_record_lock = MDB_THREAD_LOCK_INITIALIZER;
40 mdb_thread_lock(&alloc_record_lock);
47 sprintf(s,
"%s.tmalloc", filename);
48 fp_tmalloc =
fopen_e(s,
"w", 0);
49 sprintf(s,
"%s.trealloc", filename);
50 fp_trealloc =
fopen_e(s,
"w", 0);
51 sprintf(s,
"%s.tfree", filename);
53 mdb_thread_unlock(&alloc_record_lock);
68 if (size_of_block <= 0)
74 if (!(ptr = calloc(size_of_block, 1))) {
75 printf(
"error: memory allocation failure--%"PRIu64
" Bytes requested.\n",
77 mdb_thread_lock(&alloc_record_lock);
78 printf(
"tmalloc() has allocated %"PRIu64
" bytes previously\n", tmalloc_total_bytes);
79 mdb_thread_unlock(&alloc_record_lock);
82 mdb_thread_lock(&alloc_record_lock);
84 fprintf(fp_tmalloc,
"%"PRIx64
" %"PRIu64
"\n", (uint64_t)ptr, size_of_block);
87 tmalloc_total_bytes += size_of_block;
88 mdb_thread_unlock(&alloc_record_lock);
102void **
zarray_2d(uint64_t size, uint64_t n1, uint64_t n2) {
105 ptr0 = ptr1 = (
void **)
tmalloc((uint64_t)(
sizeof(*ptr0) * n1));
107 *ptr1++ = (
void *)
tmalloc((uint64_t)(size * n2));
127 void **array, uint64_t n1, uint64_t n2) {
132 if (!(array = (
void **)
trealloc((
void *)array,
133 (uint64_t)(
sizeof(*array) * n1))))
134 bomb(
"memory allocation failuire in resize_zarray_2d()", NULL);
137 while (n1-- != old_n1)
138 *--ptr = (
void *)
tmalloc(size * n2);
145 if (!(*ptr = (
void *)
trealloc((
void *)*ptr, (uint64_t)(size * n2))))
146 bomb(
"memory allocation failure in resize_zarray_2d()", NULL);
177 return (
tfree(ptr0));
190void *
trealloc(
void *old_ptr, uint64_t size_of_block) {
193 if (size_of_block <= 0)
197 return (
tmalloc(size_of_block));
198 uint64_t oldaddr = (uint64_t)old_ptr;
199 if (!(ptr = realloc(old_ptr, (uint64_t)(size_of_block)))) {
200 printf(
"error: memory reallocation failure--%"PRIu64
" bytes requested.\n",
202 mdb_thread_lock(&alloc_record_lock);
203 printf(
"trealloc() has reallocated %"PRIu64
" bytes previously\n", trealloc_total_bytes);
204 mdb_thread_unlock(&alloc_record_lock);
207 mdb_thread_lock(&alloc_record_lock);
209 fprintf(fp_trealloc,
"d:%"PRIx64
"\na:%"PRIx64
" %"PRIu64
"\n", oldaddr,
210 (uint64_t)ptr, size_of_block);
213 trealloc_total_bytes += size_of_block;
214 mdb_thread_unlock(&alloc_record_lock);
244 mdb_thread_lock(&alloc_record_lock);
246 fprintf(fp_tfree,
"%"PRIx64
"\n", (uint64_t)ptr);
249 mdb_thread_unlock(&alloc_record_lock);
268void *
array_1d(uint64_t size, uint64_t lower_index, uint64_t upper_index) {
271 if (!(ptr =
tmalloc((uint64_t)size * (upper_index - lower_index + 1))))
272 bomb(
"unable to allocate array (array_1d)", NULL);
273 ptr -= lower_index * size;
274 return ((
void *)ptr);
290void **
array_2d(uint64_t size, uint64_t lower1, uint64_t upper1,
291 uint64_t lower2, uint64_t upper2)
294 register uint64_t i, n1, n2;
297 if (!(ptr =
tmalloc((uint64_t)
sizeof(*ptr) *(n1 = upper1 - lower1 + 1))))
298 bomb(
"unable to allocate array (array_2d)", NULL);
300 n2 = upper2 - lower2 + 1;
301 for (i = 0; i < n1; i++) {
302 if (!(ptr[i] =
tmalloc((uint64_t)size * n2)))
303 bomb(
"unable to allocate array (array_2d)", NULL);
304 ptr[i] -= lower2 * size;
307 return ((
void **)(ptr - lower1));
322 uint64_t upper_index) {
325 free((
char *)array + size * lower_index);
342int free_array_2d(
void **array, uint64_t size, uint64_t lower1, uint64_t upper1,
343 uint64_t lower2, uint64_t upper2)
352 n1 = upper1 - lower1 + 1;
354 for (i = 0; i < n1; i++) {
355 if ((ptr = (
char *)array[i] + size * lower2))
373void **
czarray_2d(
const uint64_t size,
const uint64_t n1,
const uint64_t n2) {
378 ptr0 = (
char **)
tmalloc((uint64_t)(
sizeof(*ptr0) * n1));
379 buffer = (
char *)
tmalloc((uint64_t)(
sizeof(*buffer) * size * n1 * n2));
380 for (i = 0; i < n1; i++)
381 ptr0[i] = buffer + i * size * n2;
382 return ((
void **)ptr0);
403 buffer = (
char *)
trealloc(*data, (uint64_t)(
sizeof(
char) * size * n1 * n2));
404 ptr0 = (
char **)
trealloc(data, (uint64_t)(
sizeof(
char *) * n1));
405 for (i = 0; i < n1; i++)
406 ptr0[i] = buffer + i * size * n2;
407 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.