PipeWire  1.3.83
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
11 extern "C" {
12 #endif
13 
14 #ifndef PW_API_MEM
15 #define PW_API_MEM static inline
16 #endif
17 
28 enum pw_memblock_flags {
30  PW_MEMBLOCK_FLAG_READABLE = (1 << 0),
31  PW_MEMBLOCK_FLAG_WRITABLE = (1 << 1),
32  PW_MEMBLOCK_FLAG_SEAL = (1 << 2),
33  PW_MEMBLOCK_FLAG_MAP = (1 << 3),
39 };
40 
43  PW_MEMMAP_FLAG_READ = (1 << 0),
44  PW_MEMMAP_FLAG_WRITE = (1 << 1),
45  PW_MEMMAP_FLAG_TWICE = (1 << 2),
48  PW_MEMMAP_FLAG_LOCKED = (1 << 4),
50 };
51 
52 struct pw_memchunk;
53 
57 struct pw_mempool {
58  struct pw_properties *props;
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 
99 struct pw_mempool *pw_mempool_new(struct pw_properties *props);
100 
103  struct spa_hook *listener,
104  const struct pw_mempool_events *events,
105  void *data);
106 
108 void pw_mempool_clear(struct pw_mempool *pool);
109 
111 void pw_mempool_destroy(struct pw_mempool *pool);
112 
113 
116  enum pw_memblock_flags flags, uint32_t type, size_t size);
117 
120  struct pw_memblock *mem);
121 
124  enum pw_memblock_flags flags, uint32_t type, int fd);
125 
127 void pw_memblock_free(struct pw_memblock *mem);
128 
130 PW_API_MEM void pw_memblock_unref(struct pw_memblock *mem)
131 {
132  if (--mem->ref == 0)
133  pw_memblock_free(mem);
134 }
135 
137 int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id);
138 
140 struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr);
141 
143 struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id);
144 
146 struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd);
147 
148 
150 struct pw_memmap * pw_memblock_map(struct pw_memblock *block,
151  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
152  uint32_t tag[5]);
153 
155 struct pw_memmap * pw_mempool_map_id(struct pw_mempool *pool, uint32_t id,
156  enum pw_memmap_flags flags, uint32_t offset, uint32_t size,
157  uint32_t tag[5]);
158 
159 struct pw_memmap * pw_mempool_import_map(struct pw_mempool *pool,
160  struct pw_mempool *other, void *data, uint32_t size, uint32_t tag[5]);
161 
163 struct pw_memmap * pw_mempool_find_tag(struct pw_mempool *pool, uint32_t tag[5], size_t size);
164 
166 int pw_memmap_free(struct pw_memmap *map);
167 
168 
170 struct pw_map_range {
171  uint32_t start;
172  uint32_t offset;
173  uint32_t size;
174 };
175 
176 #define PW_MAP_RANGE_INIT (struct pw_map_range){ 0, }
177 
181  uint32_t offset, uint32_t size,
182  uint32_t page_size)
183 {
184  range->offset = SPA_ROUND_DOWN_N(offset, page_size);
185  range->start = offset - range->offset;
186  range->size = SPA_ROUND_UP_N(range->start + size, page_size);
187 }
188 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif /* PIPEWIRE_MEM_H */
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:390
pw_memmap_flags
Definition: mem.h:46
int pw_memmap_free(struct pw_memmap *map)
Unmap a region.
Definition: mem.c:475
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:523
struct pw_memblock * pw_mempool_import_block(struct pw_mempool *pool, struct pw_memblock *mem)
Import a block from another pool.
Definition: mem.c:711
int pw_mempool_remove_id(struct pw_mempool *pool, uint32_t id)
Remove a memblock for given id.
Definition: mem.c:796
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:212
struct pw_memblock * pw_mempool_find_ptr(struct pw_mempool *pool, const void *ptr)
Find memblock for given ptr.
Definition: mem.c:869
PW_API_MEM void pw_memblock_unref(struct pw_memblock *mem)
Unref a memblock.
Definition: mem.h:137
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:743
struct pw_memblock * pw_mempool_find_id(struct pw_mempool *pool, uint32_t id)
Find memblock for given id.
Definition: mem.c:888
void pw_mempool_clear(struct pw_mempool *pool)
Clear a pool.
Definition: mem.c:181
void pw_memblock_free(struct pw_memblock *mem)
Free a memblock regardless of the refcount and destroy all mappings.
Definition: mem.c:819
pw_memblock_flags
Flags passed to pw_mempool_alloc()
Definition: mem.h:33
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:460
void pw_mempool_destroy(struct pw_mempool *pool)
Clear and destroy a pool.
Definition: mem.c:194
PW_API_MEM 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: mem.h:188
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:646
struct pw_memblock * pw_mempool_find_fd(struct pw_mempool *pool, int fd)
Find memblock for given fd.
Definition: mem.c:902
struct pw_mempool * pw_mempool_new(struct pw_properties *props)
Create a new memory pool.
Definition: mem.c:157
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:914
@ PW_MEMMAP_FLAG_LOCKED
lock the memory into RAM
Definition: mem.h:53
@ PW_MEMMAP_FLAG_READ
map in read mode
Definition: mem.h:48
@ PW_MEMMAP_FLAG_READWRITE
Definition: mem.h:54
@ PW_MEMMAP_FLAG_WRITE
map in write mode
Definition: mem.h:49
@ PW_MEMMAP_FLAG_NONE
Definition: mem.h:47
@ PW_MEMMAP_FLAG_TWICE
map the same area twice after each other, creating a circular ringbuffer
Definition: mem.h:50
@ PW_MEMMAP_FLAG_PRIVATE
writes will be private
Definition: mem.h:52
@ PW_MEMBLOCK_FLAG_READWRITE
Definition: mem.h:43
@ PW_MEMBLOCK_FLAG_MAP
mmap the fd
Definition: mem.h:38
@ PW_MEMBLOCK_FLAG_DONT_NOTIFY
don't notify events
Definition: mem.h:40
@ PW_MEMBLOCK_FLAG_READABLE
memory is readable
Definition: mem.h:35
@ PW_MEMBLOCK_FLAG_SEAL
seal the fd
Definition: mem.h:37
@ PW_MEMBLOCK_FLAG_WRITABLE
memory is writable
Definition: mem.h:36
@ PW_MEMBLOCK_FLAG_NONE
Definition: mem.h:34
@ PW_MEMBLOCK_FLAG_UNMAPPABLE
the fd can not be mmapped
Definition: mem.h:41
@ PW_MEMBLOCK_FLAG_DONT_CLOSE
don't close fd
Definition: mem.h:39
#define SPA_ROUND_UP_N(num, align)
Definition: defs.h:360
#define SPA_ROUND_DOWN_N(num, align)
Definition: defs.h:358
pipewire/properties.h
#define PW_API_MEM
Definition: mem.h:20
parameters to map a memory range
Definition: mem.h:177
uint32_t size
page aligned offset to map
Definition: mem.h:180
uint32_t offset
offset in first page with start of data
Definition: mem.h:179
Memory block structure.
Definition: mem.h:69
uint32_t id
unique id
Definition: mem.h:71
int fd
fd
Definition: mem.h:75
struct pw_mempool * pool
owner pool
Definition: mem.h:70
uint32_t flags
flags for the memory block on of enum pw_memblock_flags
Definition: mem.h:73
uint32_t size
size of memory
Definition: mem.h:76
uint32_t type
type of the fd, one of enum spa_data_type
Definition: mem.h:74
int ref
refcount
Definition: mem.h:72
struct pw_memmap * map
optional map when PW_MEMBLOCK_FLAG_MAP was given
Definition: mem.h:77
a mapped region of a pw_memblock
Definition: mem.h:81
uint32_t offset
offset in memblock
Definition: mem.h:85
void * ptr
mapped pointer
Definition: mem.h:83
uint32_t flags
flags for the mapping on of enum pw_memmap_flags
Definition: mem.h:84
uint32_t size
size in memblock
Definition: mem.h:86
struct pw_memblock * block
owner memblock
Definition: mem.h:82
uint32_t tag[5]
user tag
Definition: mem.h:87
Definition: mem.h:90
void(* removed)(void *data, struct pw_memblock *block)
a memory block is removed from the pool
Definition: mem.h:102
void(* destroy)(void *data)
the pool is destroyed
Definition: mem.h:96
void(* added)(void *data, struct pw_memblock *block)
a new memory block is added to the pool
Definition: mem.h:99
uint32_t version
Definition: mem.h:93
A memory pool is a collection of pw_memblocks.
Definition: mem.h:63
Definition: properties.h:39
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:427