Coda Distributed File System
persistent.h
Go to the documentation of this file.
1/* BLURB gpl
2
3 Coda File System
4 Release 8
5
6 Copyright (c) 2003-2025 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU General Public Licence Version 2, as shown in the
11file LICENSE. The technical and financial contributors to Coda are
12listed in the file CREDITS.
13
14 Additional copyrights
15 none currently
16
17#*/
18
19/*
20 * Base class that implements persistent reference counted objects. Useable for
21 * VM objects that can be referenced from different places and should be
22 * automatically destroyed when the last reference is dropped.
23 */
24
25#ifndef _PERSISTENT_H_
26#define _PERSISTENT_H_
27
28#error "Only use this as a template!"
29/* RVM doesn't cooperate nicely with inheritance and virtual functions */
30
31#include <rvmlib.h>
32#include <venusrecov.h>
33#include <coda_assert.h>
34
36public:
37 void *operator new(size_t size) REQUIRES_TRANSACION
38 {
39 void *p = rvmlib_rec_malloc(size);
40 CODA_ASSERT(p);
41 return p;
42 }
43
44 void operator delete(void *p) REQUIRES_TRANSACION { rvmlib_rec_free(p); }
45
46 PersistentObject(void) REQUIRES_TRANSACION
47 {
48 RVMLIB_REC_OBJECT(rec_refcount);
49 rec_refcount = 0;
50 refcount = 1;
51 }
52
53 virtual ~PersistentObject(void)
54 {
55 CODA_ASSERT(!rec_refcount && refcount <= 1);
56 }
57
59 {
60 refcount = 0;
61 /* If there are no RVM references anymore, delayed destruction. */
62 if (rvmlib_in_transaction() && !rec_refcount)
63 delete this;
64 }
65
66 void Rec_GetRef(void) REQUIRES_TRANSACION
67 {
68 RVMLIB_REC_OBJECT(rec_refcount);
69 rec_refcount++;
70 }
71
72 virtual void Rec_PutRef(void) REQUIRES_TRANSACION
73 {
74 CODA_ASSERT(rec_refcount);
75 RVMLIB_REC_OBJECT(rec_refcount);
76 rec_refcount--;
77 if (!refcount && !rec_refcount)
78 delete this;
79 }
80
81 void GetRef(void) { refcount++; }
82
83 virtual void PutRef(void) TRANSACTION_OPTIONAL
84 {
85 CODA_ASSERT(refcount);
86 refcount--;
87 /*
88 * Only destroy the object if we happen to be in a transaction,
89 * otherwise we'll destroy ourselves later during ResetTransient,
90 * or when a reference is regained and then dropped in a transaction.
91 */
92 if (rvmlib_in_transaction() && !refcount && !rec_refcount)
93 delete this;
94 }
95
96private:
97 unsigned int rec_refcount;
98 /*T*/ unsigned int refcount;
99};
100
101#endif /* _PERSISTENT_H_ */
Definition: persistent.h:35
PersistentObject(void) REQUIRES_TRANSACION
Definition: persistent.h:46
virtual ~PersistentObject(void)
Definition: persistent.h:53
virtual void ResetTransient(void) TRANSACTION_OPTIONAL
Definition: persistent.h:58
virtual void PutRef(void) TRANSACTION_OPTIONAL
Definition: persistent.h:83
void GetRef(void)
Definition: persistent.h:81
virtual void Rec_PutRef(void) REQUIRES_TRANSACION
Definition: persistent.h:72
void Rec_GetRef(void) REQUIRES_TRANSACION
Definition: persistent.h:66
#define CODA_ASSERT(pred)
Definition: coda_assert.h:22
#define TRANSACTION_OPTIONAL
Definition: coda_tsa.h:109
int rvmlib_in_transaction(void)
Definition: rvmlib.c:282
#define RVMLIB_REC_OBJECT(object)
Definition: rvmlib.h:126
#define rvmlib_rec_free(addr)
Definition: rvmlib.h:123
#define rvmlib_rec_malloc(size)
Definition: rvmlib.h:122