15 int MDP_MP_Nprocs = 1;
16 int MDP_MP_myproc = 0;
32 int MDP_ALLO_errorOption = 3;
34 #define MDP_ALLOC_INTERFACE_ERROR 230346
36 # define MAX(x,y) (( (x) > (y) ) ? (x) : (y))
39 # define MIN(x,y) (( (x) < (y) ) ? (x) : (y))
47 static void mdp_alloc_eh(
const char* rname,
size_t bytes)
67 if (bytes == MDP_ALLOC_INTERFACE_ERROR) {
69 sprintf(mesg,
"MDP_ALLOC Interface ERROR P_%d: %s", MDP_MP_my_proc,
72 sprintf(mesg,
"MDP_ALLOC Interface ERROR: %s", rname);
75 sprintf(mesg,
"%s ERROR: out of memory while mallocing %d bytes",
78 if (MDP_ALLO_errorOption % 2 == 1) {
79 fprintf(stderr,
"\n%s", mesg);
81 if (MDP_MP_Nprocs > 1) {
82 fprintf(stderr,
": proc = %d\n", MDP_MP_myproc);
91 if (MDP_ALLO_errorOption == 2 || MDP_ALLO_errorOption == 3) {
92 throw std::bad_alloc();
94 if (MDP_ALLO_errorOption == 4 || MDP_ALLO_errorOption == 5) {
97 if (MDP_ALLO_errorOption > 5) {
106 static void mdp_alloc_eh2(
const char* rname)
123 if (MDP_ALLO_errorOption == 1) {
124 fprintf(stderr,
"%s ERROR: returning with null pointer", rname);
126 if (MDP_MP_Nprocs > 1) {
127 fprintf(stderr,
": proc = %d", MDP_MP_myproc);
130 fprintf(stderr,
"\n");
137 #define Fprintf (void) fprintf
140 #ifndef HAVE_ARRAY_ALLOC
142 static double* smalloc(
size_t n);
219 double* mdp_array_alloc(
int numdim, ...)
237 va_start(va, numdim);
241 "mdp_array_alloc ERROR: number of dimensions, %d, is <=0\n",
244 }
else if (numdim > 4) {
246 "mdp_array_alloc ERROR: number of dimensions, %d, is > 4\n",
251 dim[0].index = va_arg(va,
int);
253 if (dim[0].index <= 0) {
255 Fprintf(stderr,
"WARNING: mdp_array_alloc called with first "
256 "dimension <= 0, %d\n\twill return the nil pointer\n",
257 (
int)(dim[0].index));
259 return((
double*) NULL);
262 dim[0].total = dim[0].index;
263 dim[0].size =
sizeof(
void*);
265 for (i = 1; i < numdim; i++) {
266 dim[i].index = va_arg(va,
int);
267 if (dim[i].index <= 0) {
269 "WARNING: mdp_array_alloc called with dimension %d <= 0, "
270 "%d\n", i+1, (
int)(dim[i].index));
271 Fprintf(stderr,
"\twill return the nil pointer\n");
272 return((
double*) NULL);
274 dim[i].total = dim[i-1].total * dim[i].index;
275 dim[i].size =
sizeof(
void*);
276 dim[i].off = dim[i-1].off + dim[i-1].total * dim[i-1].size;
279 dim[numdim-1].size = va_arg(va,
int);
286 dim[numdim-1].off = dim[numdim-1].size *
287 ((dim[numdim-1].off+dim[numdim-1].size-1)/dim[numdim-1].size);
289 total = dim[numdim-1].off + dim[numdim-1].total * dim[numdim-1].size;
291 dfield = (
double*) smalloc((
size_t) total);
292 field = (
char*) dfield;
294 for (i = 0; i < numdim - 1; i++) {
295 ptr = (
char**)(field + dim[i].off);
296 data = (
char*)(field + dim[i+1].off);
297 for (j = 0; j < dim[i].total; j++) {
298 ptr[j] = data + j * dim[i+1].size * dim[i+1].index;
308 static double* smalloc(
size_t n)
317 static int firsttime = 1;
326 pntr = (
double*) malloc((
size_t) n);
328 if (pntr == NULL && n != 0) {
329 Fprintf(stderr,
"smalloc : Out of space - number of bytes "
330 "requested = %d\n",
int(n));
335 file = fopen(
"memops.txt",
"w");
337 file = fopen(
"memops.txt",
"a");
339 Fprintf(file,
"%x %d malloc\n", pntr, n);
340 if ((
int) pntr == 0x00000001) {
341 Fprintf(stderr,
"FOUND IT!\n");
352 void mdp_safe_free(
void** ptr)
370 mdp_alloc_eh(
"mdp_safe_free: handle is NULL", MDP_ALLOC_INTERFACE_ERROR);
374 file = fopen(
"memops.txt",
"a");
375 Fprintf(file,
"%x free\n", *ptr);
377 if ((
int) *ptr == 0x00000001) {
378 Fprintf(stderr,
"FOUND IT!\n");
419 int* mdp_alloc_int_1(
int nvalues,
const int val)
441 array= (
int*) mdp_array_alloc(1, nvalues,
sizeof(
int));
443 if (val != MDP_INT_NOINIT) {
445 (void) memset(array, 0,
sizeof(
int)*nvalues);
447 for (
int i = 0; i < nvalues; i++) {
453 mdp_alloc_eh(
"mdp_alloc_int_1", nvalues *
sizeof(
int));
461 void mdp_safe_alloc_int_1(
int** array_hdl,
int nvalues,
const int val)
482 if (array_hdl == NULL) {
483 mdp_alloc_eh(
"mdp_safe_alloc_int_1: handle is NULL",
484 MDP_ALLOC_INTERFACE_ERROR);
487 if (*array_hdl != NULL) {
488 mdp_safe_free((
void**) array_hdl);
490 *array_hdl = mdp_alloc_int_1(nvalues, val);
491 if (*array_hdl == NULL) {
492 mdp_alloc_eh2(
"mdp_safe_alloc_int_1");
500 mdp_realloc_int_1(
int** array_hdl,
int new_length,
int old_length,
523 if (new_length == old_length) {
526 if (new_length <= 0) {
529 "Warning: mdp_realloc_int_1 P_%d: called with n = %d ",
530 MDP_MP_myproc, new_length);
533 "Warning: mdp_realloc_int_1: called with n = %d ",
538 if (old_length < 0) {
541 if (new_length == old_length) {
544 size_t bytenum = new_length *
sizeof(int);
545 int* array = (
int*) smalloc(bytenum);
548 if (old_length > 0) {
549 bytenum =
sizeof(int) * old_length;
553 if (new_length < old_length) {
554 bytenum =
sizeof(int) * new_length;
556 (void) memcpy((
void*) array, (
void*) *array_hdl, bytenum);
557 mdp_safe_free((
void**) array_hdl);
562 if ((defval != MDP_INT_NOINIT) && (new_length > old_length)) {
564 bytenum =
sizeof(int) * (new_length - old_length);
565 (void) memset((
void*)(array+old_length), 0, bytenum);
567 for (
int i = old_length; i < new_length; i++) {
573 mdp_alloc_eh(
"mdp_realloc_int_1", bytenum);
580 int** mdp_alloc_int_2(
int ndim1,
int ndim2,
const int val)
607 array = (
int**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
int));
609 if (val != MDP_INT_NOINIT) {
611 (void) memset((
void*) array[0], 0, ndim1 * ndim2 *
sizeof(int));
613 dptr = &(array[0][0]);
614 for (i = 0; i < ndim1 * ndim2; i++) {
620 mdp_alloc_eh(
"mdp_alloc_int_2",
621 sizeof(
int) * ndim1 * ndim2 +
622 ndim1 *
sizeof(
void*));
630 double* mdp_alloc_dbl_1(
int nvalues,
const double val)
653 array = (
double*) mdp_array_alloc(1, nvalues,
sizeof(
double));
655 if (val != MDP_DBL_NOINIT) {
657 (void) memset((
void*) array, 0, nvalues *
sizeof(double));
659 for (i = 0; i < nvalues; i++) {
665 mdp_alloc_eh(
"mdp_alloc_dbl_1", nvalues *
sizeof(
double));
673 void mdp_safe_alloc_dbl_1(
double** array_hdl,
int nvalues,
const double val)
694 if (array_hdl == NULL) {
695 mdp_alloc_eh(
"mdp_safe_alloc_dbl_1: handle is NULL",
696 MDP_ALLOC_INTERFACE_ERROR);
699 if (*array_hdl != NULL) {
700 mdp_safe_free((
void**) array_hdl);
702 *array_hdl = mdp_alloc_dbl_1(nvalues, val);
703 if (*array_hdl == NULL) {
704 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_1");
711 void mdp_realloc_dbl_1(
double** array_hdl,
int new_length,
712 int old_length,
const double defval)
734 if (new_length == old_length) {
737 if (new_length <= 0) {
739 fprintf(stderr,
"Warning: mdp_realloc_dbl_1 P_%d: called with n = %d ",
740 MDP_MP_myproc, new_length);
742 fprintf(stderr,
"Warning: mdp_realloc_dbl_1: called with n = %d ",
747 if (old_length < 0) {
750 if (new_length == old_length) {
753 size_t bytenum = new_length *
sizeof(double);
754 double* array = (
double*) smalloc(bytenum);
757 if (old_length > 0) {
758 bytenum =
sizeof(double) * old_length;
762 if (new_length < old_length) {
763 bytenum =
sizeof(double) * new_length;
765 (void) memcpy((
void*) array, (
void*) *array_hdl, bytenum);
766 mdp_safe_free((
void**) array_hdl);
771 if ((defval != MDP_DBL_NOINIT) && (new_length > old_length)) {
773 bytenum =
sizeof(double) * (new_length - old_length);
774 (void) memset((
void*)(array+old_length), 0, bytenum);
776 for (
int i = old_length; i < new_length; i++) {
782 mdp_alloc_eh(
"mdp_realloc_dbl_1", bytenum);
789 char* mdp_alloc_char_1(
int nvalues,
const char val)
812 array = (
char*) mdp_array_alloc(1, nvalues,
sizeof(
char));
814 for (i = 0; i < nvalues; i++) {
818 mdp_alloc_eh(
"mdp_alloc_char_1", nvalues *
sizeof(
char));
826 void mdp_safe_alloc_char_1(
char** array_hdl,
int nvalues,
const char val)
847 if (array_hdl == NULL) {
848 mdp_alloc_eh(
"mdp_safe_alloc_char_1: handle is NULL",
849 MDP_ALLOC_INTERFACE_ERROR);
852 if (*array_hdl != NULL) {
853 mdp_safe_free((
void**) array_hdl);
855 *array_hdl = mdp_alloc_char_1(nvalues, val);
856 if (*array_hdl == NULL) {
857 mdp_alloc_eh2(
"mdp_safe_alloc_char_1");
864 double** mdp_alloc_dbl_2(
int ndim1,
int ndim2,
const double val)
884 double** array, *dptr;
891 array = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
893 if (val != MDP_DBL_NOINIT) {
895 (void) memset((
void*) array[0], 0, ndim1*ndim2 *
sizeof(double));
897 dptr = &(array[0][0]);
898 for (i = 0; i < ndim1*ndim2; i++) {
904 mdp_alloc_eh(
"mdp_alloc_dbl_2",
905 sizeof(
double) * ndim1 * ndim2 +
906 ndim1 *
sizeof(
void*));
914 void mdp_safe_alloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
937 if (array_hdl == NULL) {
938 mdp_alloc_eh(
"mdp_safe_alloc_dbl_2: handle is NULL",
939 MDP_ALLOC_INTERFACE_ERROR);
942 if (*array_hdl != NULL) {
943 mdp_safe_free((
void**) array_hdl);
945 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
946 if (*array_hdl == NULL) {
947 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_2");
954 void mdp_realloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
955 int ndim1Old,
int ndim2Old,
const double val)
992 ndim1Old = MAX(ndim1Old, 0);
993 ndim2Old = MAX(ndim2Old, 0);
998 if ((*array_hdl == NULL) || (ndim1Old <= 0 && ndim2Old <= 0)) {
999 mdp_safe_free((
void**) array_hdl);
1000 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
1001 if (*array_hdl == NULL) {
1002 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1009 double** array_old = *array_hdl;
1010 *array_hdl = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
1011 if (*array_hdl == NULL) {
1012 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1017 int ndim1Min = MIN(ndim1, ndim1Old);
1018 int ndim2Min = MIN(ndim2, ndim2Old);
1019 double** array_new = *array_hdl;
1024 if (ndim2 == ndim2Old) {
1025 size_t sz = ndim1Min * ndim2 *
sizeof(double);
1026 (void) memcpy((
void*) array_new[0], (
void*) array_old[0], sz);
1033 size_t sz = ndim2Min *
sizeof(double);
1034 size_t sz2 = (ndim2 - ndim2Min) *
sizeof(
double);
1035 for (
int i = 0; i < ndim1Min; i++) {
1036 (void) memcpy((
void*) array_new[i], (
void*) array_old[i], sz);
1037 if (ndim2 > ndim2Min && val != MDP_DBL_NOINIT) {
1039 (void) memset((
void*)(array_new[i] + ndim2Min), 0, sz2);
1041 double* dptr = array_new[i];
1042 for (
int j = ndim2Min; j < ndim2; j++) {
1052 if (ndim1 > ndim1Min && val != MDP_DBL_NOINIT) {
1054 size_t sz = (ndim1 - ndim1Min) * ndim2 *
sizeof(
double);
1055 (void) memset((
void*) array_new[ndim1Min], 0, sz);
1057 double* dptr = array_new[ndim1Min];
1058 int num = (ndim1 - ndim1Min) * ndim2;
1059 for (
int i = 0; i < num; i++) {
1067 mdp_safe_free((
void**) &array_old);
1075 char** mdp_alloc_VecFixedStrings(
int numStrings,
int lenString)
1097 if (numStrings <= 0) {
1100 if (lenString <= 0) {
1103 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1104 if (array != NULL) {
1105 for (i = 0; i < numStrings; i++) {
1109 mdp_alloc_eh(
"mdp_alloc_VecFixedStrings",
1110 sizeof(
char) * numStrings * lenString +
1111 numStrings *
sizeof(
void*));
1119 void mdp_realloc_VecFixedStrings(
char** *array_hdl,
int numStrings,
1120 int numOldStrings,
int lenString)
1142 if (numStrings <= 0) {
1145 if (numStrings == numOldStrings) {
1148 if (lenString <= 0) {
1151 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1152 if (array != NULL) {
1153 int len = MIN(numStrings, numOldStrings);
1156 for (i = 0; i < len; i++) {
1157 strncpy(array[i], ao[i], lenString);
1160 if (numStrings > numOldStrings) {
1161 for (i = numOldStrings; i < numStrings; i++) {
1165 mdp_safe_free((
void**) array_hdl);
1169 mdp_alloc_eh(
"mdp_realloc_VecFixedStrings",
1170 sizeof(
char) * numStrings * lenString +
1171 numStrings *
sizeof(
void*));
1178 void mdp_safe_alloc_VecFixedStrings(
char** *array_hdl,
1179 int numStrings,
int lenString)
1201 if (array_hdl == NULL) {
1202 mdp_alloc_eh(
"mdp_safe_alloc_VecFixedStrings: handle is NULL",
1203 MDP_ALLOC_INTERFACE_ERROR);
1206 if (*array_hdl != NULL) {
1207 mdp_safe_free((
void**) array_hdl);
1209 *array_hdl = mdp_alloc_VecFixedStrings(numStrings, lenString);
1210 if (*array_hdl == NULL) {
1211 mdp_alloc_eh2(
"mdp_safe_alloc_VecFixedStrings");
1218 C16_NAME* mdp_alloc_C16_NAME_1(
int numStrings,
const int init)
1240 if (numStrings <= 0) {
1243 C16_NAME* array = (C16_NAME*) mdp_array_alloc(1, numStrings,
sizeof(C16_NAME));
1244 if (array != NULL) {
1246 for (i = 0; i < numStrings; i++) {
1247 c_ptr = (
char*)(array + i);
1248 for (j = 0; j < (int)
sizeof(C16_NAME); j++) {
1254 mdp_alloc_eh(
"mdp_alloc_C16_NAME_1",
1255 sizeof(C16_NAME) * numStrings);
1263 void mdp_safe_alloc_C16_NAME_1(C16_NAME** array_hdl,
int numStrings,
1287 if (array_hdl == NULL) {
1288 mdp_alloc_eh(
"mdp_safe_alloc_C16_NAME_1: handle is NULL",
1289 MDP_ALLOC_INTERFACE_ERROR);
1292 if (*array_hdl != NULL) {
1293 mdp_safe_free((
void**) array_hdl);
1295 *array_hdl = mdp_alloc_C16_NAME_1(numStrings, init);
1296 if (*array_hdl == NULL) {
1297 mdp_alloc_eh2(
"mdp_safe_alloc_C16_NAME_1");
1304 void** mdp_alloc_ptr_1(
int numPointers)
1325 if (numPointers <= 0) {
1328 array = (
void**) mdp_array_alloc(1, numPointers,
sizeof(
void*));
1329 if (array != NULL) {
1330 for (i = 0; i < numPointers; i++) {
1334 mdp_alloc_eh(
"mdp_alloc_ptr_1",
1335 sizeof(
void*) * numPointers);
1343 void mdp_safe_alloc_ptr_1(
void** *array_hdl,
int numPointers)
1365 if (array_hdl == NULL) {
1366 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1367 MDP_ALLOC_INTERFACE_ERROR);
1370 if (*array_hdl != NULL) {
1371 mdp_safe_free((
void**) array_hdl);
1373 *array_hdl = mdp_alloc_ptr_1(numPointers);
1374 if (*array_hdl == NULL) {
1375 mdp_alloc_eh2(
"mdp_safe_alloc_ptr_1");
1382 void mdp_realloc_ptr_1(
void** *array_hdl,
int numLen,
int numOldLen)
1400 if (array_hdl == NULL) {
1401 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1402 MDP_ALLOC_INTERFACE_ERROR);
1408 if (numOldLen < 0) {
1411 if (numLen == numOldLen) {
1414 size_t bytenum =
sizeof(
void*) * numLen;
1415 void** array = (
void**) smalloc(bytenum);
1416 if (array != NULL) {
1417 int len = MIN(numLen, numOldLen);
1419 void** ao = *array_hdl;
1420 for (
int i = 0; i < len; i++) {
1426 if (numLen > numOldLen) {
1427 bytenum =
sizeof(
void*) * (numLen - numOldLen);
1428 (void) memset((
void*)(array + numOldLen), 0, bytenum);
1430 mdp_safe_free((
void**) array_hdl);
1433 mdp_alloc_eh(
"mdp_realloc_ptr_1",
sizeof(
void*) * numLen);
1441 void*** mdp_alloc_ptr_2(
int ndim1,
int ndim2)
1469 array = (
void***) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
void*));
1470 if (array != NULL) {
1471 (void) memset((
void*) array[0], 0, ndim1*ndim2 *
sizeof(
void*));
1473 mdp_alloc_eh(
"mdp_alloc_ptr_2",
1474 sizeof(
void*) * ndim1 * ndim2);
1483 char* mdp_copy_C16_NAME_to_string(
const C16_NAME copyFrom)
1509 C16_NAME_STR tmpString;
1510 if (copyFrom == NULL) {
1513 tmpString[
sizeof(C16_NAME)] =
'\0';
1514 (void) strncpy(tmpString, copyFrom,
sizeof(C16_NAME));
1515 return mdp_copy_string(tmpString);
1521 char* mdp_copy_string(
const char* copyFrom)
1541 if (copyFrom == NULL) {
1544 cptr = (
char*) mdp_array_alloc(1, strlen(copyFrom) + 1,
sizeof(char));
1546 (void) strcpy(cptr, copyFrom);
1548 mdp_alloc_eh(
"mdp_copy_string",
sizeof(
char) * (strlen(copyFrom) + 1));
1556 void mdp_safe_copy_string(
char** string_hdl,
const char* copyFrom)
1575 if (string_hdl == NULL) {
1576 mdp_alloc_eh(
"mdp_safe_copy_string: string_hdl is NULL",
1577 MDP_ALLOC_INTERFACE_ERROR);
1580 if (*string_hdl != NULL) {
1581 mdp_safe_free((
void**) string_hdl);
1583 if (copyFrom == NULL) {
1587 *string_hdl = mdp_copy_string(copyFrom);
1588 if (*string_hdl == NULL) {
1589 mdp_alloc_eh2(
"mdp_safe_copy_string");