From 3062b9da4f71ea236f46efda9a51add1b15cda1a Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 1 Dec 2008 16:51:37 +0100 Subject: [PATCH] Replace MAX, MIN, ABS macros by type-checking variants. Taken from the Linux kernel. These macros also evaluate each parameter only once. Fix up all resulting gcc warnings. --- aft.c | 4 ++-- attribute.c | 2 +- audiod.c | 4 ++-- compress_filter.c | 4 ++-- mp3dec_filter.c | 2 +- ogg_afh.c | 13 ++++++++++--- para.h | 24 ++++++++++++++++++------ write.h | 2 +- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/aft.c b/aft.c index 5cca5b8d..d5a35556 100644 --- a/aft.c +++ b/aft.c @@ -1260,8 +1260,8 @@ static int prepare_ls_row(struct osl_row *row, void *ls_opts) GET_NUM_DIGITS(d->afsi.num_played, &num_digits); w->num_played_width = PARA_MAX(w->num_played_width, num_digits); /* get the number of chars to print this amount of time */ - tmp = get_duration_width(d->afhi.seconds_total); - w->duration_width = PARA_MAX(w->duration_width, tmp); + num_digits = get_duration_width(d->afhi.seconds_total); + w->duration_width = PARA_MAX(w->duration_width, num_digits); GET_NUM_DIGITS(d->afsi.amp, &num_digits); w->amp_width = PARA_MAX(w->amp_width, num_digits); if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) { diff --git a/attribute.c b/attribute.c index 24a3826d..655c2866 100644 --- a/attribute.c +++ b/attribute.c @@ -343,7 +343,7 @@ static void com_addatt_callback(int fd, const struct osl_object *query) aed.name = p; aed.bitnum = bitnum; afs_event(ATTRIBUTE_ADD, &pb, &aed); - greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum); + greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum); } out: if (ret < 0 && ret2 >= 0) diff --git a/audiod.c b/audiod.c index 33eac4b9..b9bd852a 100644 --- a/audiod.c +++ b/audiod.c @@ -603,7 +603,7 @@ static int init_writers(void) struct audio_format_info *a; init_supported_writers(); - nw = PARA_MAX(1, conf.writer_given); + nw = PARA_MAX(1U, conf.writer_given); PARA_INFO_LOG("maximal number of writers: %d\n", nw); FOR_EACH_AUDIO_FORMAT(i) { a = &afi[i]; @@ -720,7 +720,7 @@ static int init_filters(void) int i, ret, nf; filter_init(filters); - nf = PARA_MAX(1, conf.filter_given); + nf = PARA_MAX(1U, conf.filter_given); PARA_INFO_LOG("maximal number of filters: %d\n", nf); FOR_EACH_AUDIO_FORMAT(i) { afi[i].filter_conf = para_malloc(nf * sizeof(void *)); diff --git a/compress_filter.c b/compress_filter.c index 6034ce79..bf129b4a 100644 --- a/compress_filter.c +++ b/compress_filter.c @@ -34,7 +34,7 @@ struct private_compress_data { /** Number of samples already seen. */ unsigned num_samples; /** Absolute value of the maximal sample in the current block. */ - unsigned peak; + int peak; }; static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn) @@ -71,7 +71,7 @@ static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn) pcd->current_gain++; } else pcd->current_gain = PARA_MAX(pcd->current_gain - 2, - 1 << pcd->conf->inertia_arg); + 1U << pcd->conf->inertia_arg); pcd->peak = 0; } fn->loaded += length; diff --git a/mp3dec_filter.c b/mp3dec_filter.c index 50e530e1..5e5df865 100644 --- a/mp3dec_filter.c +++ b/mp3dec_filter.c @@ -35,7 +35,7 @@ static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn) { int i, ret; struct private_mp3dec_data *pmd = fn->private_data; - size_t copy = PARA_MIN(len, 4096); + size_t copy = PARA_MIN(len, (size_t)4096); if (fn->loaded > fn->bufsize * 4 / 5) return 0; diff --git a/ogg_afh.c b/ogg_afh.c index 11f7f5ad..b6132edf 100644 --- a/ogg_afh.c +++ b/ogg_afh.c @@ -34,8 +34,15 @@ struct ogg_datasource { static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource) { struct ogg_datasource *ods = datasource; - size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb), - ret = copy / size; + size_t copy, ret; + + if (!size) + return 0; + + assert(ods->numbytes >= ods->fpos); + ret = ods->numbytes - ods->fpos; + copy = PARA_MIN(ret, size * nmemb); + ret = copy / size; if (!ret) return 0; memcpy(buf, ods->map + ods->fpos, copy); @@ -115,7 +122,7 @@ static int ogg_compute_header_len(char *map, size_t numbytes, struct afh_info *afhi) { int ret; - size_t len = PARA_MIN(numbytes, CHUNK_SIZE); + size_t len = PARA_MIN(numbytes, (size_t)CHUNK_SIZE); int serial; char *buf; diff --git a/para.h b/para.h index a39ed97d..3f2da13a 100644 --- a/para.h +++ b/para.h @@ -30,12 +30,24 @@ /** used in various contexts */ #define MAXLINE 255 -/** compute the minimum of \a a and \a b */ -#define PARA_MIN(a,b) ((a) < (b) ? (a) : (b)) -/** compute the maximum of \a a and \a b */ -#define PARA_MAX(a,b) ((a) > (b) ? (a) : (b)) -/** compute the absolute value of \a a */ -#define PARA_ABS(a) ((a) > 0 ? (a) : -(a)) +/** Compute the minimum of \a x and \a y. */ +#define PARA_MIN(x, y) ({ \ + typeof(x) _min1 = (x); \ + typeof(y) _min2 = (y); \ + (void) (&_min1 == &_min2); \ + _min1 < _min2 ? _min1 : _min2; }) + +/** Compute the maximum of \a x and \a y. */ +#define PARA_MAX(x, y) ({ \ + typeof(x) _max1 = (x); \ + typeof(y) _max2 = (y); \ + (void) (&_max1 == &_max2); \ + _max1 < _max2 ? _max1 : _max2; }) + +/** Compute the absolute value of \a x. */ +#define PARA_ABS(x) ({ \ + typeof(x) _x = (x); \ + _x > 0? _x : -_x; }) /** debug loglevel, gets really noisy */ #define DEBUG 1 diff --git a/write.h b/write.h index 02471f32..9723977a 100644 --- a/write.h +++ b/write.h @@ -95,7 +95,7 @@ struct writer_node_group { /** Array of pointers to the corresponding writer nodes. */ struct writer_node *writer_nodes; /** The maximum of the chunk_bytes values of the writer nodes in this group. */ - size_t max_chunk_bytes; + int max_chunk_bytes; /** Non-zero if an error or end of file was encountered by the feeding task. */ int *input_error; /** Current output buffer. */ -- 2.39.5