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);
}
/**