NEURON
resource.cpp
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2 #include <../../nrnconf.h>
3 #endif
4 /*
5  * Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
6  * Copyright (c) 1991 Silicon Graphics, Inc.
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and
9  * its documentation for any purpose is hereby granted without fee, provided
10  * that (i) the above copyright notices and this permission notice appear in
11  * all copies of the software and related documentation, and (ii) the names of
12  * Stanford and Silicon Graphics may not be used in any advertising or
13  * publicity relating to the software without the specific, prior written
14  * permission of Stanford and Silicon Graphics.
15  *
16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
21  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25  * OF THIS SOFTWARE.
26  */
27 
28 #include <vector>
29 
30 #include <InterViews/resource.h>
31 
32 class ResourceImpl {
33  friend class Resource;
34 
35  static bool deferred_;
36  static std::vector<Resource*> deletes_;
37 };
38 
39 bool ResourceImpl::deferred_ = false;
40 std::vector<Resource*> ResourceImpl::deletes_;
41 
42 void Resource::ref() const {
43  Resource* r = (Resource*)this;
44  r->refcount_ += 1;
45 }
46 
47 void Resource::unref() const {
48  Resource* r = (Resource*)this;
49  if (r->refcount_ != 0) {
50  r->refcount_ -= 1;
51  }
52  if (r->refcount_ == 0) {
53  r->cleanup();
54  delete r;
55  }
56 }
57 
59  Resource* r = (Resource*)this;
60  if (r->refcount_ != 0) {
61  r->refcount_ -= 1;
62  }
63  if (r->refcount_ == 0) {
64  r->cleanup();
66  ResourceImpl::deletes_.push_back(r);
67  } else {
68  delete r;
69  }
70  }
71 }
72 
74 
75 void Resource::ref(const Resource* r) {
76  if (r != nil) {
77  r->ref();
78  }
79 }
80 
81 void Resource::unref(const Resource* r) {
82  if (r != nil) {
83  r->unref();
84  }
85 }
86 
88  if (r != nil) {
89  r->unref_deferred();
90  }
91 }
92 
93 bool Resource::defer(bool b) {
94  bool previous = ResourceImpl::deferred_;
95  if (b != previous) {
96  flush();
98  }
99  return previous;
100 }
101 
103  bool previous = ResourceImpl::deferred_;
104  ResourceImpl::deferred_ = false;
105  for (auto& r: ResourceImpl::deletes_) {
106  delete r;
107  }
108  ResourceImpl::deletes_.clear();
109  ResourceImpl::deletes_.shrink_to_fit();
110  ResourceImpl::deferred_ = previous;
111 }
#define nil
Definition: enter-scope.h:35
virtual void ref() const
Definition: resource.cpp:42
virtual void cleanup()
Definition: resource.cpp:73
unsigned refcount_
Definition: resource.h:56
static bool defer(bool)
Definition: resource.cpp:93
virtual void unref() const
Definition: resource.cpp:47
virtual void unref_deferred() const
Definition: resource.cpp:58
static void flush()
Definition: resource.cpp:102
static bool deferred_
Definition: resource.cpp:35
static std::vector< Resource * > deletes_
Definition: resource.cpp:36