A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RefCountedObject.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef REF_COUNTED_OBJECT_CC
20 #define REF_COUNTED_OBJECT_CC
21 
22 #include <cstddef>
23 #include <iostream>
24 #include <vector>
25 #include <algorithm>
26 #include <iterator>
27 #include <climits>
28 #include <cstring>
29 
30 #include "Allocator.h"
31 #include "RefCountedObject.h"
32 #include "RefCountMacros.h"
33 
34 namespace pdb {
35 
36 template <class ObjType>
38 
39  // don't change the ref count for an un-managed object
40  if (!getAllocator().isManaged(this)) {
41  return;
42  }
43 
44  NUM_COPIES = toMe;
45 }
46 
47 template <class ObjType>
49 
50  // an un-managed ref count is always huge
51  if (!getAllocator().isManaged(this)) {
52  return UINT_MAX;
53  }
54 
55  return NUM_COPIES;
56 }
57 
58 template <class ObjType>
60 
61  // don't change the ref count for an un-managed object
62  if (!getAllocator().isManaged(this)) {
63  return;
64  }
65 
66  NUM_COPIES++;
67 }
68 
69 // remember the ref count
70 template <class ObjType>
72 
73  // don't change the ref count for an un-managed object
74  if (!getAllocator().isManaged(this)) {
75  return;
76  }
77 
78  NUM_COPIES--;
79 
80  // if the ref count goes to zero, free the pointed-to object
81  if (NUM_COPIES == 0) {
82  if (typeInfo.getTypeCode() == 0) {
83  PDB_COUT << "RefCountedObject::decRefCount: typeInfo=0 before deleteConstituentObject"
84  << std::endl;
85  }
86  // call the appropriate delete to recursively free, if needed
87  typeInfo.deleteConstituentObject(getObject());
88 
89 // and free the RAM
90 #ifdef DEBUG_OBJECT_MODEL
91  getAllocator().freeRAM(CHAR_PTR(this), typeInfo.getTypeCode());
92 #else
93  getAllocator().freeRAM(CHAR_PTR(this));
94 #endif
95  }
96 }
97 
98 template <class ObjType>
100  return (ObjType*)(CHAR_PTR(this) + REF_COUNT_PREAMBLE_SIZE);
101 }
102 }
103 
104 #endif
#define REF_COUNT_PREAMBLE_SIZE
void deleteConstituentObject(void *deleteMe) const
Allocator & getAllocator()
Definition: Allocator.cc:943
void decRefCount(PDBTemplateBase &typeInfo)
#define NUM_COPIES
#define CHAR_PTR(c)
Definition: Allocator.cc:33
int16_t getTypeCode() const
#define PDB_COUT
Definition: PDBDebug.h:31
ObjType * getObject() const
void setRefCount(unsigned toMe)
void freeRAM(void *here)
Definition: Allocator.cc:632