PipeWire 1.7.0
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 <spa/utils/overflow.h>
10
11struct spa_hook;
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17#ifndef PW_API_MEM
18#define PW_API_MEM static inline
19#endif
20
29
33 PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
34 PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
35 PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
36 PW_MEMBLOCK_FLAG_MAP = (1 << 3),
49
60struct pw_mempool {
61 struct pw_properties *props;
62};
63
66struct pw_memblock {
67 struct pw_mempool *pool;
68 uint32_t id;
69 int ref;
70 uint32_t flags;
71 uint32_t type;
72 int fd;
73 uint32_t size;
74 struct pw_memmap *map;
75};
78struct pw_memmap {
80 void *ptr;
81 uint32_t flags;
82 uint32_t offset;
83 uint32_t size;
84 uint32_t tag[5];
85};
88#define PW_VERSION_MEMPOOL_EVENTS 0
89 uint32_t version;
92 void (*destroy) (void *data);
93
94 /** a new memory block is added to the pool */
95 void (*added) (void *data, struct pw_memblock *block);
98 void (*removed) (void *data, struct pw_memblock *block);
99};
102struct pw_mempool *pw_mempool_new(struct pw_properties *props);
105void pw_mempool_add_listener(struct pw_mempool *pool,
106 struct spa_hook *listener,
107 const struct pw_mempool_events *events,
108 void *data);
109
111void pw_mempool_clear(struct pw_mempool *pool);
112
114void pw_mempool_destroy(struct pw_mempool *pool);
115
116
119 enum pw_memblock_flags flags, uint32_t type, size_t size);
120
123 struct pw_memblock *mem);
124
127 enum pw_memblock_flags flags, uint32_t type, int fd);
128
130void pw_memblock_free(struct pw_memblock *mem);
131
134{
135 if (--mem->ref == 0)
136 pw_memblock_free(mem);
137}
138
140int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
143struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
144
146struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
147
149struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
150
151
154 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
155 uint32_t tag[5]);
156
158struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
159 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
160 uint32_t tag[5]);
161
162struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
163 struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
164
166struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
167
169int pw_memmap_free(struct pw_memmap *map);
170
171
173struct pw_map_range {
174 uint32_t start;
175 uint32_t offset;
176 uint32_t size;
177};
178
179#define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
180
181/** Calculate parameters to mmap() memory into \a range so that
182 * \a size bytes at \a offset can be mapped with mmap().
183 * Returns 0 on success, -EOVERFLOW if offset + size overflows. */
185 uint32_t offset, uint32_t size,
186 uint32_t page_size)
187{
188 uint32_t sum, tmp;
189 range->offset = SPA_ROUND_DOWN_N(offset, page_size);
190 range->start = offset - range->offset;
191 if (spa_overflow_add(range->start, size, &sum))
192 return -EOVERFLOW;
193 /* Check that rounding up to page_size won't overflow */
194 if (spa_overflow_add(sum, page_size - 1, &tmp))
195 return -EOVERFLOW;
196 range->size = SPA_ROUND_UP_N(sum, page_size);
197 return 0;
198}
199
203
204#ifdef __cplusplus
205}
206#endif
207
208#endif /* PIPEWIRE_MEM_H */
uint32_t id
Definition core.h:432
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:921
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:651
pw_memmap_flags
Definition mem.h:50
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:528
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition mem.c:158
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition mem.c:480
int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id)
Remove a memblock for given id.
Definition mem.c:801
struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition mem.c:716
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:213
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:465
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:748
PW_API_MEM void pw_memblock_unref(struct pw_memblock *mem)
Unref a memblock.
Definition mem.h:141
struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition mem.c:909
struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition mem.c:895
struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition mem.c:876
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition mem.c:182
void pw_memblock_free(struct pw_memblock *mem)
Free a memblock regardless of the refcount and destroy all mappings.
Definition mem.c:824
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition mem.h:37
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition mem.c:195
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:391
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:193
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition mem.h:57
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition mem.h:52
@ PW_MEMMAP_FLAG_READWRITE
Definition mem.h:58
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition mem.h:53
@ PW_MEMMAP_FLAG_NONE
Definition mem.h:51
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition mem.h:54
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition mem.h:56
@ PW_MEMBLOCK_FLAG_READWRITE
Definition mem.h:47
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition mem.h:42
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition mem.h:44
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition mem.h:39
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition mem.h:41
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition mem.h:40
@ PW_MEMBLOCK_FLAG_NONE
Definition mem.h:38
@ PW_MEMBLOCK_FLAG_UNMAPPABLE
the fd can not be mmapped
Definition mem.h:45
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition mem.h:43
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:364
#define SPA_ROUND_DOWN_N(num, align)
Definition defs.h:362
pipewire/properties.h
#define PW_API_MEM
Definition mem.h:24
parameters to map a memory range
Definition mem.h:181
uint32_t size
page aligned offset to map
Definition mem.h:184
uint32_t offset
offset in first page with start of data
Definition mem.h:183
Memory block structure.
Definition mem.h:73
int fd
fd
Definition mem.h:79
struct pw_mempool * pool
owner pool
Definition mem.h:74
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition mem.h:77
uint32_t size
size of memory
Definition mem.h:80
uint32_t type
type of the fd, one of enum spa_data_type
Definition mem.h:78
int ref
refcount
Definition mem.h:76
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition mem.h:81
a mapped region of a pw_memblock
Definition mem.h:85
uint32_t offset
offset in memblock
Definition mem.h:89
void * ptr
mapped pointer
Definition mem.h:87
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition mem.h:88
uint32_t size
size in memblock
Definition mem.h:90
struct pw_memblock * block
owner memblock
Definition mem.h:86
uint32_t tag[5]
user tag
Definition mem.h:91
Definition mem.h:94
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition mem.h:106
void(* destroy)(void *data)
the pool is destroyed
Definition mem.h:100
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition mem.h:103
uint32_t version
Definition mem.h:97
A memory pool is a collection of pw_memblocks.
Definition mem.h:67
Definition properties.h:39
A hook, contains the structure with functions and the data passed to the functions.
Definition hook.h:427