Line data Source code
1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 : /*
3 : * lt-ext-module-data.c
4 : * Copyright (C) 2011-2012 Akira TAGOH
5 : *
6 : * Authors:
7 : * Akira TAGOH <akira@tagoh.org>
8 : *
9 : * You may distribute under the terms of either the GNU
10 : * Lesser General Public License or the Mozilla Public
11 : * License, as specified in the README file.
12 : */
13 : #ifdef HAVE_CONFIG_H
14 : #include "config.h"
15 : #endif
16 :
17 : #include <stdlib.h>
18 : #include "lt-atomic.h"
19 : #include "lt-mem.h"
20 : #include "lt-messages.h"
21 : #include "lt-ext-module-data.h"
22 :
23 :
24 : /**
25 : * SECTION: lt-ext-module-data
26 : * @Short_Description: A container class for Extension module data.
27 : * @Title: Module - Data
28 : *
29 : * This container class provides a data access for a specific data of
30 : * extension subtag.
31 : */
32 : typedef struct _lt_ext_module_data_private_t {
33 : lt_mem_t parent;
34 : lt_destroy_func_t finalizer;
35 : lt_pointer_t dummy[3];
36 : } lt_ext_module_data_private_t;
37 :
38 : /*< private >*/
39 :
40 : /*< public >*/
41 : /**
42 : * lt_ext_module_data_new:
43 : * @size: real size to allocate memory for the object.
44 : * @finalizer: (scope async): a callback function to destroy the content.
45 : *
46 : * Create a new instance of #lt_ext_module_data_t. this function allows
47 : * to create an inherited instance like:
48 : * |[
49 : * struct _my_module_data_t {
50 : * lt_ext_module_data_t parent;
51 : * ...own members...
52 : * };
53 : * ]|
54 : *
55 : * Returns: (transfer full): a #lt_ext_module_data_t.
56 : */
57 : lt_ext_module_data_t *
58 0 : lt_ext_module_data_new(size_t size,
59 : lt_destroy_func_t finalizer)
60 : {
61 : lt_ext_module_data_private_t *retval;
62 :
63 : lt_assert(sizeof (lt_ext_module_data_t) == sizeof (lt_ext_module_data_private_t));
64 :
65 0 : if (size < sizeof (lt_ext_module_data_private_t))
66 0 : size += sizeof (lt_ext_module_data_private_t);
67 :
68 0 : retval = lt_mem_alloc_object(size);
69 0 : if (retval) {
70 0 : retval->finalizer = finalizer;
71 : }
72 :
73 0 : return (lt_ext_module_data_t *)retval;
74 : }
75 :
76 : /**
77 : * lt_ext_module_data_ref:
78 : * @data: a #lt_ext_module_data_t.
79 : *
80 : * Increases the reference count of @data.
81 : *
82 : * Returns: (transfer none): the same @data object.
83 : */
84 : lt_ext_module_data_t *
85 0 : lt_ext_module_data_ref(lt_ext_module_data_t *data)
86 : {
87 0 : lt_ext_module_data_private_t *priv = (lt_ext_module_data_private_t *)data;
88 :
89 0 : lt_return_val_if_fail (data != NULL, NULL);
90 :
91 0 : return lt_mem_ref(&priv->parent);
92 : }
93 :
94 : /**
95 : * lt_ext_module_data_unref:
96 : * @data: a #lt_ext_module_data_t.
97 : *
98 : * Decreases the reference count of @data. when its reference count
99 : * drops to 0, the object is finalized (i.e. its memory is freed).
100 : */
101 : void
102 0 : lt_ext_module_data_unref(lt_ext_module_data_t *data)
103 : {
104 0 : lt_ext_module_data_private_t *priv = (lt_ext_module_data_private_t *)data;
105 :
106 0 : if (data) {
107 0 : volatile int ref_count = lt_atomic_int_get((volatile int *)&priv->parent.ref_count);
108 :
109 0 : if (ref_count == 1) {
110 0 : if (priv->finalizer) {
111 0 : priv->finalizer(data);
112 : }
113 : }
114 0 : lt_mem_unref(&priv->parent);
115 : }
116 0 : }
|