18 #include "cantera/base/mdp_allo.h"
29 int MDP_MP_Nprocs = 1;
30 int MDP_MP_myproc = 0;
46 int MDP_ALLO_errorOption = 3;
48 inline int MinI(
const int& x,
const int& y)
50 return ((x < y) ? x : y);
52 inline int MaxI(
const int& x,
const int& y)
54 return ((x > y) ? x : y);
58 const int MDP_ALLOC_INTERFACE_ERROR = -230346;
64 static void mdp_alloc_eh(
const char*
const rname,
const int bytes)
83 if (bytes == MDP_ALLOC_INTERFACE_ERROR) {
85 sprintf(mesg,
"MDP_ALLOC Interface ERROR P_%d: %s", MDP_MP_my_proc,
88 sprintf(mesg,
"MDP_ALLOC Interface ERROR: %s", rname);
91 sprintf(mesg,
"%s ERROR: out of memory while mallocing %d bytes",
94 if (MDP_ALLO_errorOption % 2 == 1) {
95 fprintf(stderr,
"\n%s", mesg);
97 if (MDP_MP_Nprocs > 1) {
98 fprintf(stderr,
": proc = %d\n", MDP_MP_myproc);
100 fprintf(stderr,
"\n");
103 fprintf(stderr,
"\n");
107 if (MDP_ALLO_errorOption == 2 || MDP_ALLO_errorOption == 3) {
108 throw std::bad_alloc();
110 if (MDP_ALLO_errorOption == 4 || MDP_ALLO_errorOption == 5) {
113 if (MDP_ALLO_errorOption > 5) {
121 static void mdp_alloc_eh2(
const char*
const rname)
134 if (MDP_ALLO_errorOption == 1 ||
135 MDP_ALLO_errorOption == 3 ||
136 MDP_ALLO_errorOption == 5 ||
137 MDP_ALLO_errorOption == 7) {
139 if (MDP_MP_Nprocs > 1) {
140 fprintf(stderr,
": proc = %d, %s ERROR: returning with null pointer\n",
141 MDP_MP_myproc, rname);
143 fprintf(stderr,
"%s ERROR: returning with null pointer", rname);
146 fprintf(stderr,
"%s ERROR: returning with null pointer", rname);
155 #ifndef HAVE_ARRAY_ALLOC
157 static double* smalloc(
size_t n);
232 double* mdp_array_alloc(
int numdim, ...)
254 va_start(va, numdim);
257 (void) fprintf(stderr,
258 "mdp_array_alloc ERROR: number of dimensions, %d, is <=0\n",
261 }
else if (numdim > 4) {
262 (void) fprintf(stderr,
263 "mdp_array_alloc ERROR: number of dimensions, %d, is > 4\n",
268 dim[0].index = va_arg(va,
int);
270 if (dim[0].index <= 0) {
272 (void) fprintf(stderr,
"WARNING: mdp_array_alloc called with first "
273 "dimension <= 0, %d\n\twill return the nil pointer\n",
274 (
int)(dim[0].index));
276 return((
double*) NULL);
279 dim[0].total = dim[0].index;
280 dim[0].size =
sizeof(
void*);
282 for (i = 1; i < numdim; i++) {
283 dim[i].index = va_arg(va,
int);
284 if (dim[i].index <= 0) {
285 (void) fprintf(stderr,
286 "WARNING: mdp_array_alloc called with dimension %d <= 0, "
287 "%d\n", i+1, (
int)(dim[i].index));
288 fprintf(stderr,
"\twill return the nil pointer\n");
289 return((
double*) NULL);
291 dim[i].total = dim[i-1].total * dim[i].index;
292 dim[i].size =
sizeof(
void*);
293 dim[i].off = dim[i-1].off + dim[i-1].total * dim[i-1].size;
296 dim[numdim-1].size = va_arg(va,
int);
303 dim[numdim-1].off = dim[numdim-1].size *
304 ((dim[numdim-1].off+dim[numdim-1].size-1)/dim[numdim-1].size);
306 total = dim[numdim-1].off + dim[numdim-1].total * dim[numdim-1].size;
308 dfield = (
double*) smalloc((
size_t) total);
309 field = (
char*) dfield;
311 for (i = 0; i < numdim - 1; i++) {
312 ptr = (
char**)(field + dim[i].off);
313 data = (
char*)(field + dim[i+1].off);
314 for (j = 0; j < dim[i].total; j++) {
315 ptr[j] = data + j * dim[i+1].size * dim[i+1].index;
325 static double* smalloc(
size_t n)
335 static int firsttime = 1;
344 pntr = (
double*) malloc((
size_t) n);
346 if (pntr == NULL && n != 0) {
347 if (MDP_ALLO_errorOption == 7 ||
348 MDP_ALLO_errorOption == 5 || MDP_ALLO_errorOption == 3 ||
349 MDP_ALLO_errorOption == 1) {
350 fprintf(stderr,
"smalloc : Out of space - number of bytes "
351 "requested = %d\n", (
int) n);
357 file = fopen(
"memops.txt",
"w");
359 file = fopen(
"memops.txt",
"a");
361 fprintf(file,
"%x %d malloc\n", pntr, n);
370 void mdp_safe_free(
void** ptr)
388 mdp_alloc_eh(
"mdp_safe_free: handle is NULL", MDP_ALLOC_INTERFACE_ERROR);
392 file = fopen(
"memops.txt",
"a");
393 Fprintf(file,
"%x free\n", *ptr);
395 if ((
int) *ptr == 0x00000001) {
396 Fprintf(stderr,
"FOUND IT!\n");
464 int* mdp_alloc_int_1(
int nvalues,
const int val)
486 array= (
int*) mdp_array_alloc(1, nvalues,
sizeof(
int));
488 if (val != MDP_INT_NOINIT) {
490 (void) memset(array, 0,
sizeof(
int)*nvalues);
492 for (
int i = 0; i < nvalues; i++) {
498 mdp_alloc_eh(
"mdp_alloc_int_1", nvalues *
sizeof(
int));
506 void mdp_safe_alloc_int_1(
int** array_hdl,
int nvalues,
const int val)
527 if (array_hdl == NULL) {
528 mdp_alloc_eh(
"mdp_safe_alloc_int_1: handle is NULL",
529 MDP_ALLOC_INTERFACE_ERROR);
532 if (*array_hdl != NULL) {
533 mdp_safe_free((
void**) array_hdl);
535 *array_hdl = mdp_alloc_int_1(nvalues, val);
536 if (*array_hdl == NULL) {
537 mdp_alloc_eh2(
"mdp_safe_alloc_int_1");
545 mdp_realloc_int_1(
int** array_hdl,
int new_length,
int old_length,
568 if (new_length == old_length) {
571 if (new_length <= 0) {
574 "Warning: mdp_realloc_int_1 P_%d: called with n = %d\n",
575 MDP_MP_myproc, new_length);
578 "Warning: mdp_realloc_int_1: called with n = %d\n",
583 if (old_length < 0) {
586 if (new_length == old_length) {
589 size_t bytenum = new_length *
sizeof(int);
590 int* array = (
int*) smalloc(bytenum);
593 if (old_length > 0) {
594 bytenum =
sizeof(int) * old_length;
598 if (new_length < old_length) {
599 bytenum =
sizeof(int) * new_length;
602 (void) memcpy((
void*) array, (
const void*) *array_hdl, bytenum);
604 mdp_safe_free((
void**) array_hdl);
609 if ((defval != MDP_INT_NOINIT) && (new_length > old_length)) {
611 bytenum =
sizeof(int) * (new_length - old_length);
612 (void) memset((
void*)(array+old_length), 0, bytenum);
614 for (
int i = old_length; i < new_length; i++) {
620 mdp_alloc_eh(
"mdp_realloc_int_1", static_cast<int>(bytenum));
627 int** mdp_alloc_int_2(
int ndim1,
int ndim2,
const int val)
654 array = (
int**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
int));
656 if (val != MDP_INT_NOINIT) {
658 (void) memset((
void*) array[0], 0, ndim1 * ndim2 *
sizeof(int));
660 dptr = &(array[0][0]);
661 for (i = 0; i < ndim1 * ndim2; i++) {
667 mdp_alloc_eh(
"mdp_alloc_int_2",
668 sizeof(
int) * ndim1 * ndim2 +
669 ndim1 *
sizeof(
void*));
677 double* mdp_alloc_dbl_1(
int nvalues,
const double val)
700 array = (
double*) mdp_array_alloc(1, nvalues,
sizeof(
double));
702 if (val != MDP_DBL_NOINIT) {
704 (void) memset((
void*) array, 0, nvalues *
sizeof(double));
706 for (i = 0; i < nvalues; i++) {
712 mdp_alloc_eh(
"mdp_alloc_dbl_1", nvalues *
sizeof(
double));
720 void mdp_safe_alloc_dbl_1(
double** array_hdl,
int nvalues,
const double val)
741 if (array_hdl == NULL) {
742 mdp_alloc_eh(
"mdp_safe_alloc_dbl_1: handle is NULL",
743 MDP_ALLOC_INTERFACE_ERROR);
746 if (*array_hdl != NULL) {
747 mdp_safe_free((
void**) array_hdl);
749 *array_hdl = mdp_alloc_dbl_1(nvalues, val);
750 if (*array_hdl == NULL) {
751 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_1");
758 void mdp_realloc_dbl_1(
double** array_hdl,
int new_length,
759 int old_length,
const double defval)
781 if (new_length == old_length) {
784 if (new_length <= 0) {
786 fprintf(stderr,
"Warning: mdp_realloc_dbl_1 P_%d: called with n = %d\n",
787 MDP_MP_myproc, new_length);
789 fprintf(stderr,
"Warning: mdp_realloc_dbl_1: called with n = %d\n",
794 if (old_length < 0) {
797 if (new_length == old_length) {
800 size_t bytenum = new_length *
sizeof(double);
801 double* array = (
double*) smalloc(bytenum);
804 if (old_length > 0) {
805 bytenum =
sizeof(double) * old_length;
809 if (new_length < old_length) {
810 bytenum =
sizeof(double) * new_length;
812 (void) memcpy((
void*) array, (
const void*) *array_hdl, bytenum);
813 mdp_safe_free((
void**) array_hdl);
818 if ((defval != MDP_DBL_NOINIT) && (new_length > old_length)) {
820 bytenum =
sizeof(double) * (new_length - old_length);
821 (void) memset((
void*)(array+old_length), 0, bytenum);
823 for (
int i = old_length; i < new_length; i++) {
829 mdp_alloc_eh(
"mdp_realloc_dbl_1", static_cast<int>(bytenum));
836 double** mdp_alloc_dbl_2(
int ndim1,
int ndim2,
const double val)
862 double** array, *dptr;
869 array = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
871 if (val != MDP_DBL_NOINIT) {
873 (void) memset((
void*) array[0], 0, ndim1*ndim2 *
sizeof(double));
875 dptr = &(array[0][0]);
876 for (i = 0; i < ndim1*ndim2; i++) {
882 mdp_alloc_eh(
"mdp_alloc_dbl_2",
883 sizeof(
double) * ndim1 * ndim2 +
884 ndim1 *
sizeof(
void*));
892 char* mdp_alloc_char_1(
int nvalues,
const char val)
915 array = (
char*) mdp_array_alloc(1, nvalues,
sizeof(
char));
918 (void) memset((
void*)array, 0,
sizeof(char)*nvalues);
920 for (i = 0; i < nvalues; i++) {
925 mdp_alloc_eh(
"mdp_alloc_char_1", nvalues *
sizeof(
char));
933 void mdp_safe_alloc_char_1(
char** array_hdl,
int nvalues,
const char val)
959 if (array_hdl == NULL) {
960 mdp_alloc_eh(
"mdp_safe_alloc_char_1: handle is NULL",
961 MDP_ALLOC_INTERFACE_ERROR);
964 if (*array_hdl != NULL) {
965 mdp_safe_free((
void**) array_hdl);
967 *array_hdl = mdp_alloc_char_1(nvalues, val);
968 if (*array_hdl == NULL) {
969 mdp_alloc_eh2(
"mdp_safe_alloc_char_1");
978 void mdp_safe_alloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
1001 if (array_hdl == NULL) {
1002 mdp_alloc_eh(
"mdp_safe_alloc_dbl_2: handle is NULL",
1003 MDP_ALLOC_INTERFACE_ERROR);
1006 if (*array_hdl != NULL) {
1007 mdp_safe_free((
void**) array_hdl);
1009 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
1010 if (*array_hdl == NULL) {
1011 mdp_alloc_eh2(
"mdp_safe_alloc_dbl_2");
1018 void mdp_realloc_dbl_2(
double** *array_hdl,
int ndim1,
int ndim2,
1019 int ndim1Old,
int ndim2Old,
const double val)
1056 ndim1Old = MaxI(ndim1Old, 0);
1057 ndim2Old = MaxI(ndim2Old, 0);
1062 if ((*array_hdl == NULL) || (ndim1Old <= 0 && ndim2Old <= 0)) {
1063 mdp_safe_free((
void**) array_hdl);
1064 *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val);
1065 if (*array_hdl == NULL) {
1066 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1073 double** array_old = *array_hdl;
1074 *array_hdl = (
double**) mdp_array_alloc(2, ndim1, ndim2,
sizeof(
double));
1075 if (*array_hdl == NULL) {
1076 mdp_alloc_eh2(
"mdp_realloc_dbl_2");
1081 int ndim1Min = MinI(ndim1, ndim1Old);
1082 int ndim2Min = MinI(ndim2, ndim2Old);
1083 double** array_new = *array_hdl;
1088 if (ndim2 == ndim2Old) {
1089 size_t sz = ndim1Min * ndim2 *
sizeof(double);
1090 (void) memcpy((
void*) array_new[0], (
const void*) array_old[0], sz);
1097 size_t sz = ndim2Min *
sizeof(double);
1098 size_t sz2 = (ndim2 - ndim2Min) *
sizeof(
double);
1099 for (
int i = 0; i < ndim1Min; i++) {
1100 (void) memcpy((
void*) array_new[i],
1101 (
const void*) array_old[i], sz);
1102 if (ndim2 > ndim2Min && val != MDP_DBL_NOINIT) {
1104 (void) memset((
void*)(array_new[i] + ndim2Min), 0, sz2);
1106 double* dptr = array_new[i];
1107 for (
int j = ndim2Min; j < ndim2; j++) {
1117 if (ndim1 > ndim1Min && val != MDP_DBL_NOINIT) {
1119 size_t sz = (ndim1 - ndim1Min) * ndim2 *
sizeof(
double);
1120 (void) memset((
void*) array_new[ndim1Min], 0, sz);
1122 double* dptr = array_new[ndim1Min];
1123 int num = (ndim1 - ndim1Min) * ndim2;
1124 for (
int i = 0; i < num; i++) {
1132 mdp_safe_free((
void**) &array_old);
1140 char** mdp_alloc_VecFixedStrings(
int numStrings,
int lenString)
1162 if (numStrings <= 0) {
1165 if (lenString <= 0) {
1168 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1169 if (array != NULL) {
1170 for (i = 0; i < numStrings; i++) {
1172 array[i][lenString - 1] =
'\0';
1175 mdp_alloc_eh(
"mdp_alloc_VecFixedStrings",
1176 sizeof(
char) * numStrings * lenString +
1177 numStrings *
sizeof(
void*));
1185 void mdp_realloc_VecFixedStrings(
char** *array_hdl,
int numStrings,
1186 int numOldStrings,
int lenString)
1208 if (numStrings <= 0) {
1211 if (numStrings == numOldStrings) {
1214 if (lenString <= 0) {
1217 array = (
char**) mdp_array_alloc(2, numStrings, lenString,
sizeof(
char));
1218 if (array != NULL) {
1219 int len = MinI(numStrings, numOldStrings);
1222 for (i = 0; i < len; i++) {
1223 strncpy(array[i], ao[i], lenString);
1226 if (numStrings > numOldStrings) {
1227 for (i = numOldStrings; i < numStrings; i++) {
1229 array[i][lenString - 1] =
'\0';
1232 mdp_safe_free((
void**) array_hdl);
1236 mdp_alloc_eh(
"mdp_realloc_VecFixedStrings",
1237 sizeof(
char) * numStrings * lenString +
1238 numStrings *
sizeof(
void*));
1245 void mdp_safe_alloc_VecFixedStrings(
char** *array_hdl,
1246 int numStrings,
int lenString)
1268 if (array_hdl == NULL) {
1269 mdp_alloc_eh(
"mdp_safe_alloc_VecFixedStrings: handle is NULL",
1270 MDP_ALLOC_INTERFACE_ERROR);
1273 if (*array_hdl != NULL) {
1274 mdp_safe_free((
void**) array_hdl);
1276 *array_hdl = mdp_alloc_VecFixedStrings(numStrings, lenString);
1277 if (*array_hdl == NULL) {
1278 mdp_alloc_eh2(
"mdp_safe_alloc_VecFixedStrings");
1285 void** mdp_alloc_ptr_1(
int numPointers)
1306 if (numPointers <= 0) {
1309 array = (
void**) mdp_array_alloc(1, numPointers,
sizeof(
void*));
1310 if (array != NULL) {
1311 for (i = 0; i < numPointers; i++) {
1315 mdp_alloc_eh(
"mdp_alloc_ptr_1",
1316 sizeof(
void*) * numPointers);
1324 void mdp_safe_alloc_ptr_1(
void** *array_hdl,
int numPointers)
1346 if (array_hdl == NULL) {
1347 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1348 MDP_ALLOC_INTERFACE_ERROR);
1351 if (*array_hdl != NULL) {
1352 mdp_safe_free((
void**) array_hdl);
1354 *array_hdl = mdp_alloc_ptr_1(numPointers);
1355 if (*array_hdl == NULL) {
1356 mdp_alloc_eh2(
"mdp_safe_alloc_ptr_1");
1363 void mdp_realloc_ptr_1(
void** *array_hdl,
int numLen,
int numOldLen)
1381 if (array_hdl == NULL) {
1382 mdp_alloc_eh(
"mdp_safe_alloc_ptr_1: handle is NULL",
1383 MDP_ALLOC_INTERFACE_ERROR);
1389 if (numOldLen < 0) {
1392 if (numLen == numOldLen) {
1395 size_t bytenum =
sizeof(
void*) * numLen;
1396 void** array = (
void**) smalloc(bytenum);
1397 if (array != NULL) {
1398 int len = MinI(numLen, numOldLen);
1400 void** ao = *array_hdl;
1401 for (
int i = 0; i < len; i++) {
1407 if (numLen > numOldLen) {
1408 bytenum =
sizeof(
void*) * (numLen - numOldLen);
1409 (void) memset((
void*)(array + numOldLen), 0, bytenum);
1411 mdp_safe_free((
void**) array_hdl);
1414 mdp_alloc_eh(
"mdp_realloc_ptr_1",
sizeof(
void*) * numLen);
1422 const char**
const copyFrom,
1423 int numStrings,
size_t maxLenString)
1444 if (maxLenString > 0) {
1447 for (
int i = 0; i < numStrings; i++) {
1449 dFrom = copyFrom[i];
1451 mdp_alloc_eh2(
"mdp_copy_VecFixedStrings");
1454 mdp_alloc_eh2(
"mdp_copy_VecFixedStrings");
1456 (void) strncpy(dTo, dFrom, maxLenString);
1457 dTo[maxLenString-1] =
'\0';
1465 char* mdp_copy_string(
const char*
const copyFrom)
1485 if (copyFrom == NULL) {
1488 cptr = (
char*) mdp_array_alloc(1, strlen(copyFrom) + 1,
sizeof(char));
1490 (void) strcpy(cptr, copyFrom);
1492 mdp_alloc_eh(
"mdp_copy_string",
1493 static_cast<int>(
sizeof(
char) * (strlen(copyFrom) + 1)));
1501 void mdp_safe_copy_string(
char** string_hdl,
const char* copyFrom)
1520 if (string_hdl == NULL) {
1521 mdp_alloc_eh(
"mdp_safe_copy_string: string_hdl is NULL",
1522 MDP_ALLOC_INTERFACE_ERROR);
1525 if (*string_hdl != NULL) {
1526 mdp_safe_free((
void**) string_hdl);
1528 if (copyFrom == NULL) {
1532 *string_hdl = mdp_copy_string(copyFrom);
1533 if (*string_hdl == NULL) {
1534 mdp_alloc_eh2(
"mdp_safe_copy_string");
1543 const double*
const copyFrom,
const int len)
1563 (void) memcpy((
void*)copyTo, (
const void*)copyFrom, len *
sizeof(
double));
1571 const double**
const copyFrom,
const int len1,
1592 if (len1 > 0 && len2 > 0) {
1593 size_t bytelen = len1 * len2 *
sizeof(double);
1594 const double*
const dFrom = copyFrom[0];
1595 double*
const dTo = copyTo[0];
1596 (void) memcpy((
void*)dTo, (
const void*)dFrom, bytelen);
1604 const int*
const copyFrom,
int len)
1624 size_t bytelen = len *
sizeof(int);
1625 (void) memcpy((
void*)copyTo, (
const void*)copyFrom, bytelen);
1633 const int**
const copyFrom,
const int len1,
1654 if (len1 > 0 && len2 > 0) {
1655 size_t bytelen = len1 * len2 *
sizeof(int);
1656 const int*
const dFrom = copyFrom[0];
1657 int*
const dTo = copyTo[0];
1658 (void) memcpy((
void*)dTo, (
const void*)dFrom, bytelen);
1666 const void*
const copyFrom,
int len)
1686 size_t bytelen = len *
sizeof(
void*);
1687 (void) memcpy((
void*)copyTo, (
const void*)copyFrom, bytelen);
1714 void** array = mdp_alloc_ptr_1(len);
1716 size_t bytelen = len *
sizeof(
void*);
1717 (void) memcpy((
void*)array, (
const void*)copyFrom, bytelen);
1745 (void) memset((
void*)v, 0, len *
sizeof(double));
1749 for (
int i = 0; i < m; i++) {
1756 for (
int i = m; i < len; i += 7) {
1787 (void) memset((
void*)v, 0, len *
sizeof(double));
1810 (void) memset((
void*)v, 0, len *
sizeof(int));
1835 int len = len1 * len2;
1836 if (len > 0 && len1 > 0 && v) {
1837 double*
const dstart = v[0];
1839 size_t bytelen = len *
sizeof(double);
1840 (void) memset((
void*)dstart, 0, bytelen);
1844 for (
int i = 0; i < m; i++) {
1851 for (
int i = m; i < len; i += 7) {
1853 dstart[i+1] = value;
1854 dstart[i+2] = value;
1855 dstart[i+3] = value;
1856 dstart[i+4] = value;
1857 dstart[i+5] = value;
1858 dstart[i+6] = value;
1885 size_t bytelen = len *
sizeof(int);
1886 (void) memset((
void*)v, 0, bytelen);
1890 for (
int i = 0; i < m; i++) {
1897 for (
int i = m; i < len; i += 7) {