4 This document describes the design of the miniobject base class.
6 The miniobject abstract base class is used to construct lightweight refcounted
7 and boxed types that are frequently created and destroyed.
14 - I must be possible to control access to the object, ie. when the object is
15 readable and writable.
16 - Subclasses must be able to use their own allocator for the memory.
22 Users of the GstMiniObject infrastructure will need to define a structure that
23 includes the GstMiniObject structure as the first field.
26 GstMiniObject mini_object;
32 The subclass should then implement a constructor method where it allocates the
33 memory for its structure and initializes the miniobject structure with
34 gst_mini_object_init(). Copy and Free functions are provided to the
35 gst_mini_object_init() function.
40 MyObject *res = g_slice_new (MyObject);
42 gst_mini_object_init (GST_MINI_OBJECT_CAST (res), 0,
44 (GstMiniObjectCopyFunction) _my_object_copy,
45 (GstMiniObjectDisposeFunction) NULL,
46 (GstMiniObjectFreeFunction) _my_object_free);
54 The Free function is responsible for freeing the allocated memory for
58 _my_object_free (MyObject *obj)
63 g_slice_free (MyObject, obj);
70 GstMiniObject is refcounted. When a GstMiniObject is first created,
71 it has a refcount of 1.
73 Each variable holding a reference to a GstMiniObject is responsible for
74 updating the refcount. This includes incrementing the refcount with
75 gst_mini_object_ref() when a reference is kept to a miniobject or
76 gst_mini_object_unref() when a reference is released.
78 When the refcount reaches 0, and thus no objects hold a reference to the
79 miniobject anymore, we can free the miniobject.
81 When freeing the miniobject, first the GstMiniObjectDisposeFunction is called.
82 This function is allowed to revive the object again by incrementing the
83 refcount, in which case it should return FALSE from the dispose function. The
84 dispose function is used by GstBuffer to revive the buffer back into the
85 GstBufferPool when needed.
87 When the dispose function returns TRUE, the GstMiniObjectFreeFunction will be
88 called and the miniobject will be freed.
94 A miniobject can be copied with gst_mini_object_copy(). This function will
95 call the custom copy function that was provided when registering the new
96 GstMiniObject subclass.
98 The copy function should try to preserve as much info from the original object
101 The new copy should be writable.
107 GstMiniObject can be shared between multiple threads. It is important that when
108 a thread writes to a GstMiniObject that the other threads don't not see the
111 To avoid exposing changes from one thread to another thread, the miniobjects
112 are managed in a Copy-On-Write way. A copy is only made when it is known that
113 the object is shared between multiple objects or threads.
115 There are 2 methods implemented for controlling access to the miniobject.
117 - A first method relies on the refcount of the object to control writability.
118 Objects using this method have the LOCKABLE flag unset.
120 - A second method relies on a separate counter for controlling
121 the access to the object. Objects using this method have the LOCKABLE flag
124 You can check if an object is writable with gst_mini_object_is_writable() and
125 you can make any miniobject writable with gst_mini_object_make_writable().
126 This will create a writable copy when the object was not writable.
129 non-LOCKABLE GstMiniObjects
130 ---------------------------
132 These GstMiniObjects have the LOCKABLE flag unset. They use the refcount value
133 to control writability of the object.
135 When the refcount of the miniobject is > 1, the objects it referenced by at
136 least 2 objects and is thus considered unwritable. A copy must be made before a
137 modification to the object can be done.
139 Using the refcount to control writability is problematic for many language
140 bindings that can keep additional references to the objects. This method is
141 mainly for historical reasons until all users of the miniobjects are
142 converted to use the LOCAKBLE flag.
145 LOCKABLE GstMiniObjects
146 -----------------------
148 These GstMiniObjects have the LOCKABLE flag set. They use a separate counter
149 for controlling writability and access to the object.
151 It consists of 2 components:
155 Each object that wants to keep a reference to a GstMiniObject and doesn't want to
156 see the changes from other owners of the same GstMiniObject needs to lock the
157 GstMiniObject in EXCLUSIVE mode, which will increase the exclusive counter.
159 The exclusive counter counts the amount of objects that share this
160 GstMiniObject. The counter is initially 0, meaning that the object is not shared with
163 When a reference to a GstMiniObject release, both the ref count and the
164 exclusive counter will be decreased with gst_mini_object_unref() and
165 gst_mini_object_unlock () respectively.
169 All read and write access must be performed between a gst_mini_object_lock() and
170 gst_mini_object_unlock() pair with the requested access method.
172 A gst_mini_object_lock() can fail when a WRITE lock is requested and the exclusive
173 counter is > 1. Indeed a GstMiniObject object with an exclusive counter > 1 is
174 locked EXCLUSIVELY by at least 2 objects and is therefore not writable.
176 Once the GstMiniObject is locked with a certain access mode, it can be recursively
177 locked with the same or narrower access mode. For example, first locking the
178 GstMiniObject in READWRITE mode allows you to recusively lock the
180 READWRITE, READ and WRITE mode. Memory locked in READ mode cannot be locked
181 recursively in WRITE or READWRITE mode.
183 Note that multiple threads can READ lock the GstMiniObject concurrently but cannot
184 lock the object in WRITE mode because the exclusive counter must be > 1.
186 All calls to gst_mini_object_lock() need to be paired with one
187 gst_mini_object_unlock() call with the same access mode. When the last refcount
188 of the object is removed, there should be no more outstanding locks.
190 Note that a shared counter of both 0 and 1 leaves the GstMiniObject writable. The
191 reason is to make it easy to create and pass ownership of the GstMiniObject to
192 another object while keeping it writable. When the GstMiniObject is
193 created with a shared count of 0, it is writable. When the GstMiniObject is then
194 added to another object, the shared count is incremented to 1 and the
195 GstMiniObject remains writable. The 0 share counter has a similar purpose as the floating
196 reference in GObject.
202 GstMiniObject has support for weak references. A callback will be called when
203 the object is freed for all registered weak references.
209 Extra data can be associated with a GstMiniObject by using the QData API.