PipeWire  0.3.5
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 
42 
44 };
45 
48  PW_MEMMAP_FLAG_READ = (1 << 0),
49  PW_MEMMAP_FLAG_WRITE = (1 << 1),
50  PW_MEMMAP_FLAG_TWICE = (1 << 2),
54 };
55 
56 struct pw_memchunk;
57 
58 struct pw_mempool {
60 };
61 
64 struct pw_memblock {
65  struct pw_mempool *pool;
66  uint32_t id;
67  int ref;
68  uint32_t flags;
69  uint32_t type;
70  int fd;
71  uint32_t size;
72  struct pw_memmap *map;
73 };
74 
76 struct pw_memmap {
77  struct pw_memblock *block;
78  void *ptr;
79  uint32_t flags;
80  uint32_t offset;
81  uint32_t size;
82  uint32_t tag[5];
83 };
84 
86 #define PW_VERSION_MEMPOOL_EVENTS 0
87  uint32_t version;
88 
90  void (*destroy) (void *data);
91 
93  void (*added) (void *data, struct pw_memblock *block);
94 
96  void (*removed) (void *data, struct pw_memblock *block);
97 };
98 
101 
103 void pw_mempool_add_listener(struct pw_mempool *pool,
104  struct spa_hook *listener,
105  const struct pw_mempool_events *events,
106  void *data);
107 
109 void pw_mempool_clear(struct pw_mempool *pool);
110 
112 void pw_mempool_destroy(struct pw_mempool *pool);
113 
114 
117  enum pw_memblock_flags flags, uint32_t type, size_t size);
118 
121  struct pw_memblock *mem);
122 
125  enum pw_memblock_flags flags, uint32_t type, int fd);
126 
128 void pw_memblock_free(struct pw_memblock *mem);
129 
131 static inline void pw_memblock_unref(struct pw_memblock *mem)
132 {
133  if (--mem->ref == 0)
134  pw_memblock_free(mem);
135 }
136 
138 int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
139 
141 struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
142 
144 struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
145 
147 struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
148 
149 
151 struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
152  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
153  uint32_t tag[5]);
154 
156 struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
157  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
158  uint32_t tag[5]);
159 
160 struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
161  struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
162 
164 struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
165 
167 int pw_memmap_free(struct pw_memmap *map);
168 
169 
171 struct pw_map_range {
172  uint32_t start;
173  uint32_t offset;
174  uint32_t size;
175 };
176 
177 #define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
178 
181 static inline void pw_map_range_init(struct pw_map_range *range,
182  uint32_t offset, uint32_t size,
183  uint32_t page_size)
184 {
185  range->offset = SPA_ROUND_DOWN_N(offset, page_size);
186  range->start = offset - range->offset;
187  range->size = SPA_ROUND_UP_N(range->start + size, page_size);
188 }
189 
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif /* PIPEWIRE_MEM_H */
pw_map_range
parameters to map a memory range
Definition: mem.h:171
PW_MEMBLOCK_FLAG_READWRITE
@ PW_MEMBLOCK_FLAG_READWRITE
Definition: mem.h:43
res
static uint32_t int int res
Definition: core.h:323
pw_mempool_events::version
uint32_t version
Definition: mem.h:87
pw_memblock::pool
struct pw_mempool * pool
owner pool
Definition: mem.h:65
pw_mempool
Definition: mem.h:58
memblock
Definition: mem.c:110
memmap::link
struct spa_list link
Definition: mem.c:130
pw_mempool_import_map
SPA_EXPORT 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:578
pw_memmap
a mapped region of a pw_memblock
Definition: mem.h:76
memblock::link
struct spa_list link
Definition: mem.c:112
PW_MEMMAP_FLAG_WRITE
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition: mem.h:49
memmap::mapping
struct mapping * mapping
Definition: mem.c:129
pw_mempool_events::added
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
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:96
pw_map::pw_map_lookup
static void * pw_map_lookup(struct pw_map *map, uint32_t id)
Find an item in the map.
Definition: map.h:169
mempool
Definition: mem.c:98
pw_map::pw_map_remove
static void pw_map_remove(struct pw_map *map, uint32_t id)
Remove an item at index.
Definition: map.h:157
mapping::ptr
void * ptr
Definition: mem.c:124
MFD_CLOEXEC
#define MFD_CLOEXEC
Definition: mem.c:68
memmap
Definition: mem.c:127
pw_map_range::start
uint32_t start
Definition: mem.h:172
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:167
pw_memblock::id
uint32_t id
unique id
Definition: mem.h:66
pw_log::pw_log_error
#define pw_log_error(...)
MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING
Definition: mem.c:72
mempool::blocks
struct spa_list blocks
Definition: mem.c:106
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:716
pw_mempool_destroy
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:167
PW_MEMBLOCK_FLAG_MAP
@ PW_MEMBLOCK_FLAG_MAP
Definition: mem.h:40
pw_map::pw_map_init
static void pw_map_init(struct pw_map *map, size_t size, size_t extend)
Initialize a map.
Definition: map.h:76
pw_memmap::block
struct pw_memblock * block
owner memblock
Definition: mem.h:77
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:683
pw_memblock::pw_memblock_flags
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:35
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:158
mempool::pagesize
uint32_t pagesize
Definition: mem.c:107
data
Definition: filter.c:70
mapping::block
struct memblock * block
Definition: mem.c:118
pw_mempool_find_ptr
SPA_EXPORT struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition: mem.c:683
pw_memblock::size
uint32_t size
size of memory
Definition: mem.h:71
PW_MEMMAP_FLAG_READ
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition: mem.h:48
pw_memblock::flags
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:68
PW_MEMMAP_FLAG_PRIVATE
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition: mem.h:52
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:384
pw_mempool::props
struct pw_properties * props
Definition: mem.h:59
PW_MEMMAP_FLAG_READWRITE
@ PW_MEMMAP_FLAG_READWRITE
Definition: mem.h:53
PW_MEMBLOCK_FLAG_DONT_CLOSE
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
Definition: mem.h:41
pw_memblock::pw_mempool_alloc
SPA_EXPORT struct pw_memblock * pw_mempool_alloc(struct pw_mempool *pool, enum pw_memblock_flags flags, uint32_t type, size_t size)
Create a new memblock.
Definition: mem.c:440
pw_mempool_emit_added
#define pw_mempool_emit_added(p, b)
Definition: mem.c:95
impl
Definition: control.c:33
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:186
pw_map_range::offset
uint32_t offset
offset in first page with start of data
Definition: mem.h:173
pw_memmap::ptr
void * ptr
mapped pointer
Definition: mem.h:78
memblock::maps
struct spa_list maps
Definition: mem.c:114
mapping::link
struct spa_list link
Definition: mem.c:123
mempool::link
struct spa_list link
Definition: mem.c:101
pw_memmap::size
uint32_t size
size in memblock
Definition: mem.h:81
pw_memmap_flags
pw_memmap_flags
Definition: mem.h:46
pw_map::pw_map_insert_new
static uint32_t pw_map_insert_new(struct pw_map *map, void *data)
Insert data in the map.
Definition: map.h:105
mapping::do_unmap
unsigned int do_unmap
Definition: mem.c:122
pw_memmap_free
SPA_EXPORT int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:399
PW_MEMBLOCK_FLAG_SEAL
@ PW_MEMBLOCK_FLAG_SEAL
Definition: mem.h:39
pw_mempool_import_block
SPA_EXPORT struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition: mem.c:569
pw_memmap::flags
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:79
pw_mempool_find_tag
SPA_EXPORT 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:728
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:702
memmap::this
struct pw_memmap this
Definition: mem.c:128
pw_mempool_events::destroy
void(* destroy)(void *data)
the pool is destroyed
Definition: mem.h:90
F_SEAL_GROW
#define F_SEAL_GROW
Definition: mem.c:87
pw_mempool_events
Definition: mem.h:85
pw_memblock_map
SPA_EXPORT 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:341
memblock::this
struct pw_memblock this
Definition: mem.c:111
mapping::ref
int ref
Definition: mem.c:119
PW_MEMBLOCK_FLAG_WRITABLE
@ PW_MEMBLOCK_FLAG_WRITABLE
Definition: mem.h:38
pw_memblock::fd
int fd
fd
Definition: mem.h:70
pw_map_range::size
uint32_t size
page aligned offset to map
Definition: mem.h:174
mapping::size
uint32_t size
Definition: mem.c:121
pw_memblock::ref
int ref
refcount
Definition: mem.h:67
mapping
Definition: mem.c:117
F_ADD_SEALS
#define F_ADD_SEALS
Definition: mem.c:82
mapping::offset
uint32_t offset
Definition: mem.c:120
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:186
pw_log::pw_log_debug
#define pw_log_debug(...)
pw_log::pw_log_warn
#define pw_log_warn(...)
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:533
pw_mempool_map_id
SPA_EXPORT 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:384
F_SEAL_SEAL
#define F_SEAL_SEAL
Definition: mem.c:85
PW_MEMBLOCK_FLAG_NONE
@ PW_MEMBLOCK_FLAG_NONE
Definition: mem.h:36
F_SEAL_SHRINK
#define F_SEAL_SHRINK
Definition: mem.c:86
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:133
pw_memblock::map
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:72
NAME
#define NAME
Definition: mem.c:44
pw_mempool_emit_removed
#define pw_mempool_emit_removed(p, b)
Definition: mem.c:96
pw_mempool_clear
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:158
pw_map::pw_map_clear
static void pw_map_clear(struct pw_map *map)
Clear a map.
Definition: map.h:87
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:569
map.h
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:626
pw_memmap::offset
uint32_t offset
offset in memblock
Definition: mem.h:80
pw_memblock::pw_memblock_free
SPA_EXPORT void pw_memblock_free(struct pw_memblock *block)
Free a memblock.
Definition: mem.c:650
pw_memblock
Definition: mem.h:64
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:578
pw_memmap::tag
uint32_t tag[5]
user tag
Definition: mem.h:82
log.h
pw_mempool_import
SPA_EXPORT 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:533
mempool::map
struct pw_map map
Definition: mem.c:105
pw_mempool_find_fd
SPA_EXPORT struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition: mem.c:716
PW_MEMBLOCK_FLAG_READABLE
@ PW_MEMBLOCK_FLAG_READABLE
Definition: mem.h:37
PW_MEMMAP_FLAG_NONE
@ PW_MEMMAP_FLAG_NONE
Definition: mem.h:47
pw_mempool_new
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:133
pw_mempool_emit_destroy
#define pw_mempool_emit_destroy(p)
Definition: mem.c:94
mempool::listener_list
struct spa_hook_list listener_list
Definition: mem.c:103
mempool::this
struct pw_mempool this
Definition: mem.c:99
impl::this
struct pw_control this
Definition: control.c:34
pw_map
A map.
Definition: map.h:50
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:626
pw_memmap_free
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:399
mem.h
properties.h
PW_MEMMAP_FLAG_TWICE
@ PW_MEMMAP_FLAG_TWICE
map the same area twice afer eachother, creating a circular ringbuffer
Definition: mem.h:50
pw_mempool_find_id
SPA_EXPORT struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition: mem.c:702
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:341
memblock::mappings
struct spa_list mappings
Definition: mem.c:113
pw_memblock::type
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:69
pw_properties
A collection of key/value pairs.
Definition: properties.h:45
pw_properties::pw_properties_free
SPA_EXPORT void pw_properties_free(struct pw_properties *properties)
Free a properties object.
Definition: properties.c:330
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:728