Skip to content

Commit

Permalink
Second part of the migration to a better interface:
Browse files Browse the repository at this point in the history
 make C code using DEFINE_SMETHODS / PUBINST_FININIT.
  • Loading branch information
sobomax committed Jul 10, 2023
1 parent ae4f40d commit 27060f7
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 159 deletions.
11 changes: 3 additions & 8 deletions src/advanced/pproc_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ static int rtpp_pproc_mgr_unregister(struct pproc_manager *, void *);
static struct pproc_manager *rtpp_pproc_mgr_clone(struct pproc_manager *);
void rtpp_pproc_mgr_reg_drop(struct pproc_manager *);

static const struct pproc_manager_smethods _rtpp_pproc_mgr_smethods = {
DEFINE_SMETHODS(pproc_manager,
.reg = &rtpp_pproc_mgr_register,
.handle = &rtpp_pproc_mgr_handle,
.handleat = &rtpp_pproc_mgr_handleat,
.lookup = &rtpp_pproc_mgr_lookup,
.unreg = &rtpp_pproc_mgr_unregister,
.clone = &rtpp_pproc_mgr_clone,
.reg_drop = &rtpp_pproc_mgr_reg_drop,
};
const struct pproc_manager_smethods * const pproc_manager_smethods = &_rtpp_pproc_mgr_smethods;
);

static void
pproc_handlers_dtor(struct pproc_handlers *hndlrs)
Expand Down Expand Up @@ -139,11 +138,7 @@ pproc_manager_ctor(struct rtpp_stats *rtpp_stats, int nprocs)
goto e2;
RTPP_OBJ_INCREF(rtpp_stats);
pvt->rtpp_stats = rtpp_stats;
#if defined(RTPP_DEBUG)
pvt->pub.smethods = pproc_manager_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_pproc_mgr_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_pproc_mgr_dtor);
return (&(pvt->pub));
e2:
pthread_mutex_destroy(&pvt->lock);
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ static int rtpp_analyzer_get_jstats(struct rtpp_analyzer *,
struct rtpa_stats_jitter *);
static void rtpp_analyzer_dtor(struct rtpp_analyzer_priv *);

static const struct rtpp_analyzer_smethods _rtpp_analyzer_smethods = {
DEFINE_SMETHODS(rtpp_analyzer,
.update = &rtpp_analyzer_update,
.get_stats = &rtpp_analyzer_get_stats,
.get_jstats = &rtpp_analyzer_get_jstats,
};
const struct rtpp_analyzer_smethods * const rtpp_analyzer_smethods = &_rtpp_analyzer_smethods;
);

struct rtpp_analyzer *
rtpp_analyzer_ctor(struct rtpp_log *log)
Expand All @@ -82,12 +81,8 @@ rtpp_analyzer_ctor(struct rtpp_log *log)
goto e0;
}
pvt->log = log;
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_analyzer_smethods;
#endif
RTPP_OBJ_INCREF(log);
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_analyzer_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_analyzer_dtor);
return (rap);
e0:
RTPP_OBJ_DECREF(&(pvt->pub));
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static int hash_table_get_length(struct rtpp_hash_table *self);
static int hash_table_purge(struct rtpp_hash_table *self);
static int hash_table_resize_locked(struct rtpp_hash_table_priv *, size_t);

static const struct rtpp_hash_table_smethods _rtpp_hash_table_smethods = {
DEFINE_SMETHODS(rtpp_hash_table,
.append_refcnt = &hash_table_append_refcnt,
.append_str_refcnt = &hash_table_append_str_refcnt,
.remove = &hash_table_remove,
Expand All @@ -133,8 +133,7 @@ static const struct rtpp_hash_table_smethods _rtpp_hash_table_smethods = {
.foreach_key_str = &hash_table_foreach_key_str,
.get_length = &hash_table_get_length,
.purge = &hash_table_purge,
};
const struct rtpp_hash_table_smethods * const rtpp_hash_table_smethods = &_rtpp_hash_table_smethods;
);

static size_t
rtpp_hash_table_l1_sizeof(size_t ht_len)
Expand Down Expand Up @@ -165,12 +164,8 @@ rtpp_hash_table_ctor(enum rtpp_ht_key_types key_type, int flags)
pvt->flags = flags;
pvt->l1->ht_len = ht_len;
pub = &(pvt->pub);
#if defined(RTPP_DEBUG)
pub->smethods = rtpp_hash_table_smethods;
#endif
pvt->pub.seed = ((uint64_t)random()) << 32 | (uint64_t)random();
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&hash_table_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, hash_table_dtor);
return (pub);
e2:
free(pvt->l1);
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_netaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static size_t rtpp_netaddr_get(struct rtpp_netaddr *, struct sockaddr *, size_t)
static size_t rtpp_netaddr_sip_print(struct rtpp_netaddr *, char *, size_t,
char);

static const struct rtpp_netaddr_smethods _rtpp_netaddr_smethods = {
DEFINE_SMETHODS(rtpp_netaddr,
.set = &rtpp_netaddr_set,
.isempty = &rtpp_netaddr_isempty,
.cmp = &rtpp_netaddr_cmp,
Expand All @@ -69,8 +69,7 @@ static const struct rtpp_netaddr_smethods _rtpp_netaddr_smethods = {
.copy = &rtpp_netaddr_copy,
.get = &rtpp_netaddr_get,
.sip_print = &rtpp_netaddr_sip_print
};
const struct rtpp_netaddr_smethods * const rtpp_netaddr_smethods = &_rtpp_netaddr_smethods;
);

struct rtpp_netaddr *
rtpp_netaddr_ctor(void)
Expand All @@ -84,11 +83,7 @@ rtpp_netaddr_ctor(void)
if (pthread_mutex_init(&pvt->lock, NULL) != 0) {
goto e1;
}
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_netaddr_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach,
(rtpp_refcnt_dtor_t)&rtpp_netaddr_dtor, pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_netaddr_dtor);
return ((&pvt->pub));

e1:
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_pcnt_strm.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ static void rtpp_pcnt_strm_get_stats(struct rtpp_pcnt_strm *,
static void rtpp_pcnt_strm_reg_pktin(struct rtpp_pcnt_strm *,
struct rtp_packet *);

static const struct rtpp_pcnt_strm_smethods _rtpp_pcnt_strm_smethods = {
DEFINE_SMETHODS(rtpp_pcnt_strm,
.get_stats = &rtpp_pcnt_strm_get_stats,
.reg_pktin = &rtpp_pcnt_strm_reg_pktin,
};
const struct rtpp_pcnt_strm_smethods * const rtpp_pcnt_strm_smethods = &_rtpp_pcnt_strm_smethods;
);

struct rtpp_pcnt_strm *
rtpp_pcnt_strm_ctor(void)
Expand All @@ -76,11 +75,7 @@ rtpp_pcnt_strm_ctor(void)
if (pthread_mutex_init(&pvt->lock, NULL) != 0) {
goto e1;
}
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_pcnt_strm_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach,
(rtpp_refcnt_dtor_t)&rtpp_pcnt_strm_dtor, pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_pcnt_strm_dtor);
return ((&pvt->pub));

e1:
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_pcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ static void rtpp_pcount_reg_drop(struct rtpp_pcount *);
static void rtpp_pcount_reg_ignr(struct rtpp_pcount *);
static void rtpp_pcount_get_stats(struct rtpp_pcount *, struct rtpps_pcount *);

static const struct rtpp_pcount_smethods _rtpp_pcount_smethods = {
DEFINE_SMETHODS(rtpp_pcount,
.reg_reld = &rtpp_pcount_reg_reld,
.reg_drop = &rtpp_pcount_reg_drop,
.reg_ignr = &rtpp_pcount_reg_ignr,
.get_stats = &rtpp_pcount_get_stats
};
const struct rtpp_pcount_smethods * const rtpp_pcount_smethods = &_rtpp_pcount_smethods;
);

struct rtpp_pcount *
rtpp_pcount_ctor(void)
Expand All @@ -71,14 +70,10 @@ rtpp_pcount_ctor(void)
if (pvt == NULL) {
goto e0;
}
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_pcount_smethods;
#endif
atomic_init(&(pvt->cnt.nrelayed), 0);
atomic_init(&(pvt->cnt.ndropped), 0);
atomic_init(&(pvt->cnt.nignored), 0);
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_pcount_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_pcount_dtor);
return ((&pvt->pub));

e0:
Expand Down
12 changes: 3 additions & 9 deletions src/rtpp_pearson_perfect.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ struct rtpp_pearson_perfect_priv
static int rtpp_pearson_perfect_hash(struct rtpp_pearson_perfect *, const char *);
static void rtpp_pearson_perfect_dtor(struct rtpp_pearson_perfect_priv *);

static const struct rtpp_pearson_perfect_smethods _rtpp_pearson_perfect_smethods = {
DEFINE_SMETHODS(rtpp_pearson_perfect,
.hash = &rtpp_pearson_perfect_hash
};
const struct rtpp_pearson_perfect_smethods * const rtpp_pearson_perfect_smethods =
&_rtpp_pearson_perfect_smethods;
);

static void
compute_perfect_hash(struct rtpp_pearson_perfect_priv *rppp)
Expand Down Expand Up @@ -92,11 +90,7 @@ rtpp_pearson_perfect_ctor(rtpp_pearson_getval_t gv, void *gv_arg)
pub = &rppp->pub;

compute_perfect_hash(rppp);
#if defined(RTPP_DEBUG)
pub->smethods = rtpp_pearson_perfect_smethods;
#endif
CALL_SMETHOD(pub->rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_pearson_perfect_dtor,
rppp);
PUBINST_FININIT(pub, rppp, rtpp_pearson_perfect_dtor);
return(pub);
}

Expand Down
10 changes: 3 additions & 7 deletions src/rtpp_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ static void rtpp_pipe_upd_cntrs(struct rtpp_pipe *, struct rtpp_acct_pipe *);
#define MT2RT_NZ(mt) ((mt).wall)
#define DRTN_NZ(bmt, emt) ((emt).mono == 0.0 || (bmt).mono == 0.0 ? 0.0 : ((emt).mono - (bmt).mono))

static const struct rtpp_pipe_smethods _rtpp_pipe_smethods = {
DEFINE_SMETHODS(rtpp_pipe,
.get_ttl = &rtpp_pipe_get_ttl,
.decr_ttl = &rtpp_pipe_decr_ttl,
.get_stats = &rtpp_pipe_get_stats,
.upd_cntrs = &rtpp_pipe_upd_cntrs,
};
const struct rtpp_pipe_smethods * const rtpp_pipe_smethods = &_rtpp_pipe_smethods;
);

struct rtpp_pipe *
rtpp_pipe_ctor(const struct r_pipe_ctor_args *ap)
Expand Down Expand Up @@ -130,11 +129,8 @@ rtpp_pipe_ctor(const struct r_pipe_ctor_args *ap)
pvt->pipe_type = ap->pipe_type;
pvt->pub.rtpp_stats = ap->rtpp_stats;
pvt->pub.log = ap->log;
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_pipe_smethods;
#endif
RTPP_OBJ_INCREF(ap->log);
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_pipe_dtor, pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_pipe_dtor);
return (&pvt->pub);

e2:
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_proc_servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ static int rtpp_proc_servers_reg(struct rtpp_proc_servers *,
static int rtpp_proc_servers_unreg(struct rtpp_proc_servers *, uint64_t);
static int rtpp_proc_servers_plr_start(struct rtpp_proc_servers *, uint64_t, double);

static const struct rtpp_proc_servers_smethods _rtpp_proc_servers_smethods = {
DEFINE_SMETHODS(rtpp_proc_servers,
.reg = &rtpp_proc_servers_reg,
.unreg = &rtpp_proc_servers_unreg,
.plr_start = &rtpp_proc_servers_plr_start,
};
const struct rtpp_proc_servers_smethods * const rtpp_proc_servers_smethods = &_rtpp_proc_servers_smethods;
);

struct foreach_args {
double dtime;
Expand Down Expand Up @@ -275,11 +274,7 @@ rtpp_proc_servers_ctor(const struct rtpp_cfg *cfsp, struct rtpp_anetio_cf *netio
(void)pthread_setname_np(stap->thread_id, "rtpp_proc_servers");
#endif

#if defined(RTPP_DEBUG)
stap->pub.smethods = rtpp_proc_servers_smethods;
#endif

CALL_SMETHOD(stap->pub.rcnt, attach, (rtpp_refcnt_dtor_t)rtpp_proc_servers_dtor, stap);
PUBINST_FININIT(&stap->pub, stap, rtpp_proc_servers_dtor);
return (&stap->pub);
e5:
RTPP_OBJ_DECREF(stap->inact_servers);
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_proc_wakeup.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ struct rtpp_proc_wakeup_priv {
static void rtpp_proc_wakeup_dtor(struct rtpp_proc_wakeup_priv *);
static int rtpp_proc_wakeup_nudge(struct rtpp_proc_wakeup *);

static const struct rtpp_proc_wakeup_smethods _rtpp_proc_wakeup_smethods = {
DEFINE_SMETHODS(rtpp_proc_wakeup,
.nudge = &rtpp_proc_wakeup_nudge,
};
const struct rtpp_proc_wakeup_smethods * const rtpp_proc_wakeup_smethods = &_rtpp_proc_wakeup_smethods;
);

static void
rtpp_proc_wakeup_run(void *arg)
Expand Down Expand Up @@ -156,11 +155,7 @@ rtpp_proc_wakeup_ctor(int rtp_wakefd, int rtcp_wakefd)
#else
sched_yield();
#endif
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_proc_wakeup_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_proc_wakeup_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_proc_wakeup_dtor);
return (&pvt->pub);
e4:
pthread_cond_destroy(&pvt->cond);
Expand Down
5 changes: 2 additions & 3 deletions src/rtpp_record.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ static int get_hdr_size(const struct sockaddr *);
#define ARRIVAL_TIME(rp, pp) (pp->rtime.wall)
#endif

static const struct rtpp_record_smethods _rtpp_record_smethods = {
DEFINE_SMETHODS(rtpp_record,
.pktwrite = &rtpp_record_write
};
const struct rtpp_record_smethods * const rtpp_record_smethods = &_rtpp_record_smethods;
);

static int
ropen_remote_ctor_pa(struct rtpp_record_channel *rrc, struct rtpp_log *log,
Expand Down
5 changes: 2 additions & 3 deletions src/rtpp_refcnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void rtpp_refcnt_use_stdfree(struct rtpp_refcnt *, void *);
static void rtpp_refcnt_traceen(struct rtpp_refcnt *);
#endif

static const struct rtpp_refcnt_smethods _rtpp_refcnt_smethods = {
DEFINE_SMETHODS(rtpp_refcnt,
.incref = &rtpp_refcnt_incref,
.decref = &rtpp_refcnt_decref,
.getdata = &rtpp_refcnt_getdata,
Expand All @@ -97,8 +97,7 @@ static const struct rtpp_refcnt_smethods _rtpp_refcnt_smethods = {
#endif
.attach = &rtpp_refcnt_attach,
.use_stdfree = &rtpp_refcnt_use_stdfree,
};
const struct rtpp_refcnt_smethods * const rtpp_refcnt_smethods = &_rtpp_refcnt_smethods;
);

#if defined(RTPP_CHECK_LEAKS)
static void
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ static void rtpp_ringbuf_push(struct rtpp_ringbuf *, void *);
static void rtpp_ringbuf_flush(struct rtpp_ringbuf *);
static int rtpp_ringbuf_locate(struct rtpp_ringbuf *, void *);

static const struct rtpp_ringbuf_smethods _rtpp_ringbuf_smethods = {
DEFINE_SMETHODS(rtpp_ringbuf,
.push = &rtpp_ringbuf_push,
.flush = &rtpp_ringbuf_flush,
.locate = &rtpp_ringbuf_locate,
};
const struct rtpp_ringbuf_smethods * const rtpp_ringbuf_smethods = &_rtpp_ringbuf_smethods;
);

struct rtpp_ringbuf *
rtpp_ringbuf_ctor(size_t el_size, int nelements)
Expand All @@ -73,11 +72,7 @@ rtpp_ringbuf_ctor(size_t el_size, int nelements)
}
pvt->el_size = el_size;
pvt->nelements = nelements;
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_ringbuf_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_ringbuf_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, rtpp_ringbuf_dtor, pvt);
return (&pvt->pub);
e1:
RTPP_OBJ_DECREF(&(pvt->pub));
Expand Down
11 changes: 3 additions & 8 deletions src/rtpp_rw_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ static void rtpp_rw_lock_lock(struct rtpp_rw_lock *, enum rtpp_rw_lock_mode);
static void rtpp_rw_lock_unlock(struct rtpp_rw_lock *, enum rtpp_rw_lock_mode);
static int rtpp_rw_lock_upgrade(struct rtpp_rw_lock *);

static const struct rtpp_rw_lock_smethods _rtpp_rw_lock_smethods = {
DEFINE_SMETHODS(rtpp_rw_lock,
.lock = &rtpp_rw_lock_lock,
.unlock = &rtpp_rw_lock_unlock,
.upgrade = &rtpp_rw_lock_upgrade,
};
const struct rtpp_rw_lock_smethods * const rtpp_rw_lock_smethods = &_rtpp_rw_lock_smethods;
);

static void
rtpp_rw_lock_dtor(struct rtpp_rw_lock_priv *pvt)
Expand All @@ -76,11 +75,7 @@ rtpp_rw_lock_ctor(void)
goto e2;
if (pthread_mutex_init(&pvt->write_lock, NULL) != 0)
goto e3;
#if defined(RTPP_DEBUG)
pvt->pub.smethods = rtpp_rw_lock_smethods;
#endif
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_rw_lock_dtor,
pvt);
PUBINST_FININIT(&pvt->pub, pvt, rtpp_rw_lock_dtor);
return ((&pvt->pub));
e3:
pthread_mutex_destroy(&pvt->cnt_lock);
Expand Down
Loading

0 comments on commit 27060f7

Please sign in to comment.