PipeWire  0.2.9
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),
53 };
54 
55 struct pw_memchunk;
56 
57 struct pw_mempool {
59 };
60 
63 struct pw_memblock {
64  struct pw_mempool *pool;
65  uint32_t id;
66  int ref;
67  uint32_t flags;
68  uint32_t type;
69  int fd;
70  uint32_t size;
71  struct pw_memmap *map;
72 };
73 
75 struct pw_memmap {
76  struct pw_memblock *block;
77  void *ptr;
78  uint32_t flags;
79  uint32_t offset;
80  uint32_t size;
81  uint32_t tag[5];
82 };
83 
85 #define PW_VERSION_MEMPOOL_EVENTS 0
86  uint32_t version;
87 
89  void (*destroy) (void *data);
90 
92  void (*added) (void *data, struct pw_memblock *block);
93 
95  void (*removed) (void *data, struct pw_memblock *block);
96 };
97 
100 
102 void pw_mempool_add_listener(struct pw_mempool *pool,
103  struct spa_hook *listener,
104  const struct pw_mempool_events *events,
105  void *data);
106 
108 void pw_mempool_destroy(struct pw_mempool *pool);
109 
110 
113  enum pw_memblock_flags flags, uint32_t type, size_t size);
114 
117  struct pw_memblock *mem);
118 
121  enum pw_memblock_flags flags, uint32_t type, int fd);
122 
124 void pw_memblock_free(struct pw_memblock *mem);
125 
127 static inline void pw_memblock_unref(struct pw_memblock *mem)
128 {
129  if (--mem->ref == 0)
130  pw_memblock_free(mem);
131 }
132 
134 int pw_mempool_unref_id(struct pw_mempool *pool, uint32_t id);
135 
137 struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
138 
140 struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
141 
143 struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
144 
145 
147 struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
148  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
149  uint32_t tag[5]);
150 
152 struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
153  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
154  uint32_t tag[5]);
155 
156 struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
157  struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
158 
160 struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
161 
163 int pw_memmap_free(struct pw_memmap *map);
164 
165 
167 struct pw_map_range {
168  uint32_t start;
169  uint32_t offset;
170  uint32_t size;
171 };
172 
173 #define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
174 
177 static inline void pw_map_range_init(struct pw_map_range *range,
178  uint32_t offset, uint32_t size,
179  uint32_t page_size)
180 {
181  range->offset = SPA_ROUND_DOWN_N(offset, page_size);
182  range->start = offset - range->offset;
183  range->size = SPA_ROUND_UP_N(range->start + size, page_size);
184 }
185 
186 
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif /* PIPEWIRE_MEM_H */
parameters to map a memory range
Definition: mem.h:167
struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition: mem.c:669
A collection of key/value pairs.
Definition: properties.h:43
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:381
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:71
Definition: stream.c:95
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:509
uint32_t offset
offset in first page with start of data
Definition: mem.h:169
Definition: mem.h:52
struct pw_memblock * block
owner memblock
Definition: mem.h:76
struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition: mem.c:650
void * ptr
mapped pointer
Definition: mem.h:77
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:68
pw_memmap_flags
Definition: mem.h:46
Definition: mem.h:36
Definition: mem.h:40
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:35
uint32_t version
Definition: mem.h:86
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:174
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:67
uint32_t size
size in memblock
Definition: mem.h:80
int fd
fd
Definition: mem.h:69
struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition: mem.c:683
Definition: mem.h:43
Definition: mem.h:38
uint32_t offset
offset in memblock
Definition: mem.h:79
struct pw_mempool * pool
owner pool
Definition: mem.h:64
uint32_t start
Definition: mem.h:168
map in read mode
Definition: mem.h:48
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:129
void(* destroy)(void *data)
the pool is destroyed
Definition: mem.h:89
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition: mem.h:92
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:695
a mapped region of a pw_memblock
Definition: mem.h:75
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:78
uint32_t id
unique id
Definition: mem.h:65
int ref
refcount
Definition: mem.h:66
map the same area twice afer eachother, creating a circular ringbuffer
Definition: mem.h:50
Memory block structure.
Definition: mem.h:63
uint32_t tag[5]
user tag
Definition: mem.h:81
Definition: mem.h:47
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:323
uint32_t size
size of memory
Definition: mem.h:70
Definition: mem.h:39
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition: mem.h:95
struct pw_properties * props
Definition: mem.h:58
SPA_EXPORT void pw_memblock_free(struct pw_memblock *block)
Free a memblock.
Definition: mem.c:618
struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition: mem.c:545
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:366
Definition: mem.h:57
uint32_t size
page aligned offset to map
Definition: mem.h:170
int pw_mempool_unref_id(struct pw_mempool *pool, uint32_t id)
Unref a memblock for given id.
Definition: mem.c:600
void pw_mempool_destroy(struct pw_mempool *pool)
Destroy a pool.
Definition: mem.c:154
map in write mode
Definition: mem.h:49
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:410
Definition: stream.c:69
Definition: mem.h:84
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:554