#include "memory.h"
#include <cstdlib>
#include "error.h"
#if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER)
#ifndef LMP_INTEL_NO_TBB
#define LMP_USE_TBB_ALLOCATOR
#include "tbb/scalable_allocator.h"
#else
#include <malloc.h>
#endif
#endif
#if defined(LMP_USER_INTEL) && !defined(LAMMPS_MEMALIGN) && !defined(_WIN32)
#define LAMMPS_MEMALIGN 64
#endif
using namespace LAMMPS_NS;
Memory::Memory(LAMMPS *lmp) : Pointers(lmp) {}
void *Memory::smalloc(bigint nbytes, const char *name)
{
if (nbytes == 0) return NULL;
#if defined(LAMMPS_MEMALIGN)
void *ptr;
#if defined(LMP_USE_TBB_ALLOCATOR)
ptr = scalable_aligned_malloc(nbytes, LAMMPS_MEMALIGN);
#else
int retval = posix_memalign(&ptr, LAMMPS_MEMALIGN, nbytes);
if (retval) ptr = NULL;
#endif
#else
void *ptr = malloc(nbytes);
#endif
if (ptr == NULL) {
char str[128];
sprintf(str,"Failed to allocate " BIGINT_FORMAT " bytes for array %s",
nbytes,name);
error->one(FLERR,str);
}
return ptr;
}
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
{
if (nbytes == 0) {
destroy(ptr);
return NULL;
}
#if defined(LMP_USE_TBB_ALLOCATOR)
ptr = scalable_aligned_realloc(ptr, nbytes, LAMMPS_MEMALIGN);
#elif defined(LMP_INTEL_NO_TBB) && defined(LAMMPS_MEMALIGN) && \
defined(__INTEL_COMPILER)
ptr = realloc(ptr, nbytes);
uintptr_t offset = ((uintptr_t)(const void *)(ptr)) % LAMMPS_MEMALIGN;
if (offset) {
void *optr = ptr;
ptr = smalloc(nbytes, name);
memcpy(ptr, optr, MIN(nbytes,malloc_usable_size(optr)));
free(optr);
}
#else
ptr = realloc(ptr,nbytes);
#endif
if (ptr == NULL) {
char str[128];
sprintf(str,"Failed to reallocate " BIGINT_FORMAT " bytes for array %s",
nbytes,name);
error->one(FLERR,str);
}
return ptr;
}
void Memory::sfree(void *ptr)
{
if (ptr == NULL) return;
#if defined(LMP_USE_TBB_ALLOCATOR)
scalable_aligned_free(ptr);
#else
free(ptr);
#endif
}
void Memory::fail(const char *name)
{
char str[128];
snprintf(str,128,
"Cannot create/grow a vector/array of pointers for %s",name);
error->one(FLERR,str);
}