From aa9daa1b9dfc74deb2ab4993f74ba61ee2712f73 Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Mon, 28 Dec 2015 18:49:12 +0100 Subject: [PATCH] tbf: Replace static casts by calls to as_ul_tbf/as_dl_tbf Currently casts from gprs_rlcmac_tbf to gprs_rlcmac_ul_tbf and gprs_rlcmac_dl_tbf are done by using static_cast. This doesn't provide protection against converting a gprs_rlcmac_ul_tbf pointer to a gprs_rlcmac_dl_tbf pointer and vice versa. This commit provides two functions as_ul_tbf and as_dl_tbf, that behave similar like dynamic_cast but use the direction field instead of RTTI. Sponsored-by: On-Waves ehf --- src/bts.cpp | 14 ++++++-------- src/gprs_ms.cpp | 4 ++-- src/gprs_rlcmac_ts_alloc.cpp | 12 ++++-------- src/tbf.cpp | 10 +++++----- src/tbf.h | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/bts.cpp b/src/bts.cpp index b7244aa5..1eea0824 100644 --- a/src/bts.cpp +++ b/src/bts.cpp @@ -305,7 +305,7 @@ gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts) && tbf->poll_state == GPRS_RLCMAC_POLL_SCHED && tbf->poll_fn == fn && tbf->trx->trx_no == trx && tbf->control_ts == ts) { - return static_cast(tbf); + return tbf; } } return NULL; @@ -322,7 +322,7 @@ gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts) && tbf->poll_state == GPRS_RLCMAC_POLL_SCHED && tbf->poll_fn == fn && tbf->trx->trx_no == trx && tbf->control_ts == ts) { - return static_cast(tbf); + return tbf; } } return NULL; @@ -1259,14 +1259,12 @@ gprs_rlcmac_tbf *gprs_rlcmac_pdch::tbf_from_list_by_tfi(struct llist_head *tbf_l gprs_rlcmac_ul_tbf *gprs_rlcmac_pdch::ul_tbf_by_tfi(uint8_t tfi) { - return static_cast( - tbf_by_tfi(tfi, GPRS_RLCMAC_UL_TBF)); + return as_ul_tbf(tbf_by_tfi(tfi, GPRS_RLCMAC_UL_TBF)); } gprs_rlcmac_dl_tbf *gprs_rlcmac_pdch::dl_tbf_by_tfi(uint8_t tfi) { - return static_cast( - tbf_by_tfi(tfi, GPRS_RLCMAC_DL_TBF)); + return as_dl_tbf(tbf_by_tfi(tfi, GPRS_RLCMAC_DL_TBF)); } /* lookup TBF Entity (by TFI) */ @@ -1302,7 +1300,7 @@ void gprs_rlcmac_pdch::attach_tbf(gprs_rlcmac_tbf *tbf) m_num_tbfs[tbf->direction] += 1; if (tbf->direction == GPRS_RLCMAC_UL_TBF) { - ul_tbf = static_cast(tbf); + ul_tbf = as_ul_tbf(tbf); m_assigned_usf |= 1 << ul_tbf->m_usf[ts_no]; } m_assigned_tfi[tbf->direction] |= 1UL << tbf->tfi(); @@ -1322,7 +1320,7 @@ void gprs_rlcmac_pdch::detach_tbf(gprs_rlcmac_tbf *tbf) m_num_tbfs[tbf->direction] -= 1; if (tbf->direction == GPRS_RLCMAC_UL_TBF) { - ul_tbf = static_cast(tbf); + ul_tbf = as_ul_tbf(tbf); m_assigned_usf &= ~(1 << ul_tbf->m_usf[ts_no]); } m_assigned_tfi[tbf->direction] &= ~(1UL << tbf->tfi()); diff --git a/src/gprs_ms.cpp b/src/gprs_ms.cpp index 76fe47c2..2d482846 100644 --- a/src/gprs_ms.cpp +++ b/src/gprs_ms.cpp @@ -218,9 +218,9 @@ void GprsMs::stop_timer() void GprsMs::attach_tbf(struct gprs_rlcmac_tbf *tbf) { if (tbf->direction == GPRS_RLCMAC_DL_TBF) - attach_dl_tbf(static_cast(tbf)); + attach_dl_tbf(as_dl_tbf(tbf)); else - attach_ul_tbf(static_cast(tbf)); + attach_ul_tbf(as_ul_tbf(tbf)); } void GprsMs::attach_ul_tbf(struct gprs_rlcmac_ul_tbf *tbf) diff --git a/src/gprs_rlcmac_ts_alloc.cpp b/src/gprs_rlcmac_ts_alloc.cpp index 0daeaf5c..e6c8ad4c 100644 --- a/src/gprs_rlcmac_ts_alloc.cpp +++ b/src/gprs_rlcmac_ts_alloc.cpp @@ -480,14 +480,12 @@ int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, /* The allocation will be successful, so the system state and tbf_/ms_ * may be modified from now on. */ if (tbf->direction == GPRS_RLCMAC_UL_TBF) { - struct gprs_rlcmac_ul_tbf *ul_tbf = - static_cast(tbf_); + struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_); LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d TFI=%d USF=%d\n", ts, tfi, usf); assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf); } else { - struct gprs_rlcmac_dl_tbf *dl_tbf = - static_cast(tbf_); + struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_); LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d TFI=%d\n", ts, tfi); assign_dlink_tbf(pdch, dl_tbf, tfi); @@ -1010,8 +1008,7 @@ int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, tbf_->first_ts = first_ts; if (tbf->direction == GPRS_RLCMAC_DL_TBF) { - struct gprs_rlcmac_dl_tbf *dl_tbf = - static_cast(tbf_); + struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_); for (ts = 0; ts < 8; ts++) { if (!(dl_slots & (1 << ts))) continue; @@ -1021,8 +1018,7 @@ int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi); } } else { - struct gprs_rlcmac_ul_tbf *ul_tbf = - static_cast(tbf_); + struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_); for (ts = 0; ts < 8; ts++) { if (!(ul_slots & (1 << ts))) diff --git a/src/tbf.cpp b/src/tbf.cpp index 86d99acc..ea1b89d7 100644 --- a/src/tbf.cpp +++ b/src/tbf.cpp @@ -315,7 +315,7 @@ void tbf_free(struct gprs_rlcmac_tbf *tbf) /* Give final measurement report */ gprs_rlcmac_rssi_rep(tbf); if (tbf->direction == GPRS_RLCMAC_DL_TBF) { - gprs_rlcmac_dl_tbf *dl_tbf = static_cast(tbf); + gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf); gprs_rlcmac_lost_rep(dl_tbf); dl_tbf->cleanup(); } @@ -446,7 +446,7 @@ void gprs_rlcmac_tbf::poll_timeout() ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE; bts->rlc_ack_timedout(); if (state_is(GPRS_RLCMAC_FINISHED)) { - gprs_rlcmac_ul_tbf *ul_tbf = static_cast(this); + gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(this); ul_tbf->m_n3103++; if (ul_tbf->m_n3103 == ul_tbf->bts->bts_data()->n3103) { LOGP(DRLCMAC, LOGL_NOTICE, @@ -500,7 +500,7 @@ void gprs_rlcmac_tbf::poll_timeout() /* reschedule DL assignment */ dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS; } else if (direction == GPRS_RLCMAC_DL_TBF) { - gprs_rlcmac_dl_tbf *dl_tbf = static_cast(this); + gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(this); if (!(dl_tbf->state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK))) { LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling " @@ -732,7 +732,7 @@ void gprs_rlcmac_tbf::handle_timeout() "in assign state\n", tbf_name(this)); } if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) { - gprs_rlcmac_dl_tbf *dl_tbf = static_cast(this); + gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(this); dl_tbf->m_wait_confirm = 0; if (dl_tbf->state_is(GPRS_RLCMAC_ASSIGN)) { tbf_assign_control_ts(dl_tbf); @@ -828,7 +828,7 @@ struct msgb *gprs_rlcmac_tbf::create_dl_ass(uint32_t fn) /* on uplink TBF we get the downlink TBF to be assigned. */ if (direction == GPRS_RLCMAC_UL_TBF) { - gprs_rlcmac_ul_tbf *ul_tbf = static_cast(this); + gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(this); /* be sure to check first, if contention resolution is done, * otherwise we cannot send the assignment yet */ diff --git a/src/tbf.h b/src/tbf.h index a3a2eeba..190101bf 100644 --- a/src/tbf.h +++ b/src/tbf.h @@ -452,6 +452,23 @@ inline enum gprs_rlcmac_tbf_direction reverse(enum gprs_rlcmac_tbf_direction dir return (enum gprs_rlcmac_tbf_direction) ((int)GPRS_RLCMAC_UL_TBF - (int)dir + (int)GPRS_RLCMAC_DL_TBF); } + +inline gprs_rlcmac_ul_tbf *as_ul_tbf(gprs_rlcmac_tbf *tbf) +{ + if (tbf && tbf->direction == GPRS_RLCMAC_UL_TBF) + return static_cast(tbf); + else + return NULL; +} + +inline gprs_rlcmac_dl_tbf *as_dl_tbf(gprs_rlcmac_tbf *tbf) +{ + if (tbf && tbf->direction == GPRS_RLCMAC_DL_TBF) + return static_cast(tbf); + else + return NULL; +} + #endif #ifdef __cplusplus