PipeWire: mem.h Source File

PipeWire 0.3.26

mem.h
Go to the documentation of this file.
1 /* PipeWire
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef PIPEWIRE_MEM_H
26 #define PIPEWIRE_MEM_H
27
28 #include <pipewire/properties.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
35 enum pw_memblock_flags {
36 PW_MEMBLOCK_FLAG_NONE = 0,
37 PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
38 PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
39 PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
40 PW_MEMBLOCK_FLAG_MAP = (1 << 3),
41 PW_MEMBLOCK_FLAG_DONT_CLOSE = (1 << 4),
42 PW_MEMBLOCK_FLAG_DONT_NOTIFY = (1 << 5),
44 PW_MEMBLOCK_FLAG_READWRITE = PW_MEMBLOCK_FLAG_READABLE | PW_MEMBLOCK_FLAG_WRITABLE,
45 };
46
47 enum pw_memmap_flags {
48 PW_MEMMAP_FLAG_NONE = 0,
49 PW_MEMMAP_FLAG_READ = (1 << 0),
50 PW_MEMMAP_FLAG_WRITE = (1 << 1),
51 PW_MEMMAP_FLAG_TWICE = (1 << 2),
53 PW_MEMMAP_FLAG_PRIVATE = (1 << 3),
54 PW_MEMMAP_FLAG_LOCKED = (1 << 4),
55 PW_MEMMAP_FLAG_READWRITE = PW_MEMMAP_FLAG_READ | PW_MEMMAP_FLAG_WRITE,
56 };
57
58 struct pw_memchunk;
59
63 struct pw_mempool {
64 struct pw_properties *props;
65 };
66
69 struct pw_memblock {
70 struct pw_mempool *pool;
71 uint32_t id;
72 int ref;
73 uint32_t flags;
74 uint32_t type;
75 int fd;
76 uint32_t size;
77 struct pw_memmap *map;
78 };
79
81 struct pw_memmap {
82 struct pw_memblock *block;
83 void *ptr;
84 uint32_t flags;
85 uint32_t offset;
86 uint32_t size;
87 uint32_t tag[5];
88 };
89
90 struct pw_mempool_events {
91 #define PW_VERSION_MEMPOOL_EVENTS 0
92 uint32_t version;
93
95 void (*destroy) (void *data);
96
98 void (*added) (void *data, struct pw_memblock *block);
99
101 void (*removed) (void *data, struct pw_memblock *block);
102 };
103
105 struct pw_mempool *pw_mempool_new(struct pw_properties *props);
106
108 void pw_mempool_add_listener(struct pw_mempool *pool,
109 struct spa_hook *listener,
110 const struct pw_mempool_events *events,
111 void *data);
112
114 void pw_mempool_clear(struct pw_mempool *pool);
115
117 void pw_mempool_destroy(struct pw_mempool *pool);
118
119
121 struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool,
122 enum pw_memblock_flags flags, uint32_t type, size_t size);
123
125 struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool,
126 struct pw_memblock *mem);
127
129 struct pw_memblock * pw_mempool_import(struct pw_mempool *pool,
130 enum pw_memblock_flags flags, uint32_t type, int fd);
131
133 void pw_memblock_free(struct pw_memblock *mem);
134
136 static inline void pw_memblock_unref(struct pw_memblock *mem)
137 {
138 if (--mem->ref == 0)
139 pw_memblock_free(mem);
140 }
141
143 int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
144
146 struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
147
149 struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
150
152 struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
153
154
156 struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
157 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
158 uint32_t tag[5]);
159
161 struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
162 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
163 uint32_t tag[5]);
164
165 struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
166 struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
167
169 struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
170
172 int pw_memmap_free(struct pw_memmap *map);
173
174
176 struct pw_map_range {
177 uint32_t start;
178 uint32_t offset;
179 uint32_t size;
180 };
181
182 #define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
183
186 static inline void pw_map_range_init(struct pw_map_range *range,
187 uint32_t offset, uint32_t size,
188 uint32_t page_size)
189 {
190 range->offset = SPA_ROUND_DOWN_N(offset, page_size);
191 range->start = offset - range->offset;
192 range->size = SPA_ROUND_UP_N(range->start + size, page_size);
193 }
194
195
196 #ifdef __cplusplus
197 }
198 #endif
199
200 #endif /* PIPEWIRE_MEM_H */
PW_MEMBLOCK_FLAG_READWRITE
@ PW_MEMBLOCK_FLAG_READWRITE
Definition: mem.h:44
PW_MEMBLOCK_FLAG_MAP
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition: mem.h:40
pw_memblock_map
struct pw_memmap * pw_memblock_map(struct pw_memblock *block, enum pw_memmap_flags flags, uint32_t offset, uint32_t size, uint32_t tag[5])
Map a region of a memory block.
Definition: mem.c:364
PW_MEMBLOCK_FLAG_DONT_NOTIFY
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition: mem.h:42
pw_memmap_flags
pw_memmap_flags
Definition: mem.h:47
PW_MEMMAP_FLAG_LOCKED
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition: mem.h:54
PW_MEMMAP_FLAG_READ
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition: mem.h:49
PW_MEMMAP_FLAG_READWRITE
@ PW_MEMMAP_FLAG_READWRITE
Definition: mem.h:55
PW_MEMMAP_FLAG_WRITE
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition: mem.h:50
PW_MEMMAP_FLAG_NONE
@ PW_MEMMAP_FLAG_NONE
Definition: mem.h:48
PW_MEMMAP_FLAG_TWICE
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition: mem.h:51
PW_MEMMAP_FLAG_PRIVATE
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition: mem.h:53
pw_memmap_free
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:426
pw_mempool_import_block
struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition: mem.c:605
pw_mempool_remove_id
int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id)
Remove a memblock for given id.
Definition: mem.c:668
pw_mempool_add_listener
void pw_mempool_add_listener(struct pw_mempool *pool, struct spa_hook *listener, const struct pw_mempool_events *events, void *data)
Listen for events.
Definition: mem.c:195
pw_mempool_find_ptr
struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition: mem.c:732
PW_MEMBLOCK_FLAG_READABLE
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition: mem.h:37
PW_MEMBLOCK_FLAG_SEAL
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition: mem.h:39
PW_MEMBLOCK_FLAG_WRITABLE
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition: mem.h:38
pw_mempool_import_map
struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool, struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5])
Definition: mem.c:616
pw_mempool_find_id
struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition: mem.c:751
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:162
pw_mempool_map_id
struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id, enum pw_memmap_flags flags, uint32_t offset, uint32_t size, uint32_t tag[5])
Map a region of a memory block with id.
Definition: mem.c:411
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:174
pw_mempool_import
struct pw_memblock * pw_mempool_import(struct pw_mempool *pool, enum pw_memblock_flags flags, uint32_t type, int fd)
Import an fd into the pool.
Definition: mem.c:568
PW_MEMBLOCK_FLAG_NONE
@ PW_MEMBLOCK_FLAG_NONE
Definition: mem.h:36
pw_mempool_find_fd
struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition: mem.c:765
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:137
pw_mempool_find_tag
struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size)
find a map with the given tag
Definition: mem.c:777
PW_MEMBLOCK_FLAG_DONT_CLOSE
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition: mem.h:41
properties.h
data
Definition: filter.c:74
pw_map_range
parameters to map a memory range
Definition: mem.h:176
pw_map_range::start
uint32_t start
Definition: mem.h:177
pw_map_range::size
uint32_t size
page aligned offset to map
Definition: mem.h:179
pw_map_range::offset
uint32_t offset
offset in first page with start of data
Definition: mem.h:178
pw_memblock
A memory pool is a collection of pw_memblocks.
Definition: mem.h:69
pw_memblock::id
uint32_t id
unique id
Definition: mem.h:71
pw_memblock::pw_mempool_alloc
SPA_EXPORT struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool, enum pw_memblock_flags flags, uint32_t type, size_t size)
Create a new memblock.
Definition: mem.c:467
pw_memblock::pw_memblock_free
SPA_EXPORT void pw_memblock_free(struct pw_memblock *block)
Free a memblock.
Definition: mem.c:692
pw_memblock::fd
int fd
fd
Definition: mem.h:75
pw_memblock::pool
struct pw_mempool * pool
owner pool
Definition: mem.h:70
pw_memblock::flags
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:73
pw_memblock::size
uint32_t size
size of memory
Definition: mem.h:76
pw_memblock::type
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:74
pw_memblock::pw_memblock_flags
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:35
pw_memblock::ref
int ref
refcount
Definition: mem.h:72
pw_memblock::map
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:77
pw_memmap
a mapped region of a pw_memblock
Definition: mem.h:81
pw_memmap::offset
uint32_t offset
offset in memblock
Definition: mem.h:85
pw_memmap::ptr
void * ptr
mapped pointer
Definition: mem.h:83
pw_memmap::flags
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:84
pw_memmap::size
uint32_t size
size in memblock
Definition: mem.h:86
pw_memmap::block
struct pw_memblock * block
owner memblock
Definition: mem.h:82
pw_memmap::tag
uint32_t tag[5]
user tag
Definition: mem.h:87
pw_mempool_events
Definition: mem.h:90
pw_mempool_events::removed
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition: mem.h:101
pw_mempool_events::destroy
void(* destroy)(void *data)
the pool is destroyed
Definition: mem.h:95
pw_mempool_events::added
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition: mem.h:98
pw_mempool_events::version
uint32_t version
Definition: mem.h:92
pw_mempool
Definition: mem.h:63
pw_mempool::props
struct pw_properties * props
Definition: mem.h:64
pw_properties
A collection of key/value pairs.
Definition: properties.h:45