Skip to content

Commit

Permalink
Allow users to specify custom destroy function
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven-Roberts committed Oct 28, 2024
1 parent 51406bc commit f6b7866
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 6 deletions.
21 changes: 20 additions & 1 deletion doc/shared/sunstepper/SUNStepper_Description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ from an ARKODE integrator.

.. c:function:: SUNErrCode SUNStepper_Destroy(SUNStepper *stepper)
This function destroys a :c:type:`SUNStepper` object.
This function frees memory allocated by the :c:type:`SUNStepper` base class
and uses the function pointer optionally specified with
:c:type:`SUNStepper_SetDestroyFn` to free the content.
:param stepper: a pointer to a stepper object.
:return: A :c:type:`SUNErrCode` indicating success or failure.
Expand Down Expand Up @@ -284,6 +286,17 @@ determined by the "consumer" of the :c:type:`SUNStepper`.
:return: A :c:type:`SUNErrCode` indicating success or failure.
.. c:function:: SUNErrCode SUNStepper_SetDestroyFn(SUNStepper stepper, SUNStepperDestroyFn fn)
This function attaches a :c:type:`SUNStepperDestroyFn` function to a
:c:type:`SUNStepper`. The provided function is responsible for freeing any
memory allocated for the :c:type:`SUNStepper` content.
:param stepper: a stepper object.
:param fn: the :c:type:`SUNStepperDestroyFn` function to attach.
:return: A :c:type:`SUNErrCode` indicating success or failure.
.. _SUNStepper.Description.ImplMethods:
Implementation Specific Methods
Expand Down Expand Up @@ -322,3 +335,9 @@ abstract base class.
This type represents a function with the signature of
:c:func:`SUNStepper_SetForcing`.
.. c:type:: SUNErrCode (*SUNStepperDestroyFn)(SUNStepper stepper)
This type represents a function with the signature similar to
:c:func:`SUNStepper_Destroy` for freeing the content associated with a
:c:type:`SUNStepper`.
5 changes: 5 additions & 0 deletions include/sundials/sundials_stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef SUNErrCode (*SUNStepperSetForcingFn)(SUNStepper stepper,
sunrealtype tscale,
N_Vector* forcing, int nforcing);

typedef SUNErrCode (*SUNStepperDestroyFn)(SUNStepper stepper);

SUNDIALS_EXPORT
SUNErrCode SUNStepper_Create(SUNContext sunctx, SUNStepper* stepper);

Expand Down Expand Up @@ -98,6 +100,9 @@ SUNErrCode SUNStepper_SetStopTimeFn(SUNStepper stepper,
SUNDIALS_EXPORT SUNErrCode SUNStepper_SetForcingFn(SUNStepper stepper,
SUNStepperSetForcingFn fn);

SUNDIALS_EXPORT SUNErrCode SUNStepper_SetDestroyFn(SUNStepper stepper,
SUNStepperDestroyFn fn);

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 14 additions & 0 deletions src/sundials/fmod_int32/fsundials_core_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -2882,4 +2882,18 @@ SWIGEXPORT int _wrap_FSUNStepper_SetForcingFn(void *farg1, SUNStepperSetForcingF
}


SWIGEXPORT int _wrap_FSUNStepper_SetDestroyFn(void *farg1, SUNStepperDestroyFn farg2) {
int fresult ;
SUNStepper arg1 = (SUNStepper) 0 ;
SUNStepperDestroyFn arg2 = (SUNStepperDestroyFn) 0 ;
SUNErrCode result;

arg1 = (SUNStepper)(farg1);
arg2 = (SUNStepperDestroyFn)(farg2);
result = (SUNErrCode)SUNStepper_SetDestroyFn(arg1,arg2);
fresult = (SUNErrCode)(result);
return fresult;
}



26 changes: 26 additions & 0 deletions src/sundials/fmod_int32/fsundials_core_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ module fsundials_core_mod
public :: FSUNStepper_SetResetFn
public :: FSUNStepper_SetStopTimeFn
public :: FSUNStepper_SetForcingFn
public :: FSUNStepper_SetDestroyFn

! WRAPPER DECLARATIONS
interface
Expand Down Expand Up @@ -2208,6 +2209,15 @@ function swigc_FSUNStepper_SetForcingFn(farg1, farg2) &
integer(C_INT) :: fresult
end function

function swigc_FSUNStepper_SetDestroyFn(farg1, farg2) &
bind(C, name="_wrap_FSUNStepper_SetDestroyFn") &
result(fresult)
use, intrinsic :: ISO_C_BINDING
type(C_PTR), value :: farg1
type(C_FUNPTR), value :: farg2
integer(C_INT) :: fresult
end function

end interface


Expand Down Expand Up @@ -5226,5 +5236,21 @@ function FSUNStepper_SetForcingFn(stepper, fn) &
swig_result = fresult
end function

function FSUNStepper_SetDestroyFn(stepper, fn) &
result(swig_result)
use, intrinsic :: ISO_C_BINDING
integer(C_INT) :: swig_result
type(C_PTR) :: stepper
type(C_FUNPTR), intent(in), value :: fn
integer(C_INT) :: fresult
type(C_PTR) :: farg1
type(C_FUNPTR) :: farg2

farg1 = stepper
farg2 = fn
fresult = swigc_FSUNStepper_SetDestroyFn(farg1, farg2)
swig_result = fresult
end function


end module
14 changes: 14 additions & 0 deletions src/sundials/fmod_int64/fsundials_core_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -2882,4 +2882,18 @@ SWIGEXPORT int _wrap_FSUNStepper_SetForcingFn(void *farg1, SUNStepperSetForcingF
}


SWIGEXPORT int _wrap_FSUNStepper_SetDestroyFn(void *farg1, SUNStepperDestroyFn farg2) {
int fresult ;
SUNStepper arg1 = (SUNStepper) 0 ;
SUNStepperDestroyFn arg2 = (SUNStepperDestroyFn) 0 ;
SUNErrCode result;

arg1 = (SUNStepper)(farg1);
arg2 = (SUNStepperDestroyFn)(farg2);
result = (SUNErrCode)SUNStepper_SetDestroyFn(arg1,arg2);
fresult = (SUNErrCode)(result);
return fresult;
}



26 changes: 26 additions & 0 deletions src/sundials/fmod_int64/fsundials_core_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ module fsundials_core_mod
public :: FSUNStepper_SetResetFn
public :: FSUNStepper_SetStopTimeFn
public :: FSUNStepper_SetForcingFn
public :: FSUNStepper_SetDestroyFn

! WRAPPER DECLARATIONS
interface
Expand Down Expand Up @@ -2208,6 +2209,15 @@ function swigc_FSUNStepper_SetForcingFn(farg1, farg2) &
integer(C_INT) :: fresult
end function

function swigc_FSUNStepper_SetDestroyFn(farg1, farg2) &
bind(C, name="_wrap_FSUNStepper_SetDestroyFn") &
result(fresult)
use, intrinsic :: ISO_C_BINDING
type(C_PTR), value :: farg1
type(C_FUNPTR), value :: farg2
integer(C_INT) :: fresult
end function

end interface


Expand Down Expand Up @@ -5226,5 +5236,21 @@ function FSUNStepper_SetForcingFn(stepper, fn) &
swig_result = fresult
end function

function FSUNStepper_SetDestroyFn(stepper, fn) &
result(swig_result)
use, intrinsic :: ISO_C_BINDING
integer(C_INT) :: swig_result
type(C_PTR) :: stepper
type(C_FUNPTR), intent(in), value :: fn
integer(C_INT) :: fresult
type(C_PTR) :: farg1
type(C_FUNPTR) :: farg2

farg1 = stepper
farg2 = fn
fresult = swigc_FSUNStepper_SetDestroyFn(farg1, farg2)
swig_result = fresult
end function


end module
4 changes: 3 additions & 1 deletion src/sundials/sundials_stepper.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ SUNErrCode SUNStepper_Destroy(SUNStepper* stepper_ptr)
{
if (stepper_ptr != NULL)
{
free((*stepper_ptr)->ops);
const SUNStepper_Ops ops = (*stepper_ptr)->ops;
if (ops && ops->destroy) { ops->destroy(*stepper_ptr); }
free(ops);
free(*stepper_ptr);
*stepper_ptr = NULL;
}
Expand Down
7 changes: 3 additions & 4 deletions src/sundials/sundials_stepper_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
extern "C" {
#endif

typedef struct SUNStepper_Ops_* SUNStepper_Ops;

struct SUNStepper_Ops_
typedef struct
{
SUNStepperEvolveFn evolve;
SUNStepperFullRhsFn fullrhs;
SUNStepperResetFn reset;
SUNStepperSetStopTimeFn setstoptime;
SUNStepperSetForcingFn setforcing;
};
SUNStepperDestroyFn destroy;
}* SUNStepper_Ops;

struct SUNStepper_
{
Expand Down

0 comments on commit f6b7866

Please sign in to comment.