use const std::string & instead of const char *. avoid exception in sfree()

This commit is contained in:
Axel Kohlmeyer
2021-08-06 08:38:06 -04:00
parent 912a1fc561
commit 8baaed5724
2 changed files with 13 additions and 19 deletions

View File

@ -141,9 +141,9 @@ namespace ReaxFF
// toolbox // toolbox
extern void *scalloc(LAMMPS_NS::Error *, rc_bigint, rc_bigint, const char *); extern void *scalloc(LAMMPS_NS::Error *, rc_bigint, rc_bigint, const std::string &);
extern void *smalloc(LAMMPS_NS::Error *, rc_bigint, const char *); extern void *smalloc(LAMMPS_NS::Error *, rc_bigint, const std::string &);
extern void sfree(LAMMPS_NS::Error *, void *, const char *); extern void sfree(LAMMPS_NS::Error *, void *, const std::string &);
// torsion angles // torsion angles

View File

@ -36,13 +36,12 @@
namespace ReaxFF { namespace ReaxFF {
/* safe malloc */ /* safe malloc */
void *smalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, const char *name) void *smalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, const std::string &name)
{ {
void *ptr; void *ptr;
if (n <= 0) { if (n <= 0) {
auto errmsg = fmt::format("Trying to allocate {} bytes for array {}. " auto errmsg = fmt::format("Invalid size {} for array {}. Returning NULL.", n, name);
"returning NULL.", n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg); if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
@ -51,8 +50,7 @@ namespace ReaxFF {
ptr = malloc(n); ptr = malloc(n);
if (ptr == nullptr) { if (ptr == nullptr) {
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", n, name);
n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg); if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
} }
@ -61,21 +59,19 @@ namespace ReaxFF {
} }
/* safe calloc */ /* safe calloc */
void *scalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, rc_bigint size, const char *name) void *scalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, rc_bigint size, const std::string &name)
{ {
void *ptr; void *ptr;
if (n <= 0) { if (n <= 0) {
auto errmsg = fmt::format("Trying to allocate {} elements for array {}. " auto errmsg = fmt::format("Invalid size {} for array {}. Returning NULL.\n", n, name);
"returning NULL.\n", n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg); if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
return nullptr; return nullptr;
} }
if (size <= 0) { if (size <= 0) {
auto errmsg = fmt::format("Elements size for array {} is {}. " auto errmsg = fmt::format("Elements size for array {} is {}. Returning NULL", name, size);
"returning NULL", name, size);
if (error_ptr) error_ptr->one(FLERR,errmsg); if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
return nullptr; return nullptr;
@ -83,8 +79,7 @@ namespace ReaxFF {
ptr = calloc(n, size); ptr = calloc(n, size);
if (ptr == nullptr) { if (ptr == nullptr) {
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", n*size, name);
n*size, name);
if (error_ptr) error_ptr->one(FLERR,errmsg); if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
} }
@ -93,12 +88,11 @@ namespace ReaxFF {
} }
/* safe free */ /* safe free */
void sfree(LAMMPS_NS::Error* error_ptr, void *ptr, const char *name) void sfree(LAMMPS_NS::Error *error_ptr, void *ptr, const std::string &name)
{ {
if (ptr == nullptr) { if (ptr == nullptr) {
auto errmsg = fmt::format("Trying to free the already free()'d pointer {}", auto errmsg = std::string("Trying to free the already free()'d pointer: ") + name;
name); if (error_ptr) error_ptr->one(FLERR, errmsg);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr); else fputs(errmsg.c_str(),stderr);
return; return;
} }