2006-09-28 03:51:33 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
2007-01-30 08:22:05 +08:00
|
|
|
http://lammps.sandia.gov, Sandia National Laboratories
|
|
|
|
Steve Plimpton, sjplimp@sandia.gov
|
2006-09-28 03:51:33 +08:00
|
|
|
|
|
|
|
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
|
|
|
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
|
|
|
certain rights in this software. This software is distributed under
|
|
|
|
the GNU General Public License.
|
|
|
|
|
|
|
|
See the README file in the top-level LAMMPS directory.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-02-23 06:09:18 +08:00
|
|
|
#include "lmptype.h"
|
2006-09-28 03:51:33 +08:00
|
|
|
#include "mpi.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
2007-01-30 08:22:05 +08:00
|
|
|
using namespace LAMMPS_NS;
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
2011-01-05 08:29:35 +08:00
|
|
|
Memory::Memory(LAMMPS *lmp) : Pointers(lmp) {}
|
2007-01-30 08:22:05 +08:00
|
|
|
|
2006-09-28 03:51:33 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
safe malloc
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void *Memory::smalloc(bigint nbytes, const char *name)
|
2006-09-28 03:51:33 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (nbytes == 0) return NULL;
|
|
|
|
|
|
|
|
void *ptr = malloc(nbytes);
|
2006-09-28 03:51:33 +08:00
|
|
|
if (ptr == NULL) {
|
|
|
|
char str[128];
|
2011-03-18 00:06:28 +08:00
|
|
|
sprintf(str,"Failed to allocate " BIGINT_FORMAT "bytes for array %s",
|
|
|
|
nbytes,name);
|
2006-09-28 03:51:33 +08:00
|
|
|
error->one(str);
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
safe realloc
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
|
2006-09-28 03:51:33 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (nbytes == 0) {
|
2009-08-07 06:21:38 +08:00
|
|
|
sfree(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
ptr = realloc(ptr,nbytes);
|
2006-09-28 03:51:33 +08:00
|
|
|
if (ptr == NULL) {
|
|
|
|
char str[128];
|
2011-03-18 00:06:28 +08:00
|
|
|
sprintf(str,"Failed to reallocate " BIGINT_FORMAT "bytes for array %s",
|
|
|
|
nbytes,name);
|
2006-09-28 03:51:33 +08:00
|
|
|
error->one(str);
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:21:59 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
2011-03-18 00:06:28 +08:00
|
|
|
safe free
|
2011-03-16 23:21:59 +08:00
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void Memory::sfree(void *ptr)
|
2011-03-16 23:21:59 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (ptr == NULL) return;
|
|
|
|
free(ptr);
|
2011-03-16 23:21:59 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
erroneous usage of templated create/grow functions
|
|
|
|
------------------------------------------------------------------------- */
|
2011-03-16 23:21:59 +08:00
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void Memory::fail(const char *name)
|
2011-03-16 23:21:59 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
char str[128];
|
|
|
|
sprintf(str,"Cannot create/grow a vector/array of pointers for %s",name);
|
|
|
|
error->one(str);
|
2011-03-16 23:21:59 +08:00
|
|
|
}
|