16 int MDP_MP_Nprocs = 1;
17 int MDP_MP_myproc = 0;
33 int MDP_ALLO_errorOption = 3;
35 #define MDP_ALLOC_INTERFACE_ERROR 230346
41 static void mdp_alloc_eh(
const char* rname,
size_t bytes)
61 if (bytes == MDP_ALLOC_INTERFACE_ERROR) {
63 sprintf(mesg,
"MDP_ALLOC Interface ERROR P_%d: %s", MDP_MP_my_proc,
66 sprintf(mesg,
"MDP_ALLOC Interface ERROR: %s", rname);
69 sprintf(mesg,
"%s ERROR: out of memory while mallocing %d bytes",
72 if (MDP_ALLO_errorOption % 2 == 1) {
73 fprintf(stderr,
"\n%s", mesg);
75 if (MDP_MP_Nprocs > 1) {
76 fprintf(stderr,
": proc = %d\n", MDP_MP_myproc);
85 if (MDP_ALLO_errorOption == 2 || MDP_ALLO_errorOption == 3) {
86 throw std::bad_alloc();
88 if (MDP_ALLO_errorOption == 4 || MDP_ALLO_errorOption == 5) {
91 if (MDP_ALLO_errorOption > 5) {
100 static void mdp_alloc_eh2(
const char* rname)
117 if (MDP_ALLO_errorOption == 1) {
118 fprintf(stderr,
"%s ERROR: returning with null pointer", rname);
120 if (MDP_MP_Nprocs > 1) {
121 fprintf(stderr,
": proc = %d", MDP_MP_myproc);
124 fprintf(stderr,
"\n");
131 #define Fprintf (void) fprintf
134 #ifndef HAVE_ARRAY_ALLOC
136 static double* smalloc(
size_t n);
213 double* mdp_array_alloc(
int numdim, ...)
231 va_start(va, numdim);
235 "mdp_array_alloc ERROR: number of dimensions, %d, is <=0\n",
238 }
else if (numdim > 4) {
240 "mdp_array_alloc ERROR: number of dimensions, %d, is > 4\n",
245 dim[0].index = va_arg(va,
int);
247 if (dim[0].index <= 0) {
249 Fprintf(stderr,
"WARNING: mdp_array_alloc called with first "
250 "dimension <= 0, %d\n\twill return the nil pointer\n",
251 (
int)(dim[0].index));
253 return((
double*) NULL);
256 dim[0].total = dim[0].index;
257 dim[0].size =
sizeof(
void*);
259 for (i = 1; i < numdim; i++) {
260 dim[i].index = va_arg(va,
int);
261 if (dim[i].index <= 0) {
263 "WARNING: mdp_array_alloc called with dimension %d <= 0, "
264 "%d\n", i+1, (
int)(dim[i].index));
265 Fprintf(stderr,
"\twill return the nil pointer\n");
266 return((
double*) NULL);
268 dim[i].total = dim[i-1].total * dim[i].index;
269 dim[i].size =
sizeof(
void*);
270 dim[i].off = dim[i-1].off + dim[i-1].total * dim[i-1].size;
273 dim[numdim-1].size = va_arg(va,
int);
280 dim[numdim-1].off = dim[numdim-1].size *
281 ((dim[numdim-1].off+dim[numdim-1].size-1)/dim[numdim-1].size);
283 total = dim[numdim-1].off + dim[numdim-1].total * dim[numdim-1].size;
285 dfield = (
double*) smalloc((
size_t) total);
286 field = (
char*) dfield;
288 for (i = 0; i < numdim - 1; i++) {
289 ptr = (
char**)(field + dim[i].off);
290 data = (
char*)(field + dim[i+1].off);
291 for (j = 0; j < dim[i].total; j++) {
292 ptr[j] = data + j * dim[i+1].size * dim[i+1].index;
302 static double* smalloc(
size_t n)
311 static int firsttime = 1;
320 pntr = (
double*) malloc((
size_t) n);
322 if (pntr == NULL && n != 0) {
323 Fprintf(stderr,
"smalloc : Out of space - number of bytes "
324 "requested = %d\n",
int(n));
329 file = fopen(
"memops.txt",
"w");
331 file = fopen(
"memops.txt",
"a");
333 Fprintf(file,
"%x %d malloc\n", pntr, n);
334 if ((
int) pntr == 0x00000001) {
335 Fprintf(stderr,
"FOUND IT!\n");
346 void mdp_safe_free(
void** ptr)
364 mdp_alloc_eh(
"mdp_safe_free: handle is NULL", MDP_ALLOC_INTERFACE_ERROR);
368 file = fopen(
"memops.txt",
"a");
369 Fprintf(file,
"%x free\n", *ptr);
371 if ((
int) *ptr == 0x00000001) {
372 Fprintf(stderr,
"FOUND IT!\n");
413 int* mdp_alloc_int_1(
int nvalues,
const int val)
435 array= (
int*) mdp_array_alloc(1, nvalues,
sizeof(
int));
437 if (val != MDP_INT_NOINIT) {
439 (void) memset(array, 0,
sizeof(
int)*nvalues);
441 for (
int i = 0; i < nvalues; i++) {
447 mdp_alloc_eh(
"mdp_alloc_int_1", nvalues *
sizeof(
int));
455 void mdp_safe_alloc_int_1(
int** array_hdl,
int nvalues,
const int val)
476 if (array_hdl == NULL) {
477 mdp_alloc_eh(
"mdp_safe_alloc_int_1: handle is NULL",
478 MDP_ALLOC_INTERFACE_ERROR);
481 if (*array_hdl != NULL) {
482 mdp_safe_free((
void**) array_hdl);
484 *array_hdl = mdp_alloc_int_1(nvalues, val);
485 if (*array_hdl == NULL) {
486 mdp_alloc_eh2(
"mdp_safe_alloc_int_1");
494 mdp_realloc_int_1(
int** array_hdl,
int new_length,
int old_length,
517 if (new_length == old_length) {
520 if (new_length <= 0) {
523 "Warning: mdp_realloc_int_1 P_%d: called with n = %d ",
524 MDP_MP_myproc, new_length);
527 "Warning: mdp_realloc_int_1: called with n = %d ",
532 if (old_length < 0) {
535 if (new_length == old_length) {
538 size_t bytenum = new_length *
sizeof(int);
539 int* array = (
int*) smalloc(bytenum);
542 if (old_length > 0) {
543 bytenum =
sizeof(int) * old_length;
547 if (new_length < old_length) {
548 bytenum =
sizeof(int) * new_length;
550 (void) memcpy((
void*) array, (
void*) *array_hdl, bytenum);
551 mdp_safe_free((
void**) array_hdl);
556 if ((defval != MDP_INT_NOINIT) && (new_length > old_length)) {
558 bytenum =
sizeof(int) * (new_length - old_length);
559 (void) memset((
void*)(array+old_length), 0, bytenum);
561 for (
int i = old_length; i < new_length; i++) {
567 mdp_alloc_eh(
"mdp_realloc_int_1", bytenum);
574 int** mdp_alloc_int_2(
int ndim1,
int ndim2,
const int val)
601 array = (
int**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
int));
603 if (val != MDP_INT_NOINIT) {
605 (void) memset((
void*) array[0], 0, ndim1 * ndim2 *
sizeof(int));
607 dptr = &(array[0][0]);
608 for (i = 0; i < ndim1 * ndim2; i++) {
614 mdp_alloc_eh(
"mdp_alloc_int_2",
615 sizeof(
int) * ndim1 * ndim2 +
616 ndim1 *
sizeof(
void*));
624 double* mdp_alloc_dbl_1(
int nvalues,
const double val)
647 array = (
double*) mdp_array_alloc(1, nvalues,
sizeof(
double));
649 if (val != MDP_DBL_NOINIT) {
651 (void) memset((
void*) array, 0, nvalues *
sizeof(double));
653 for (i = 0; i < nvalues; i++) {
659 mdp_alloc_eh(
"mdp_alloc_dbl_1", nvalues *
sizeof(
double));
667 void mdp_safe_alloc_dbl_1(
double** array_hdl,
int nvalues,
const double val)
688 if (array_hdl == NULL) {
689 mdp_alloc_eh(
"mdp_safe_alloc_dbl_1: handle is NULL",
690 MDP_ALLOC_INTERFACE_ERROR);
693 if (*array_hdl != NULL) {
694 mdp_safe_free((
void**) array_hdl);
696 *array_hdl = mdp_alloc_dbl_1(nvalues, val);
697 if (*array_hdl == NULL) {
698 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_1");
705 void mdp_realloc_dbl_1(
double** array_hdl,
int new_length,
706 int old_length,
const double defval)
728 if (new_length == old_length) {
731 if (new_length <= 0) {
733 fprintf(stderr,
"Warning: mdp_realloc_dbl_1 P_%d: called with n = %d ",
734 MDP_MP_myproc, new_length);
736 fprintf(stderr,
"Warning: mdp_realloc_dbl_1: called with n = %d ",
741 if (old_length < 0) {
744 if (new_length == old_length) {
747 size_t bytenum = new_length *
sizeof(double);
748 double* array = (
double*) smalloc(bytenum);
751 if (old_length > 0) {
752 bytenum =
sizeof(double) * old_length;
756 if (new_length < old_length) {
757 bytenum =
sizeof(double) * new_length;
759 (void) memcpy((
void*) array, (
void*) *array_hdl, bytenum);
760 mdp_safe_free((
void**) array_hdl);
765 if ((defval != MDP_DBL_NOINIT) && (new_length > old_length)) {
767 bytenum =
sizeof(double) * (new_length - old_length);
768 (void) memset((
void*)(array+old_length), 0, bytenum);
770 for (
int i = old_length; i < new_length; i++) {
776 mdp_alloc_eh(
"mdp_realloc_dbl_1", bytenum);
783 char* mdp_alloc_char_1(
int nvalues,
const char val)
806 array = (
char*) mdp_array_alloc(1, nvalues,
sizeof(
char));
808 for (i = 0; i < nvalues; i++) {
812 mdp_alloc_eh(
"mdp_alloc_char_1", nvalues *
sizeof(
char));
820 void mdp_safe_alloc_char_1(
char** array_hdl,
int nvalues,
const char val)
841 if (array_hdl == NULL) {
842 mdp_alloc_eh(
"mdp_safe_alloc_char_1: handle is NULL",
843 MDP_ALLOC_INTERFACE_ERROR);
846 if (*array_hdl != NULL) {
847 mdp_safe_free((
void**) array_hdl);
849 *array_hdl = mdp_alloc_char_1(nvalues, val);
850 if (*array_hdl == NULL) {
851 mdp_alloc_eh2(
"mdp_safe_alloc_char_1");
858 double** mdp_alloc_dbl_2(
int ndim1,
int ndim2,
const double val)
878 double** array, *dptr;
885 array = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
887 if (val != MDP_DBL_NOINIT) {
889 (void) memset((
void*) array[0], 0, ndim1*ndim2 *
sizeof(double));
891 dptr = &(array[0][0]);
892 for (i = 0; i < ndim1*ndim2; i++) {
898 mdp_alloc_eh(
"mdp_alloc_dbl_2",
899 sizeof(
double) * ndim1 * ndim2 +
900 ndim1 *
sizeof(
void*));
908 void mdp_safe_alloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
931 if (array_hdl == NULL) {
932 mdp_alloc_eh(
"mdp_safe_alloc_dbl_2: handle is NULL",
933 MDP_ALLOC_INTERFACE_ERROR);
936 if (*array_hdl != NULL) {
937 mdp_safe_free((
void**) array_hdl);
939 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
940 if (*array_hdl == NULL) {
941 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_2");
948 void mdp_realloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
949 int ndim1Old,
int ndim2Old,
const double val)
986 ndim1Old = std::max(ndim1Old, 0);
987 ndim2Old = std::max(ndim2Old, 0);
992 if ((*array_hdl == NULL) || (ndim1Old <= 0 && ndim2Old <= 0)) {
993 mdp_safe_free((
void**) array_hdl);
994 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
995 if (*array_hdl == NULL) {
996 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1003 double** array_old = *array_hdl;
1004 *array_hdl = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
1005 if (*array_hdl == NULL) {
1006 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1011 int ndim1Min = std::min(ndim1, ndim1Old);
1012 int ndim2Min = std::min(ndim2, ndim2Old);
1013 double** array_new = *array_hdl;
1018 if (ndim2 == ndim2Old) {
1019 size_t sz = ndim1Min * ndim2 *
sizeof(double);
1020 (void) memcpy((
void*) array_new[0], (
void*) array_old[0], sz);
1027 size_t sz = ndim2Min *
sizeof(double);
1028 size_t sz2 = (ndim2 - ndim2Min) *
sizeof(
double);
1029 for (
int i = 0; i < ndim1Min; i++) {
1030 (void) memcpy((
void*) array_new[i], (
void*) array_old[i], sz);
1031 if (ndim2 > ndim2Min && val != MDP_DBL_NOINIT) {
1033 (void) memset((
void*)(array_new[i] + ndim2Min), 0, sz2);
1035 double* dptr = array_new[i];
1036 for (
int j = ndim2Min; j < ndim2; j++) {
1046 if (ndim1 > ndim1Min && val != MDP_DBL_NOINIT) {
1048 size_t sz = (ndim1 - ndim1Min) * ndim2 *
sizeof(
double);
1049 (void) memset((
void*) array_new[ndim1Min], 0, sz);
1051 double* dptr = array_new[ndim1Min];
1052 int num = (ndim1 - ndim1Min) * ndim2;
1053 for (
int i = 0; i < num; i++) {
1061 mdp_safe_free((
void**) &array_old);
1069 char** mdp_alloc_VecFixedStrings(
int numStrings,
int lenString)
1091 if (numStrings <= 0) {
1094 if (lenString <= 0) {
1097 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1098 if (array != NULL) {
1099 for (i = 0; i < numStrings; i++) {
1103 mdp_alloc_eh(
"mdp_alloc_VecFixedStrings",
1104 sizeof(
char) * numStrings * lenString +
1105 numStrings *
sizeof(
void*));
1113 void mdp_realloc_VecFixedStrings(
char** *array_hdl,
int numStrings,
1114 int numOldStrings,
int lenString)
1136 if (numStrings <= 0) {
1139 if (numStrings == numOldStrings) {
1142 if (lenString <= 0) {
1145 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1146 if (array != NULL) {
1147 int len = std::min(numStrings, numOldStrings);
1150 for (i = 0; i < len; i++) {
1151 strncpy(array[i], ao[i], lenString);
1154 if (numStrings > numOldStrings) {
1155 for (i = numOldStrings; i < numStrings; i++) {
1159 mdp_safe_free((
void**) array_hdl);
1163 mdp_alloc_eh(
"mdp_realloc_VecFixedStrings",
1164 sizeof(
char) * numStrings * lenString +
1165 numStrings *
sizeof(
void*));
1172 void mdp_safe_alloc_VecFixedStrings(
char** *array_hdl,
1173 int numStrings,
int lenString)
1195 if (array_hdl == NULL) {
1196 mdp_alloc_eh(
"mdp_safe_alloc_VecFixedStrings: handle is NULL",
1197 MDP_ALLOC_INTERFACE_ERROR);
1200 if (*array_hdl != NULL) {
1201 mdp_safe_free((
void**) array_hdl);
1203 *array_hdl = mdp_alloc_VecFixedStrings(numStrings, lenString);
1204 if (*array_hdl == NULL) {
1205 mdp_alloc_eh2(
"mdp_safe_alloc_VecFixedStrings");
1212 C16_NAME* mdp_alloc_C16_NAME_1(
int numStrings,
const int init)
1234 if (numStrings <= 0) {
1237 C16_NAME* array = (C16_NAME*) mdp_array_alloc(1, numStrings,
sizeof(C16_NAME));
1238 if (array != NULL) {
1240 for (i = 0; i < numStrings; i++) {
1241 c_ptr = (
char*)(array + i);
1242 for (j = 0; j < (int)
sizeof(C16_NAME); j++) {
1248 mdp_alloc_eh(
"mdp_alloc_C16_NAME_1",
1249 sizeof(C16_NAME) * numStrings);
1257 void mdp_safe_alloc_C16_NAME_1(C16_NAME** array_hdl,
int numStrings,
1281 if (array_hdl == NULL) {
1282 mdp_alloc_eh(
"mdp_safe_alloc_C16_NAME_1: handle is NULL",
1283 MDP_ALLOC_INTERFACE_ERROR);
1286 if (*array_hdl != NULL) {
1287 mdp_safe_free((
void**) array_hdl);
1289 *array_hdl = mdp_alloc_C16_NAME_1(numStrings, init);
1290 if (*array_hdl == NULL) {
1291 mdp_alloc_eh2(
"mdp_safe_alloc_C16_NAME_1");
1298 void** mdp_alloc_ptr_1(
int numPointers)
1319 if (numPointers <= 0) {
1322 array = (
void**) mdp_array_alloc(1, numPointers,
sizeof(
void*));
1323 if (array != NULL) {
1324 for (i = 0; i < numPointers; i++) {
1328 mdp_alloc_eh(
"mdp_alloc_ptr_1",
1329 sizeof(
void*) * numPointers);
1337 void mdp_safe_alloc_ptr_1(
void** *array_hdl,
int numPointers)
1359 if (array_hdl == NULL) {
1360 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1361 MDP_ALLOC_INTERFACE_ERROR);
1364 if (*array_hdl != NULL) {
1365 mdp_safe_free((
void**) array_hdl);
1367 *array_hdl = mdp_alloc_ptr_1(numPointers);
1368 if (*array_hdl == NULL) {
1369 mdp_alloc_eh2(
"mdp_safe_alloc_ptr_1");
1376 void mdp_realloc_ptr_1(
void** *array_hdl,
int numLen,
int numOldLen)
1394 if (array_hdl == NULL) {
1395 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1396 MDP_ALLOC_INTERFACE_ERROR);
1402 if (numOldLen < 0) {
1405 if (numLen == numOldLen) {
1408 size_t bytenum =
sizeof(
void*) * numLen;
1409 void** array = (
void**) smalloc(bytenum);
1410 if (array != NULL) {
1411 int len = std::min(numLen, numOldLen);
1413 void** ao = *array_hdl;
1414 for (
int i = 0; i < len; i++) {
1420 if (numLen > numOldLen) {
1421 bytenum =
sizeof(
void*) * (numLen - numOldLen);
1422 (void) memset((
void*)(array + numOldLen), 0, bytenum);
1424 mdp_safe_free((
void**) array_hdl);
1427 mdp_alloc_eh(
"mdp_realloc_ptr_1",
sizeof(
void*) * numLen);
1435 void*** mdp_alloc_ptr_2(
int ndim1,
int ndim2)
1463 array = (
void***) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
void*));
1464 if (array != NULL) {
1465 (void) memset((
void*) array[0], 0, ndim1*ndim2 *
sizeof(
void*));
1467 mdp_alloc_eh(
"mdp_alloc_ptr_2",
1468 sizeof(
void*) * ndim1 * ndim2);
1477 char* mdp_copy_C16_NAME_to_string(
const C16_NAME copyFrom)
1503 C16_NAME_STR tmpString;
1504 if (copyFrom == NULL) {
1507 tmpString[
sizeof(C16_NAME)] =
'\0';
1508 (void) strncpy(tmpString, copyFrom,
sizeof(C16_NAME));
1509 return mdp_copy_string(tmpString);
1515 char* mdp_copy_string(
const char* copyFrom)
1535 if (copyFrom == NULL) {
1538 cptr = (
char*) mdp_array_alloc(1, strlen(copyFrom) + 1,
sizeof(char));
1540 (void) strcpy(cptr, copyFrom);
1542 mdp_alloc_eh(
"mdp_copy_string",
sizeof(
char) * (strlen(copyFrom) + 1));
1550 void mdp_safe_copy_string(
char** string_hdl,
const char* copyFrom)
1569 if (string_hdl == NULL) {
1570 mdp_alloc_eh(
"mdp_safe_copy_string: string_hdl is NULL",
1571 MDP_ALLOC_INTERFACE_ERROR);
1574 if (*string_hdl != NULL) {
1575 mdp_safe_free((
void**) string_hdl);
1577 if (copyFrom == NULL) {
1581 *string_hdl = mdp_copy_string(copyFrom);
1582 if (*string_hdl == NULL) {
1583 mdp_alloc_eh2(
"mdp_safe_copy_string");