PipeWire: pipewire/mem.h Source File

PipeWire 0.3.59

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
31extern "C" {
32#endif
33
44enum pw_memblock_flags {
45 PW_MEMBLOCK_FLAG_NONE = 0,
46 PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
47 PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
48 PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
49 PW_MEMBLOCK_FLAG_MAP = (1 << 3),
50 PW_MEMBLOCK_FLAG_DONT_CLOSE = (1 << 4),
51 PW_MEMBLOCK_FLAG_DONT_NOTIFY = (1 << 5),
53 PW_MEMBLOCK_FLAG_READWRITE = PW_MEMBLOCK_FLAG_READABLE | PW_MEMBLOCK_FLAG_WRITABLE,
54};
55
56enum pw_memmap_flags {
57 PW_MEMMAP_FLAG_NONE = 0,
58 PW_MEMMAP_FLAG_READ = (1 << 0),
59 PW_MEMMAP_FLAG_WRITE = (1 << 1),
60 PW_MEMMAP_FLAG_TWICE = (1 << 2),
62 PW_MEMMAP_FLAG_PRIVATE = (1 << 3),
63 PW_MEMMAP_FLAG_LOCKED = (1 << 4),
64 PW_MEMMAP_FLAG_READWRITE = PW_MEMMAP_FLAG_READ | PW_MEMMAP_FLAG_WRITE,
65};
66
67 struct pw_memchunk;
68
72struct pw_mempool {
73 struct pw_properties *props;
74};
75
78struct pw_memblock {
79 struct pw_mempool *pool;
80 uint32_t id;
81 int ref;
82 uint32_t flags;
83 uint32_t type;
84 int fd;
85 uint32_t size;
86 struct pw_memmap *map;
87};
88
90 struct pw_memmap {
91 struct pw_memblock *block;
92 void *ptr;
93 uint32_t flags;
94 uint32_t offset;
95 uint32_t size;
96 uint32_t tag[5];
97};
98
99 struct pw_mempool_events {
100#define PW_VERSION_MEMPOOL_EVENTS 0
101 uint32_t version;
102
104 void (*destroy) (void *data);
105
107 void (*added) (void *data, struct pw_memblock *block);
108
110 void (*removed) (void *data, struct pw_memblock *block);
111};
112
114struct pw_mempool *pw_mempool_new(struct pw_properties *props);
115
117void pw_mempool_add_listener(struct pw_mempool *pool,
118 struct spa_hook *listener,
119 const struct pw_mempool_events *events,
120 void *data);
121
123void pw_mempool_clear(struct pw_mempool *pool);
124
126void pw_mempool_destroy(struct pw_mempool *pool);
127
128
130struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool,
131 enum pw_memblock_flags flags, uint32_t type, size_t size);
132
134struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool,
135 struct pw_memblock *mem);
136
138struct pw_memblock * pw_mempool_import(struct pw_mempool *pool,
139 enum pw_memblock_flags flags, uint32_t type, int fd);
140
142void pw_memblock_free(struct pw_memblock *mem);
143
145static inline void pw_memblock_unref(struct pw_memblock *mem)
146{
147 if (--mem->ref == 0)
148 pw_memblock_free(mem);
149}
150
152int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
153
155struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
156
158struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
159
161struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
162
163
165struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
166 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
167 uint32_t tag[5]);
168
170struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
171 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
172 uint32_t tag[5]);
173
174struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
175 struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
176
178struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
179
181int pw_memmap_free(struct pw_memmap *map);
182
183
185struct pw_map_range {
186 uint32_t start;
187 uint32_t offset;
188 uint32_t size;
189};
190
191#define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
192
195static inline void pw_map_range_init(struct pw_map_range *range,
196 uint32_t offset, uint32_t size,
197 uint32_t page_size)
198{
199 range->offset = SPA_ROUND_DOWN_N(offset, page_size);
200 range->start = offset - range->offset;
201 range->size = SPA_ROUND_UP_N(range->start + size, page_size);
202}
203
208#ifdef __cplusplus
209}
210#endif
211
212#endif /* PIPEWIRE_MEM_H */
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:790
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:581
pw_memmap_flags
pw_memmap_flags
Definition: mem.h:60
pw_mempool_alloc
struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool, enum pw_memblock_flags flags, uint32_t type, size_t size)
Allocate a memory block from the pool.
Definition: mem.c:468
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:135
pw_memmap_free
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:420
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:682
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:618
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:190
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:405
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:629
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:778
pw_memblock_unref
static void pw_memblock_unref(struct pw_memblock *mem)
Unref a memblock.
Definition: mem.h:151
pw_map_range_init
static void pw_map_range_init(struct pw_map_range *range, uint32_t offset, uint32_t size, uint32_t page_size)
Calculate parameters to mmap() memory into range so that size bytes at offset can be mapped with mmap...
Definition: mem.h:202
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:764
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:745
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:159
pw_memblock_free
void pw_memblock_free(struct pw_memblock *mem)
Free a memblock regardless of the refcount and destroy all mappings.
Definition: mem.c:705
pw_memblock_flags
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:48
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:172
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:358
PW_MEMMAP_FLAG_LOCKED
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition: mem.h:67
PW_MEMMAP_FLAG_READ
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition: mem.h:62
PW_MEMMAP_FLAG_READWRITE
@ PW_MEMMAP_FLAG_READWRITE
Definition: mem.h:68
PW_MEMMAP_FLAG_WRITE
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition: mem.h:63
PW_MEMMAP_FLAG_NONE
@ PW_MEMMAP_FLAG_NONE
Definition: mem.h:61
PW_MEMMAP_FLAG_TWICE
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition: mem.h:64
PW_MEMMAP_FLAG_PRIVATE
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition: mem.h:66
PW_MEMBLOCK_FLAG_READWRITE
@ PW_MEMBLOCK_FLAG_READWRITE
Definition: mem.h:57
PW_MEMBLOCK_FLAG_MAP
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition: mem.h:53
PW_MEMBLOCK_FLAG_DONT_NOTIFY
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition: mem.h:55
PW_MEMBLOCK_FLAG_READABLE
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition: mem.h:50
PW_MEMBLOCK_FLAG_SEAL
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition: mem.h:52
PW_MEMBLOCK_FLAG_WRITABLE
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition: mem.h:51
PW_MEMBLOCK_FLAG_NONE
@ PW_MEMBLOCK_FLAG_NONE
Definition: mem.h:49
PW_MEMBLOCK_FLAG_DONT_CLOSE
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition: mem.h:54
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition: defs.h:333
SPA_ROUND_DOWN_N
#define SPA_ROUND_DOWN_N(num, align)
Definition: defs.h:331
properties.h
pipewire/properties.h
pw_map_range
parameters to map a memory range
Definition: mem.h:191
pw_map_range::size
uint32_t size
page aligned offset to map
Definition: mem.h:194
pw_map_range::offset
uint32_t offset
offset in first page with start of data
Definition: mem.h:193
pw_memblock
Memory block structure.
Definition: mem.h:83
pw_memblock::id
uint32_t id
unique id
Definition: mem.h:85
pw_memblock::fd
int fd
fd
Definition: mem.h:89
pw_memblock::pool
struct pw_mempool * pool
owner pool
Definition: mem.h:84
pw_memblock::flags
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:87
pw_memblock::size
uint32_t size
size of memory
Definition: mem.h:90
pw_memblock::type
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:88
pw_memblock::ref
int ref
refcount
Definition: mem.h:86
pw_memblock::map
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:91
pw_memchunk
pw_memmap
a mapped region of a pw_memblock
Definition: mem.h:95
pw_memmap::offset
uint32_t offset
offset in memblock
Definition: mem.h:99
pw_memmap::ptr
void * ptr
mapped pointer
Definition: mem.h:97
pw_memmap::flags
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:98
pw_memmap::size
uint32_t size
size in memblock
Definition: mem.h:100
pw_memmap::block
struct pw_memblock * block
owner memblock
Definition: mem.h:96
pw_memmap::tag
uint32_t tag[5]
user tag
Definition: mem.h:101
pw_mempool_events
Definition: mem.h:104
pw_mempool_events::removed
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition: mem.h:116
pw_mempool_events::destroy
void(* destroy)(void *data)
the pool is destroyed
Definition: mem.h:110
pw_mempool_events::added
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition: mem.h:113
pw_mempool_events::version
uint32_t version
Definition: mem.h:107
pw_mempool
A memory pool is a collection of pw_memblocks.
Definition: mem.h:77
pw_properties
Definition: properties.h:53
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:351