]> code.delx.au - pulseaudio/blob - src/pulsecore/memblock.h
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / memblock.h
1 #ifndef foomemblockhfoo
2 #define foomemblockhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <sys/types.h>
26 #include <inttypes.h>
27
28 /* A pa_memblock is a reference counted memory block. Polypaudio
29 * passed references to pa_memblocks around instead of copying
30 * data. See pa_memchunk for a structure that describes parts of
31 * memory blocks. */
32
33 /* The type of memory this block points to */
34 typedef enum pa_memblock_type {
35 PA_MEMBLOCK_FIXED, /* data is a pointer to fixed memory that needs not to be freed */
36 PA_MEMBLOCK_APPENDED, /* The most common kind: the data is appended to the memory block */
37 PA_MEMBLOCK_DYNAMIC, /* data is a pointer to some memory allocated with pa_xmalloc() */
38 PA_MEMBLOCK_USER /* User supplied memory, to be freed with free_cb */
39 } pa_memblock_type_t;
40
41 /* A structure of keeping memory block statistics */
42 /* Maintains statistics about memory blocks */
43 typedef struct pa_memblock_stat {
44 int ref;
45 unsigned total;
46 unsigned total_size;
47 unsigned allocated;
48 unsigned allocated_size;
49 } pa_memblock_stat;
50
51 typedef struct pa_memblock {
52 pa_memblock_type_t type;
53 unsigned ref; /* the reference counter */
54 int read_only; /* boolean */
55 size_t length;
56 void *data;
57 void (*free_cb)(void *p); /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
58 pa_memblock_stat *stat;
59 } pa_memblock;
60
61 /* Allocate a new memory block of type PA_MEMBLOCK_APPENDED */
62 pa_memblock *pa_memblock_new(size_t length, pa_memblock_stat*s);
63
64 /* Allocate a new memory block of type PA_MEMBLOCK_DYNAMIC. The pointer data is to be maintained be the memory block */
65 pa_memblock *pa_memblock_new_dynamic(void *data, size_t length, pa_memblock_stat*s);
66
67 /* Allocate a new memory block of type PA_MEMBLOCK_FIXED */
68 pa_memblock *pa_memblock_new_fixed(void *data, size_t length, int read_only, pa_memblock_stat*s);
69
70 /* Allocate a new memory block of type PA_MEMBLOCK_USER */
71 pa_memblock *pa_memblock_new_user(void *data, size_t length, void (*free_cb)(void *p), int read_only, pa_memblock_stat*s);
72
73 void pa_memblock_unref(pa_memblock*b);
74 pa_memblock* pa_memblock_ref(pa_memblock*b);
75
76 /* This special unref function has to be called by the owner of the
77 memory of a static memory block when he wants to release all
78 references to the memory. This causes the memory to be copied and
79 converted into a PA_MEMBLOCK_DYNAMIC type memory block */
80 void pa_memblock_unref_fixed(pa_memblock*b);
81
82 pa_memblock_stat* pa_memblock_stat_new(void);
83 void pa_memblock_stat_unref(pa_memblock_stat *s);
84 pa_memblock_stat * pa_memblock_stat_ref(pa_memblock_stat *s);
85
86 #endif