PipeWire: pipewire/mem.h Source File

PipeWire 1.6.4

Loading...
Searching...
No Matches
mem.h
Go to the documentation of this file.
1/* PipeWire */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef PIPEWIRE_MEM_H
6#define PIPEWIRE_MEM_H
7
8#include <pipewire/properties.h>
9
10struct spa_hook;
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#ifndef PW_API_MEM
17#define PW_API_MEM static inline
18#endif
19
23
28
30enum pw_memblock_flags {
31 PW_MEMBLOCK_FLAG_NONE = 0,
32 PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
33 PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
34 PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
35 PW_MEMBLOCK_FLAG_MAP = (1 << 3),
36 PW_MEMBLOCK_FLAG_DONT_CLOSE = (1 << 4),
37 PW_MEMBLOCK_FLAG_DONT_NOTIFY = (1 << 5),
38 PW_MEMBLOCK_FLAG_UNMAPPABLE = (1 << 6),
39
40 PW_MEMBLOCK_FLAG_READWRITE = PW_MEMBLOCK_FLAG_READABLE | PW_MEMBLOCK_FLAG_WRITABLE,
41};
42
43 enum pw_memmap_flags {
44 PW_MEMMAP_FLAG_NONE = 0,
45 PW_MEMMAP_FLAG_READ = (1 << 0),
46 PW_MEMMAP_FLAG_WRITE = (1 << 1),
47 PW_MEMMAP_FLAG_TWICE = (1 << 2),
48
49 PW_MEMMAP_FLAG_PRIVATE = (1 << 3),
50 PW_MEMMAP_FLAG_LOCKED = (1 << 4),
51 PW_MEMMAP_FLAG_READWRITE = PW_MEMMAP_FLAG_READ | PW_MEMMAP_FLAG_WRITE,
52};
53
54struct pw_memchunk;
55
59struct pw_mempool {
60 struct pw_properties *props;
61};
62
65struct pw_memblock {
66 struct pw_mempool *pool;
67 uint32_t id;
68 int ref;
69 uint32_t flags;
70 uint32_t type;
71 int fd;
72 uint32_t size;
73 struct pw_memmap *map;
74};
75
77 struct pw_memmap {
78 struct pw_memblock *block;
79 void *ptr;
80 uint32_t flags;
81 uint32_t offset;
82 uint32_t size;
83 uint32_t tag[5];
84};
85
86 struct pw_mempool_events {
87#define PW_VERSION_MEMPOOL_EVENTS 0
88 uint32_t version;
89
91 void (*destroy) (void *data);
92
93 /** a new memory block is added to the pool */
94 void (*added) (void *data, struct pw_memblock *block);
95
97 void (*removed) (void *data, struct pw_memblock *block);
98};
99
101struct pw_mempool *pw_mempool_new(struct pw_properties *props);
102
104void pw_mempool_add_listener(struct pw_mempool *pool,
105 struct spa_hook *listener,
106 const struct pw_mempool_events *events,
107 void *data);
108
110void pw_mempool_clear(struct pw_mempool *pool);
111
113void pw_mempool_destroy(struct pw_mempool *pool);
114
115
117struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool,
118 enum pw_memblock_flags flags, uint32_t type, size_t size);
119
121struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool,
122 struct pw_memblock *mem);
123
125struct pw_memblock * pw_mempool_import(struct pw_mempool *pool,
126 enum pw_memblock_flags flags, uint32_t type, int fd);
127
129void pw_memblock_free(struct pw_memblock *mem);
130
132 PW_API_MEM void pw_memblock_unref(struct pw_memblock *mem)
133{
134 if (--mem->ref == 0)
135 pw_memblock_free(mem);
136}
137
139int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
140
142struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
143
145struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
146
148struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
149
150
152struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
153 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
154 uint32_t tag[5]);
155
157struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
158 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
159 uint32_t tag[5]);
160
161struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
162 struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
163
165struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
166
168int pw_memmap_free(struct pw_memmap *map);
169
170
172struct pw_map_range {
173 uint32_t start;
174 uint32_t offset;
175 uint32_t size;
176};
177
178#define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
179
180/** Calculate parameters to mmap() memory into \a range so that
181 * \a size bytes at \a offset can be mapped with mmap().
182 * Returns 0 on success, -EOVERFLOW if offset + size overflows. */
183 PW_API_MEM int pw_map_range_init(struct pw_map_range *range,
184 uint32_t offset, uint32_t size,
185 uint32_t page_size)
186{
187 range->offset = SPA_ROUND_DOWN_N(offset, page_size);
188 range->start = offset - range->offset;
189 if (size > UINT32_MAX - range->start)
190 return -EOVERFLOW;
191 /* Check that rounding up to page_size won't overflow */
192 if (range->start + size > UINT32_MAX - (page_size - 1))
193 return -EOVERFLOW;
194 range->size = SPA_ROUND_UP_N(range->start + size, page_size);
195 return 0;
196}
197
201
202#ifdef __cplusplus
203}
204#endif
205
206#endif /* PIPEWIRE_MEM_H */
id
uint32_t id
Definition core.h:432
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:918
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:650
pw_memmap_flags
pw_memmap_flags
Definition mem.h:49
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:527
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition mem.c:157
pw_memmap_free
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition mem.c:479
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:800
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:715
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:212
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:464
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:747
pw_memblock_unref
PW_API_MEM void pw_memblock_unref(struct pw_memblock *mem)
Unref a memblock.
Definition mem.h:140
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:906
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:892
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:873
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition mem.c:181
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:823
pw_memblock_flags
pw_memblock_flags
Flags passed to pw_mempool_alloc().
Definition mem.h:36
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition mem.c:194
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:390
pw_map_range_init
PW_API_MEM int 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:192
PW_MEMMAP_FLAG_LOCKED
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition mem.h:56
PW_MEMMAP_FLAG_READ
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition mem.h:51
PW_MEMMAP_FLAG_READWRITE
@ PW_MEMMAP_FLAG_READWRITE
Definition mem.h:57
PW_MEMMAP_FLAG_WRITE
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition mem.h:52
PW_MEMMAP_FLAG_NONE
@ PW_MEMMAP_FLAG_NONE
Definition mem.h:50
PW_MEMMAP_FLAG_TWICE
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition mem.h:53
PW_MEMMAP_FLAG_PRIVATE
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition mem.h:55
PW_MEMBLOCK_FLAG_READWRITE
@ PW_MEMBLOCK_FLAG_READWRITE
Definition mem.h:46
PW_MEMBLOCK_FLAG_MAP
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition mem.h:41
PW_MEMBLOCK_FLAG_DONT_NOTIFY
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition mem.h:43
PW_MEMBLOCK_FLAG_READABLE
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition mem.h:38
PW_MEMBLOCK_FLAG_SEAL
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition mem.h:40
PW_MEMBLOCK_FLAG_WRITABLE
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition mem.h:39
PW_MEMBLOCK_FLAG_NONE
@ PW_MEMBLOCK_FLAG_NONE
Definition mem.h:37
PW_MEMBLOCK_FLAG_UNMAPPABLE
@ PW_MEMBLOCK_FLAG_UNMAPPABLE
the fd can not be mmapped
Definition mem.h:44
PW_MEMBLOCK_FLAG_DONT_CLOSE
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition mem.h:42
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:364
SPA_ROUND_DOWN_N
#define SPA_ROUND_DOWN_N(num, align)
Definition defs.h:362
properties.h
pipewire/properties.h
PW_API_MEM
#define PW_API_MEM
Definition mem.h:23
pw_map_range
parameters to map a memory range
Definition mem.h:180
pw_map_range::size
uint32_t size
page aligned offset to map
Definition mem.h:183
pw_map_range::offset
uint32_t offset
offset in first page with start of data
Definition mem.h:182
pw_memblock
Memory block structure.
Definition mem.h:72
pw_memblock::fd
int fd
fd
Definition mem.h:78
pw_memblock::pool
struct pw_mempool * pool
owner pool
Definition mem.h:73
pw_memblock::flags
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition mem.h:76
pw_memblock::size
uint32_t size
size of memory
Definition mem.h:79
pw_memblock::type
uint32_t type
type of the fd, one of enum spa_data_type
Definition mem.h:77
pw_memblock::ref
int ref
refcount
Definition mem.h:75
pw_memblock::map
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition mem.h:80
pw_memchunk
pw_memmap
a mapped region of a pw_memblock
Definition mem.h:84
pw_memmap::offset
uint32_t offset
offset in memblock
Definition mem.h:88
pw_memmap::ptr
void * ptr
mapped pointer
Definition mem.h:86
pw_memmap::flags
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition mem.h:87
pw_memmap::size
uint32_t size
size in memblock
Definition mem.h:89
pw_memmap::block
struct pw_memblock * block
owner memblock
Definition mem.h:85
pw_memmap::tag
uint32_t tag[5]
user tag
Definition mem.h:90
pw_mempool_events
Definition mem.h:93
pw_mempool_events::removed
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition mem.h:105
pw_mempool_events::destroy
void(* destroy)(void *data)
the pool is destroyed
Definition mem.h:99
pw_mempool_events::added
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition mem.h:102
pw_mempool_events::version
uint32_t version
Definition mem.h:96
pw_mempool
A memory pool is a collection of pw_memblocks.
Definition mem.h:66
pw_properties
Definition properties.h:39
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition hook.h:427