git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@5795 f3b2605a-c512-4ea7-a41b-209d697bcdaa

This commit is contained in:
sjplimp
2011-03-17 16:06:28 +00:00
parent 09c391fb2d
commit 19890f3846
2 changed files with 140 additions and 117 deletions

View File

@ -29,13 +29,36 @@ Memory::Memory(LAMMPS *lmp) : Pointers(lmp) {}
safe malloc safe malloc
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void *Memory::smalloc(int n, const char *name) void *Memory::smalloc(bigint nbytes, const char *name)
{ {
if (n == 0) return NULL; if (nbytes == 0) return NULL;
void *ptr = malloc(n);
void *ptr = malloc(nbytes);
if (ptr == NULL) { if (ptr == NULL) {
char str[128]; char str[128];
sprintf(str,"Failed to allocate %d bytes for array %s",n,name); sprintf(str,"Failed to allocate " BIGINT_FORMAT "bytes for array %s",
nbytes,name);
error->one(str);
}
return ptr;
}
/* ----------------------------------------------------------------------
safe realloc
------------------------------------------------------------------------- */
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
{
if (nbytes == 0) {
sfree(ptr);
return NULL;
}
ptr = realloc(ptr,nbytes);
if (ptr == NULL) {
char str[128];
sprintf(str,"Failed to reallocate " BIGINT_FORMAT "bytes for array %s",
nbytes,name);
error->one(str); error->one(str);
} }
return ptr; return ptr;
@ -52,68 +75,18 @@ void Memory::sfree(void *ptr)
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
safe realloc erroneous usage of templated create/grow functions
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void *Memory::srealloc(void *ptr, int n, const char *name) void Memory::fail(const char *name)
{ {
if (n == 0) {
sfree(ptr);
return NULL;
}
ptr = realloc(ptr,n);
if (ptr == NULL) {
char str[128]; char str[128];
sprintf(str,"Failed to reallocate %d bytes for array %s",n,name); sprintf(str,"Cannot create/grow a vector/array of pointers for %s",name);
error->one(str); error->one(str);
}
return ptr;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
newer routines older routines, will be deprecated at some point
------------------------------------------------------------------------- */
void *Memory::smalloc_new(bigint nbytes, const char *name)
{
if (nbytes == 0) return NULL;
void *ptr = malloc(nbytes);
if (ptr == NULL) {
char str[128];
sprintf(str,"Failed to allocate " BIGINT_FORMAT "bytes for array %s",
nbytes,name);
error->one(str);
}
return ptr;
}
void *Memory::srealloc_new(void *ptr, bigint nbytes, const char *name)
{
if (nbytes == 0) {
sfree_new(ptr);
return NULL;
}
ptr = realloc(ptr,nbytes);
if (ptr == NULL) {
char str[128];
sprintf(str,"Failed to reallocate " BIGINT_FORMAT "bytes for array %s",
nbytes,name);
error->one(str);
}
return ptr;
}
void Memory::sfree_new(void *ptr)
{
if (ptr == NULL) return;
free(ptr);
}
/* ----------------------------------------------------------------------
older routines
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------

View File

@ -14,6 +14,7 @@
#ifndef LMP_MEMORY_H #ifndef LMP_MEMORY_H
#define LMP_MEMORY_H #define LMP_MEMORY_H
#include "error.h"
#include "pointers.h" #include "pointers.h"
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -22,11 +23,12 @@ class Memory : protected Pointers {
public: public:
Memory(class LAMMPS *); Memory(class LAMMPS *);
// older routines void *smalloc(bigint n, const char *);
void *srealloc(void *, bigint n, const char *);
void *smalloc(int n, const char *);
void sfree(void *); void sfree(void *);
void *srealloc(void *, int n, const char *); void fail(const char *);
// older routines, will be deprecated at some point
double *create_1d_double_array(int, int, const char *); double *create_1d_double_array(int, int, const char *);
void destroy_1d_double_array(double *, int); void destroy_1d_double_array(double *, int);
@ -60,16 +62,12 @@ class Memory : protected Pointers {
// newer routines // newer routines
public:
void *smalloc_new(bigint n, const char *);
void *srealloc_new(void *, bigint n, const char *);
void sfree_new(void *);
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
to avoid code bloat, only use these for int,double,float,char create/grow/destroy vecs and multidim arrays with contiguous memory blocks
not for int* or double** or arbitrary structs only use with primitive data types, e.g. 1d vec of ints, 2d array of doubles
in those cases, just use smalloc,srealloc,sfree directly cannot use with pointers, e.g. 1d vec of int*, due to mismatched destroy
avoid use with non-primitive data types to avoid code bloat
for these other cases, use smalloc/srealloc/sfree directly
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -80,10 +78,13 @@ class Memory : protected Pointers {
TYPE *create(TYPE *&array, int n, const char *name) TYPE *create(TYPE *&array, int n, const char *name)
{ {
bigint nbytes = sizeof(TYPE) * n; bigint nbytes = sizeof(TYPE) * n;
array = (TYPE *) smalloc_new(nbytes,name); array = (TYPE *) smalloc(nbytes,name);
return array; return array;
}; };
template <typename TYPE>
TYPE **create(TYPE **&array, int n, const char *name) {fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
grow or shrink 1d array grow or shrink 1d array
if dim is 0, return NULL if dim is 0, return NULL
@ -99,10 +100,13 @@ class Memory : protected Pointers {
} }
bigint nbytes = sizeof(TYPE) * n; bigint nbytes = sizeof(TYPE) * n;
array = (TYPE *) srealloc_new(array,nbytes,name); array = (TYPE *) srealloc(array,nbytes,name);
return array; return array;
}; };
template <typename TYPE>
TYPE **grow(TYPE **&array, int n, const char *name) {fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 1d array destroy a 1d array
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -110,30 +114,35 @@ class Memory : protected Pointers {
template <typename TYPE> template <typename TYPE>
void destroy(TYPE *array) void destroy(TYPE *array)
{ {
sfree_new(array); sfree(array);
}; };
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
create a 1d array with index from nlo to nhi inclusive create a 1d array with index from nlo to nhi inclusive
cannot grow it
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
TYPE *create(TYPE *&array, int nlo, int nhi, const char *name) TYPE *create1d_offset(TYPE *&array, int nlo, int nhi, const char *name)
{ {
bigint nbytes = sizeof(TYPE) * (nhi-nlo+1); bigint nbytes = sizeof(TYPE) * (nhi-nlo+1);
array = (TYPE *) smalloc_new(nbytes,name); array = (TYPE *) smalloc(nbytes,name);
array = array-nlo; array = array-nlo;
return array; return array;
} }
template <typename TYPE>
TYPE **create1d_offset(TYPE **&array, int nlo, int nhi, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 1d array with index offset destroy a 1d array with index offset
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
void destroy(TYPE *array, int offset) void destroy1d_offset(TYPE *array, int offset)
{ {
if (array) sfree_new(array+offset); if (array) sfree(array+offset);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -144,9 +153,9 @@ class Memory : protected Pointers {
TYPE **create(TYPE **&array, int n1, int n2, const char *name) TYPE **create(TYPE **&array, int n1, int n2, const char *name)
{ {
bigint nbytes = sizeof(TYPE) * n1*n2; bigint nbytes = sizeof(TYPE) * n1*n2;
TYPE *data = (TYPE *) smalloc_new(nbytes,name); TYPE *data = (TYPE *) smalloc(nbytes,name);
nbytes = sizeof(TYPE *) * n1; nbytes = sizeof(TYPE *) * n1;
array = (TYPE **) smalloc_new(nbytes,name); array = (TYPE **) smalloc(nbytes,name);
int n = 0; int n = 0;
for (int i = 0; i < n1; i++) { for (int i = 0; i < n1; i++) {
@ -156,6 +165,10 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE ***create(TYPE ***&array, int n1, int n2, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
grow or shrink 1st dim of a 2d array grow or shrink 1st dim of a 2d array
last dim must stay the same last dim must stay the same
@ -172,9 +185,9 @@ class Memory : protected Pointers {
} }
bigint nbytes = sizeof(TYPE) * n1*n2; bigint nbytes = sizeof(TYPE) * n1*n2;
TYPE *data = (TYPE *) srealloc_new(array[0],nbytes,name); TYPE *data = (TYPE *) srealloc(array[0],nbytes,name);
nbytes = sizeof(TYPE *) * n1; nbytes = sizeof(TYPE *) * n1;
array = (TYPE **) srealloc_new(array,nbytes,name); array = (TYPE **) srealloc(array,nbytes,name);
int n = 0; int n = 0;
for (int i = 0; i < n1; i++) { for (int i = 0; i < n1; i++) {
@ -184,6 +197,10 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE ***grow(TYPE ***&array, int n1, int n2, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 2d array destroy a 2d array
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -192,16 +209,17 @@ class Memory : protected Pointers {
void destroy(TYPE **array) void destroy(TYPE **array)
{ {
if (array == NULL) return; if (array == NULL) return;
sfree_new(array[0]); sfree(array[0]);
sfree_new(array); sfree(array);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
create a 2d array with 2nd index from n2lo to n2hi inclusive create a 2d array with 2nd index from n2lo to n2hi inclusive
cannot grow it
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
TYPE **create(TYPE **&array, int n1, int n2lo, int n2hi, TYPE **create2d_offset(TYPE **&array, int n1, int n2lo, int n2hi,
const char *name) const char *name)
{ {
int n2 = n2hi - n2lo + 1; int n2 = n2hi - n2lo + 1;
@ -210,16 +228,20 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE ***create2d_offset(TYPE ***&array, int n1, int n2lo, int n2hi,
const char *name) {fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 2d array with 2nd index offset destroy a 2d array with 2nd index offset
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
void destroy(TYPE **array, int offset) void destroy2d_offset(TYPE **array, int offset)
{ {
if (array == NULL) return; if (array == NULL) return;
sfree_new(&array[0][offset]); sfree(&array[0][offset]);
sfree_new(array); sfree(array);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -230,11 +252,11 @@ class Memory : protected Pointers {
TYPE ***create(TYPE ***&array, int n1, int n2, int n3, const char *name) TYPE ***create(TYPE ***&array, int n1, int n2, int n3, const char *name)
{ {
bigint nbytes = sizeof(TYPE) * n1*n2*n3; bigint nbytes = sizeof(TYPE) * n1*n2*n3;
TYPE *data = (TYPE *) smalloc_new(nbytes,name); TYPE *data = (TYPE *) smalloc(nbytes,name);
nbytes = sizeof(TYPE *) * n1*n2; nbytes = sizeof(TYPE *) * n1*n2;
TYPE **plane = (TYPE **) smalloc_new(nbytes,name); TYPE **plane = (TYPE **) smalloc(nbytes,name);
nbytes = sizeof(TYPE **) * n1; nbytes = sizeof(TYPE **) * n1;
array = (TYPE ***) smalloc_new(nbytes,name); array = (TYPE ***) smalloc(nbytes,name);
int i,j; int i,j;
int n = 0; int n = 0;
@ -248,6 +270,10 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE ****create(TYPE ****&array, int n1, int n2, int n3, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
grow or shrink 1st dim of a 3d array grow or shrink 1st dim of a 3d array
last 2 dims must stay the same last 2 dims must stay the same
@ -264,11 +290,11 @@ class Memory : protected Pointers {
} }
bigint nbytes = sizeof(TYPE) * n1*n2*n3; bigint nbytes = sizeof(TYPE) * n1*n2*n3;
TYPE *data = (TYPE *) srealloc_new(array[0][0],nbytes,name); TYPE *data = (TYPE *) srealloc(array[0][0],nbytes,name);
nbytes = sizeof(TYPE *) * n1*n2; nbytes = sizeof(TYPE *) * n1*n2;
TYPE **plane = (TYPE **) srealloc_new(array[0],nbytes,name); TYPE **plane = (TYPE **) srealloc(array[0],nbytes,name);
nbytes = sizeof(TYPE **) * n1; nbytes = sizeof(TYPE **) * n1;
array = (TYPE ***) srealloc_new(array,nbytes,name); array = (TYPE ***) srealloc(array,nbytes,name);
int i,j; int i,j;
int n = 0; int n = 0;
@ -282,6 +308,10 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE ****grow(TYPE ****&array, int n1, int n2, int n3, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 3d array destroy a 3d array
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -290,17 +320,18 @@ class Memory : protected Pointers {
void destroy(TYPE ***array) void destroy(TYPE ***array)
{ {
if (array == NULL) return; if (array == NULL) return;
sfree_new(array[0][0]); sfree(array[0][0]);
sfree_new(array[0]); sfree(array[0]);
sfree_new(array); sfree(array);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
create a 3d array with 1st index from n1lo to n1hi inclusive create a 3d array with 1st index from n1lo to n1hi inclusive
cannot grow it
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
TYPE ***create(TYPE ***&array, int n1lo, int n1hi, TYPE ***create3d_offset(TYPE ***&array, int n1lo, int n1hi,
int n2, int n3, const char *name) int n2, int n3, const char *name)
{ {
int n1 = n1hi - n1lo + 1; int n1 = n1hi - n1lo + 1;
@ -308,12 +339,17 @@ class Memory : protected Pointers {
return array-n1lo; return array-n1lo;
} }
template <typename TYPE>
TYPE ****create3d_offset(TYPE ****&array, int n1lo, int n1hi,
int n2, int n3, const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
free a 3d array with 1st index offset free a 3d array with 1st index offset
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
void destroy(TYPE ***array, int offset) void destroy3d_offset(TYPE ***array, int offset)
{ {
if (array) destroy(array+offset); if (array) destroy(array+offset);
} }
@ -323,11 +359,13 @@ class Memory : protected Pointers {
1st index from n1lo to n1hi inclusive, 1st index from n1lo to n1hi inclusive,
2nd index from n2lo to n2hi inclusive, 2nd index from n2lo to n2hi inclusive,
3rd index from n3lo to n3hi inclusive 3rd index from n3lo to n3hi inclusive
cannot grow it
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
TYPE ***create(TYPE ***&array, int n1lo, int n1hi, int n2lo, int n2hi, TYPE ***create3d_offset(TYPE ***&array, int n1lo, int n1hi,
int n3lo, int n3hi, const char *name) int n2lo, int n2hi, int n3lo, int n3hi,
const char *name)
{ {
int n1 = n1hi - n1lo + 1; int n1 = n1hi - n1lo + 1;
int n2 = n2hi - n2lo + 1; int n2 = n2hi - n2lo + 1;
@ -339,17 +377,24 @@ class Memory : protected Pointers {
return array-n1lo; return array-n1lo;
} }
template <typename TYPE>
TYPE ****create3d_offset(TYPE ****&array, int n1lo, int n1hi,
int n2lo, int n2hi, int n3lo, int n3hi,
const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
free a 3d array with all 3 indices offset free a 3d array with all 3 indices offset
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
template <typename TYPE> template <typename TYPE>
void destroy(TYPE ***array, int n1_offset, int n2_offset, int n3_offset) void destroy3d_offset(TYPE ***array,
int n1_offset, int n2_offset, int n3_offset)
{ {
if (array == NULL) return; if (array == NULL) return;
sfree_new(&array[n1_offset][n2_offset][n3_offset]); sfree(&array[n1_offset][n2_offset][n3_offset]);
sfree_new(&array[n1_offset][n2_offset]); sfree(&array[n1_offset][n2_offset]);
sfree_new(array + n1_offset); sfree(array + n1_offset);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -361,13 +406,13 @@ class Memory : protected Pointers {
const char *name) const char *name)
{ {
bigint nbytes = sizeof(TYPE) * n1*n2*n3*n4; bigint nbytes = sizeof(TYPE) * n1*n2*n3*n4;
TYPE *data = (double *) smalloc_new(nbytes,name); TYPE *data = (double *) smalloc(nbytes,name);
nbytes = sizeof(TYPE *) * n1*n2*n3; nbytes = sizeof(TYPE *) * n1*n2*n3;
TYPE **cube = (double **) smalloc_new(nbytes,name); TYPE **cube = (double **) smalloc(nbytes,name);
nbytes = sizeof(TYPE **) * n1*n2; nbytes = sizeof(TYPE **) * n1*n2;
TYPE ***plane = (double ***) smalloc_new(nbytes,name); TYPE ***plane = (double ***) smalloc(nbytes,name);
nbytes = sizeof(TYPE ***) * n1; nbytes = sizeof(TYPE ***) * n1;
array = (double ****) smalloc_new(nbytes,name); array = (double ****) smalloc(nbytes,name);
int i,j,k; int i,j,k;
int n = 0; int n = 0;
@ -384,6 +429,11 @@ class Memory : protected Pointers {
return array; return array;
} }
template <typename TYPE>
TYPE *****create(TYPE *****&array, int n1, int n2, int n3, int n4,
const char *name)
{fail(name);}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
destroy a 4d array destroy a 4d array
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -392,10 +442,10 @@ class Memory : protected Pointers {
void destroy(TYPE ****array) void destroy(TYPE ****array)
{ {
if (array == NULL) return; if (array == NULL) return;
sfree_new(array[0][0][0]); sfree(array[0][0][0]);
sfree_new(array[0][0]); sfree(array[0][0]);
sfree_new(array[0]); sfree(array[0]);
sfree_new(array); sfree(array);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------