2016-12-05 21:12:24 +00:00
|
|
|
# GstMemory
|
|
|
|
|
|
|
|
This document describes the design of the memory objects.
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
`GstMemory` objects are usually added to `GstBuffer` objects and contain the
|
2016-12-05 21:12:24 +00:00
|
|
|
multimedia data passed around in the pipeline.
|
|
|
|
|
2017-02-27 23:45:59 +00:00
|
|
|
``` c
|
|
|
|
struct GstMemory {
|
|
|
|
GstMiniObject mini_object;
|
|
|
|
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
|
|
|
GstMemory *parent;
|
|
|
|
gsize maxsize;
|
|
|
|
gsize align;
|
|
|
|
gsize offset;
|
|
|
|
gsize size;
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2016-12-05 21:12:24 +00:00
|
|
|
## Requirements
|
|
|
|
|
|
|
|
- It must be possible to have different memory allocators
|
|
|
|
- It must be possible to efficiently share memory objects, copy, span and trim.
|
|
|
|
|
|
|
|
## Memory layout
|
|
|
|
|
2017-02-27 23:15:57 +00:00
|
|
|
A `GstMemory` has a pointer to a memory region of `maxsize`. The accessible part
|
|
|
|
of this managed region is defined by an `offset` relative to the start of the
|
|
|
|
region and a `size`. This means that the managed region can be larger than what
|
|
|
|
is visible to the user of the `GstMemory` API.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
memory
|
|
|
|
GstMemory ->*----------------------------------------------------*
|
|
|
|
^----------------------------------------------------^
|
|
|
|
maxsize
|
|
|
|
^--------------------------------------^
|
|
|
|
offset size
|
|
|
|
```
|
|
|
|
|
|
|
|
The current properties of the accessible memory can be retrieved with:
|
|
|
|
|
|
|
|
``` c
|
2017-02-03 23:37:27 +00:00
|
|
|
gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
|
2016-12-05 21:12:24 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
The offset and size can be changed with:
|
|
|
|
|
|
|
|
``` c
|
2017-02-03 23:37:27 +00:00
|
|
|
void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
|
2016-12-05 21:12:24 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Allocators
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
`GstMemory` objects are created by allocators. Allocators are a subclass
|
|
|
|
of `GstObject` and can be subclassed to make custom allocators.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
``` c
|
|
|
|
struct _GstAllocator {
|
|
|
|
GstObject object;
|
|
|
|
|
2017-02-03 23:40:54 +00:00
|
|
|
const gchar *mem_type;
|
2016-12-05 21:12:24 +00:00
|
|
|
|
2017-02-03 23:40:54 +00:00
|
|
|
GstMemoryMapFunction mem_map;
|
|
|
|
GstMemoryUnmapFunction mem_unmap;
|
|
|
|
GstMemoryCopyFunction mem_copy;
|
|
|
|
GstMemoryShareFunction mem_share;
|
|
|
|
GstMemoryIsSpanFunction mem_is_span;
|
|
|
|
|
|
|
|
GstMemoryMapFullFunction mem_map_full;
|
|
|
|
GstMemoryUnmapFullFunction mem_unmap_full;
|
2016-12-05 21:12:24 +00:00
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
The allocator class has 2 virtual methods. One to create a `GstMemory`,
|
2016-12-27 20:23:30 +00:00
|
|
|
another to free it.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
``` c
|
|
|
|
struct _GstAllocatorClass {
|
|
|
|
GstObjectClass object_class;
|
|
|
|
|
|
|
|
GstMemory * (*alloc) (GstAllocator *allocator, gsize size,
|
|
|
|
GstAllocationParams *params);
|
|
|
|
void (*free) (GstAllocator *allocator, GstMemory *memory);
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
Allocators are refcounted. It is also possible to register the allocator to the
|
|
|
|
GStreamer system. This way, the allocator can be retrieved by name.
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
After an allocator is created, new `GstMemory` can be created with
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
``` c
|
2017-02-03 23:37:27 +00:00
|
|
|
GstMemory * gst_allocator_alloc (const GstAllocator * allocator,
|
|
|
|
gsize size, GstAllocationParams *params);
|
2016-12-05 21:12:24 +00:00
|
|
|
```
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
`GstAllocationParams` contain extra info such as flags, alignment, prefix and
|
2016-12-05 21:12:24 +00:00
|
|
|
padding.
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
The `GstMemory` object is a refcounted object that must be freed with
|
|
|
|
`gst_memory_unref()`.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
The `GstMemory` keeps a ref to the allocator that allocated it. Inside the
|
|
|
|
allocator are the most common `GstMemory` operations listed. Custom
|
|
|
|
`GstAllocator` implementations must implement the various operations on
|
2016-12-05 21:12:24 +00:00
|
|
|
the memory they allocate.
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
It is also possible to create a new `GstMemory` object that wraps existing
|
2016-12-05 21:12:24 +00:00
|
|
|
memory with:
|
|
|
|
|
|
|
|
``` c
|
2017-02-03 23:37:27 +00:00
|
|
|
GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags,
|
|
|
|
gpointer data, gsize maxsize,
|
|
|
|
gsize offset, gsize size,
|
|
|
|
gpointer user_data,
|
|
|
|
GDestroyNotify notify);
|
2016-12-05 21:12:24 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Lifecycle
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
`GstMemory` extends from `GstMiniObject` and therefore uses its lifecycle
|
2019-05-26 22:32:55 +00:00
|
|
|
management (See [miniobject](additional/design/miniobject.md)).
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
## Data Access
|
|
|
|
|
2017-06-02 22:20:50 +00:00
|
|
|
Access to the memory region is always controlled with a `map()` and `unmap()` method
|
2016-12-05 21:12:24 +00:00
|
|
|
call. This allows the implementation to monitor the access patterns or set up
|
|
|
|
the required memory mappings when needed.
|
|
|
|
|
|
|
|
The access of the memory object is controlled with the locking mechanism on
|
2019-05-26 22:32:55 +00:00
|
|
|
`GstMiniObject` (See [miniobject](additional/design/miniobject.md)).
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
Mapping a memory region requires the caller to specify the access method: READ
|
|
|
|
and/or WRITE. Mapping a memory region will first try to get a lock on the
|
|
|
|
memory in the requested access mode. This means that the map operation can
|
|
|
|
fail when WRITE access is requested on a non-writable memory object (it has
|
|
|
|
an exclusive counter > 1, the memory is already locked in an incompatible
|
|
|
|
access mode or the memory is marked readonly).
|
|
|
|
|
2016-12-27 20:23:30 +00:00
|
|
|
After the data has been accessed in the object, the `unmap()` call must be
|
2016-12-05 21:12:24 +00:00
|
|
|
performed, which will unlock the memory again.
|
|
|
|
|
|
|
|
It is allowed to recursively map multiple times with the same or narrower
|
2017-02-03 23:37:27 +00:00
|
|
|
access modes. For each of the `map()` calls, a corresponding `unmap()` call
|
|
|
|
needs to be made. WRITE-only memory cannot be mapped in READ mode and
|
|
|
|
READ-only memory cannot be mapped in WRITE mode.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
2017-02-03 23:37:27 +00:00
|
|
|
The memory pointer returned from the `map()` call is guaranteed to remain
|
|
|
|
valid in the requested mapping mode until the corresponding `unmap()` call is
|
|
|
|
performed on the pointer.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
2017-02-03 23:37:27 +00:00
|
|
|
When multiple `map()` operations are nested and return the same pointer, the
|
|
|
|
pointer is valid until the last `unmap()` call is done.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
When the final reference on a memory object is dropped, all outstanding
|
|
|
|
mappings should have been unmapped.
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
Resizing a `GstMemory` does not influence any current mappings in any way.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
## Copy
|
|
|
|
|
2016-12-19 22:19:13 +00:00
|
|
|
A `GstMemory` copy can be made with the `gst_memory_copy()` call. Normally,
|
2016-12-05 21:12:24 +00:00
|
|
|
allocators will implement a custom version of this function to make a copy of
|
2016-12-27 20:23:30 +00:00
|
|
|
the same kind of memory as the original one. This is what the fallback version
|
|
|
|
of the copy function does, albeit slower than what a custom implementation
|
|
|
|
could do.
|
2016-12-05 21:12:24 +00:00
|
|
|
|
|
|
|
The copy operation is only required to copy the visible range of the memory
|
|
|
|
block.
|
|
|
|
|
|
|
|
## Share
|
|
|
|
|
2016-12-27 20:23:30 +00:00
|
|
|
A memory region can be shared between `GstMemory` objects with the
|
2016-12-05 21:12:24 +00:00
|
|
|
`gst_memory_share()` operation.
|