From b03e9609cefa2c3b8a6dd520250c26dfba3bc232 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 13 Jan 2023 05:30:49 -0500 Subject: [PATCH] synchronize API with library.h, zero struct on allocation, determine exception support at runtime. --- examples/COUPLE/plugin/liblammpsplugin.c | 17 +++++++---------- examples/COUPLE/plugin/liblammpsplugin.h | 21 +++++++++++---------- examples/COUPLE/plugin/simple.c | 6 ++++++ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/examples/COUPLE/plugin/liblammpsplugin.c b/examples/COUPLE/plugin/liblammpsplugin.c index 0b7eda7537..759be31a81 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.c +++ b/examples/COUPLE/plugin/liblammpsplugin.c @@ -56,7 +56,8 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib) #endif if (handle == NULL) return NULL; - lmp = (liblammpsplugin_t *) malloc(sizeof(liblammpsplugin_t)); + lmp = (liblammpsplugin_t *) calloc(1, sizeof(liblammpsplugin_t)); + lmp->abiversion = LAMMPSPLUGIN_ABI_VERSION; lmp->handle = handle; #ifdef _WIN32 @@ -179,15 +180,11 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib) ADDSYM(is_running); ADDSYM(force_timeout); -#ifdef LAMMPS_EXCEPTIONS - lmp->has_exceptions = 1; - ADDSYM(has_error); - ADDSYM(get_last_error_message); -#else - lmp->has_exceptions = 0; - lmp->has_error = NULL; - lmp->get_last_error_message = NULL; -#endif + lmp->has_exceptions = lmp->config_has_exceptions(); + if (lmp->has_exceptions) { + ADDSYM(has_error); + ADDSYM(get_last_error_message); + } ADDSYM(python_api_version); return lmp; diff --git a/examples/COUPLE/plugin/liblammpsplugin.h b/examples/COUPLE/plugin/liblammpsplugin.h index d31bdc4504..ee77fff31e 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.h +++ b/examples/COUPLE/plugin/liblammpsplugin.h @@ -105,6 +105,7 @@ typedef void (*FixExternalFnPtr)(void *, int, int, int *, double **, double **); typedef void (*FixExternalFnPtr)(void *, int64_t, int, int *, double **, double **); #endif +#define LAMMPSPLUGIN_ABI_VERSION 1 struct _liblammpsplugin { int abiversion; int has_exceptions; @@ -153,19 +154,19 @@ struct _liblammpsplugin { int (*extract_variable_datatype)(void *, const char *); int (*set_variable)(void *, char *, char *); - void (*gather_atoms)(void *, char *, int, int, void *); - void (*gather_atoms_concat)(void *, char *, int, int, void *); - void (*gather_atoms_subset)(void *, char *, int, int, int, int *, void *); - void (*scatter_atoms)(void *, char *, int, int, void *); - void (*scatter_atoms_subset)(void *, char *, int, int, int, int *, void *); + void (*gather_atoms)(void *, const char *, int, int, void *); + void (*gather_atoms_concat)(void *, const char *, int, int, void *); + void (*gather_atoms_subset)(void *, const char *, int, int, int, int *, void *); + void (*scatter_atoms)(void *, const char *, int, int, void *); + void (*scatter_atoms_subset)(void *, const char *, int, int, int, int *, void *); void (*gather_bonds)(void *, void *); - void (*gather)(void *, char *, int, int, void *); - void (*gather_concat)(void *, char *, int, int, void *); - void (*gather_subset)(void *, char *, int, int, int, int *,void *); - void (*scatter)(void *, char *, int, int, void *); - void (*scatter_subset)(void *, char *, int, int, int, int *, void *); + void (*gather)(void *, const char *, int, int, void *); + void (*gather_concat)(void *, const char *, int, int, void *); + void (*gather_subset)(void *, const char *, int, int, int, int *,void *); + void (*scatter)(void *, const char *, int, int, void *); + void (*scatter_subset)(void *, const char *, int, int, int, int *, void *); /* lammps_create_atoms() takes tagint and imageint as args * the ifdef insures they are compatible with rest of LAMMPS diff --git a/examples/COUPLE/plugin/simple.c b/examples/COUPLE/plugin/simple.c index 6cc453bd34..535086f7ad 100644 --- a/examples/COUPLE/plugin/simple.c +++ b/examples/COUPLE/plugin/simple.c @@ -92,6 +92,12 @@ int main(int narg, char **arg) if (me == 0) printf("ERROR: Could not load shared LAMMPS library\n"); MPI_Abort(MPI_COMM_WORLD,1); } + /* must match the plugin ABI version */ + if (plugin->abiversion != LAMMPSPLUGIN_ABI_VERSION) { + if (me == 0) printf("ERROR: Plugin abi version does not match: %d vs %d\n", + plugin->abiversion, LAMMPSPLUGIN_ABI_VERSION); + MPI_Abort(MPI_COMM_WORLD,1); + } } if (lammps == 1) { if (plugin->open == NULL) {