Skip to content

Commit

Permalink
Addressed clang-asan's complaint about calling network change callbac…
Browse files Browse the repository at this point in the history
…k functions from incorrect function type by instead passing a wrapper that takes a void* and passes the arguments through to the actual u_func. Dyadgen's callbacks have also been updated.
  • Loading branch information
krivit committed Nov 5, 2024
1 parent f8c16ac commit 6bdb3b7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions inst/include/ergm_dyadgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ DyadGen *DyadGenInitialize(DyadGenType type, void *dyads, void *track_nwp);
DyadGen *DyadGenInitializeR(SEXP pR, void *any_nwp, Rboolean el);
void DyadGenDestroy(DyadGen *gen);

void DyadGenUpdate(Vertex tail, Vertex head, DyadGen *gen, Network *nwp, Rboolean edgestate);
void WtDyadGenUpdate(Vertex tail, Vertex head, double weight, DyadGen *gen, WtNetwork *nwp, double edgestate);
void DyadGenUpdate(Vertex tail, Vertex head, void *gen, Network *nwp, Rboolean edgestate);
void WtDyadGenUpdate(Vertex tail, Vertex head, double weight, void *gen, WtNetwork *nwp, double edgestate);

static inline void DyadGenRandDyad(Vertex *tail, Vertex *head, DyadGen *gen){
switch(gen->type){
Expand Down
8 changes: 6 additions & 2 deletions src/MHproposal.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "ergm_changestat.h"
#include "ergm_Rutil.h"

void OnNetworkEdgeChangeMUWrap(Vertex tail, Vertex head, void *MHp, Network *nwp, Rboolean edgestate){
((MHProposal *) MHp)->u_func(tail, head, MHp, nwp, edgestate);
}

/*********************
void MHProposalInitialize
Expand Down Expand Up @@ -91,7 +95,7 @@ MHProposal *MHProposalInitialize(SEXP pR, Network *nwp, void **aux_storage){
MHp->togglehead = (Vertex *)R_Calloc(MHp->ntoggles, Vertex);

if(MHp->u_func){
AddOnNetworkEdgeChange(nwp, (OnNetworkEdgeChange) MHp->u_func, MHp, 0); // Need to insert at the start.
AddOnNetworkEdgeChange(nwp, OnNetworkEdgeChangeMUWrap, MHp, 0); // Need to insert at the start.
}

return MHp;
Expand All @@ -104,7 +108,7 @@ MHProposal *MHProposalInitialize(SEXP pR, Network *nwp, void **aux_storage){
*********************/
void MHProposalDestroy(MHProposal *MHp, Network *nwp){
if(!MHp) return;
if(MHp->u_func) DeleteOnNetworkEdgeChange(nwp, (OnNetworkEdgeChange) MHp->u_func, MHp);
if(MHp->u_func) DeleteOnNetworkEdgeChange(nwp, OnNetworkEdgeChangeMUWrap, MHp);
if(MHp->f_func) (*(MHp->f_func))(MHp, nwp);
if(MHp->storage){
R_Free(MHp->storage);
Expand Down
6 changes: 4 additions & 2 deletions src/ergm_dyadgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ void DyadGenDestroy(DyadGen *gen){
}


void DyadGenUpdate(Vertex tail, Vertex head, DyadGen *gen, Network *nwp, Rboolean edgestate){
void DyadGenUpdate(Vertex tail, Vertex head, void *payload, Network *nwp, Rboolean edgestate){
DyadGen *gen = payload;
if(gen->sleeping) return;

switch(gen->intertype){
Expand All @@ -193,7 +194,8 @@ void DyadGenUpdate(Vertex tail, Vertex head, DyadGen *gen, Network *nwp, Rboolea
}


void WtDyadGenUpdate(Vertex tail, Vertex head, double weight, DyadGen *gen, WtNetwork *nwp, double edgestate){
void WtDyadGenUpdate(Vertex tail, Vertex head, double weight, void *payload, WtNetwork *nwp, double edgestate){
DyadGen *gen = payload;
if(gen->sleeping) return;

switch(gen->intertype){
Expand Down
8 changes: 6 additions & 2 deletions src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "ergm_omp.h"
#include "ergm_util.h"

void OnNetworkEdgeChangeUWrap(Vertex tail, Vertex head, void *mtp, Network *nwp, Rboolean edgestate){
((ModelTerm *) mtp)->u_func(tail, head, mtp, nwp, edgestate);
}

/*
InitStats
A helper's helper function to initialize storage for functions that use it.
Expand Down Expand Up @@ -56,7 +60,7 @@ static inline void InitStats(Network *nwp, Model *m){
}
// Now, bind the term to the network through the callback API.
if(mtp->u_func && (!m->noinit_s || !mtp->s_func)) // Skip if noinit_s is set and s_func is present.
AddOnNetworkEdgeChange(nwp, (OnNetworkEdgeChange) mtp->u_func, mtp, on_edge_change_pos);
AddOnNetworkEdgeChange(nwp, OnNetworkEdgeChangeUWrap, mtp, on_edge_change_pos);
});
}

Expand All @@ -69,7 +73,7 @@ static inline void DestroyStats(Network *nwp, Model *m){
EXEC_THROUGH_TERMS(m, {
if(!m->noinit_s || !mtp->s_func){ // Skip if noinit_s is set and s_func is present.
if(mtp->u_func)
DeleteOnNetworkEdgeChange(nwp, (OnNetworkEdgeChange) mtp->u_func, mtp);
DeleteOnNetworkEdgeChange(nwp, OnNetworkEdgeChangeUWrap, mtp);
if(mtp->f_func)
(*(mtp->f_func))(mtp, nwp); /* Call f_??? function */
}
Expand Down
8 changes: 6 additions & 2 deletions src/wtMHproposal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "ergm_wtMHproposal.h"
#include "ergm_Rutil.h"

void OnWtNetworkEdgeChangeMUWrap(Vertex tail, Vertex head, double weight, void *MHp, WtNetwork *nwp, double edgestate){
((WtMHProposal *) MHp)->u_func(tail, head, weight, MHp, nwp, edgestate);
}

/*********************
void WtMHProposalInitialize
Expand Down Expand Up @@ -92,7 +96,7 @@ WtMHProposal *WtMHProposalInitialize(SEXP pR, WtNetwork *nwp, void **aux_storage
MHp->toggleweight = (double *)R_Calloc(MHp->ntoggles, double);

if(MHp->u_func){
AddOnWtNetworkEdgeChange(nwp, (OnWtNetworkEdgeChange) MHp->u_func, MHp, 0); // Need to insert at the start.
AddOnWtNetworkEdgeChange(nwp, OnWtNetworkEdgeChangeMUWrap, MHp, 0); // Need to insert at the start.
}

return MHp;
Expand All @@ -105,7 +109,7 @@ WtMHProposal *WtMHProposalInitialize(SEXP pR, WtNetwork *nwp, void **aux_storage
*********************/
void WtMHProposalDestroy(WtMHProposal *MHp, WtNetwork *nwp){
if(!MHp) return;
if(MHp->u_func) DeleteOnWtNetworkEdgeChange(nwp, (OnWtNetworkEdgeChange) MHp->u_func, MHp);
if(MHp->u_func) DeleteOnWtNetworkEdgeChange(nwp, OnWtNetworkEdgeChangeMUWrap, MHp);
if(MHp->f_func) (*(MHp->f_func))(MHp, nwp);
if(MHp->storage){
R_Free(MHp->storage);
Expand Down
8 changes: 6 additions & 2 deletions src/wtmodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "ergm_omp.h"
#include "ergm_util.h"

void OnWtNetworkEdgeChangeUWrap(Vertex tail, Vertex head, double weight, void *mtp, WtNetwork *nwp, double edgestate){
((WtModelTerm *) mtp)->u_func(tail, head, weight, mtp, nwp, edgestate);
}

/*
WtInitStats
A helper's helper function to initialize storage for functions that use it.
Expand Down Expand Up @@ -56,7 +60,7 @@ static inline void WtInitStats(WtNetwork *nwp, WtModel *m){
}
// Now, bind the term to the network through the callback API.
if(mtp->u_func && (!m->noinit_s || !mtp->s_func)) // Skip if noinit_s is set and s_func is present.
AddOnWtNetworkEdgeChange(nwp, (OnWtNetworkEdgeChange) mtp->u_func, mtp, on_edge_change_pos);
AddOnWtNetworkEdgeChange(nwp, OnWtNetworkEdgeChangeUWrap, mtp, on_edge_change_pos);
});
}

Expand All @@ -69,7 +73,7 @@ static inline void WtDestroyStats(WtNetwork *nwp, WtModel *m){
WtEXEC_THROUGH_TERMS(m, {
if(!m->noinit_s || !mtp->s_func){ // Skip if noinit_s is set and s_func is present.
if(mtp->u_func)
DeleteOnWtNetworkEdgeChange(nwp, (OnWtNetworkEdgeChange) mtp->u_func, mtp);
DeleteOnWtNetworkEdgeChange(nwp, OnWtNetworkEdgeChangeUWrap, mtp);
if(mtp->f_func)
(*(mtp->f_func))(mtp, nwp); /* Call f_??? function */
}
Expand Down

0 comments on commit 6bdb3b7

Please sign in to comment.