]> code.delx.au - pulseaudio/blob - src/pulsecore/memchunk.c
7111e1ec18efe75a19f4fc5d090608f2a887c276
[pulseaudio] / src / pulsecore / memchunk.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2.1 of the
11 License, or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include "memchunk.h"
36
37 void pa_memchunk_make_writable(pa_memchunk *c, size_t min) {
38 pa_memblock *n;
39 size_t l;
40
41 assert(c);
42 assert(c->memblock);
43 assert(PA_REFCNT_VALUE(c->memblock) > 0);
44
45 if (PA_REFCNT_VALUE(c->memblock) == 1 &&
46 !c->memblock->read_only &&
47 c->memblock->length >= c->index+min)
48 return;
49
50 l = c->length;
51 if (l < min)
52 l = min;
53
54 n = pa_memblock_new(c->memblock->pool, l);
55 memcpy(n->data, (uint8_t*) c->memblock->data + c->index, c->length);
56 pa_memblock_unref(c->memblock);
57 c->memblock = n;
58 c->index = 0;
59 }
60
61 void pa_memchunk_reset(pa_memchunk *c) {
62 assert(c);
63
64 c->memblock = NULL;
65 c->length = c->index = 0;
66 }