AVPKit
RefCounted.h
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 #ifndef REFCOUNTED_H_
21 #define REFCOUNTED_H_
22 
23 // For size_t
24 #include <new>
25 
26 // For int32_t
27 #include <inttypes.h>
28 
29 // for std::bad_alloc
30 #include <stdexcept>
31 
32 #include <com/avpkit/ferry/Ferry.h>
33 
34 namespace com { namespace avpkit { namespace ferry {
35  class AtomicInteger;
36 
84  class VS_API_FERRY RefCounted
85  {
86  public:
102  virtual int32_t acquire();
103 
118  virtual int32_t release();
119 
120 #ifndef SWIG
134  virtual RefCounted* copyReference();
135 #endif // ! SWIG
136 
152  virtual int32_t getCurrentRefCount();
153 #ifndef SWIG
154  // These are used by the JNIMemoryManager.cpp and wrapped there.
165  void setJavaAllocator(void* allocator);
171  void* getJavaAllocator();
172 #endif
173 
174  protected:
179  virtual void destroy();
180 
181  RefCounted();
182  virtual ~RefCounted();
183 
189 
193  void * mAllocator;
194  };
195 
196 #define VS_JNIUTILS_REFCOUNTED_MAKE(__class) \
197  static __class* make() { \
198  __class *obj = new __class(); \
199  if (obj) { \
200  (void) obj->acquire(); \
201  } else { \
202  throw std::bad_alloc(); \
203  } \
204  return obj; \
205  } \
206  private: \
207  __class &operator=(const __class &); \
208  __class(const __class &); \
209  static void * operator new (size_t aSize) { return ::operator new(aSize); }
210 
211 #define VS_JNIUTILS_REFCOUNTED_OBJECT(__class) \
212  public: \
213  VS_JNIUTILS_REFCOUNTED_MAKE(__class) \
214  private:
215 
216 #define VS_JNIUTILS_REFCOUNTED_OBJECT_PRIVATE_MAKE(__className) \
217  private: \
218  VS_JNIUTILS_REFCOUNTED_MAKE(__className) \
219  private:
220 
221 }}}
222 
223 #define VS_REF_ACQUIRE(__object) \
224  do { \
225  if (__object) { \
226  (__object)->acquire(); \
227  } \
228  } while (0)
229 
230 #define VS_REF_RELEASE(__object) \
231  do { \
232  if (__object) { \
233  (__object)->release(); \
234  } \
235  (__object) = 0; \
236  } while (0)
237 
238 #endif /*REFCOUNTED_H_*/
Parent of all Ferry objects – it mains reference counts in native code.
Definition: RefCounted.h:85
void * mAllocator
Not part of public API.
Definition: RefCounted.h:193
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...