PipeWire  0.3.10
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 
39  PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
40  PW_MEMBLOCK_FLAG_MAP = (1 << 3),
45 };
46 
49  PW_MEMMAP_FLAG_READ = (1 << 0),
50  PW_MEMMAP_FLAG_WRITE = (1 << 1),
51  PW_MEMMAP_FLAG_TWICE = (1 << 2),
55 };
56 
57 struct pw_memchunk;
58 
62 struct pw_mempool {
64 };
65 
68 struct pw_memblock {
69  struct pw_mempool *pool;
70  uint32_t id;
71  int ref;
72  uint32_t flags;
73  uint32_t type;
74  int fd;
75  uint32_t size;
76  struct pw_memmap *map;
77 };
78 
80 struct pw_memmap {
81  struct pw_memblock *block;
82  void *ptr;
83  uint32_t flags;
84  uint32_t offset;
85  uint32_t size;
86  uint32_t tag[5];
87 };
88 
90 #define PW_VERSION_MEMPOOL_EVENTS 0
91  uint32_t version;
92 
94  void (*destroy) (void *data);
95 
97  void (*added) (void *data, struct pw_memblock *block);
98 
100  void (*removed) (void *data, struct pw_memblock *block);
101 };
102 
105 
107 void pw_mempool_add_listener(struct pw_mempool *pool,
108  struct spa_hook *listener,
109  const struct pw_mempool_events *events,
110  void *data);
111 
113 void pw_mempool_clear(struct pw_mempool *pool);
114 
116 void pw_mempool_destroy(struct pw_mempool *pool);
117 
118 
121  enum pw_memblock_flags flags, uint32_t type, size_t size);
122 
125  struct pw_memblock *mem);
126 
129  enum pw_memblock_flags flags, uint32_t type, int fd);
130 
132 void pw_memblock_free(struct pw_memblock *mem);
133 
135 static inline void pw_memblock_unref(struct pw_memblock *mem)
136 {
137  if (--mem->ref == 0)
138  pw_memblock_free(mem);
139 }
140 
142 int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
143 
145 struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
146 
148 struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
149 
151 struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
152 
153 
155 struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
156  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
157  uint32_t tag[5]);
158 
160 struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
161  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
162  uint32_t tag[5]);
163 
164 struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
165  struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
166 
168 struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
169 
171 int pw_memmap_free(struct pw_memmap *map);
172 
173 
175 struct pw_map_range {
176  uint32_t start;
177  uint32_t offset;
178  uint32_t size;
179 };
180 
181 #define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
182 
185 static inline void pw_map_range_init(struct pw_map_range *range,
186  uint32_t offset, uint32_t size,
187  uint32_t page_size)
188 {
189  range->offset = SPA_ROUND_DOWN_N(offset, page_size);
190  range->start = offset - range->offset;
191  range->size = SPA_ROUND_UP_N(range->start + size, page_size);
192 }
193 
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #endif /* PIPEWIRE_MEM_H */
parameters to map a memory range
Definition: mem.h:175
struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition: mem.c:737
A collection of key/value pairs.
Definition: properties.h:45
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:419
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:76
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:554
uint32_t offset
offset in first page with start of data
Definition: mem.h:177
Definition: mem.h:54
struct pw_memblock * block
owner memblock
Definition: mem.h:81
struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition: mem.c:718
void * ptr
mapped pointer
Definition: mem.h:82
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:73
pw_memmap_flags
Definition: mem.h:47
Definition: mem.h:36
mmap the fd
Definition: mem.h:40
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:35
uint32_t version
Definition: mem.h:91
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:191
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:72
writes will be private
Definition: mem.h:53
uint32_t size
size in memblock
Definition: mem.h:85
int fd
fd
Definition: mem.h:74
struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition: mem.c:751
Definition: mem.h:44
memory is writable
Definition: mem.h:38
uint32_t offset
offset in memblock
Definition: mem.h:84
void pw_memblock_free(struct pw_memblock *mem)
Free a memblock regardless of the refcount and destroy all mappings.
struct pw_mempool * pool
owner pool
Definition: mem.h:69
uint32_t start
Definition: mem.h:176
map in read mode
Definition: mem.h:49
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:135
memory is readable
Definition: mem.h:37
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:763
a mapped region of a pw_memblock
Definition: mem.h:80
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:83
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:160
uint32_t id
unique id
Definition: mem.h:70
int ref
refcount
Definition: mem.h:71
map the same area twice after each other, creating a circular ringbuffer
Definition: mem.h:51
A memory pool is a collection of pw_memblocks.
Definition: mem.h:68
uint32_t tag[5]
user tag
Definition: mem.h:86
Definition: mem.h:48
don&#39;t close fd
Definition: mem.h:41
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:357
don&#39;t notify events
Definition: mem.h:42
uint32_t size
size of memory
Definition: mem.h:75
seal the fd
Definition: mem.h:39
int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id)
Remove a memblock for given id.
Definition: mem.c:654
struct pw_properties * props
Definition: mem.h:63
struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition: mem.c:591
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:404
Definition: mem.h:62
uint32_t size
page aligned offset to map
Definition: mem.h:178
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.
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:172
map in write mode
Definition: mem.h:50
Definition: filter.c:71
Definition: mem.h:89
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:602