xref: /aosp_15_r20/external/angle/src/libANGLE/CLObject.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLObject.h: Defines the cl::Object class, which is the base class of all ANGLE CL objects.
7 
8 #ifndef LIBANGLE_CLOBJECT_H_
9 #define LIBANGLE_CLOBJECT_H_
10 
11 #include "libANGLE/cl_types.h"
12 #include "libANGLE/renderer/cl_types.h"
13 
14 #include <atomic>
15 
16 namespace cl
17 {
18 
19 class Object
20 {
21   public:
22     Object();
23     virtual ~Object();
24 
getRefCount()25     cl_uint getRefCount() const noexcept { return mRefCount; }
26 
retain()27     void retain() noexcept { ++mRefCount; }
28 
release()29     bool release()
30     {
31         if (mRefCount == 0u)
32         {
33             WARN() << "Unreferenced object without references";
34             return true;
35         }
36         return --mRefCount == 0u;
37     }
38 
39     template <typename T, typename... Args>
Create(Args &&...args)40     static T *Create(Args &&...args)
41     {
42         T *object = new T(std::forward<Args>(args)...);
43         return object;
44     }
45 
46   private:
47     std::atomic<cl_uint> mRefCount;
48 };
49 
50 }  // namespace cl
51 
52 #endif  // LIBANGLE_CLCONTEXT_H_
53