1 |
/* |
2 |
* object.C: Object management. |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id: object.C,v 1.3 2007-07-21 13:23:22 pippijn Exp $"; |
9 |
|
10 |
#include "atheme.h" |
11 |
|
12 |
/* |
13 |
* object_init |
14 |
* |
15 |
* Populates the object manager part of an object. |
16 |
* |
17 |
* Inputs: |
18 |
* - pointer to object manager area |
19 |
* - (optional) name of object |
20 |
* - (optional) custom destructor |
21 |
* |
22 |
* Outputs: |
23 |
* - none |
24 |
* |
25 |
* Side Effects: |
26 |
* - none |
27 |
*/ |
28 |
void |
29 |
object_init (object_t * obj, const char *name, destructor_t des) |
30 |
{ |
31 |
return_if_fail (obj != NULL); |
32 |
|
33 |
if (name != NULL) |
34 |
obj->name = sstrdup (name); |
35 |
else |
36 |
obj->name = sstrdup (""); |
37 |
|
38 |
obj->destructor = des; |
39 |
obj->refcount = 1; |
40 |
} |
41 |
|
42 |
/* |
43 |
* object_ref |
44 |
* |
45 |
* Increment the reference counter on an object. |
46 |
* |
47 |
* Inputs: |
48 |
* - the object to refcount |
49 |
* |
50 |
* Outputs: |
51 |
* - none |
52 |
* |
53 |
* Side Effects: |
54 |
* - none |
55 |
*/ |
56 |
void * |
57 |
object_ref (void *object) |
58 |
{ |
59 |
return_val_if_fail (object != NULL, NULL); |
60 |
|
61 |
asobject (object)->refcount++; |
62 |
|
63 |
return object; |
64 |
} |
65 |
|
66 |
/* |
67 |
* object_unref |
68 |
* |
69 |
* Decrement the reference counter on an object. |
70 |
* |
71 |
* Inputs: |
72 |
* - the object to refcount |
73 |
* |
74 |
* Outputs: |
75 |
* - none |
76 |
* |
77 |
* Side Effects: |
78 |
* - if the refcount is 0, the object is destroyed. |
79 |
*/ |
80 |
void |
81 |
object_unref (void *object) |
82 |
{ |
83 |
object_t *obj; |
84 |
|
85 |
return_if_fail (object != NULL); |
86 |
obj = asobject (object); |
87 |
|
88 |
obj->refcount--; |
89 |
|
90 |
if (obj->refcount <= 0) |
91 |
{ |
92 |
sfree (obj->name); |
93 |
|
94 |
if (obj->destructor != NULL) |
95 |
obj->destructor (obj); |
96 |
else |
97 |
sfree (obj); |
98 |
} |
99 |
} |