• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foopulsememblockhfoo
2 #define foopulsememblockhfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2004-2006 Lennart Poettering
8   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 
10   PulseAudio is free software; you can redistribute it and/or modify
11   it under the terms of the GNU Lesser General Public License as
12   published by the Free Software Foundation; either version 2.1 of the
13   License, or (at your option) any later version.
14 
15   PulseAudio is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22 ***/
23 
24 typedef struct pa_memblock pa_memblock;
25 
26 #include <sys/types.h>
27 #include <inttypes.h>
28 
29 #include <pulse/def.h>
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/memchunk.h>
33 #include <pulsecore/mem.h>
34 
35 /* A pa_memblock is a reference counted memory block. PulseAudio
36  * passes 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 is 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_mempool pa_mempool;
52 typedef struct pa_mempool_stat pa_mempool_stat;
53 typedef struct pa_memimport_segment pa_memimport_segment;
54 typedef struct pa_memimport pa_memimport;
55 typedef struct pa_memexport pa_memexport;
56 
57 typedef void (*pa_memimport_release_cb_t)(pa_memimport *i, uint32_t block_id, void *userdata);
58 typedef void (*pa_memexport_revoke_cb_t)(pa_memexport *e, uint32_t block_id, void *userdata);
59 
60 /* Please note that updates to this structure are not locked,
61  * i.e. n_allocated might be updated at a point in time where
62  * n_accumulated is not yet. Take these values with a grain of salt,
63  * they are here for purely statistical reasons.*/
64 struct pa_mempool_stat {
65     pa_atomic_t n_allocated;
66     pa_atomic_t n_accumulated;
67     pa_atomic_t n_imported;
68     pa_atomic_t n_exported;
69     pa_atomic_t allocated_size;
70     pa_atomic_t accumulated_size;
71     pa_atomic_t imported_size;
72     pa_atomic_t exported_size;
73 
74     pa_atomic_t n_too_large_for_pool;
75     pa_atomic_t n_pool_full;
76 
77     pa_atomic_t n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
78     pa_atomic_t n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
79 };
80 
81 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL or PA_MEMBLOCK_APPENDED, depending on the size */
82 pa_memblock *pa_memblock_new(pa_mempool *, size_t length);
83 
84 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL. If the requested size is too large, return NULL */
85 pa_memblock *pa_memblock_new_pool(pa_mempool *, size_t length);
86 
87 /* Allocate a new memory block of type PA_MEMBLOCK_USER */
88 pa_memblock *pa_memblock_new_user(pa_mempool *, void *data, size_t length, pa_free_cb_t free_cb, void *free_cb_data, bool read_only);
89 
90 /* A special case of pa_memblock_new_user: take a memory buffer previously allocated with pa_xmalloc()  */
pa_memblock_new_malloced(pa_mempool * p,void * data,size_t length)91 static inline pa_memblock *pa_memblock_new_malloced(pa_mempool *p, void *data, size_t length) {
92     return pa_memblock_new_user(p, data, length, pa_xfree, data, 0);
93 }
94 
95 /* Allocate a new memory block of type PA_MEMBLOCK_FIXED */
96 pa_memblock *pa_memblock_new_fixed(pa_mempool *, void *data, size_t length, bool read_only);
97 
98 void pa_memblock_unref(pa_memblock*b);
99 pa_memblock* pa_memblock_ref(pa_memblock*b);
100 
101 /* This special unref function has to be called by the owner of the
102 memory of a static memory block when they want to release all
103 references to the memory. This causes the memory to be copied and
104 converted into a pool of malloc'ed memory block. Please note that this
105 function is not multiple caller safe, i.e. needs to be locked
106 manually if called from more than one thread at the same time. */
107 void pa_memblock_unref_fixed(pa_memblock*b);
108 
109 bool pa_memblock_is_ours(pa_memblock *b);
110 bool pa_memblock_is_read_only(pa_memblock *b);
111 bool pa_memblock_is_silence(pa_memblock *b);
112 bool pa_memblock_ref_is_one(pa_memblock *b);
113 void pa_memblock_set_is_silence(pa_memblock *b, bool v);
114 
115 void* pa_memblock_acquire(pa_memblock *b);
116 void *pa_memblock_acquire_chunk(const pa_memchunk *c);
117 void pa_memblock_release(pa_memblock *b);
118 
119 size_t pa_memblock_get_length(pa_memblock *b);
120 
121 /* Note! Always unref the returned pool after use */
122 pa_mempool * pa_memblock_get_pool(pa_memblock *b);
123 
124 pa_memblock *pa_memblock_will_need(pa_memblock *b);
125 
126 /* The memory block manager */
127 pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size, bool per_client);
128 void pa_mempool_unref(pa_mempool *p);
129 pa_mempool* pa_mempool_ref(pa_mempool *p);
130 const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
131 void pa_mempool_vacuum(pa_mempool *p);
132 int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id);
133 bool pa_mempool_is_shared(pa_mempool *p);
134 bool pa_mempool_is_memfd_backed(const pa_mempool *p);
135 bool pa_mempool_is_global(pa_mempool *p);
136 bool pa_mempool_is_per_client(pa_mempool *p);
137 bool pa_mempool_is_remote_writable(pa_mempool *p);
138 void pa_mempool_set_is_remote_writable(pa_mempool *p, bool writable);
139 size_t pa_mempool_block_size_max(pa_mempool *p);
140 
141 int pa_mempool_take_memfd_fd(pa_mempool *p);
142 int pa_mempool_get_memfd_fd(pa_mempool *p);
143 
144 /* For receiving blocks from other nodes */
145 pa_memimport* pa_memimport_new(pa_mempool *p, pa_memimport_release_cb_t cb, void *userdata);
146 void pa_memimport_free(pa_memimport *i);
147 int pa_memimport_attach_memfd(pa_memimport *i, uint32_t shm_id, int memfd_fd, bool writable);
148 pa_memblock* pa_memimport_get(pa_memimport *i, pa_mem_type_t type, uint32_t block_id,
149                               uint32_t shm_id, size_t offset, size_t size, bool writable);
150 int pa_memimport_process_revoke(pa_memimport *i, uint32_t block_id);
151 
152 /* For sending blocks to other nodes */
153 pa_memexport* pa_memexport_new(pa_mempool *p, pa_memexport_revoke_cb_t cb, void *userdata);
154 void pa_memexport_free(pa_memexport *e);
155 int pa_memexport_put(pa_memexport *e, pa_memblock *b, pa_mem_type_t *type, uint32_t *block_id,
156                      uint32_t *shm_id, size_t *offset, size_t * size);
157 int pa_memexport_process_release(pa_memexport *e, uint32_t id);
158 
159 #endif
160