Small cleanups

Make macros without side effects
Use SPA_MIN to calculate ringbuffer areas.
This commit is contained in:
Wim Taymans 2017-11-16 16:31:03 +01:00
parent 0966e703fe
commit d040747d4b
5 changed files with 40 additions and 47 deletions

View file

@ -61,10 +61,26 @@ struct spa_fraction {
};
#define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
#define SPA_MIN(a,b) ((a)<(b) ? (a) : (b))
#define SPA_MAX(a,b) ((a)>(b) ? (a) : (b))
#define SPA_ABS(a) ((a)>0 ? (a) : -(a))
#define SPA_CLAMP(v,a,b) ((v)>(b) ? (b) : ((v) < (a) ? (a) : (v)))
#define SPA_MIN(a,b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a < _b ? _a : _b; \
})
#define SPA_MAX(a,b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; \
})
#define SPA_CLAMP(v,low,high) \
({ \
typeof(v) _v = (v); \
typeof(low) _low = (low); \
typeof(high) _high = (high); \
_v > _high ? _high : ( _v < _low ? _low : _v); \
})
#define SPA_MEMBER(b,o,t) ((t*)((uint8_t*)(b) + (o)))

View file

@ -96,10 +96,10 @@ spa_ringbuffer_read_data(struct spa_ringbuffer *rbuf,
const void *buffer, uint32_t size,
uint32_t offset, void *data, uint32_t len)
{
uint32_t first = SPA_MIN(len, size - offset);
memcpy(data, buffer + offset, first);
if (SPA_UNLIKELY(len > first))
memcpy(data + first, buffer, len - first);
uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
memcpy(data, buffer + offset, l0);
if (SPA_UNLIKELY(l1 > 0))
memcpy(data + l0, buffer, l1);
}
/**
@ -146,10 +146,10 @@ spa_ringbuffer_write_data(struct spa_ringbuffer *rbuf,
void *buffer, uint32_t size,
uint32_t offset, const void *data, uint32_t len)
{
uint32_t first = SPA_MIN(len, size - offset);
memcpy(buffer + offset, data, first);
if (SPA_UNLIKELY(len > first))
memcpy(buffer, data + first, len - first);
uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
memcpy(buffer + offset, data, l0);
if (SPA_UNLIKELY(l1 > 0))
memcpy(buffer, data + l0, l1);
}
/**

View file

@ -703,17 +703,10 @@ add_port_data(struct impl *this, void *out, size_t outsize, size_t next, struct
outsize = SPA_MIN(outsize, insize);
offset = index % maxsize;
if (offset + outsize > maxsize) {
len1 = maxsize - offset;
len2 = outsize - len1;
}
else {
len1 = outsize;
len2 = 0;
}
len1 = SPA_MIN(outsize, maxsize - offset);
mix(out, SPA_MEMBER(data, offset, void), len1);
if (len2 > 0)
if ((len2 = outsize - len1) > 0)
mix(out + len1, data, len2);
spa_ringbuffer_read_update(rb, index + outsize);
@ -763,18 +756,11 @@ static int mix_output(struct impl *this, size_t n_bytes)
filled = spa_ringbuffer_get_write_index(rb, &index);
avail = maxsize - filled;
offset = index % maxsize;
n_bytes = SPA_MIN(n_bytes, avail);
if (offset + n_bytes > maxsize) {
len1 = maxsize - offset;
len2 = n_bytes - len1;
}
else {
len1 = n_bytes;
len2 = 0;
}
offset = index % maxsize;
len1 = SPA_MIN(n_bytes, maxsize - offset);
len2 = n_bytes - len1;
spa_log_trace(this->log, NAME " %p: dequeue output buffer %d %zd %d %d %d",
this, outbuf->outbuf->id, n_bytes, offset, len1, len2);

View file

@ -335,21 +335,15 @@ static int make_buffer(struct impl *this)
avail = maxsize - filled;
n_bytes = SPA_MIN(avail, n_bytes);
n_samples = n_bytes / this->bpf;
offset = index % maxsize;
if (offset + n_bytes > maxsize) {
l0 = (maxsize - offset) / this->bpf;
l1 = n_samples - l0;
}
else {
l0 = n_samples;
l1 = 0;
}
n_samples = n_bytes / this->bpf;
l0 = SPA_MIN(n_bytes, maxsize - offset) / this->bpf;
l1 = n_samples - l0;
this->render_func(this, SPA_MEMBER(data, offset, void), l0);
if (l1)
if (l1 > 0)
this->render_func(this, data, l1);
spa_ringbuffer_write_update(rb, index + n_bytes);

View file

@ -711,11 +711,8 @@ static void do_volume(struct impl *this, struct spa_buffer *dbuf, struct spa_buf
src = SPA_MEMBER(sd[0].data, soffset, int16_t);
dst = SPA_MEMBER(dd[0].data, doffset, int16_t);
n_bytes = towrite;
if (soffset + n_bytes > sd[0].maxsize)
n_bytes = sd[0].maxsize - soffset;
if (doffset + n_bytes > dd[0].maxsize)
n_bytes = dd[0].maxsize - doffset;
n_bytes = SPA_MIN(towrite, sd[0].maxsize - soffset);
n_bytes = SPA_MIN(n_bytes, dd[0].maxsize - doffset);
n_samples = n_bytes / sizeof(int16_t);
for (i = 0; i < n_samples; i++)