force all memory methods to return nullptr if requested size <= 0

This commit is contained in:
Steve Plimpton
2021-08-26 15:43:12 -06:00
parent e3af1544f1
commit 21b017368b

View File

@ -27,7 +27,7 @@ class Memory : protected Pointers {
void sfree(void *);
void fail(const char *);
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create/grow/destroy vecs and multidim arrays with contiguous memory blocks
only use with primitive data types, e.g. 1d vec of ints, 2d array of doubles
fail() prevents use with pointers,
@ -36,12 +36,14 @@ class Memory : protected Pointers {
for these other cases, use smalloc/srealloc/sfree directly
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 1d array
------------------------------------------------------------------------- */
template <typename TYPE> TYPE *create(TYPE *&array, int n, const char *name)
{
if (n <= 0) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * n;
array = (TYPE *) smalloc(nbytes, name);
return array;
@ -53,12 +55,17 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
grow or shrink 1d array
------------------------------------------------------------------------- */
template <typename TYPE> TYPE *grow(TYPE *&array, int n, const char *name)
{
if (n <= 0) {
destroy(array);
return nullptr;
}
if (array == nullptr) return create(array, n, name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n;
@ -72,7 +79,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 1d array
------------------------------------------------------------------------- */
@ -82,13 +89,15 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 1d array with index from nlo to nhi inclusive
cannot grow it
------------------------------------------------------------------------- */
template <typename TYPE> TYPE *create1d_offset(TYPE *&array, int nlo, int nhi, const char *name)
{
if (nlo > nhi) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * (nhi - nlo + 1);
array = (TYPE *) smalloc(nbytes, name);
array -= nlo;
@ -102,7 +111,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 1d array with index offset
------------------------------------------------------------------------- */
@ -112,12 +121,14 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 2d array
------------------------------------------------------------------------- */
template <typename TYPE> TYPE **create(TYPE **&array, int n1, int n2, const char *name)
{
if (n1 <= 0 || n2 <= 0) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2;
TYPE *data = (TYPE *) smalloc(nbytes, name);
nbytes = ((bigint) sizeof(TYPE *)) * n1;
@ -138,13 +149,18 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
grow or shrink 1st dim of a 2d array
last dim must stay the same
------------------------------------------------------------------------- */
template <typename TYPE> TYPE **grow(TYPE **&array, int n1, int n2, const char *name)
{
if (n1 <= 0 || n2 <= 0) {
destroy(array);
return nullptr;
}
if (array == nullptr) return create(array, n1, n2, name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2;
@ -167,7 +183,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 2d array
------------------------------------------------------------------------- */
@ -179,12 +195,14 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 2d array with a ragged 2nd dimension
------------------------------------------------------------------------- */
template <typename TYPE> TYPE **create_ragged(TYPE **&array, int n1, int *n2, const char *name)
{
if (n1 <= 0) return nullptr;
bigint n2sum = 0;
for (int i = 0; i < n1; i++) n2sum += n2[i];
@ -208,7 +226,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 2d array with 2nd index from n2lo to n2hi inclusive
cannot grow it
------------------------------------------------------------------------- */
@ -216,6 +234,8 @@ class Memory : protected Pointers {
template <typename TYPE>
TYPE **create2d_offset(TYPE **&array, int n1, int n2lo, int n2hi, const char *name)
{
if (n1 <= 0 || n2lo > n2hi) return nullptr;
int n2 = n2hi - n2lo + 1;
create(array, n1, n2, name);
for (int i = 0; i < n1; i++) array[i] -= n2lo;
@ -230,7 +250,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 2d array with 2nd index offset
------------------------------------------------------------------------- */
@ -242,12 +262,14 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 3d array
------------------------------------------------------------------------- */
template <typename TYPE> TYPE ***create(TYPE ***&array, int n1, int n2, int n3, const char *name)
{
if (n1 <= 0 || n2 <= 0 || n3 <= 0) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2 * n3;
TYPE *data = (TYPE *) smalloc(nbytes, name);
nbytes = ((bigint) sizeof(TYPE *)) * n1 * n2;
@ -276,13 +298,18 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
grow or shrink 1st dim of a 3d array
last 2 dims must stay the same
------------------------------------------------------------------------- */
template <typename TYPE> TYPE ***grow(TYPE ***&array, int n1, int n2, int n3, const char *name)
{
if (n1 <= 0 || n2 <= 0 || n3 <= 0) {
destroy(array);
return nullptr;
};
if (array == nullptr) return create(array, n1, n2, n3, name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2 * n3;
@ -313,7 +340,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 3d array
------------------------------------------------------------------------- */
@ -326,7 +353,7 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 3d array with 1st index from n1lo to n1hi inclusive
cannot grow it
------------------------------------------------------------------------- */
@ -334,6 +361,8 @@ class Memory : protected Pointers {
template <typename TYPE>
TYPE ***create3d_offset(TYPE ***&array, int n1lo, int n1hi, int n2, int n3, const char *name)
{
if (n1lo > n1hi || n2 <= 0 || n3 <= 0) return nullptr;
int n1 = n1hi - n1lo + 1;
create(array, n1, n2, n3, name);
array -= n1lo;
@ -348,7 +377,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
free a 3d array with 1st index offset
------------------------------------------------------------------------- */
@ -361,7 +390,7 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 3d array with
1st index from n1lo to n1hi inclusive,
2nd index from n2lo to n2hi inclusive,
@ -373,6 +402,8 @@ class Memory : protected Pointers {
TYPE ***create3d_offset(TYPE ***&array, int n1lo, int n1hi, int n2lo, int n2hi, int n3lo,
int n3hi, const char *name)
{
if (n1lo > n1hi || n2lo > n2hi || n3lo > n3hi) return nullptr;
int n1 = n1hi - n1lo + 1;
int n2 = n2hi - n2lo + 1;
int n3 = n3hi - n3lo + 1;
@ -393,7 +424,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
free a 3d array with all 3 indices offset
------------------------------------------------------------------------- */
@ -407,13 +438,15 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 4d array
------------------------------------------------------------------------- */
template <typename TYPE>
TYPE ****create(TYPE ****&array, int n1, int n2, int n3, int n4, const char *name)
{
if (n1 <= 0 || n2 <= 0 || n3 <= 0 || n4 <= 0) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2 * n3 * n4;
TYPE *data = (TYPE *) smalloc(nbytes, name);
nbytes = ((bigint) sizeof(TYPE *)) * n1 * n2 * n3;
@ -451,7 +484,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
grow or shrink 1st dim of a 4d array
last 3 dims must stay the same
------------------------------------------------------------------------- */
@ -459,6 +492,11 @@ class Memory : protected Pointers {
template <typename TYPE>
TYPE ****grow(TYPE ****&array, int n1, int n2, int n3, int n4, const char *name)
{
if (n1 <= 0 || n2 <= 0 || n3 <= 0 || n4 <= 0) {
destroy(array);
return nullptr;
}
if (array == nullptr) return create(array, n1, n2, n3, n4, name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2 * n3 * n4;
@ -498,7 +536,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 4d array
------------------------------------------------------------------------- */
@ -512,7 +550,7 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 4d array with indices
2nd index from n2lo to n2hi inclusive
3rd index from n3lo to n3hi inclusive
@ -524,6 +562,8 @@ class Memory : protected Pointers {
TYPE ****create4d_offset(TYPE ****&array, int n1, int n2lo, int n2hi, int n3lo, int n3hi,
int n4lo, int n4hi, const char *name)
{
if (n1 <= 0 || n2lo > n2hi || n3lo > n3hi || n4lo > n4hi) return nullptr;
int n2 = n2hi - n2lo + 1;
int n3 = n3hi - n3lo + 1;
int n4 = n4hi - n4lo + 1;
@ -545,8 +585,8 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
free a 4d array with indices 2,3, and 4 offset
/* ----------------------------------------------------------------------
free a 4d array with indices 2,3,4 offset
------------------------------------------------------------------------- */
template <typename TYPE>
@ -560,13 +600,15 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
create a 5d array
------------------------------------------------------------------------- */
template <typename TYPE>
TYPE *****create(TYPE *****&array, int n1, int n2, int n3, int n4, int n5, const char *name)
{
if (n1 <= 0 || n2 <= 0 || n3 <= 0 || n4 <= 0 || n5 <= 0) return nullptr;
bigint nbytes = ((bigint) sizeof(TYPE)) * n1 * n2 * n3 * n4 * n5;
TYPE *data = (TYPE *) smalloc(nbytes, name);
nbytes = ((bigint) sizeof(TYPE *)) * n1 * n2 * n3 * n4;
@ -611,7 +653,7 @@ class Memory : protected Pointers {
return nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
destroy a 5d array
------------------------------------------------------------------------- */
@ -626,7 +668,7 @@ class Memory : protected Pointers {
array = nullptr;
}
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
memory usage of arrays, including pointers
------------------------------------------------------------------------- */