]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Replace MAX, MIN, ABS macros by type-checking variants.
authorAndre Noll <maan@systemlinux.org>
Mon, 1 Dec 2008 15:51:37 +0000 (16:51 +0100)
committerAndre Noll <maan@systemlinux.org>
Mon, 1 Dec 2008 15:51:37 +0000 (16:51 +0100)
Taken from the Linux kernel. These macros also evaluate each parameter
only once. Fix up all resulting gcc warnings.

aft.c
attribute.c
audiod.c
compress_filter.c
mp3dec_filter.c
ogg_afh.c
para.h
write.h

diff --git a/aft.c b/aft.c
index 5cca5b8d88f0e6de4e7f5770df63fc0785815049..d5a355566e387df5bc6e8b1ec06fad03bb660c3e 100644 (file)
--- 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) {
index 24a3826d3e051c4cdee6675d01466c12fd9d3d1b..655c286697a6ca68c94da410ec5841276dd97ef5 100644 (file)
@@ -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)
index 33eac4b910f00936c599a71b7cad0c251ae5190f..b9bd852aeba0ee3c65f9eec62ed6de977d41126a 100644 (file)
--- 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 *));
index 6034ce790276c7bbcb8d35312e717394e10f5098..bf129b4ad13b939ec7ceb7cbbd2cf6d3e9563225 100644 (file)
@@ -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;
index 50e530e112aa9992197a7ea7739bd7b2e27013ca..5e5df865b50b17373c404a0fe494373b607dbde2 100644 (file)
@@ -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;
index 11f7f5ad53a9a1f999fa7c7076d88df3aeb4407b..b6132edf7bc3740c55836df48d62d66c9ee5609e 100644 (file)
--- 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 a39ed97df45502a0f93dcd8217cf5d9d6e792c78..3f2da13a859782ba8c5a014d6e6511ecd97d1f28 100644 (file)
--- a/para.h
+++ b/para.h
 /** 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 02471f32b1d1ba5e424bb8571225fed4f02418e8..9723977ae4357506ac1c6b64284695577d1e5683 100644 (file)
--- 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. */