From 5fa6438b25a85092259019d8c60e6c914af5ef3d Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Fri, 13 Feb 2015 11:56:55 +0200 Subject: [PATCH] Clean up calls to memset. - Replaces all calls to memset with new FILL and FILL_ARRAY macros. The use of memset was inconsistent and we never use it for anything complicated. --- src/config.c | 3 ++- src/cu.c | 2 +- src/encoderstate.c | 2 +- src/global.h | 9 +++++++++ src/rdo.c | 19 ++++++++++--------- src/sao.c | 10 ++++------ src/search.c | 6 +++--- src/strategyselector.c | 2 +- src/threadqueue.c | 2 +- src/videoframe.c | 2 +- tests/intra_sad_tests.c | 6 +++--- tests/satd_tests.c | 4 ++-- 12 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/config.c b/src/config.c index c134a8a2..30bfdbee 100644 --- a/src/config.c +++ b/src/config.c @@ -41,7 +41,8 @@ config *config_alloc(void) return cfg; } - memset(cfg, 0, sizeof(config)); + FILL(*cfg, 0); + return cfg; } diff --git a/src/cu.c b/src/cu.c index a986cbc8..449f60c2 100644 --- a/src/cu.c +++ b/src/cu.c @@ -60,7 +60,7 @@ cu_array * cu_array_alloc(const int width_in_scu, const int height_in_scu) { cua = MALLOC(cu_array, 1); cua->data = (cu_info*)malloc(sizeof(cu_info) * cu_array_size); cua->refcount = 1; - memset(cua->data, 0, sizeof(cu_info) * cu_array_size); + FILL_ARRAY(cua->data, 0, cu_array_size); return cua; } diff --git a/src/encoderstate.c b/src/encoderstate.c index c2bd0f22..182b4e60 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -1589,7 +1589,7 @@ void encode_coeff_nxn(encoder_state * const encoder_state, coefficient *coeff, u cabac_ctx *base_coeff_group_ctx = &(cabac->ctx.cu_sig_coeff_group_model[type]); cabac_ctx *baseCtx = (type == 0) ? &(cabac->ctx.cu_sig_model_luma[0]) : &(cabac->ctx.cu_sig_model_chroma[0]); - memset(sig_coeffgroup_flag,0,sizeof(uint32_t)*64); + FILL(sig_coeffgroup_flag, 0); // Count non-zero coeffs for (i = 0; i < width * width; i++) { diff --git a/src/global.h b/src/global.h index b96c7215..65ed2c2f 100644 --- a/src/global.h +++ b/src/global.h @@ -159,6 +159,15 @@ typedef int16_t coefficient; #define MALLOC(type, num) malloc(sizeof(type) * (num)) #endif +// Use memset through FILL and FILL_ARRAY when appropriate, such as when +// initializing whole structures or arrays. It's still ok to use memset +// directly when doing something more complicated. + +// Fill a structure or a static array with val bytes. +#define FILL(var, val) memset(&(var), (val), sizeof(var)) +// Fill a number of elements in an array with val bytes. +#define FILL_ARRAY(ar, val, size) memset((ar), (val), (size) * sizeof(*(ar))) + #define FREE_POINTER(pointer) { free((void*)pointer); pointer = NULL; } #define MOVE_POINTER(dst_pointer,src_pointer) { dst_pointer = src_pointer; src_pointer = NULL; } diff --git a/src/rdo.c b/src/rdo.c index 2e2fd904..73b1ca08 100644 --- a/src/rdo.c +++ b/src/rdo.c @@ -620,18 +620,19 @@ void rdoq(encoder_state * const encoder_state, coefficient *coef, coefficient * int32_t last_x_bits[32],last_y_bits[32]; calc_last_bits(encoder_state, width, height, type,last_x_bits, last_y_bits); - memset( cost_coeff, 0, sizeof(double) * max_num_coeff ); - memset( cost_sig, 0, sizeof(double) * max_num_coeff ); + FILL_ARRAY(cost_coeff, 0, max_num_coeff); + FILL_ARRAY(cost_sig, 0, max_num_coeff); + if (encoder->sign_hiding) { - memset(rate_inc_up, 0, sizeof(int32_t) * max_num_coeff); - memset(rate_inc_down, 0, sizeof(int32_t) * max_num_coeff); - memset(sig_rate_delta, 0, sizeof(int32_t) * max_num_coeff); - memset(delta_u, 0, sizeof(int32_t) * max_num_coeff); + FILL_ARRAY(rate_inc_up, 0, max_num_coeff); + FILL_ARRAY(rate_inc_down, 0, max_num_coeff); + FILL_ARRAY(sig_rate_delta, 0, max_num_coeff); + FILL_ARRAY(delta_u, 0, max_num_coeff); } - memset( cost_coeffgroup_sig, 0, sizeof(double) * 64 ); - memset( sig_coeffgroup_flag, 0, sizeof(uint32_t) * 64 ); + FILL(cost_coeffgroup_sig, 0); + FILL(sig_coeffgroup_flag, 0); for (cg_scanpos = cg_num-1; cg_scanpos >= 0; cg_scanpos--) { uint32_t cg_blkpos = scan_cg[ cg_scanpos ]; @@ -642,7 +643,7 @@ void rdoq(encoder_state * const encoder_state, coefficient *coef, coefficient * int32_t pattern_sig_ctx = context_calc_pattern_sig_ctx(sig_coeffgroup_flag, cg_pos_x, cg_pos_y, width); - memset( &rd_stats, 0, sizeof (coeffgroup_rd_stats)); + FILL(rd_stats, 0); for (scanpos_in_cg = cg_size-1; scanpos_in_cg >= 0; scanpos_in_cg--) { uint32_t blkpos; int32_t q; diff --git a/src/sao.c b/src/sao.c index 18ebe16c..118d5804 100644 --- a/src/sao.c +++ b/src/sao.c @@ -278,8 +278,8 @@ static int calc_sao_band_offsets(int sao_bands[2][32], int offsets[4], int temp_rate[32]; int best_dist_pos = 0; - memset(dist, 0, 32*sizeof(int)); - memset(temp_rate, 0, 32*sizeof(int)); + FILL(dist, 0); + FILL(temp_rate, 0); // Calculate distortion for each band using N*h^2 - 2*h*E for (band = 0; band < 32; band++) { @@ -614,7 +614,7 @@ static void sao_search_edge_sao(const encoder_state * const encoder_state, // Call calc_sao_edge_dir once for luma and twice for chroma. for (i = 0; i < buf_cnt; ++i) { - memset(cat_sum_cnt, 0, sizeof(int) * 2 * NUM_SAO_EDGE_CATEGORIES); + FILL(cat_sum_cnt, 0); calc_sao_edge_dir(data[i], recdata[i], edge_class, block_width, block_height, cat_sum_cnt); @@ -689,7 +689,7 @@ static void sao_search_band_sao(const encoder_state * const encoder_state, const float temp_rate = 0.0; for (i = 0; i < buf_cnt; ++i) { - memset(sao_bands, 0, 2 * 32 * sizeof(int)); + FILL(sao_bands, 0); calc_sao_bands(encoder_state, data[i], recdata[i],block_width, block_height,sao_bands); @@ -733,8 +733,6 @@ static void sao_search_best_mode(const encoder_state * const encoder_state, cons band_sao.offsets[0] = 0; band_sao.offsets[5] = 0; band_sao.eo_class = SAO_EO0; - //memset(&edge_sao, 0, sizeof(sao_info)); - //memset(&band_sao, 0, sizeof(sao_info)); sao_search_edge_sao(encoder_state, data, recdata, block_width, block_height, buf_cnt, &edge_sao, sao_top, sao_left); sao_search_band_sao(encoder_state, data, recdata, block_width, block_height, buf_cnt, &band_sao, sao_top, sao_left); diff --git a/src/search.c b/src/search.c index 0959b1b6..f24d7775 100644 --- a/src/search.c +++ b/src/search.c @@ -1661,7 +1661,7 @@ static void search_intra_rdo(encoder_state * const encoder_state, pred_cu.intra[2].mode = modes[rdo_mode]; pred_cu.intra[3].mode = modes[rdo_mode]; pred_cu.intra[0].mode_chroma = modes[rdo_mode]; - memset(&pred_cu.cbf, 0, sizeof(pred_cu.cbf)); + FILL(pred_cu.cbf, 0); // Reset transform split data in lcu.cu for this area. lcu_set_trdepth(lcu, x_px, y_px, depth, depth); @@ -1686,7 +1686,7 @@ static void search_intra_rdo(encoder_state * const encoder_state, pred_cu.intra[2].mode = modes[0]; pred_cu.intra[3].mode = modes[0]; pred_cu.intra[0].mode_chroma = modes[0]; - memset(&pred_cu.cbf, 0, sizeof(pred_cu.cbf)); + FILL(pred_cu.cbf, 0); search_intra_trdepth(encoder_state, x_px, y_px, depth, tr_depth, modes[0], MAX_INT, &pred_cu, lcu); } } @@ -2252,7 +2252,7 @@ void search_lcu(encoder_state * const encoder_state, const int x, const int y, c int depth; // Initialize work tree. for (depth = 0; depth <= MAX_PU_DEPTH; ++depth) { - memset(&work_tree[depth], 0, sizeof(work_tree[depth])); + FILL(work_tree[depth], 0); init_lcu_t(encoder_state, x, y, &work_tree[depth], hor_buf, ver_buf); } diff --git a/src/strategyselector.c b/src/strategyselector.c index ccfa7aca..bff44ce6 100644 --- a/src/strategyselector.c +++ b/src/strategyselector.c @@ -239,7 +239,7 @@ out_close: #endif //COMPILE_POWERPC static void set_hardware_flags(int32_t cpuid) { - memset(&g_hardware_flags, 0, sizeof(g_hardware_flags)); + FILL(g_hardware_flags, 0); g_hardware_flags.arm = COMPILE_ARM; g_hardware_flags.intel = COMPILE_INTEL; diff --git a/src/threadqueue.c b/src/threadqueue.c index eb97e937..15e32c60 100644 --- a/src/threadqueue.c +++ b/src/threadqueue.c @@ -482,7 +482,7 @@ int threadqueue_waitfor(threadqueue_queue * const threadqueue, threadqueue_job * threadqueue->queue_count -= i; threadqueue->queue_start = 0; memmove(threadqueue->queue, &threadqueue->queue[i], threadqueue->queue_count * sizeof(*threadqueue->queue)); - memset(&threadqueue->queue[threadqueue->queue_count], 0, i * sizeof(*threadqueue->queue)); + FILL_ARRAY(&threadqueue->queue[threadqueue->queue_count], 0, i); } PTHREAD_UNLOCK(&threadqueue->lock); diff --git a/src/videoframe.c b/src/videoframe.c index 95ce43e9..fc584d1d 100644 --- a/src/videoframe.c +++ b/src/videoframe.c @@ -39,7 +39,7 @@ videoframe *videoframe_alloc(const int32_t width, const int32_t height, const in if (!frame) return 0; - memset(frame, 0, sizeof(videoframe)); + FILL(*frame, 0); frame->width = width; frame->height = height; diff --git a/tests/intra_sad_tests.c b/tests/intra_sad_tests.c index 76e6da23..f529dd39 100644 --- a/tests/intra_sad_tests.c +++ b/tests/intra_sad_tests.c @@ -60,8 +60,8 @@ static void setup_tests() int test = 0; for (int w = LCU_MIN_LOG_W; w <= LCU_MAX_LOG_W; ++w) { unsigned size = 1 << (w * 2); - memset(bufs[test][w][0], 0, size); - memset(bufs[test][w][1], 255, size); + FILL_ARRAY(bufs[test][w][0], 0, size); + FILL_ARRAY(bufs[test][w][1], 255, size); } test = 1; @@ -70,7 +70,7 @@ static void setup_tests() unsigned size = 1 << (w * 2); init_gradient(3, 1, width, 1, bufs[test][w][0]); //init_gradient(width / 2, 0, width, 1, bufs[test][w][1]); - memset(bufs[test][w][1], 128, size); + FILL_ARRAY(bufs[test][w][1], 128, size); } } diff --git a/tests/satd_tests.c b/tests/satd_tests.c index e577a5b8..747acae1 100644 --- a/tests/satd_tests.c +++ b/tests/satd_tests.c @@ -43,8 +43,8 @@ static void setup_tests() int test = 0; for (int w = LCU_MIN_LOG_W; w <= LCU_MAX_LOG_W; ++w) { unsigned size = 1 << (w * 2); - memset(satd_bufs[test][w][0], 0, size); - memset(satd_bufs[test][w][1], 255, size); + FILL_ARRAY(satd_bufs[test][w][0], 0, size); + FILL_ARRAY(satd_bufs[test][w][1], 255, size); } //Checker patterns, buffer 1 is negative of buffer 2