fe4773d445dcfa2206f58296f5d00e809ee8efc6
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / memblock.h
1 #ifndef foopulsememblockhfoo
2 #define foopulsememblockhfoo
3
4 /* $Id$ */
5
6 /***
7   This file is part of PulseAudio.
8
9   Copyright 2004-2006 Lennart Poettering
10   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
11
12   PulseAudio is free software; you can redistribute it and/or modify
13   it under the terms of the GNU Lesser General Public License as
14   published by the Free Software Foundation; either version 2.1 of the
15   License, or (at your option) any later version.
16
17   PulseAudio is distributed in the hope that it will be useful, but
18   WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   Lesser General Public License for more details.
21
22   You should have received a copy of the GNU Lesser General Public
23   License along with PulseAudio; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25   USA.
26 ***/
27
28 #include <sys/types.h>
29 #include <inttypes.h>
30
31 #include <pulsecore/llist.h>
32 #include <pulsecore/refcnt.h>
33 #include <pulsecore/atomic.h>
34
35 /* A pa_memblock is a reference counted memory block. PulseAudio
36  * passed references to pa_memblocks around instead of copying
37  * data. See pa_memchunk for a structure that describes parts of
38  * memory blocks. */
39
40 /* The type of memory this block points to */
41 typedef enum pa_memblock_type {
42     PA_MEMBLOCK_POOL,             /* Memory is part of the memory pool */
43     PA_MEMBLOCK_POOL_EXTERNAL,    /* Data memory is part of the memory pool but the pa_memblock structure itself not */
44     PA_MEMBLOCK_APPENDED,         /* the data is appended to the memory block */
45     PA_MEMBLOCK_USER,             /* User supplied memory, to be freed with free_cb */
46     PA_MEMBLOCK_FIXED,            /* data is a pointer to fixed memory that needs not to be freed */
47     PA_MEMBLOCK_IMPORTED,         /* Memory is imported from another process via shm */
48     PA_MEMBLOCK_TYPE_MAX
49 } pa_memblock_type_t;
50
51 typedef struct pa_memblock pa_memblock;
52 typedef struct pa_mempool pa_mempool;
53 typedef struct pa_mempool_stat pa_mempool_stat;
54 typedef struct pa_memimport_segment pa_memimport_segment;
55 typedef struct pa_memimport pa_memimport;
56 typedef struct pa_memexport pa_memexport;
57
58 typedef void (*pa_memimport_release_cb_t)(pa_memimport *i, uint32_t block_id, void *userdata);
59 typedef void (*pa_memexport_revoke_cb_t)(pa_memexport *e, uint32_t block_id, void *userdata);
60
61 struct pa_memblock {
62     pa_memblock_type_t type;
63     int read_only; /* boolean */
64     PA_REFCNT_DECLARE; /* the reference counter */
65     size_t length;
66     void *data;
67     pa_mempool *pool;
68
69     union {
70         struct {
71             void (*free_cb)(void *p);  /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
72         } user;
73
74         struct  {
75             uint32_t id;
76             pa_memimport_segment *segment;
77         } imported;
78     } per_type;
79 };
80
81 /* Please note that updates to this structure are not locked,
82  * i.e. n_allocated might be updated at a point in time where
83  * n_accumulated is not yet. Take these values with a grain of salt,
84  * threy are here for purely statistical reasons.*/
85 struct pa_mempool_stat {
86     pa_atomic_int_t n_allocated;
87     pa_atomic_int_t n_accumulated;
88     pa_atomic_int_t n_imported;
89     pa_atomic_int_t n_exported;
90     pa_atomic_int_t allocated_size;
91     pa_atomic_int_t accumulated_size;
92     pa_atomic_int_t imported_size;
93     pa_atomic_int_t exported_size;
94
95     pa_atomic_int_t n_too_large_for_pool;
96     pa_atomic_int_t n_pool_full;
97
98     pa_atomic_int_t n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
99     pa_atomic_int_t n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
100 };
101
102 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL or PA_MEMBLOCK_APPENDED, depending on the size */
103 pa_memblock *pa_memblock_new(pa_mempool *, size_t length);
104
105 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL. If the requested size is too large, return NULL */
106 pa_memblock *pa_memblock_new_pool(pa_mempool *, size_t length);
107
108 /* Allocate a new memory block of type PA_MEMBLOCK_USER */
109 pa_memblock *pa_memblock_new_user(pa_mempool *, void *data, size_t length, void (*free_cb)(void *p), int read_only);
110
111 /* A special case of pa_memblock_new_user: take a memory buffer previously allocated with pa_xmalloc()  */
112 #define pa_memblock_new_malloced(p,data,length) pa_memblock_new_user(p, data, length, pa_xfree, 0)
113
114 /* Allocate a new memory block of type PA_MEMBLOCK_FIXED */
115 pa_memblock *pa_memblock_new_fixed(pa_mempool *, void *data, size_t length, int read_only);
116
117 void pa_memblock_unref(pa_memblock*b);
118 pa_memblock* pa_memblock_ref(pa_memblock*b);
119
120 /* This special unref function has to be called by the owner of the
121 memory of a static memory block when he wants to release all
122 references to the memory. This causes the memory to be copied and
123 converted into a PA_MEMBLOCK_DYNAMIC type memory block */
124 void pa_memblock_unref_fixed(pa_memblock*b);
125
126 /* The memory block manager */
127 pa_mempool* pa_mempool_new(int shared);
128 void pa_mempool_free(pa_mempool *p);
129 const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
130 void pa_mempool_vacuum(pa_mempool *p);
131 int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id);
132 int pa_mempool_is_shared(pa_mempool *p);
133
134 /* For recieving blocks from other nodes */
135 pa_memimport* pa_memimport_new(pa_mempool *p, pa_memimport_release_cb_t cb, void *userdata);
136 void pa_memimport_free(pa_memimport *i);
137 pa_memblock* pa_memimport_get(pa_memimport *i, uint32_t block_id, uint32_t shm_id, size_t offset, size_t size);
138 int pa_memimport_process_revoke(pa_memimport *i, uint32_t block_id);
139
140 /* For sending blocks to other nodes */
141 pa_memexport* pa_memexport_new(pa_mempool *p, pa_memexport_revoke_cb_t cb, void *userdata);
142 void pa_memexport_free(pa_memexport *e);
143 int pa_memexport_put(pa_memexport *e, pa_memblock *b, uint32_t *block_id, uint32_t *shm_id, size_t *offset, size_t *size);
144 int pa_memexport_process_release(pa_memexport *e, uint32_t id);
145
146 #endif