xref: /haiku/src/kits/support/Referenceable.cpp (revision a6e73cb9e8addfe832c064bfcb68067f1c2fa3eb)
1 /*
2  * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Referenceable.h>
8 
9 
10 //#define TRACE_REFERENCEABLE
11 #ifdef TRACE_REFERENCEABLE
12 #	include <tracing.h>
13 #	define TRACE(x, ...) ktrace_printf(x, __VA_ARGS__);
14 #else
15 #	define TRACE(x, ...)
16 #endif
17 
18 
19 BReferenceable::BReferenceable()
20 	:
21 	fReferenceCount(1)
22 {
23 }
24 
25 
26 BReferenceable::~BReferenceable()
27 {
28 }
29 
30 
31 int32
32 BReferenceable::AcquireReference()
33 {
34 	int32 previousReferenceCount = atomic_add(&fReferenceCount, 1);
35 	if (previousReferenceCount == 0)
36 		FirstReferenceAcquired();
37 
38 	TRACE("%p: acquire %ld\n", this, fReferenceCount);
39 
40 	return previousReferenceCount;
41 }
42 
43 
44 int32
45 BReferenceable::ReleaseReference()
46 {
47 	int32 previousReferenceCount = atomic_add(&fReferenceCount, -1);
48 	TRACE("%p: release %ld\n", this, fReferenceCount);
49 	if (previousReferenceCount == 1)
50 		LastReferenceReleased();
51 	return previousReferenceCount;
52 }
53 
54 
55 void
56 BReferenceable::FirstReferenceAcquired()
57 {
58 }
59 
60 
61 void
62 BReferenceable::LastReferenceReleased()
63 {
64 	delete this;
65 }
66