From 7b4df937be9c7c6fb98b7f90c0151e017a7fa117 Mon Sep 17 00:00:00 2001 From: Maksym Sobolyev Date: Mon, 10 Jul 2023 11:55:49 -0700 Subject: [PATCH] Implement simplified (fast) version of the refcounter class. --- src/genfincode.sub | 17 ++++++-- src/rtp_packet.c | 4 +- src/rtp_packet.h | 4 +- src/rtpp_mallocs.c | 76 +++++++++++------------------------ src/rtpp_mallocs.h | 30 +++++++------- src/rtpp_netio_async.c | 2 +- src/rtpp_refcnt.c | 85 ++++++++++++++++++++++++++++++++++++++-- src/rtpp_refcnt.h | 38 ++++++++++++++++++ src/rtpp_rzmalloc_perf.c | 40 +++++++++++++++++-- src/rtpp_types.h | 2 + src/rtpp_util.h | 3 ++ src/rtpp_wi.h | 2 +- src/rtpp_wi_apis.c | 5 ++- src/rtpp_wi_data.c | 8 ++-- src/rtpp_wi_pkt.c | 10 ++--- src/rtpp_wi_private.h | 1 + src/rtpp_wi_sgnl.c | 6 +-- 17 files changed, 236 insertions(+), 97 deletions(-) diff --git a/src/genfincode.sub b/src/genfincode.sub index 377af34e6..32bbb0d90 100644 --- a/src/genfincode.sub +++ b/src/genfincode.sub @@ -76,7 +76,12 @@ emit_fintestfunction() { echo " } *tp;" echo "" echo " naborts_s = _naborts;" - echo " tp = rtpp_rzmalloc(sizeof(*tp), offsetof(typeof(*tp), pub.rcnt));" + if [ "${oname}" != "rtpp_refcnt_fast" ] + then + echo " tp = rtpp_rzmalloc(sizeof(*tp), offsetof(typeof(*tp), pub.rcnt));" + else + echo " tp = rtpp_rgmalloc(MLT_RF, MLT_ZR, sizeof(*tp), offsetof(typeof(*tp), pub.rcnt));" + fi echo " assert(tp != NULL);" echo " assert(tp->pub.rcnt != NULL);" if [ ${static} -ne 0 ] @@ -98,8 +103,14 @@ emit_fintestfunction() { echo " };" echo " tp->pub.smethods = &dummy;" fi - echo " CALL_SMETHOD(tp->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&${oname}_fin," - echo " &tp->pub);" + if [ "${oname}" != "rtpp_refcnt_fast" ] + then + echo " CALL_SMETHOD(tp->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&${oname}_fin," + echo " &tp->pub);" + else + echo " CALL_SMETHOD(tp->pub.rcnt, attach, USE_DTOR(${oname}_fin," + echo " &tp->pub));" + fi echo " RTPP_OBJ_DECREF(&(tp->pub));" i=0 for mname in ${mnames} diff --git a/src/rtp_packet.c b/src/rtp_packet.c index 8f21c0c32..0f5aab726 100644 --- a/src/rtp_packet.c +++ b/src/rtp_packet.c @@ -87,11 +87,11 @@ rtp_packet_alloc() { struct rtp_packet_full *pkt; - pkt = rtpp_rzmalloc(sizeof(*pkt), PVT_RCOFFS(pkt)); + pkt = rtpp_rgmalloc(MLT_RF, MLT_ZR, sizeof(*pkt), PVT_RCOFFS(pkt)); if (pkt == NULL) { return (NULL); } - CALL_SMETHOD(pkt->pub.rcnt, use_stdfree, pkt); + CALL_SMETHOD(pkt->pub.rcnt, attach, USE_STDFREE(pkt)); pkt->pub.wi = &(pkt->pvt.wip.pub); return &(pkt->pub); diff --git a/src/rtp_packet.h b/src/rtp_packet.h index f1401a9be..1e47a16e5 100644 --- a/src/rtp_packet.h +++ b/src/rtp_packet.h @@ -31,14 +31,14 @@ struct rtp_info; struct rtpp_wi; -struct rtpp_refcnt; +struct rtpp_refcnt_fast; struct sthread_args; struct packet_processor_if; #define MAX_RPKT_LEN 8192 struct rtp_packet { - struct rtpp_refcnt *rcnt; + struct rtpp_refcnt_fast *rcnt; #if defined(_RTP_H_) struct rtpp_wi *wi; struct rtp_packet *next; diff --git a/src/rtpp_mallocs.c b/src/rtpp_mallocs.c index 533a89471..6ad1639db 100644 --- a/src/rtpp_mallocs.c +++ b/src/rtpp_mallocs.c @@ -66,27 +66,27 @@ struct alig_help { void * #if !defined(RTPP_CHECK_LEAKS) -rtpp_rzmalloc(size_t msize, size_t rcntp_offs) +rtpp_rgmalloc(enum rtpp_ml_reftype rt, enum rtpp_ml_zflag zf, size_t msize, + size_t rcntp_offs) #else -rtpp_rzmalloc_memdeb(size_t msize, size_t rcntp_offs, void *memdeb_p, +rtpp_rgmalloc_memdeb(enum rtpp_ml_reftype rt, enum rtpp_ml_zflag zf, + size_t msize, size_t rcntp_offs, void *memdeb_p, const struct rtpp_codeptr *mlp) #endif { void *rval; - struct rtpp_refcnt *rcnt; - size_t pad_size, asize; + union { + struct rtpp_refcnt *norm; + struct rtpp_refcnt_fast *fast; + } rcnt; + size_t pad_size, asize, rosize; void *rco; RTPP_DBG_ASSERT(msize >= rcntp_offs + sizeof(struct rtpp_refcnt *)); - if (offsetof(struct alig_help, b) > 1) { - pad_size = msize % offsetof(struct alig_help, b); - if (pad_size != 0) { - pad_size = offsetof(struct alig_help, b) - pad_size; - } - } else { - pad_size = 0; - } - asize = msize + pad_size + rtpp_refcnt_osize(); + size_t norm_off = offsetof(struct alig_help, b); + pad_size = (norm_off - msize) & (norm_off - 1); + rosize = (rt != MLT_RF) ? rtpp_refcnt_osize() : rtpp_refcnt_fast_osize; + asize = msize + pad_size + rosize; #if !defined(RTPP_CHECK_LEAKS) rval = malloc(asize); #else @@ -95,49 +95,19 @@ rtpp_rzmalloc_memdeb(size_t msize, size_t rcntp_offs, void *memdeb_p, if (rval == NULL) { return (NULL); } - memset(rval, '\0', asize); + if (zf == MLT_ZR) { + memset(rval, '\0', asize); + } rco = (char *)rval + msize + pad_size; - rcnt = rtpp_refcnt_ctor_pa(rco); - *PpP(rval, rcntp_offs, struct rtpp_refcnt **) = rcnt; - - return (rval); -} - -void * -#if !defined(RTPP_CHECK_LEAKS) -rtpp_rmalloc(size_t msize, size_t rcntp_offs) -#else -rtpp_rmalloc_memdeb(size_t msize, size_t rcntp_offs, void *memdeb_p, - const struct rtpp_codeptr *mlp) -#endif -{ - void *rval; - struct rtpp_refcnt *rcnt; - size_t pad_size, asize; - void *rco; - - RTPP_DBG_ASSERT(msize >= rcntp_offs + sizeof(struct rtpp_refcnt *)); - if (offsetof(struct alig_help, b) > 1) { - pad_size = msize % offsetof(struct alig_help, b); - if (pad_size != 0) { - pad_size = offsetof(struct alig_help, b) - pad_size; - } - } else { - pad_size = 0; + if (zf != MLT_ZR) { + memset(rco, '\0', rosize); } - asize = msize + pad_size + rtpp_refcnt_osize(); -#if !defined(RTPP_CHECK_LEAKS) - rval = malloc(asize); -#else - rval = rtpp_memdeb_malloc(asize, memdeb_p, mlp); -#endif - if (rval == NULL) { - return (NULL); + if (rt != MLT_RF) { + rcnt.norm = rtpp_refcnt_ctor_pa(rco); + } else { + rcnt.fast = rtpp_refcnt_fast_ctor(rco); } - rco = (char *)rval + msize + pad_size; - memset(rco, '\0', rtpp_refcnt_osize()); - rcnt = rtpp_refcnt_ctor_pa(rco); - *PpP(rval, rcntp_offs, struct rtpp_refcnt **) = rcnt; + *PpP(rval, rcntp_offs, struct rtpp_refcnt **) = rcnt.norm; return (rval); } diff --git a/src/rtpp_mallocs.h b/src/rtpp_mallocs.h index 93a22faa9..ba8e65de5 100644 --- a/src/rtpp_mallocs.h +++ b/src/rtpp_mallocs.h @@ -29,26 +29,28 @@ #ifndef _RTPP_MALLOCS_H_ #define _RTPP_MALLOCS_H_ +enum rtpp_ml_reftype {MLT_R, MLT_RF}; +enum rtpp_ml_zflag {MLT_NZ, MLT_ZR}; + /* Function prototypes */ #if defined(RTPP_CHECK_LEAKS) +void *rtpp_rgmalloc_memdeb(enum rtpp_ml_reftype, enum rtpp_ml_zflag, size_t, size_t, void *, HERETYPE); +static inline void *rtpp_rzmalloc_memdeb(size_t msize, size_t rcntp_offs, void *memdeb_p, + const struct rtpp_codeptr *mlp) {return(rtpp_rgmalloc_memdeb(MLT_R, MLT_ZR, msize, rcntp_offs, + memdeb_p, mlp));} + #define rtpp_zmalloc(s) rtpp_zmalloc_memdeb((s), MEMDEB_SYM, HEREVAL) +#define rtpp_rzmalloc(x, y) rtpp_rgmalloc_memdeb(MLT_R, MLT_ZR, x, y, MEMDEB_SYM, HEREVAL) +#define rtpp_rmalloc(x, y) rtpp_rgmalloc_memdeb(MLT_R, MLT_NZ, x, y, MEMDEB_SYM, HEREVAL) +#define rtpp_rgmalloc(a, b, x, y) rtpp_rgmalloc_memdeb(a, b, x, y, MEMDEB_SYM, HEREVAL) void *rtpp_zmalloc_memdeb(size_t, void *, HERETYPE); #else -void *rtpp_zmalloc(size_t); -#endif +void *rtpp_rgmalloc(enum rtpp_ml_reftype, enum rtpp_ml_zflag, size_t, size_t); +static inline void *rtpp_rzmalloc(size_t a, size_t b) {return(rtpp_rgmalloc(MLT_R, MLT_ZR, a, b));} -#if defined(RTPP_CHECK_LEAKS) -#define rtpp_rzmalloc(x, y) rtpp_rzmalloc_memdeb(x, y, MEMDEB_SYM, HEREVAL) -void *rtpp_rzmalloc_memdeb(size_t, size_t, void *, HERETYPE); -#else -void *rtpp_rzmalloc(size_t, size_t); -#endif - -#if defined(RTPP_CHECK_LEAKS) -#define rtpp_rmalloc(x, y) rtpp_rmalloc_memdeb(x, y, MEMDEB_SYM, HEREVAL) -void *rtpp_rmalloc_memdeb(size_t, size_t, void *, HERETYPE); -#else -void *rtpp_rmalloc(size_t, size_t); +#define rtpp_rzmalloc(x, y) rtpp_rgmalloc(MLT_R, MLT_ZR, x, y) +#define rtpp_rmalloc(x, y) rtpp_rgmalloc(MLT_R, MLT_NZ, x, y) +void *rtpp_zmalloc(size_t); #endif #endif diff --git a/src/rtpp_netio_async.c b/src/rtpp_netio_async.c index 5324b874f..9b687463b 100644 --- a/src/rtpp_netio_async.c +++ b/src/rtpp_netio_async.c @@ -49,11 +49,11 @@ #include "rtpp_time.h" #include "rtp_packet.h" #include "rtpp_types.h" +#include "rtpp_refcnt.h" #include "rtpp_wi.h" #include "rtpp_wi_pkt.h" #include "rtpp_wi_sgnl.h" #include "rtpp_wi_private.h" -#include "rtpp_refcnt.h" #include "rtpp_log_obj.h" #include "rtpp_queue.h" #include "rtpp_network.h" diff --git a/src/rtpp_refcnt.c b/src/rtpp_refcnt.c index 167759549..ee7a4f931 100644 --- a/src/rtpp_refcnt.c +++ b/src/rtpp_refcnt.c @@ -41,6 +41,7 @@ #include "rtpp_types.h" #include "rtpp_mallocs.h" #include "rtpp_refcnt.h" +#include "rtpp_util.h" #include "rtpp_refcnt_fin.h" #if RTPP_DEBUG_refcnt @@ -50,9 +51,6 @@ #endif #endif -static void rtpp_refcnt_incref(struct rtpp_refcnt *); -static void rtpp_refcnt_decref(struct rtpp_refcnt *); - /* * Somewhat arbitrary cap on the maximum value of the references. Just here * to catch any runaway situations, i.e. bugs in the code. @@ -76,6 +74,8 @@ struct rtpp_refcnt_priv int flags; }; +static void rtpp_refcnt_incref(struct rtpp_refcnt *); +static void rtpp_refcnt_decref(struct rtpp_refcnt *); static void rtpp_refcnt_attach(struct rtpp_refcnt *, rtpp_refcnt_dtor_t, void *); static void *rtpp_refcnt_getdata(struct rtpp_refcnt *); @@ -297,3 +297,82 @@ rtpp_refcnt_use_stdfree(struct rtpp_refcnt *pub, void *data) pvt->flags |= RC_FLAG_PA_STDFREE; pvt->data = data; } + +struct rtpp_refcnt_fast_priv +{ + struct rtpp_refcnt_fast pub; + atomic_long cnt; + refcnt_dtorspec_t dspec; +}; +const size_t rtpp_refcnt_fast_osize = sizeof(struct rtpp_refcnt_fast_priv); + +static void rtpp_refcnt_fast_incref(rtpp_refcnt_fast_rot *); +static void rtpp_refcnt_fast_decref(rtpp_refcnt_fast_rot *); +static refcnt_dtorspec_t rtpp_refcnt_fast_attach(rtpp_refcnt_fast_rot *, + const refcnt_dtorspec_t *); + +DEFINE_SMETHODS(rtpp_refcnt_fast, + .incref = &rtpp_refcnt_fast_incref, + .decref = &rtpp_refcnt_fast_decref, + .attach = &rtpp_refcnt_fast_attach, +); + +struct rtpp_refcnt_fast * +rtpp_refcnt_fast_ctor(void *pap) +{ + struct rtpp_refcnt_fast_priv *pvt; + + pvt = (struct rtpp_refcnt_fast_priv *)pap; +#if defined(RTPP_DEBUG) + pvt->pub.smethods = rtpp_refcnt_fast_smethods; +#endif + atomic_init(&pvt->cnt, 1); + return (&pvt->pub); +} + +static void +rtpp_refcnt_fast_incref(struct rtpp_refcnt_fast *pub) +{ + struct rtpp_refcnt_fast_priv *pvt; + + PUB2PVT(pub, pvt); + RTPP_DBG_ASSERT(((uintptr_t)&pvt->cnt & 0b111) == 0 && atomic_load(&pvt->cnt) > 0 && atomic_load(&pvt->cnt) < RC_ABS_MAX); + atomic_fetch_add_explicit(&pvt->cnt, 1, memory_order_relaxed); +} + +static void +rtpp_refcnt_fast_decref(struct rtpp_refcnt_fast *pub) +{ + struct rtpp_refcnt_fast_priv *pvt; + int oldcnt; + + PUB2PVT(pub, pvt); + RTPP_DBG_ASSERT(((uintptr_t)&pvt->cnt & 0b111) == 0); + oldcnt = atomic_fetch_sub_explicit(&pvt->cnt, 1, memory_order_release); + if (likely(oldcnt > 1)) + return; + atomic_thread_fence(memory_order_acquire); + pvt->dspec.dtor(pvt->dspec.ptr); + return; +} + +static refcnt_dtorspec_t +rtpp_refcnt_fast_attach(struct rtpp_refcnt_fast *pub, const refcnt_dtorspec_t *dsp) +{ + struct rtpp_refcnt_fast_priv *pvt; + refcnt_dtorspec_t old; + + PUB2PVT(pub, pvt); + old = pvt->dspec; + pvt->dspec.ptr = dsp->ptr; + if (dsp->dtor == DTOR_STDFREE) { +#if !defined(RTPP_CHECK_LEAKS) + pvt->dspec.dtor = free; +#else + pvt->dspec.dtor = rtpp_refcnt_free; +#endif + } else { + pvt->dspec.dtor = dsp->dtor; + } + return (old); +} diff --git a/src/rtpp_refcnt.h b/src/rtpp_refcnt.h index e85425c56..6d304bc82 100644 --- a/src/rtpp_refcnt.h +++ b/src/rtpp_refcnt.h @@ -65,3 +65,41 @@ rtpp_refcnt_rot *rtpp_refcnt_ctor_pa(void *); #define RC_INCREF(rp) CALL_SMETHOD(rp, incref); #define RC_DECREF(rp) CALL_SMETHOD(rp, decref); + +DECLARE_CLASS(rtpp_refcnt_fast, void *); + +extern const size_t rtpp_refcnt_fast_osize; + +#define DTOR_STDFREE ((rtpp_refcnt_dtor_t)(void *)(-1)) +#define USE_STDFREE(p) (&(refcnt_dtorspec_t){.dtor = DTOR_STDFREE, .ptr = (p)}) +#define USE_DTOR(dtr, p) (&(refcnt_dtorspec_t){.dtor = (rtpp_refcnt_dtor_t)(dtr), .ptr = (p)}) + +struct refcnt_dtorspec { + rtpp_refcnt_dtor_t dtor; + void *ptr; +}; +typedef struct refcnt_dtorspec refcnt_dtorspec_t; + +DECLARE_METHOD(rtpp_refcnt_fast, refcnt_fast_incref, void); +DECLARE_METHOD(rtpp_refcnt_fast, refcnt_fast_decref, void); +DECLARE_METHOD(rtpp_refcnt_fast, refcnt_fast_getdata, void *); +DECLARE_METHOD(rtpp_refcnt_fast, refcnt_fast_attach, refcnt_dtorspec_t, + const refcnt_dtorspec_t *); + +DECLARE_SMETHODS(rtpp_refcnt_fast) +{ + METHOD_ENTRY(refcnt_fast_incref, incref); + METHOD_ENTRY(refcnt_fast_decref, decref); + METHOD_ENTRY(refcnt_fast_getdata, getdata); + METHOD_ENTRY(refcnt_fast_attach, attach); +}; + +struct rtpp_refcnt_fast +{ +#if defined(RTPP_FINTEST) + struct rtpp_refcnt_fast *rcnt; +#endif +#if defined(RTPP_DEBUG) + const struct rtpp_refcnt_fast_smethods * smethods; +#endif +}; diff --git a/src/rtpp_rzmalloc_perf.c b/src/rtpp_rzmalloc_perf.c index 62976e06f..ae264780b 100644 --- a/src/rtpp_rzmalloc_perf.c +++ b/src/rtpp_rzmalloc_perf.c @@ -48,7 +48,10 @@ RTPP_MEMDEB_APP_STATIC; struct dummy { struct { - struct rtpp_refcnt *rcnt; + union { + struct rtpp_refcnt *rcnt; + struct rtpp_refcnt_fast *rcnt_fast; + }; } pub; }; @@ -68,6 +71,22 @@ rtpp_rzmalloc_perf(void) return (NULL); } +static struct dummy * +rtpp_rgmalloc_perf(void) +{ + struct dummy *pvt; + + pvt = rtpp_rgmalloc(MLT_RF, MLT_ZR, sizeof(struct dummy), PVT_RCOFFS(pvt)); + if (pvt == NULL) { + goto e0; + } + CALL_SMETHOD(pvt->pub.rcnt_fast, attach, USE_STDFREE(pvt)); + return (pvt); + +e0: + return (NULL); +} + static struct dummy * rtpp_refcnt_perf(void) { @@ -130,6 +149,7 @@ main(int argc, char **argv) const char *tname; } *tp, tests[] = { {.pfunc_ctor = rtpp_rzmalloc_perf, .tname = "rtpp_rzmalloc()"}, + {.pfunc_ctor = rtpp_rgmalloc_perf, .tname = "rtpp_rgmalloc()"}, {.pfunc_ctor = rtpp_refcnt_perf, .tname = "rtpp_zmalloc()+rtpp_refcnt()"}, #if defined(RTPP_DEBUG) {.pfunc_ctor = rtpp_refcnt_trace_perf, .tname = "rtpp_zmalloc()+rtpp_refcnt(traceen)"}, @@ -155,17 +175,29 @@ main(int argc, char **argv) j = i % 1000; if (i >= 1000) { for (k = 0; k < 11; k++) { - RTPP_OBJ_DECREF(&(dpbuf[j]->pub)); + if (tp->pfunc_ctor != rtpp_rgmalloc_perf) { + RTPP_OBJ_DECREF(&(dpbuf[j]->pub)); + } else { + RC_DECREF(dpbuf[j]->pub.rcnt_fast); + } } } dpbuf[j] = tp->pfunc_ctor(); for (k = 0; k < 10; k++) { - RTPP_OBJ_INCREF(&(dpbuf[j]->pub)); + if (tp->pfunc_ctor != rtpp_rgmalloc_perf) { + RTPP_OBJ_INCREF(&(dpbuf[j]->pub)); + } else { + RC_INCREF(dpbuf[j]->pub.rcnt_fast); + } } } for (i = 0; i < 1000; i++) { for (k = 0; k < 11; k++) { - RTPP_OBJ_DECREF(&(dpbuf[i]->pub)); + if (tp->pfunc_ctor != rtpp_rgmalloc_perf) { + RTPP_OBJ_DECREF(&(dpbuf[i]->pub)); + } else { + RC_DECREF(dpbuf[i]->pub.rcnt_fast); + } } } etime = getdtime() - stime; diff --git a/src/rtpp_types.h b/src/rtpp_types.h index 966f7706c..5f08d0e30 100644 --- a/src/rtpp_types.h +++ b/src/rtpp_types.h @@ -83,6 +83,7 @@ struct rtpp_type_linkable { #define CALL_METHOD(obj, method, args...) (obj)->method(obj, ## args) extern const struct rtpp_refcnt_smethods * const rtpp_refcnt_smethods; +extern const struct rtpp_refcnt_fast_smethods * const rtpp_refcnt_fast_smethods; extern const struct rtpp_pearson_perfect_smethods * const rtpp_pearson_perfect_smethods; extern const struct rtpp_netaddr_smethods * const rtpp_netaddr_smethods; extern const struct rtpp_server_smethods * const rtpp_server_smethods; @@ -106,6 +107,7 @@ extern const struct pproc_manager_smethods * const pproc_manager_smethods; #define GET_SMETHODS(obj) _Generic((obj), \ struct rtpp_refcnt *: rtpp_refcnt_smethods, \ + struct rtpp_refcnt_fast *: rtpp_refcnt_fast_smethods, \ struct rtpp_pearson_perfect *: rtpp_pearson_perfect_smethods, \ struct rtpp_netaddr *: rtpp_netaddr_smethods, \ struct rtpp_server *: rtpp_server_smethods, \ diff --git a/src/rtpp_util.h b/src/rtpp_util.h index 73024d232..d65c5f298 100644 --- a/src/rtpp_util.h +++ b/src/rtpp_util.h @@ -85,3 +85,6 @@ void rtpp_strsplit(char *, char *, size_t, size_t); #define warnx(format, args...) \ fprintf(stderr, format "\n", ## args) #endif + +#define unlikely(expr) __builtin_expect(!!(expr), 0) +#define likely(expr) __builtin_expect(!!(expr), 1) diff --git a/src/rtpp_wi.h b/src/rtpp_wi.h index cd596acc0..f981488aa 100644 --- a/src/rtpp_wi.h +++ b/src/rtpp_wi.h @@ -36,7 +36,7 @@ enum rtpp_wi_type {RTPP_WI_TYPE_OPKT = 0, RTPP_WI_TYPE_SGNL = 1, #define rtpp_wi_get_type(wip) ((wip)->wi_type) struct rtpp_wi { - struct rtpp_refcnt *rcnt; + struct rtpp_refcnt_fast *rcnt; struct rtpp_wi *next; enum rtpp_wi_type wi_type; }; diff --git a/src/rtpp_wi_apis.c b/src/rtpp_wi_apis.c index 55312637e..e29a40c10 100644 --- a/src/rtpp_wi_apis.c +++ b/src/rtpp_wi_apis.c @@ -49,11 +49,12 @@ rtpp_wi_malloc_apis(const char *apiname, void *data, size_t datalen) { struct rtpp_wi_apis *wipp; - wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_apis) + datalen, PVT_RCOFFS(wipp)); + wipp = rtpp_rgmalloc(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_apis) + datalen, PVT_RCOFFS(wipp)); if (wipp == NULL) { return (NULL); } - CALL_SMETHOD(wipp->pub.rcnt, use_stdfree, wipp); + + CALL_SMETHOD(wipp->pub.rcnt, attach, USE_STDFREE(wipp)); *wipp = (const struct rtpp_wi_apis) { .pub.wi_type = RTPP_WI_TYPE_API_STR, .pub.rcnt = wipp->pub.rcnt, diff --git a/src/rtpp_wi_data.c b/src/rtpp_wi_data.c index b414238bd..4041720e5 100644 --- a/src/rtpp_wi_data.c +++ b/src/rtpp_wi_data.c @@ -48,11 +48,11 @@ rtpp_wi_malloc_data(void *dataptr, size_t datalen) { struct rtpp_wi_data *wipp; - wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); + wipp = rtpp_rgmalloc(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); if (wipp == NULL) { return (NULL); } - CALL_SMETHOD(wipp->pub.rcnt, use_stdfree, wipp); + CALL_SMETHOD(wipp->pub.rcnt, attach, USE_STDFREE(wipp)); *wipp = (const struct rtpp_wi_data) { .pub.wi_type = RTPP_WI_TYPE_DATA, .pub.rcnt = wipp->pub.rcnt @@ -69,11 +69,11 @@ rtpp_wi_malloc_udata(void **dataptr, size_t datalen) { struct rtpp_wi_data *wipp; - wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); + wipp = rtpp_rgmalloc(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); if (wipp == NULL) { return (NULL); } - CALL_SMETHOD(wipp->pub.rcnt, use_stdfree, wipp); + CALL_SMETHOD(wipp->pub.rcnt, attach, USE_STDFREE(wipp)); *wipp = (const struct rtpp_wi_data) { .pub.wi_type = RTPP_WI_TYPE_DATA, .pub.rcnt = wipp->pub.rcnt diff --git a/src/rtpp_wi_pkt.c b/src/rtpp_wi_pkt.c index c9fcd228b..aa43e288a 100644 --- a/src/rtpp_wi_pkt.c +++ b/src/rtpp_wi_pkt.c @@ -60,7 +60,7 @@ rtpp_wi_malloc(int sock, const void *msg, size_t msg_len, int flags, { struct rtpp_wi_sendto *wis; - wis = rtpp_rmalloc(sizeof(struct rtpp_wi_sendto) + msg_len, PVT_RCOFFS(&wis->wip)); + wis = rtpp_rgmalloc(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_sendto) + msg_len, PVT_RCOFFS(&wis->wip)); if (wis == NULL) { return (NULL); } @@ -77,8 +77,7 @@ rtpp_wi_malloc(int sock, const void *msg, size_t msg_len, int flags, }; memcpy(wis->msg, msg, msg_len); memcpy(&(wis->to), sendto, tolen); - CALL_SMETHOD(wis->wip.pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_wi_free, - wis); + CALL_SMETHOD(wis->wip.pub.rcnt, attach, USE_DTOR(&rtpp_wi_free, wis)); return (&(wis->wip.pub)); } @@ -104,8 +103,8 @@ rtpp_wi_malloc_pkt_na(int sock, struct rtp_packet *pkt, RC_INCREF(sock_rcnt); wipp->sock_rcnt = sock_rcnt; } - CALL_SMETHOD(pkt->rcnt, reg_pd, (rtpp_refcnt_dtor_t)rtpp_wi_pkt_free, - wipp); + wipp->prevdtor = CALL_SMETHOD(pkt->rcnt, attach, USE_DTOR(rtpp_wi_pkt_free, + wipp)); return (&(wipp->pub)); } @@ -129,4 +128,5 @@ rtpp_wi_pkt_free(struct rtpp_wi_pvt *wipp) if (wipp->log != NULL) { RTPP_OBJ_DECREF(wipp->log); } + wipp->prevdtor.dtor(wipp->prevdtor.ptr); } diff --git a/src/rtpp_wi_private.h b/src/rtpp_wi_private.h index 5615a8f76..15f8bed98 100644 --- a/src/rtpp_wi_private.h +++ b/src/rtpp_wi_private.h @@ -43,6 +43,7 @@ struct rtpp_wi_pvt { int nsend; int debug; struct rtpp_log *log; + refcnt_dtorspec_t prevdtor; char data[0]; }; diff --git a/src/rtpp_wi_sgnl.c b/src/rtpp_wi_sgnl.c index 5de3567b8..af7e6b2fd 100644 --- a/src/rtpp_wi_sgnl.c +++ b/src/rtpp_wi_sgnl.c @@ -53,15 +53,15 @@ rtpp_wi_malloc_sgnl_memdeb(const struct rtpp_codeptr *mlp, int signum, const voi { struct rtpp_wi_sgnl *wipp; #if !defined(RTPP_CHECK_LEAKS) - wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_sgnl) + datalen, PVT_RCOFFS(wipp)); + wipp = rtpp_rgmalloc(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_sgnl) + datalen, PVT_RCOFFS(wipp)); #else - wipp = rtpp_rmalloc_memdeb(sizeof(struct rtpp_wi_sgnl) + datalen, + wipp = rtpp_rgmalloc_memdeb(MLT_RF, MLT_NZ, sizeof(struct rtpp_wi_sgnl) + datalen, PVT_RCOFFS(wipp), MEMDEB_SYM, mlp); #endif if (wipp == NULL) { return (NULL); } - CALL_SMETHOD(wipp->pub.rcnt, use_stdfree, wipp); + CALL_SMETHOD(wipp->pub.rcnt, attach, USE_STDFREE(wipp)); *wipp = (const struct rtpp_wi_sgnl) { .pub.wi_type = RTPP_WI_TYPE_SGNL, .pub.rcnt = wipp->pub.rcnt,