| 1 |
pippijn |
1.1 |
/* |
| 2 |
|
|
* object.C: Object management. |
| 3 |
|
|
* Rights to this code are documented in doc/LICENSE. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
static char const rcsid[] = "$Id"; |
| 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 |
|
|
|
| 36 |
|
|
obj->destructor = des; |
| 37 |
|
|
obj->refcount = 1; |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
|
/* |
| 41 |
|
|
* object_ref |
| 42 |
|
|
* |
| 43 |
|
|
* Increment the reference counter on an object. |
| 44 |
|
|
* |
| 45 |
|
|
* Inputs: |
| 46 |
|
|
* - the object to refcount |
| 47 |
|
|
* |
| 48 |
|
|
* Outputs: |
| 49 |
|
|
* - none |
| 50 |
|
|
* |
| 51 |
|
|
* Side Effects: |
| 52 |
|
|
* - none |
| 53 |
|
|
*/ |
| 54 |
|
|
void * |
| 55 |
|
|
object_ref (void *object) |
| 56 |
|
|
{ |
| 57 |
|
|
return_val_if_fail (object != NULL, NULL); |
| 58 |
|
|
|
| 59 |
|
|
asobject (object)->refcount++; |
| 60 |
|
|
|
| 61 |
|
|
return object; |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
/* |
| 65 |
|
|
* object_unref |
| 66 |
|
|
* |
| 67 |
|
|
* Decrement the reference counter on an object. |
| 68 |
|
|
* |
| 69 |
|
|
* Inputs: |
| 70 |
|
|
* - the object to refcount |
| 71 |
|
|
* |
| 72 |
|
|
* Outputs: |
| 73 |
|
|
* - none |
| 74 |
|
|
* |
| 75 |
|
|
* Side Effects: |
| 76 |
|
|
* - if the refcount is 0, the object is destroyed. |
| 77 |
|
|
*/ |
| 78 |
|
|
void |
| 79 |
|
|
object_unref (void *object) |
| 80 |
|
|
{ |
| 81 |
|
|
object_t *obj; |
| 82 |
|
|
|
| 83 |
|
|
return_if_fail (object != NULL); |
| 84 |
|
|
obj = asobject (object); |
| 85 |
|
|
|
| 86 |
|
|
obj->refcount--; |
| 87 |
|
|
|
| 88 |
|
|
if (obj->refcount <= 0) |
| 89 |
|
|
{ |
| 90 |
|
|
if (obj->name != NULL) |
| 91 |
|
|
free (obj->name); |
| 92 |
|
|
|
| 93 |
|
|
if (obj->destructor != NULL) |
| 94 |
|
|
obj->destructor (obj); |
| 95 |
|
|
else |
| 96 |
|
|
free (obj); |
| 97 |
|
|
} |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 101 |
|
|
* vim:ts=8 |
| 102 |
|
|
* vim:sw=8 |
| 103 |
|
|
* vim:noexpandtab |
| 104 |
|
|
*/ |