Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_BUFFER_ALLOC_H
#define SPA_BUFFER_ALLOC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/buffer/buffer.h>
struct spa_buffer_alloc_info {
#define SPA_BUFFER_ALLOC_FLAG_INLINE_META (1<<0)
#define SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK (1<<1)
#define SPA_BUFFER_ALLOC_FLAG_INLINE_DATA (1<<2)
#define SPA_BUFFER_ALLOC_FLAG_INLINE_ALL 0b111
#define SPA_BUFFER_ALLOC_FLAG_NO_DATA (1<<3)
uint32_t flags;
uint32_t max_align;
uint32_t n_metas;
uint32_t n_datas;
struct spa_meta *metas;
struct spa_data *datas;
uint32_t *data_aligns;
size_t skel_size;
size_t meta_size;
size_t chunk_size;
size_t data_size;
size_t mem_size;
};
static inline int spa_buffer_alloc_fill_info(struct spa_buffer_alloc_info *info,
uint32_t n_metas, struct spa_meta metas[],
uint32_t n_datas, struct spa_data datas[],
uint32_t data_aligns[])
{
size_t size, *target;
uint32_t i;
info->n_metas = n_metas;
info->metas = metas;
info->n_datas = n_datas;
info->datas = datas;
info->data_aligns = data_aligns;
info->max_align = 16;
info->mem_size = 0;
/*
* The buffer skeleton is placed in memory like below and can
* be accessed as a regular structure.
*
* +==============================+
* | struct spa_buffer |
* | uint32_t n_metas | number of metas
* | uint32_t n_datas | number of datas
* +-| struct spa_meta *metas | pointer to array of metas
* +|-| struct spa_data *datas | pointer to array of datas
* || +------------------------------+
* |+>| struct spa_meta |
* | | uint32_t type | metadata
* | | uint32_t size | size of metadata
* +|--| void *data | pointer to metadata
* || | ... <n_metas> | more spa_meta follow
* || +------------------------------+
* |+->| struct spa_data |
* | | uint32_t type | memory type
* | | uint32_t flags |
* | | int fd | fd of shared memory block
* | | uint32_t mapoffset | offset in shared memory of data
* | | uint32_t maxsize | size of data block
* | +-| void *data | pointer to data
* |+|-| struct spa_chunk *chunk | pointer to chunk
* ||| | ... <n_datas> | more spa_data follow
* ||| +==============================+
* VVV
*
* metadata, chunk and memory can either be placed right
* after the skeleton (inlined) or in a separate piece of memory.
*
* vvv
* ||| +==============================+
* +-->| meta data memory | metadata memory, 8 byte aligned
* || | ... <n_metas> |
* || +------------------------------+
* +->| struct spa_chunk | memory for n_datas chunks
* | | uint32_t offset |
* | | uint32_t size |
* | | int32_t stride |
* | | int32_t dummy |
* | | ... <n_datas> chunks |
* | +------------------------------+
* +>| data | memory for n_datas data, aligned
* | ... <n_datas> blocks | according to alignments
* +==============================+
*/
info->skel_size = sizeof(struct spa_buffer);
info->skel_size += n_metas * sizeof(struct spa_meta);
info->skel_size += n_datas * sizeof(struct spa_data);
for (i = 0, size = 0; i < n_metas; i++)
size += SPA_ROUND_UP_N(metas[i].size, 8);
info->meta_size = size;
if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_META))
target = &info->skel_size;
else
target = &info->mem_size;
*target += info->meta_size;
info->chunk_size = n_datas * sizeof(struct spa_chunk);
if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK))
target = &info->skel_size;
else
target = &info->mem_size;
*target += info->chunk_size;
for (i = 0, size = 0; i < n_datas; i++) {
int64_t align = data_aligns[i];
info->max_align = SPA_MAX(info->max_align, data_aligns[i]);
size = SPA_ROUND_UP_N(size, align);
size += datas[i].maxsize;
}
info->data_size = size;
if (!SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_NO_DATA) &&
SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_DATA))
target = &info->skel_size;
else
target = &info->mem_size;
*target = SPA_ROUND_UP_N(*target, n_datas ? data_aligns[0] : 1);
*target += info->data_size;
*target = SPA_ROUND_UP_N(*target, info->max_align);
return 0;
}
static inline struct spa_buffer *
spa_buffer_alloc_layout(struct spa_buffer_alloc_info *info,
void *skel_mem, void *data_mem)
{
struct spa_buffer *b = (struct spa_buffer*)skel_mem;
size_t size;
uint32_t i;
void **dp, *skel, *data;
struct spa_chunk *cp;
b->n_metas = info->n_metas;
b->metas = SPA_PTROFF(b, sizeof(struct spa_buffer), struct spa_meta);
b->n_datas = info->n_datas;
b->datas = SPA_PTROFF(b->metas, info->n_metas * sizeof(struct spa_meta), struct spa_data);
skel = SPA_PTROFF(b->datas, info->n_datas * sizeof(struct spa_data), void);
data = data_mem;
if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_META))
dp = &skel;
else
dp = &data;
for (i = 0; i < info->n_metas; i++) {
struct spa_meta *m = &b->metas[i];
*m = info->metas[i];
m->data = *dp;
*dp = SPA_PTROFF(*dp, SPA_ROUND_UP_N(m->size, 8), void);
}
size = info->n_datas * sizeof(struct spa_chunk);
if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK)) {
cp = (struct spa_chunk*)skel;
skel = SPA_PTROFF(skel, size, void);
}
else {
cp = (struct spa_chunk*)data;
data = SPA_PTROFF(data, size, void);
}
if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_DATA))
dp = &skel;
else
dp = &data;
for (i = 0; i < info->n_datas; i++) {
struct spa_data *d = &b->datas[i];
*d = info->datas[i];
d->chunk = &cp[i];
if (!SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_NO_DATA)) {
*dp = SPA_PTR_ALIGN(*dp, info->data_aligns[i], void);
d->data = *dp;
*dp = SPA_PTROFF(*dp, d->maxsize, void);
}
}
return b;
}
static inline int
spa_buffer_alloc_layout_array(struct spa_buffer_alloc_info *info,
uint32_t n_buffers, struct spa_buffer *buffers[],
void *skel_mem, void *data_mem)
{
uint32_t i;
for (i = 0; i < n_buffers; i++) {
buffers[i] = spa_buffer_alloc_layout(info, skel_mem, data_mem);
skel_mem = SPA_PTROFF(skel_mem, info->skel_size, void);
data_mem = SPA_PTROFF(data_mem, info->mem_size, void);
}
return 0;
}
static inline struct spa_buffer **
spa_buffer_alloc_array(uint32_t n_buffers, uint32_t flags,
uint32_t n_metas, struct spa_meta metas[],
uint32_t n_datas, struct spa_data datas[],
uint32_t data_aligns[])
{
struct spa_buffer **buffers;
struct spa_buffer_alloc_info info = { flags | SPA_BUFFER_ALLOC_FLAG_INLINE_ALL,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
void *skel;
spa_buffer_alloc_fill_info(&info, n_metas, metas, n_datas, datas, data_aligns);
buffers = (struct spa_buffer **)calloc(1, info.max_align +
n_buffers * (sizeof(struct spa_buffer *) + info.skel_size));
if (buffers == NULL)
return NULL;
skel = SPA_PTROFF(buffers, sizeof(struct spa_buffer *) * n_buffers, void);
skel = SPA_PTR_ALIGN(skel, info.max_align, void);
spa_buffer_alloc_layout_array(&info, n_buffers, buffers, skel, NULL);
return buffers;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_BUFFER_ALLOC_H */
buffer.h
spa/buffer/buffer.h
spa_buffer_alloc_array
static struct spa_buffer ** spa_buffer_alloc_array(uint32_t n_buffers, uint32_t flags, uint32_t n_metas, struct spa_meta metas[], uint32_t n_datas, struct spa_data datas[], uint32_t data_aligns[])
Allocate an array of buffers.
Definition alloc.h:306
SPA_BUFFER_ALLOC_FLAG_INLINE_ALL
#define SPA_BUFFER_ALLOC_FLAG_INLINE_ALL
Definition alloc.h:33
spa_buffer_alloc_layout
static struct spa_buffer * spa_buffer_alloc_layout(struct spa_buffer_alloc_info *info, void *skel_mem, void *data_mem)
Fill skeleton and data according to the allocation info.
Definition alloc.h:193
SPA_BUFFER_ALLOC_FLAG_INLINE_META
#define SPA_BUFFER_ALLOC_FLAG_INLINE_META
add metadata data in the skeleton
Definition alloc.h:27
spa_buffer_alloc_fill_info
static int spa_buffer_alloc_fill_info(struct spa_buffer_alloc_info *info, uint32_t n_metas, struct spa_meta metas[], uint32_t n_datas, struct spa_data datas[], uint32_t data_aligns[])
Fill buffer allocation information.
Definition alloc.h:71
spa_buffer_alloc_layout_array
static int spa_buffer_alloc_layout_array(struct spa_buffer_alloc_info *info, uint32_t n_buffers, struct spa_buffer *buffers[], void *skel_mem, void *data_mem)
Layout an array of buffers.
Definition alloc.h:271
SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK
#define SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK
add chunk data in the skeleton
Definition alloc.h:29
SPA_BUFFER_ALLOC_FLAG_NO_DATA
#define SPA_BUFFER_ALLOC_FLAG_NO_DATA
don't set data pointers
Definition alloc.h:35
SPA_BUFFER_ALLOC_FLAG_INLINE_DATA
#define SPA_BUFFER_ALLOC_FLAG_INLINE_DATA
add buffer data to the skeleton
Definition alloc.h:31
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:324
SPA_PTR_ALIGN
#define SPA_PTR_ALIGN(p, align, type)
Definition defs.h:339
SPA_FLAG_IS_SET
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:76
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:198
SPA_MAX
#define SPA_MAX(a, b)
Definition defs.h:157
spa_buffer_alloc_info
information about the buffer layout
Definition alloc.h:25
spa_buffer_alloc_info::mem_size
size_t mem_size
size of the total memory if not inlined
Definition alloc.h:47
spa_buffer_alloc_info::data_size
size_t data_size
size of the data if not inlined
Definition alloc.h:46
spa_buffer_alloc_info::n_metas
uint32_t n_metas
Definition alloc.h:38
spa_buffer_alloc_info::max_align
uint32_t max_align
max of all alignments
Definition alloc.h:37
spa_buffer_alloc_info::meta_size
size_t meta_size
size of the meta if not inlined
Definition alloc.h:44
spa_buffer_alloc_info::chunk_size
size_t chunk_size
size of the chunk if not inlined
Definition alloc.h:45
spa_buffer_alloc_info::skel_size
size_t skel_size
size of the struct spa_buffer and inlined meta/chunk/data
Definition alloc.h:43
spa_buffer_alloc_info::datas
struct spa_data * datas
Definition alloc.h:41
spa_buffer_alloc_info::data_aligns
uint32_t * data_aligns
Definition alloc.h:42
spa_buffer_alloc_info::n_datas
uint32_t n_datas
Definition alloc.h:39
spa_buffer_alloc_info::metas
struct spa_meta * metas
Definition alloc.h:40
spa_buffer_alloc_info::flags
uint32_t flags
Definition alloc.h:36
spa_buffer
A Buffer.
Definition buffer.h:94
spa_buffer::n_metas
uint32_t n_metas
number of metadata
Definition buffer.h:95
spa_buffer::metas
struct spa_meta * metas
array of metadata
Definition buffer.h:97
spa_buffer::datas
struct spa_data * datas
array of data members
Definition buffer.h:98
spa_buffer::n_datas
uint32_t n_datas
number of data members
Definition buffer.h:96
spa_chunk
Chunk of memory, can change for each buffer.
Definition buffer.h:43
spa_chunk::size
uint32_t size
size of valid data.
Definition buffer.h:47
spa_data
Data for a buffer this stays constant for a buffer.
Definition buffer.h:62
spa_data::chunk
struct spa_chunk * chunk
valid chunk of memory
Definition buffer.h:90
spa_data::data
void * data
optional data pointer
Definition buffer.h:89
spa_data::maxsize
uint32_t maxsize
max size of data
Definition buffer.h:88
spa_meta
A metadata element.
Definition meta.h:47
spa_meta::size
uint32_t size
size of metadata
Definition meta.h:49
spa_meta::data
void * data
pointer to metadata
Definition meta.h:50