PipeWire: mem.h Source File

PipeWire 0.3.67

Loading...
Searching...
No Matches
src/pipewire/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
10#ifdef __cplusplus
11extern "C" {
12#endif
13
24enum pw_memblock_flags {
25 PW_MEMBLOCK_FLAG_NONE = 0,
26 PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
27 PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
28 PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
29 PW_MEMBLOCK_FLAG_MAP = (1 << 3),
30 PW_MEMBLOCK_FLAG_DONT_CLOSE = (1 << 4),
31 PW_MEMBLOCK_FLAG_DONT_NOTIFY = (1 << 5),
33 PW_MEMBLOCK_FLAG_READWRITE = PW_MEMBLOCK_FLAG_READABLE | PW_MEMBLOCK_FLAG_WRITABLE,
34};
35
36enum pw_memmap_flags {
37 PW_MEMMAP_FLAG_NONE = 0,
38 PW_MEMMAP_FLAG_READ = (1 << 0),
39 PW_MEMMAP_FLAG_WRITE = (1 << 1),
40 PW_MEMMAP_FLAG_TWICE = (1 << 2),
42 PW_MEMMAP_FLAG_PRIVATE = (1 << 3),
43 PW_MEMMAP_FLAG_LOCKED = (1 << 4),
44 PW_MEMMAP_FLAG_READWRITE = PW_MEMMAP_FLAG_READ | PW_MEMMAP_FLAG_WRITE,
45};
46
47 struct pw_memchunk;
48
52struct pw_mempool {
53 struct pw_properties *props;
54};
55
58struct pw_memblock {
59 struct pw_mempool *pool;
60 uint32_t id;
61 int ref;
62 uint32_t flags;
63 uint32_t type;
64 int fd;
65 uint32_t size;
66 struct pw_memmap *map;
67};
68
70 struct pw_memmap {
71 struct pw_memblock *block;
72 void *ptr;
73 uint32_t flags;
74 uint32_t offset;
75 uint32_t size;
76 uint32_t tag[5];
77};
78
79 struct pw_mempool_events {
80#define PW_VERSION_MEMPOOL_EVENTS 0
81 uint32_t version;
82
84 void (*destroy) (void *data);
85
87 void (*added) (void *data, struct pw_memblock *block);
88
90 void (*removed) (void *data, struct pw_memblock *block);
91};
92
94struct pw_mempool *pw_mempool_new(struct pw_properties *props);
95
97void pw_mempool_add_listener(struct pw_mempool *pool,
98 struct spa_hook *listener,
99 const struct pw_mempool_events *events,
100 void *data);
101
103void pw_mempool_clear(struct pw_mempool *pool);
104
106void pw_mempool_destroy(struct pw_mempool *pool);
107
108
110struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool,
111 enum pw_memblock_flags flags, uint32_t type, size_t size);
112
114struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool,
115 struct pw_memblock *mem);
116
118struct pw_memblock * pw_mempool_import(struct pw_mempool *pool,
119 enum pw_memblock_flags flags, uint32_t type, int fd);
120
122void pw_memblock_free(struct pw_memblock *mem);
123
125static inline void pw_memblock_unref(struct pw_memblock *mem)
126{
127 if (--mem->ref == 0)
128 pw_memblock_free(mem);
129}
130
132int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
133
135struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
136
138struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
139
141struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
142
143
145struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
146 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
147 uint32_t tag[5]);
148
150struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
151 enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
152 uint32_t tag[5]);
153
154struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
155 struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
156
158struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
159
161int pw_memmap_free(struct pw_memmap *map);
162
163
165struct pw_map_range {
166 uint32_t start;
167 uint32_t offset;
168 uint32_t size;
169};
170
171#define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
172
175static inline void pw_map_range_init(struct pw_map_range *range,
176 uint32_t offset, uint32_t size,
177 uint32_t page_size)
178{
179 range->offset = SPA_ROUND_DOWN_N(offset, page_size);
180 range->start = offset - range->offset;
181 range->size = SPA_ROUND_UP_N(range->start + size, page_size);
182}
183
188#ifdef __cplusplus
189}
190#endif
191
192#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:788
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:579
pw_memmap_flags
pw_memmap_flags
Definition: src/pipewire/mem.h:40
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:466
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:116
pw_memmap_free
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:418
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:680
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:616
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:171
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:403
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:627
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:776
pw_memblock_unref
static void pw_memblock_unref(struct pw_memblock *mem)
Unref a memblock.
Definition: src/pipewire/mem.h:131
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: src/pipewire/mem.h:182
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:762
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:743
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:140
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:703
pw_memblock_flags
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: src/pipewire/mem.h:28
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:153
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:339
PW_MEMMAP_FLAG_LOCKED
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition: src/pipewire/mem.h:47
PW_MEMMAP_FLAG_READ
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition: src/pipewire/mem.h:42
PW_MEMMAP_FLAG_READWRITE
@ PW_MEMMAP_FLAG_READWRITE
Definition: src/pipewire/mem.h:48
PW_MEMMAP_FLAG_WRITE
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition: src/pipewire/mem.h:43
PW_MEMMAP_FLAG_NONE
@ PW_MEMMAP_FLAG_NONE
Definition: src/pipewire/mem.h:41
PW_MEMMAP_FLAG_TWICE
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition: src/pipewire/mem.h:44
PW_MEMMAP_FLAG_PRIVATE
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition: src/pipewire/mem.h:46
PW_MEMBLOCK_FLAG_READWRITE
@ PW_MEMBLOCK_FLAG_READWRITE
Definition: src/pipewire/mem.h:37
PW_MEMBLOCK_FLAG_MAP
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition: src/pipewire/mem.h:33
PW_MEMBLOCK_FLAG_DONT_NOTIFY
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition: src/pipewire/mem.h:35
PW_MEMBLOCK_FLAG_READABLE
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition: src/pipewire/mem.h:30
PW_MEMBLOCK_FLAG_SEAL
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition: src/pipewire/mem.h:32
PW_MEMBLOCK_FLAG_WRITABLE
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition: src/pipewire/mem.h:31
PW_MEMBLOCK_FLAG_NONE
@ PW_MEMBLOCK_FLAG_NONE
Definition: src/pipewire/mem.h:29
PW_MEMBLOCK_FLAG_DONT_CLOSE
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition: src/pipewire/mem.h:34
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition: defs.h:320
SPA_ROUND_DOWN_N
#define SPA_ROUND_DOWN_N(num, align)
Definition: defs.h:318
properties.h
pipewire/properties.h
pw_map_range
parameters to map a memory range
Definition: src/pipewire/mem.h:171
pw_map_range::size
uint32_t size
page aligned offset to map
Definition: src/pipewire/mem.h:174
pw_map_range::offset
uint32_t offset
offset in first page with start of data
Definition: src/pipewire/mem.h:173
pw_memblock
Memory block structure.
Definition: src/pipewire/mem.h:63
pw_memblock::id
uint32_t id
unique id
Definition: src/pipewire/mem.h:65
pw_memblock::fd
int fd
fd
Definition: src/pipewire/mem.h:69
pw_memblock::pool
struct pw_mempool * pool
owner pool
Definition: src/pipewire/mem.h:64
pw_memblock::flags
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: src/pipewire/mem.h:67
pw_memblock::size
uint32_t size
size of memory
Definition: src/pipewire/mem.h:70
pw_memblock::type
uint32_t type
type of the fd, one of enum spa_data_type
Definition: src/pipewire/mem.h:68
pw_memblock::ref
int ref
refcount
Definition: src/pipewire/mem.h:66
pw_memblock::map
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: src/pipewire/mem.h:71
pw_memchunk
pw_memmap
a mapped region of a pw_memblock
Definition: src/pipewire/mem.h:75
pw_memmap::offset
uint32_t offset
offset in memblock
Definition: src/pipewire/mem.h:79
pw_memmap::ptr
void * ptr
mapped pointer
Definition: src/pipewire/mem.h:77
pw_memmap::flags
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: src/pipewire/mem.h:78
pw_memmap::size
uint32_t size
size in memblock
Definition: src/pipewire/mem.h:80
pw_memmap::block
struct pw_memblock * block
owner memblock
Definition: src/pipewire/mem.h:76
pw_memmap::tag
uint32_t tag[5]
user tag
Definition: src/pipewire/mem.h:81
pw_mempool_events
Definition: src/pipewire/mem.h:84
pw_mempool_events::removed
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition: src/pipewire/mem.h:96
pw_mempool_events::destroy
void(* destroy)(void *data)
the pool is destroyed
Definition: src/pipewire/mem.h:90
pw_mempool_events::added
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition: src/pipewire/mem.h:93
pw_mempool_events::version
uint32_t version
Definition: src/pipewire/mem.h:87
pw_mempool
A memory pool is a collection of pw_memblocks.
Definition: src/pipewire/mem.h:57
pw_properties
Definition: properties.h:33
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:331