Bug 22803 - Make header conform to implementation
[swfdec.git] / swfdec / swfdec_gc_object.c
blob6e6451b7e12fdd340d5cf725d11582ff86dd548e
1 /* Swfdec
2 * Copyright (C) 2008 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <string.h>
26 #include "swfdec_gc_object.h"
27 #include "swfdec_as_context.h"
28 #include "swfdec_as_internal.h"
30 /**
31 * SECTION:SwfdecGcObject
32 * @title: SwfdecGcObject
33 * @short_description: the base object type for garbage-collected objects
35 * This type is the basic garbage-collected object in Swfdec. It contains the
36 * simple facilities for required by the garbage collector. The initial
37 * reference of this object will be owned by the context that created it and
38 * will be released automatically when no other object references it anymore
39 * in the garbage collection cycle.
41 * Note that you cannot know the lifetime of a #SwfdecGcObject, since scripts
42 * may assign it as a variable to other objects. So you should not assume to
43 * know when an object gets removed.
46 /**
47 * SwfdecGcObject:
49 * If you want to add custom objects to the garbage collection lifecycle, you
50 * need to subclass this object as this object is abstract. Note that you have
51 * to provide a valid #SwfdecAsContext whenever you construct objects of this
52 * type.
55 /**
56 * SwfdecGcObjectClass:
57 * @mark: Called in the mark phase of garbage collection. Mark all the
58 * garbage-collected object you still use here using the marking
59 * functions such as swfdec_gc_object_mark() or swfdec_as_string_mark()
61 * This is the base class for garbage-collected objects.
64 enum {
65 PROP_0,
66 PROP_CONTEXT
69 G_DEFINE_ABSTRACT_TYPE (SwfdecGcObject, swfdec_gc_object, G_TYPE_OBJECT)
71 static gsize
72 swfdec_gc_object_get_size (SwfdecGcObject *object)
74 GTypeQuery query;
76 /* FIXME: This only uses the size of the public instance but doesn't include
77 * private members. http://bugzilla.gnome.org/show_bug.cgi?id=354457 blocks
78 * this. */
79 g_type_query (G_OBJECT_TYPE (object), &query);
80 return query.instance_size;
83 static void
84 swfdec_gc_object_dispose (GObject *gobject)
86 SwfdecGcObject *object = SWFDEC_GC_OBJECT (gobject);
88 swfdec_as_context_unuse_mem (object->context, swfdec_gc_object_get_size (object));
89 G_OBJECT_CLASS (swfdec_gc_object_parent_class)->dispose (gobject);
92 static void
93 swfdec_gc_object_do_mark (SwfdecGcObject *object)
97 static GObject *
98 swfdec_gc_object_constructor (GType type, guint n_construct_properties,
99 GObjectConstructParam *construct_properties)
101 GObject *gobject;
102 SwfdecGcObject *object;
103 SwfdecAsContext *context;
105 gobject = G_OBJECT_CLASS (swfdec_gc_object_parent_class)->constructor (type,
106 n_construct_properties, construct_properties);
107 object = SWFDEC_GC_OBJECT (gobject);
109 context = object->context;
110 swfdec_as_context_use_mem (context, swfdec_gc_object_get_size (object));
111 object->next = context->gc_objects;
112 context->gc_objects = object;
114 return gobject;
117 static void
118 swfdec_gc_object_get_property (GObject *object, guint param_id, GValue *value,
119 GParamSpec * pspec)
121 SwfdecGcObject *gc = SWFDEC_GC_OBJECT (object);
123 switch (param_id) {
124 case PROP_CONTEXT:
125 g_value_set_object (value, gc->context);
126 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
129 break;
133 static void
134 swfdec_gc_object_set_property (GObject *object, guint param_id, const GValue *value,
135 GParamSpec * pspec)
137 SwfdecGcObject *gc = SWFDEC_GC_OBJECT (object);
139 switch (param_id) {
140 case PROP_CONTEXT:
141 gc->context = g_value_get_object (value);
142 g_assert (gc->context != NULL);
143 break;
144 default:
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
146 break;
150 static void
151 swfdec_gc_object_class_init (SwfdecGcObjectClass *klass)
153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
155 object_class->dispose = swfdec_gc_object_dispose;
156 object_class->set_property = swfdec_gc_object_set_property;
157 object_class->get_property = swfdec_gc_object_get_property;
158 object_class->constructor = swfdec_gc_object_constructor;
160 g_object_class_install_property (object_class, PROP_CONTEXT,
161 g_param_spec_object ("context", "context", "context managing this object",
162 SWFDEC_TYPE_AS_CONTEXT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
164 klass->mark = swfdec_gc_object_do_mark;
167 static void
168 swfdec_gc_object_init (SwfdecGcObject *object)
173 * swfdec_gc_object_get_context:
174 * @object: a #SwfdecGcObject. This function takes a gpointer argument only to
175 * save you from having to cast it manually. For language bindings,
176 * please treat this argument as having the #SwfdecGcObject type.
178 * Gets the context that garbage-collects this object.
180 * Returns: the context this object belongs to
182 SwfdecAsContext *
183 swfdec_gc_object_get_context (gpointer object)
185 g_return_val_if_fail (SWFDEC_IS_GC_OBJECT (object), NULL);
187 return SWFDEC_GC_OBJECT (object)->context;
191 * swfdec_gc_object_mark:
192 * @object: a #SwfdecGcObject. This function takes a gpointer argument only to
193 * save you from having to cast it manually. For language bindings,
194 * please treat this argument as having the #SwfdecGcObject type.
196 * Mark @object as being in use. Calling this function is only valid during
197 * the marking phase of garbage collection.
199 void
200 swfdec_gc_object_mark (gpointer object)
202 SwfdecGcObject *gc = object;
203 SwfdecGcObjectClass *klass;
205 g_return_if_fail (SWFDEC_IS_GC_OBJECT (object));
207 if (gc->flags & SWFDEC_AS_GC_MARK)
208 return;
209 gc->flags |= SWFDEC_AS_GC_MARK;
210 klass = SWFDEC_GC_OBJECT_GET_CLASS (gc);
211 g_assert (klass->mark);
212 klass->mark (gc);