AVPKit
RefCounted.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #include "RefCounted.h"
21 #include "AtomicInteger.h"
22 #include "JNIHelper.h"
23 //#include "Logger.h"
24 
25 //VS_LOG_SETUP(VS_CPP_PACKAGE);
26 
27 namespace com { namespace avpkit { namespace ferry {
28 
29  RefCounted :: RefCounted()
30  {
31  mRefCount = new AtomicInteger(0);
32  mAllocator = 0;
33  }
34 
35  RefCounted :: ~RefCounted()
36  {
37  if (mRefCount)
38  delete mRefCount;
39  mRefCount = 0;
40  if (mAllocator)
41  JNIHelper::sDeleteGlobalRef((jobject)mAllocator);
42  mAllocator = 0;
43  }
44 
45  void
47  {
48  if (mAllocator)
49  JNIHelper::sDeleteGlobalRef((jobject)mAllocator);
50  if (allocator)
51  mAllocator = JNIHelper::sNewGlobalRef((jobject)allocator);
52  else
53  mAllocator = 0;
54  }
55 
56  void*
58  {
59  return mAllocator;
60  }
61 
62  int32_t
64  {
65  //VS_LOG_DEBUG("acquire: %p", this);
66  return mRefCount->incrementAndGet();
67  }
68 
69  int32_t
71  {
72  //VS_LOG_DEBUG("release: %p", this);
73  int32_t retval = mRefCount->decrementAndGet();
74  if (!retval)
75  this->destroy();
76  return retval;
77  }
78 
79  int32_t
81  {
82  return mRefCount->get();
83  }
84 
85  void
87  {
88  //VS_LOG_DEBUG("destroy: %p", this);
89  // by default just call the destructor
90  delete this;
91  }
92 
93  RefCounted*
95  {
96  this->acquire();
97  return this;
98  }
99 
100 }}}
Parent of all Ferry objects – it mains reference counts in native code.
Definition: RefCounted.h:85
virtual int32_t acquire()
Internal Only.
Definition: RefCounted.cpp:63
virtual void destroy()
This method is called by RefCounted objects when their Ref Count reaches zero and they are about to b...
Definition: RefCounted.cpp:86
void * mAllocator
Not part of public API.
Definition: RefCounted.h:193
void * getJavaAllocator()
This method is public but not part of the standard API.
Definition: RefCounted.cpp:57
virtual int32_t release()
Internal Only.
Definition: RefCounted.cpp:70
virtual RefCounted * copyReference()
Create a new Java object that refers to the same native object.
Definition: RefCounted.cpp:94
virtual int32_t getCurrentRefCount()
Return the current reference count on this object.
Definition: RefCounted.cpp:80
void setJavaAllocator(void *allocator)
This method is public but not part of the standard API.
Definition: RefCounted.cpp:46
AtomicInteger * mRefCount
This is the internal reference count, represented as an AtomicInteger to make sure it is thread safe.
Definition: RefCounted.h:188
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...