[pulseaudio-discuss] [PATCH] memblock: Add pa_memblock_acquire_chunk().

David Henningsson david.henningsson at canonical.com
Fri Aug 17 20:13:53 PDT 2012


On 08/17/2012 05:09 PM, Tanu Kaskinen wrote:
> Besides making the code a bit cleaner, this also gets rid of
> a few "cast increases required alignment of target type"
> warnings.

Looks good. Thanks!

> ---
>   src/modules/bluetooth/module-bluetooth-device.c |    4 +--
>   src/modules/jack/module-jack-sink.c             |    2 +-
>   src/modules/module-equalizer-sink.c             |    2 +-
>   src/modules/module-ladspa-sink.c                |    4 +--
>   src/modules/module-virtual-sink.c               |    4 +--
>   src/modules/module-virtual-source.c             |    3 +-
>   src/modules/module-virtual-surround-sink.c      |    4 +--
>   src/modules/rtp/rtp.c                           |    4 +--
>   src/pulsecore/memblock.c                        |    7 +++++
>   src/pulsecore/memblock.h                        |    6 +++-
>   src/pulsecore/memchunk.c                        |    2 +-
>   src/pulsecore/memchunk.h                        |    6 ++--
>   src/pulsecore/pstream.c                         |    2 +-
>   src/pulsecore/resampler.c                       |   36 +++++++++++------------
>   src/pulsecore/sample-util.c                     |    4 +--
>   src/tests/mix-test.c                            |    2 +-
>   16 files changed, 52 insertions(+), 40 deletions(-)
>
> diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
> index 19d62a6..406e127 100644
> --- a/src/modules/bluetooth/module-bluetooth-device.c
> +++ b/src/modules/bluetooth/module-bluetooth-device.c
> @@ -532,7 +532,7 @@ static int hsp_process_render(struct userdata *u) {
>            * SEQPACKET, and we generated the data of the MTU size, so this
>            * should just work. */
>
> -        p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
> +        p = (const uint8_t *) pa_memblock_acquire_chunk(&u->write_memchunk);
>           l = pa_write(u->stream_fd, p, u->write_memchunk.length, &u->stream_write_type);
>           pa_memblock_release(u->write_memchunk.memblock);
>
> @@ -706,7 +706,7 @@ static int a2dp_process_render(struct userdata *u) {
>
>       /* Try to create a packet of the full MTU */
>
> -    p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
> +    p = (const uint8_t *) pa_memblock_acquire_chunk(&u->write_memchunk);
>       to_encode = u->write_memchunk.length;
>
>       d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
> diff --git a/src/modules/jack/module-jack-sink.c b/src/modules/jack/module-jack-sink.c
> index 017fbf6..f18ff6e 100644
> --- a/src/modules/jack/module-jack-sink.c
> +++ b/src/modules/jack/module-jack-sink.c
> @@ -133,7 +133,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
>
>                   pa_sink_render_full(u->sink, nbytes, &chunk);
>
> -                p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index;
> +                p = pa_memblock_acquire_chunk(&chunk);
>                   pa_deinterleave(p, u->buffer, u->channels, sizeof(float), (unsigned) offset);
>                   pa_memblock_release(chunk.memblock);
>
> diff --git a/src/modules/module-equalizer-sink.c b/src/modules/module-equalizer-sink.c
> index adaef69..f8bb8fc 100644
> --- a/src/modules/module-equalizer-sink.c
> +++ b/src/modules/module-equalizer-sink.c
> @@ -575,7 +575,7 @@ static void process_samples(struct userdata *u){
>   static void input_buffer(struct userdata *u, pa_memchunk *in){
>       size_t fs = pa_frame_size(&(u->sink->sample_spec));
>       size_t samples = in->length/fs;
> -    float *src = (float*) ((uint8_t*) pa_memblock_acquire(in->memblock) + in->index);
> +    float *src = pa_memblock_acquire_chunk(in);
>       pa_assert(u->samples_gathered + samples <= u->input_buffer_max);
>       for(size_t c = 0; c < u->channels; c++) {
>           //buffer with an offset after the overlap from previous
> diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c
> index 64fe5ec..8cbd54a 100644
> --- a/src/modules/module-ladspa-sink.c
> +++ b/src/modules/module-ladspa-sink.c
> @@ -488,8 +488,8 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
>
>       pa_memblockq_drop(u->memblockq, chunk->length);
>
> -    src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
> -    dst = (float*) pa_memblock_acquire(chunk->memblock);
> +    src = pa_memblock_acquire_chunk(&tchunk);
> +    dst = pa_memblock_acquire(chunk->memblock);
>
>       for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
>           for (c = 0; c < u->input_count; c++)
> diff --git a/src/modules/module-virtual-sink.c b/src/modules/module-virtual-sink.c
> index cf11ffa..005df64 100644
> --- a/src/modules/module-virtual-sink.c
> +++ b/src/modules/module-virtual-sink.c
> @@ -238,8 +238,8 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
>
>       pa_memblockq_drop(u->memblockq, chunk->length);
>
> -    src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
> -    dst = (float*) pa_memblock_acquire(chunk->memblock);
> +    src = pa_memblock_acquire_chunk(&tchunk);
> +    dst = pa_memblock_acquire(chunk->memblock);
>
>       /* (3) PUT YOUR CODE HERE TO DO SOMETHING WITH THE DATA */
>
> diff --git a/src/modules/module-virtual-source.c b/src/modules/module-virtual-source.c
> index bf07580..b2a8359 100644
> --- a/src/modules/module-virtual-source.c
> +++ b/src/modules/module-virtual-source.c
> @@ -311,8 +311,7 @@ static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk)
>           pa_assert( target_chunk.memblock );
>
>           /* get target pointer */
> -        target = (void*)((uint8_t*)pa_memblock_acquire(target_chunk.memblock)
> -                         + target_chunk.index);
> +        target = pa_memblock_acquire_chunk(&target_chunk);
>
>           /* set-up mixing structure
>              volume was taken care of in sink and source already */
> diff --git a/src/modules/module-virtual-surround-sink.c b/src/modules/module-virtual-surround-sink.c
> index e13d92a..2a5c596 100644
> --- a/src/modules/module-virtual-surround-sink.c
> +++ b/src/modules/module-virtual-surround-sink.c
> @@ -252,8 +252,8 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
>
>       pa_memblockq_drop(u->memblockq, n * u->sink_fs);
>
> -    src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
> -    dst = (float*) pa_memblock_acquire(chunk->memblock);
> +    src = pa_memblock_acquire_chunk(&tchunk);
> +    dst = pa_memblock_acquire(chunk->memblock);
>
>       for (l = 0; l < n; l++) {
>           memcpy(((char*) u->input_buffer) + u->input_buffer_offset * u->sink_fs, ((char *) src) + l * u->sink_fs, u->sink_fs);
> diff --git a/src/modules/rtp/rtp.c b/src/modules/rtp/rtp.c
> index 04e756d..c094fcf 100644
> --- a/src/modules/rtp/rtp.c
> +++ b/src/modules/rtp/rtp.c
> @@ -88,7 +88,7 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
>
>               pa_assert(chunk.memblock);
>
> -            iov[iov_idx].iov_base = ((uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index);
> +            iov[iov_idx].iov_base = pa_memblock_acquire_chunk(&chunk);
>               iov[iov_idx].iov_len = k;
>               mb[iov_idx] = chunk.memblock;
>               iov_idx ++;
> @@ -203,7 +203,7 @@ int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct
>       chunk->memblock = pa_memblock_ref(c->memchunk.memblock);
>       chunk->index = c->memchunk.index;
>
> -    iov.iov_base = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
> +    iov.iov_base = pa_memblock_acquire_chunk(chunk);
>       iov.iov_len = (size_t) size;
>
>       m.msg_name = NULL;
> diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
> index e30ded8..ea7b274 100644
> --- a/src/pulsecore/memblock.c
> +++ b/src/pulsecore/memblock.c
> @@ -459,6 +459,13 @@ void* pa_memblock_acquire(pa_memblock *b) {
>       return pa_atomic_ptr_load(&b->data);
>   }
>
> +/* No lock necessary */
> +void *pa_memblock_acquire_chunk(const pa_memchunk *c) {
> +    pa_assert(c);
> +
> +    return (uint8_t *) pa_memblock_acquire(c->memblock) + c->index;
> +}
> +
>   /* No lock necessary, in corner cases locks by its own */
>   void pa_memblock_release(pa_memblock *b) {
>       int r;
> diff --git a/src/pulsecore/memblock.h b/src/pulsecore/memblock.h
> index ee97454..84c5d05 100644
> --- a/src/pulsecore/memblock.h
> +++ b/src/pulsecore/memblock.h
> @@ -23,11 +23,14 @@
>     USA.
>   ***/
>
> +typedef struct pa_memblock pa_memblock;
> +
>   #include <sys/types.h>
>   #include <inttypes.h>
>
>   #include <pulse/def.h>
>   #include <pulsecore/atomic.h>
> +#include <pulsecore/memchunk.h>
>
>   /* A pa_memblock is a reference counted memory block. PulseAudio
>    * passes references to pa_memblocks around instead of copying
> @@ -45,7 +48,6 @@ typedef enum pa_memblock_type {
>       PA_MEMBLOCK_TYPE_MAX
>   } pa_memblock_type_t;
>
> -typedef struct pa_memblock pa_memblock;
>   typedef struct pa_mempool pa_mempool;
>   typedef struct pa_mempool_stat pa_mempool_stat;
>   typedef struct pa_memimport_segment pa_memimport_segment;
> @@ -108,7 +110,9 @@ pa_bool_t pa_memblock_ref_is_one(pa_memblock *b);
>   void pa_memblock_set_is_silence(pa_memblock *b, pa_bool_t v);
>
>   void* pa_memblock_acquire(pa_memblock *b);
> +void *pa_memblock_acquire_chunk(const pa_memchunk *c);
>   void pa_memblock_release(pa_memblock *b);
> +
>   size_t pa_memblock_get_length(pa_memblock *b);
>   pa_mempool * pa_memblock_get_pool(pa_memblock *b);
>
> diff --git a/src/pulsecore/memchunk.c b/src/pulsecore/memchunk.c
> index cc242e4..5f03597 100644
> --- a/src/pulsecore/memchunk.c
> +++ b/src/pulsecore/memchunk.c
> @@ -83,7 +83,7 @@ pa_memchunk *pa_memchunk_will_need(const pa_memchunk *c) {
>       /* A version of pa_memblock_will_need() that works on memchunks
>        * instead of memblocks */
>
> -    p = (uint8_t*) pa_memblock_acquire(c->memblock) + c->index;
> +    p = pa_memblock_acquire_chunk(c);
>       pa_will_need(p, c->length);
>       pa_memblock_release(c->memblock);
>
> diff --git a/src/pulsecore/memchunk.h b/src/pulsecore/memchunk.h
> index 217f92d..922ffaa 100644
> --- a/src/pulsecore/memchunk.h
> +++ b/src/pulsecore/memchunk.h
> @@ -22,16 +22,18 @@
>     USA.
>   ***/
>
> +typedef struct pa_memchunk pa_memchunk;
> +
>   #include <pulsecore/memblock.h>
>
>   /* A memchunk describes a part of a memblock. In contrast to the memblock, a
>    * memchunk is not allocated dynamically or reference counted, instead
>    * it is usually stored on the stack and copied around */
>
> -typedef struct pa_memchunk {
> +struct pa_memchunk {
>       pa_memblock *memblock;
>       size_t index, length;
> -} pa_memchunk;
> +};
>
>   /* Make a memchunk writable, i.e. make sure that the caller may have
>    * exclusive access to the memblock and it is not read-only. If needed
> diff --git a/src/pulsecore/pstream.c b/src/pulsecore/pstream.c
> index 9945797..c0262a7 100644
> --- a/src/pulsecore/pstream.c
> +++ b/src/pulsecore/pstream.c
> @@ -577,7 +577,7 @@ static int do_write(pa_pstream *p) {
>           if (p->write.data)
>               d = p->write.data;
>           else {
> -            d = (uint8_t*) pa_memblock_acquire(p->write.memchunk.memblock) + p->write.memchunk.index;
> +            d = pa_memblock_acquire_chunk(&p->write.memchunk);
>               release_memblock = p->write.memchunk.memblock;
>           }
>
> diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
> index 9f19559..df5f8f1 100644
> --- a/src/pulsecore/resampler.c
> +++ b/src/pulsecore/resampler.c
> @@ -1112,8 +1112,8 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input)
>           r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
>       }
>
> -    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
> -    dst = (uint8_t*) pa_memblock_acquire(r->to_work_format_buf.memblock);
> +    src = pa_memblock_acquire_chunk(input);
> +    dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
>
>       r->to_work_format_func(n_samples, src, dst);
>
> @@ -1181,7 +1181,7 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
>           }
>       }
>
> -    src = (uint8_t *) pa_memblock_acquire(input->memblock) + input->index;
> +    src = pa_memblock_acquire_chunk(input);
>       dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
>
>       if (r->map_required) {
> @@ -1261,7 +1261,7 @@ static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input
>           r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
>       }
>
> -    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
> +    src = pa_memblock_acquire_chunk(input);
>       dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
>       r->from_work_format_func(n_samples, src, dst);
>       pa_memblock_release(input->memblock);
> @@ -1338,10 +1338,10 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
>
>       memset(&data, 0, sizeof(data));
>
> -    data.data_in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
> +    data.data_in = pa_memblock_acquire_chunk(input);
>       data.input_frames = (long int) in_n_frames;
>
> -    data.data_out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
> +    data.data_out = pa_memblock_acquire_chunk(output);
>       data.output_frames = (long int) *out_n_frames;
>
>       data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
> @@ -1410,8 +1410,8 @@ static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsi
>       pa_assert(output);
>       pa_assert(out_n_frames);
>
> -    in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
> -    out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
> +    in = pa_memblock_acquire_chunk(input);
> +    out = pa_memblock_acquire_chunk(output);
>
>       pa_assert_se(speex_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);
>
> @@ -1431,8 +1431,8 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
>       pa_assert(output);
>       pa_assert(out_n_frames);
>
> -    in = (int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
> -    out = (int16_t*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
> +    in = pa_memblock_acquire_chunk(input);
> +    out = pa_memblock_acquire_chunk(output);
>
>       pa_assert_se(speex_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);
>
> @@ -1508,8 +1508,8 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
>
>       fz = r->w_sz * r->o_ss.channels;
>
> -    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
> -    dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
> +    src = pa_memblock_acquire_chunk(input);
> +    dst = pa_memblock_acquire_chunk(output);
>
>       for (o_index = 0;; o_index++, r->trivial.o_counter++) {
>           i_index = (r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate;
> @@ -1570,8 +1570,8 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
>       pa_assert(output);
>       pa_assert(out_n_frames);
>
> -    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
> -    dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
> +    src = pa_memblock_acquire_chunk(input);
> +    dst = pa_memblock_acquire_chunk(output);
>
>       i = (r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
>       i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
> @@ -1701,8 +1701,8 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
>           p = pa_memblock_acquire(b);
>
>           /* Now copy the input data, splitting up channels */
> -        t = ((int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index)) + c;
> -        k = (int16_t*) ((uint8_t*) p);
> +        t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
> +        k = p;
>           for (u = 0; u < in_n_frames; u++) {
>               *k = *t;
>               t += r->o_ss.channels;
> @@ -1729,7 +1729,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
>           previous_consumed_frames = consumed_frames;
>
>           /* And place the results in the output buffer */
> -        s = (short*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index) + c;
> +        s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
>           for (u = 0; u < used_frames; u++) {
>               *s = *q;
>               q++;
> @@ -1741,7 +1741,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
>       }
>
>       if (previous_consumed_frames < (int) in_n_frames) {
> -        void *leftover_data = (int16_t *) ((uint8_t *) pa_memblock_acquire(input->memblock) + output->index) + previous_consumed_frames * r->o_ss.channels;
> +        void *leftover_data = (int16_t *) pa_memblock_acquire_chunk(input) + previous_consumed_frames * r->o_ss.channels;
>           size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->o_ss.channels * sizeof(int16_t);
>
>           save_leftover(r, leftover_data, leftover_length);
> diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c
> index 38201b2..d44e140 100644
> --- a/src/pulsecore/sample-util.c
> +++ b/src/pulsecore/sample-util.c
> @@ -202,7 +202,7 @@ size_t pa_mix(
>       }
>
>       for (k = 0; k < nstreams; k++)
> -        streams[k].ptr = (uint8_t*) pa_memblock_acquire(streams[k].chunk.memblock) + streams[k].chunk.index;
> +        streams[k].ptr = pa_memblock_acquire_chunk(&streams[k].chunk);
>
>       for (z = 0; z < nstreams; z++)
>           if (length > streams[z].chunk.length)
> @@ -741,7 +741,7 @@ void pa_volume_memchunk(
>
>       calc_volume_table[spec->format] ((void *)linear, volume);
>
> -    ptr = (uint8_t*) pa_memblock_acquire(c->memblock) + c->index;
> +    ptr = pa_memblock_acquire_chunk(c);
>
>       do_volume (ptr, (void *)linear, spec->channels, c->length);
>
> diff --git a/src/tests/mix-test.c b/src/tests/mix-test.c
> index d797a68..e5190c7 100644
> --- a/src/tests/mix-test.c
> +++ b/src/tests/mix-test.c
> @@ -450,7 +450,7 @@ START_TEST (mix_test) {
>           k.length = i.length;
>           k.index = 0;
>
> -        ptr = (uint8_t*) pa_memblock_acquire(k.memblock) + k.index;
> +        ptr = pa_memblock_acquire_chunk(&k);
>           pa_mix(m, 2, ptr, k.length, &a, NULL, FALSE);
>           pa_memblock_release(k.memblock);
>
>



-- 
David Henningsson, Canonical Ltd.
https://launchpad.net/~diwic


More information about the pulseaudio-discuss mailing list