Debian Codegraph @

src/dex-object.c

raw symbols SCIP
1 /*
2 * dex-object.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <stdatomic.h>
25
26 #include <gobject/gvaluecollector.h>
27
28 #include "dex-object-private.h"
29 #include "dex-profiler.h"
30
31 /**
32 * DexObject: (ref-func dex_ref) (unref-func dex_unref)
33 * (set-value-func dex_value_set_object)
34 * (get-value-func dex_value_get_object)
35 *
36 * `DexObject` is the basic building block of types defined within
37 * libdex. Futures, Schedulers, and Channels all inherit from `DexObject`
38 * which provides features like thread-safe weak pointers and memory
39 * management operations.
40 *
41 * Objects that are integrating with GIO instead inherit from their
42 * natural type in GIO.
43 */
44
45 static GType dex_object_type = G_TYPE_INVALID;
46 static GType dex_param_spec_object_type = G_TYPE_INVALID;
47
48 #undef DEX_TYPE_OBJECT
49 #define DEX_TYPE_OBJECT dex_object_type
50
51 #define DEX_TYPE_PARAM_OBJECT (dex_param_spec_object_type)
52
53 static void
54 dex_object_finalize (DexObject *object)
55 {
56 g_assert (object != NULL);
57 g_assert (atomic_load_explicit (&object->ref_count, memory_order_relaxed) == 0);
58
59 #ifdef HAVE_SYSPROF
60 DEX_PROFILER_MARK (0, DEX_OBJECT_TYPE_NAME (object), "dex_object_finalize()");
61 DEX_PROFILER_MARK (SYSPROF_CAPTURE_CURRENT_TIME - object->ctime, DEX_OBJECT_TYPE_NAME (object), "lifetime");
62 #endif
63
64 g_type_free_instance ((GTypeInstance *)object);
65 }
66
67 /**
68 * dex_ref: (method)
69 * @object: (type DexObject): the object to reference
70 *
71 * Acquires a reference on the given object, and increases its reference count by one.
72 *
73 * Returns: (transfer full) (type DexObject): the object with its reference count increased
74 */
75 gpointer
76 dex_ref (gpointer object)
77 {
78 DexObject *self = object;
79 atomic_fetch_add_explicit (&self->ref_count, 1, memory_order_relaxed);
80 return object;
81 }
82
83 /**
84 * dex_unref: (method)
85 * @object: (type DexObject) (transfer full): the object to unreference
86 *
87 * Releases a reference on the given object, and decreases its reference count by one.
88 *
89 * If it was the last reference, the resources associated to the instance are freed.
90 */
91 void
92 dex_unref (gpointer object)
93 {
94 DexObject *obj = object;
95 DexObjectClass *object_class;
96 DexWeakRef *weak_refs;
97 guint watermark;
98
99 g_return_if_fail (object != NULL);
100 g_return_if_fail (DEX_IS_OBJECT (object));
101
102 /* Fetch a watermark before we decrement so that we can be
103 * sure that if we reached zero, that anything that extended
104 * the life of the mem_block will be responsible to free
105 * the object in the end.
106 */
107 watermark = atomic_load_explicit (&obj->weak_refs_watermark, memory_order_acquire);
108
109 /* If we decrement and it's not zero, then there is nothing
110 * for this thread to do. Fast path.
111 */
112 if G_LIKELY (atomic_fetch_sub_explicit (&obj->ref_count, 1, memory_order_release) != 1)
113 return;
114
115 atomic_thread_fence (memory_order_acquire);
116
117 object_class = DEX_OBJECT_GET_CLASS (object);
118
119 /* We reached zero. We need to go through our weak references and
120 * acquire each of their mutexes so that we can be sure none of them
121 * have raced against us to increment the value beyond 0. If they did,
122 * then we can bail early and ignore any request to finalize.
123 *
124 * If the watermark is beyond ours, then we know that the weak-ref
125 * extended liveness beyond this call and we are not responsible
126 * for finalizing this object.
127 */
128 dex_object_lock (object);
129 for (DexWeakRef *wr = obj->weak_refs; wr; wr = wr->next)
130 g_mutex_lock (&wr->mutex);
131
132 /* If we increased our reference count on another thread by extending the
133 * lifetime of the mem_block using a weak_ref-to-full_ref, then we don't need
134 * to do anything here. Just bail and move along allowing a future unref to
135 * finalize when it once again approaches zero.
136 */
137 if (atomic_load_explicit (&obj->ref_count, memory_order_acquire) > 0 ||
138 atomic_load_explicit (&obj->weak_refs_watermark, memory_order_acquire) != watermark)
139 {
140 for (DexWeakRef *wr = obj->weak_refs; wr; wr = wr->next)
141 g_mutex_unlock (&wr->mutex);
142 dex_object_unlock (object);
143 return;
144 }
145
146 weak_refs = g_steal_pointer (&obj->weak_refs);
147
148 /* So we have locks on everything we can, we still are at zero, so that
149 * means we need to zero out all our weak refs and then finalize the
150 * object using the provided @clear_func.
151 */
152 while (weak_refs != NULL)
153 {
154 DexWeakRef *wr = weak_refs;
155
156 weak_refs = weak_refs->next;
157
158 wr->prev = NULL;
159 wr->next = NULL;
160 wr->mem_block = NULL;
161
162 g_mutex_unlock (&wr->mutex);
163 }
164
165 dex_object_unlock (object);
166
167 /* If we did not create an immortal ref, then we are safe to finalize */
168 if (atomic_load_explicit (&obj->ref_count, memory_order_acquire) == 0)
169 object_class->finalize (object);
170 }
171
172 static void
173 dex_object_class_init (DexObjectClass *klass)
174 {
175 klass->finalize = dex_object_finalize;
176 }
177
178 static void
179 dex_object_init (DexObject *self,
180 DexObjectClass *object_class)
181 {
182 #ifdef HAVE_SYSPROF
183 self->ctime = SYSPROF_CAPTURE_CURRENT_TIME;
184 DEX_PROFILER_MARK (0,
185 g_type_name (G_TYPE_FROM_CLASS (object_class)),
186 "dex_object_init()");
187 #endif
188
189 atomic_init (&self->ref_count, 1);
190 g_mutex_init (&self->mutex);
191 atomic_init (&self->weak_refs_watermark, 1);
192 }
193
194 static void
195 dex_object_add_weak (gpointer mem_block,
196 DexWeakRef *weak_ref)
197 {
198 DexObject *object = mem_block;
199
200 g_assert (object != NULL);
201 g_assert (weak_ref != NULL);
202 g_assert (weak_ref->prev == NULL);
203 g_assert (weak_ref->next == NULL);
204 g_assert (weak_ref->mem_block == mem_block);
205
206 /* Must own a full ref to acquire a weak ref */
207 g_return_if_fail (atomic_load_explicit (&object->ref_count, memory_order_relaxed) > 0);
208
209 dex_object_lock (object);
210 weak_ref->prev = NULL;
211 weak_ref->next = object->weak_refs;
212 if (object->weak_refs != NULL)
213 object->weak_refs->prev = weak_ref;
214 object->weak_refs = weak_ref;
215 dex_object_unlock (object);
216 }
217
218 static void
219 dex_object_unlink_weak (gpointer mem_block,
220 DexWeakRef *weak_ref)
221 {
222 DexObject *object = mem_block;
223
224 g_assert (object != NULL);
225 g_assert (weak_ref != NULL);
226
227 /* Must own a full ref to release a weak ref */
228 g_return_if_fail (atomic_load_explicit (&object->ref_count, memory_order_relaxed) > 0);
229
230 dex_object_lock (object);
231
232 if (weak_ref->prev != NULL)
233 weak_ref->prev->next = weak_ref->next;
234
235 if (weak_ref->next != NULL)
236 weak_ref->next->prev = weak_ref->prev;
237
238 if (object->weak_refs == weak_ref)
239 object->weak_refs = weak_ref->next;
240
241 g_assert (object->weak_refs == NULL ||
242 object->weak_refs->prev == NULL);
243
244 weak_ref->next = NULL;
245 weak_ref->prev = NULL;
246
247 dex_object_unlock (object);
248 }
249
250 static void
251 dex_object_remove_weak (gpointer mem_block,
252 DexWeakRef *weak_ref)
253 {
254 dex_object_unlink_weak (mem_block, weak_ref);
255 weak_ref->mem_block = NULL;
256 }
257
258 /**
259 * dex_weak_ref_init: (skip)
260 * @weak_ref: (out caller-allocates): uninitialized memory to store a weak ref
261 * @mem_block: (nullable): the mem_block weak reference
262 *
263 * Creates a new weak reference to @mem_block.
264 *
265 * @mem_block must be a type that is created with dex_object_alloc0(),
266 * dex_object_new0(), or similar; otherwise %NULL.
267 *
268 * It is an error to create a new weak reference after @mem_block has
269 * started disposing which in practice means you must own a full reference
270 * to create a weak reference.
271 */
272 void
273 dex_weak_ref_init (DexWeakRef *weak_ref,
274 gpointer mem_block)
275 {
276 g_return_if_fail (weak_ref != NULL);
277 g_return_if_fail (!mem_block || DEX_IS_OBJECT (mem_block));
278 g_return_if_fail (!mem_block ||
279 atomic_load_explicit (&DEX_OBJECT (mem_block)->ref_count,
280 memory_order_relaxed) > 0);
281
282 memset (weak_ref, 0, sizeof *weak_ref);
283 g_mutex_init (&weak_ref->mutex);
284
285 if (mem_block)
286 dex_weak_ref_set (weak_ref, mem_block);
287 }
288
289 static inline gpointer
290 dex_weak_ref_get_locked (DexWeakRef *weak_ref)
291 {
292 if (weak_ref->mem_block != NULL)
293 {
294 guint watermark;
295
296 /* We have a pointer to our mem_block still. That means either the
297 * object has a reference count greater-than zero, or we are running
298 * against the finalizer (which must acquire weak_ref->mutex before it
299 * can attempt to run finalizers).
300 */
301 DexObject *object = weak_ref->mem_block;
302
303 /* Increment the watermark so that any calls to dex_unref() racing
304 * against us can detect that we extended liveness. If we have raced
305 * G_MAXUINT32 times, then something nefarious is going on and we can
306 * just make the object immortal.
307 *
308 * Otherwise, just add a single reference to own the object.
309 */
310 watermark = atomic_fetch_add_explicit (&object->weak_refs_watermark,
311 1,
312 memory_order_acq_rel);
313 atomic_fetch_add_explicit (&object->ref_count,
314 1 + (watermark == G_MAXUINT32),
315 memory_order_relaxed);
316
317 #ifdef HAVE_SYSPROF
318 {
319 char *message = g_strdup_printf ("%s@%p converted to full",
320 DEX_OBJECT_TYPE_NAME (weak_ref->mem_block),
321 weak_ref->mem_block);
322 DEX_PROFILER_MARK (0, "DexWeakRef", message);
323 g_free (message);
324 }
325 #endif
326
327 return weak_ref->mem_block;
328 }
329
330 return NULL;
331 }
332
333 /**
334 * dex_weak_ref_get: (skip)
335 * @weak_ref: a `DexWeakRef`
336 *
337 * Converts a weak ref into a full reference.
338 *
339 * This attempts to convert the `DexWeakRef` created with
340 * dex_weak_ref_init() into a full reference.
341 *
342 * If the mem_block pointed to by @weak_ref has already been released, or
343 * is racing against disposal, %NULL is returned.
344 *
345 * Returns: (transfer full) (nullable): the mem_block or %NULL
346 *
347 * Stability: Private
348 */
349 gpointer
350 dex_weak_ref_get (DexWeakRef *weak_ref)
351 {
352 gpointer ret;
353
354 g_return_val_if_fail (weak_ref != NULL, NULL);
355
356 g_mutex_lock (&weak_ref->mutex);
357 ret = dex_weak_ref_get_locked (weak_ref);
358 g_mutex_unlock (&weak_ref->mutex);
359
360 return ret;
361 }
362
363 /**
364 * dex_weak_ref_clear: (skip)
365 * @weak_ref: a `DexWeakRef`
366 *
367 * Clears a `DexWeakRef` that was previous registered with a mem_block
368 * using `dex_weak_ref_init()`.
369 *
370 * It is an error to call this method while other threads are accessing
371 * the `DexWeakRef`.
372 *
373 * Stability: Private
374 */
375 void
376 dex_weak_ref_clear (DexWeakRef *weak_ref)
377 {
378 gpointer mem_block;
379
380 g_return_if_fail (weak_ref != NULL);
381
382 /* To detach a weak ref, you MUST own a full reference first. If we
383 * fail to acquire a full reference, then our weak ref has been
384 * abandoned by the mem_block and we are free to clean it up by
385 * clearing our mutex.
386 */
387
388 mem_block = dex_weak_ref_get (weak_ref);
389
390 if (mem_block != NULL)
391 {
392 dex_object_remove_weak (mem_block, weak_ref);
393 dex_clear (&mem_block);
394 }
395
396 /* These should be NULL because they've been removed above, or were
397 * already removed during finalization.
398 */
399 g_assert (weak_ref->prev == NULL);
400 g_assert (weak_ref->next == NULL);
401 g_assert (weak_ref->mem_block == NULL);
402
403 g_mutex_clear (&weak_ref->mutex);
404 }
405
406 /**
407 * dex_weak_ref_set: (skip)
408 * @weak_ref: a `DexWeakRef`
409 * @mem_block: (nullable): the mem_block or %NULL
410 *
411 * Sets a `DexWeakRef` to @mem_block.
412 *
413 * @mem_block must be a type allocated with `dex_object_alloc0()` or
414 * equivalent allocator.
415 *
416 * It is an error to call this method without a full reference to
417 * @mem_block.
418 *
419 * Stability: Private
420 */
421 void
422 dex_weak_ref_set (DexWeakRef *weak_ref,
423 gpointer mem_block)
424 {
425 gpointer old_mem_block;
426
427 g_return_if_fail (weak_ref != NULL);
428 g_return_if_fail (!mem_block || DEX_IS_OBJECT (mem_block));
429 g_return_if_fail (!mem_block ||
430 atomic_load_explicit (&DEX_OBJECT (mem_block)->ref_count,
431 memory_order_relaxed) > 0);
432
433 g_mutex_lock (&weak_ref->mutex);
434
435 old_mem_block = dex_weak_ref_get_locked (weak_ref);
436
437 if (old_mem_block != mem_block)
438 weak_ref->mem_block = mem_block;
439
440 g_mutex_unlock (&weak_ref->mutex);
441
442 if (old_mem_block != mem_block)
443 {
444 if (old_mem_block != NULL)
445 dex_object_unlink_weak (old_mem_block, weak_ref);
446
447 if (mem_block != NULL)
448 dex_object_add_weak (mem_block, weak_ref);
449 }
450
451 dex_clear (&old_mem_block);
452 }
453
454 static void
455 value_init (GValue *value)
456 {
457 value->data[0].v_pointer = NULL;
458 }
459
460 static void
461 value_free_value (GValue *value)
462 {
463 dex_clear (&value->data[0].v_pointer);
464 }
465
466 static void
467 value_copy_value (const GValue *src,
468 GValue *dst)
469 {
470 if (src->data[0].v_pointer != NULL)
471 dst->data[0].v_pointer = dex_ref (src->data[0].v_pointer);
472 else
473 dst->data[0].v_pointer = NULL;
474 }
475
476 static gpointer
477 value_peek_pointer (const GValue *value)
478 {
479 return value->data[0].v_pointer;
480 }
481
482 static char *
483 value_collect_value (GValue *value,
484 guint n_collect_values,
485 GTypeCValue *collect_values,
486 guint collect_flags)
487 {
488 DexObject *object = collect_values[0].v_pointer;
489
490 if (object == NULL)
491 {
492 value->data[0].v_pointer = NULL;
493 return NULL;
494 }
495
496 if (object->parent_instance.g_class == NULL)
497 return g_strconcat ("invalid unclassed DexObject pointer for value type '",
498 G_VALUE_TYPE_NAME (value),
499 "'",
500 NULL);
501
502 value->data[0].v_pointer = dex_ref (object);
503
504 return NULL;
505 }
506
507 static char *
508 value_lcopy_value (const GValue *value,
509 guint n_collect_values,
510 GTypeCValue *collect_values,
511 guint collect_flags)
512 {
513 const DexObject **object_p = collect_values[0].v_pointer;
514
515 if G_UNLIKELY (object_p == NULL)
516 return g_strconcat ("value location for '",
517 G_VALUE_TYPE_NAME (value),
518 "' passed as NULL",
519 NULL);
520
521 if (value->data[0].v_pointer == NULL)
522 *object_p = NULL;
523 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
524 *object_p = value->data[0].v_pointer;
525 else
526 *object_p = dex_ref (value->data[0].v_pointer);
527
528 return NULL;
529 }
530
531 static void
532 dex_param_object_set_default (GParamSpec *pspec,
533 GValue *value)
534 {
535 value->data[0].v_pointer = NULL;
536 }
537
538 static gboolean
539 dex_param_object_is_valid (GParamSpec *pspec,
540 const GValue *value)
541 {
542 DexObject *object = value->data[0].v_pointer;
543
544 return object != NULL &&
545 g_value_type_compatible (DEX_OBJECT_TYPE (object), G_PARAM_SPEC_VALUE_TYPE (pspec));
546 }
547
548 static gboolean
549 dex_param_object_validate (GParamSpec *pspec,
550 GValue *value)
551 {
552 DexObject *object = value->data[0].v_pointer;
553
554 if (object != NULL &&
555 !g_value_type_compatible (DEX_OBJECT_TYPE (object), G_PARAM_SPEC_VALUE_TYPE (pspec)))
556 {
557 dex_unref (object);
558 value->data[0].v_pointer = NULL;
559
560 return TRUE;
561 }
562
563 return FALSE;
564 }
565
566 static int
567 dex_param_object_values_cmp (GParamSpec *pspec,
568 const GValue *value1,
569 const GValue *value2)
570 {
571 const guint8 *p1 = value1->data[0].v_pointer;
572 const guint8 *p2 = value2->data[0].v_pointer;
573
574 return p1 < p2 ? -1 : p1 > p2;
575 }
576
577 static GType
578 dex_param_spec_object_get_type (void)
579 {
580 if (g_once_init_enter (&dex_param_spec_object_type))
581 {
582 GType gtype =
583 g_param_type_register_static (g_intern_static_string ("DexParamObject"),
584 &(const GParamSpecTypeInfo) {
585 sizeof (GParamSpec),
586 16,
587 NULL,
588 DEX_TYPE_OBJECT,
589 NULL,
590 dex_param_object_set_default,
591 dex_param_object_validate,
592 dex_param_object_values_cmp,
593 });
594 GParamSpecClass *klass = g_type_class_get (gtype);
595
596 klass->value_is_valid = dex_param_object_is_valid;
597
598 g_assert (gtype != G_TYPE_INVALID);
599 g_once_init_leave (&dex_param_spec_object_type, gtype);
600 }
601
602 return dex_param_spec_object_type;
603 }
604
605 GType
606 dex_object_get_type (void)
607 {
608 if (g_once_init_enter (&dex_object_type))
609 {
610 GType gtype =
611 g_type_register_fundamental (g_type_fundamental_next (),
612 g_intern_static_string ("DexObject"),
613 &(const GTypeInfo) {
614 sizeof (DexObjectClass),
615 (GBaseInitFunc) NULL,
616 (GBaseFinalizeFunc) NULL,
617 (GClassInitFunc) dex_object_class_init,
618 (GClassFinalizeFunc) NULL,
619 NULL,
620
621 sizeof (DexObject),
622 0,
623 (GInstanceInitFunc) dex_object_init,
624
625 /* GValue */
626 &(const GTypeValueTable) {
627 value_init,
628 value_free_value,
629 value_copy_value,
630 value_peek_pointer,
631 "p",
632 value_collect_value,
633 "p",
634 value_lcopy_value,
635 },
636 },
637 &(const GTypeFundamentalInfo) {
638 (G_TYPE_FLAG_INSTANTIATABLE |
639 G_TYPE_FLAG_CLASSED |
640 G_TYPE_FLAG_DERIVABLE |
641 G_TYPE_FLAG_DEEP_DERIVABLE),
642 },
643 G_TYPE_FLAG_ABSTRACT);
644 g_assert (gtype != G_TYPE_INVALID);
645 g_once_init_leave (&dex_object_type, gtype);
646 }
647
648 return dex_object_type;
649 }
650
651 DexObject *
652 dex_object_create_instance (GType instance_type)
653 {
654 return (DexObject *)(gpointer)g_type_create_instance (instance_type);
655 }
656
657 /**
658 * dex_param_spec_object:
659 * @name: canonical name of the property specified
660 * @nick: (nullable): nick name for the property specified
661 * @blurb: (nullable): description of the property specified
662 * @object_type: a `DEX_TYPE_OBJECT` derived type for this property
663 * @flags: flags for the property specified
664 *
665 * Creates a new [[email protected]] instance specifying a
666 * `DEX_TYPE_OBJECT` derived property.
667 *
668 * This is similar to [[email protected]_spec_object], but for `DexObject`
669 * instances such as [[email protected]].
670 *
671 * Returns: (transfer full): a newly created parameter specification
672 *
673 * Since: 1.2
674 */
675 GParamSpec *
676 dex_param_spec_object (const char *name,
677 const char *nick,
678 const char *blurb,
679 GType object_type,
680 GParamFlags flags)
681 {
682 GParamSpec *pspec;
683
684 g_return_val_if_fail (g_type_is_a (object_type, DEX_TYPE_OBJECT), NULL);
685
686 pspec = g_param_spec_internal (dex_param_spec_object_get_type (),
687 name,
688 nick,
689 blurb,
690 flags);
691 pspec->value_type = object_type;
692
693 return pspec;
694 }
695
696 /**
697 * dex_value_dup_object:
698 * @value: a `GValue` initialized with type `DEX_TYPE_OBJECT`
699 *
700 * Retrieves the `DexObject` stored inside the given `value`.
701 *
702 * Returns: (transfer full) (nullable): a `DexObject`
703 *
704 * Since: 1.0
705 */
706 DexObject *
707 dex_value_dup_object (const GValue *value)
708 {
709 g_return_val_if_fail (G_VALUE_HOLDS (value, DEX_TYPE_OBJECT), NULL);
710
711 if (value->data[0].v_pointer != NULL)
712 return dex_ref (value->data[0].v_pointer);
713
714 return NULL;
715 }
716
717 /**
718 * dex_value_get_object:
719 * @value: a `GValue` initialized with type `DEX_TYPE_OBJECT`
720 *
721 * Retrieves the `DexObject` stored inside the given `value`.
722 *
723 * Returns: (transfer none) (nullable): a `DexObject`
724 *
725 * Since: 0.4
726 */
727 DexObject *
728 dex_value_get_object (const GValue *value)
729 {
730 g_return_val_if_fail (G_VALUE_HOLDS (value, DEX_TYPE_OBJECT), NULL);
731
732 return value->data[0].v_pointer;
733 }
734
735 /**
736 * dex_value_set_object:
737 * @value: a [[email protected]] initialized with type `DEX_TYPE_OBJECT`
738 * @object: (nullable): a `DexObject` or %NULL
739 *
740 * Stores the given `DexObject` inside `value`.
741 *
742 * The [[email protected]] will acquire a reference to the `object`.
743 *
744 * Since: 0.4
745 */
746 void
747 dex_value_set_object (GValue *value,
748 DexObject *object)
749 {
750 if (object != NULL)
751 dex_ref (object);
752
753 dex_value_take_object (value, object);
754 }
755
756 /**
757 * dex_value_take_object:
758 * @value: a [[email protected]] initialized with type `DEX_TYPE_OBJECT`
759 * @object: (transfer full) (nullable): a `DexObject`
760 *
761 * Stores the given `DexObject` inside `value`.
762 *
763 * This function transfers the ownership of the `object` to the `GValue`.
764 *
765 * Since: 0.4
766 */
767 void
768 dex_value_take_object (GValue *value,
769 DexObject *object)
770 {
771 DexObject *old_object;
772
773 g_return_if_fail (G_VALUE_HOLDS (value, DEX_TYPE_OBJECT));
774
775 old_object = value->data[0].v_pointer;
776
777 if (object != NULL)
778 {
779 g_return_if_fail (DEX_IS_OBJECT (object));
780 g_return_if_fail (g_value_type_compatible (DEX_OBJECT_TYPE (object), G_VALUE_TYPE (value)));
781
782 value->data[0].v_pointer = object;
783 }
784 else
785 {
786 value->data[0].v_pointer = NULL;
787 }
788
789 if (old_object != NULL)
790 dex_unref (old_object);
791 }