ScummVM API documentation
managed_object_pool.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_ENGINE_AC_DYNOBJ_CC_MANAGED_OBJECT_POOL_H
23 #define AGS_ENGINE_AC_DYNOBJ_CC_MANAGED_OBJECT_POOL_H
24 
25 #include "common/std/vector.h"
26 #include "common/std/queue.h"
27 #include "common/std/map.h"
28 
29 #include "ags/shared/core/platform.h"
30 #include "ags/engine/script/runtime_script_value.h"
31 #include "ags/engine/ac/dynobj/cc_dynamic_object.h" // ICCDynamicObject
32 
33 namespace AGS3 {
34 
35 namespace AGS {
36 namespace Shared {
37 class Stream;
38 } // namespace Shared
39 } // namespace AGS
40 
41 using namespace AGS; // FIXME later
42 
43 struct Pointer_Hash {
44  uint operator()(const char *v) const {
45  return static_cast<uint>(reinterpret_cast<uintptr>(v));
46  }
47 };
48 
49 
50 struct ManagedObjectPool final {
51 private:
52  // TODO: find out if we can make handle size_t
53  struct ManagedObject {
54  ScriptValueType obj_type;
55  int32_t handle;
56  // TODO: this makes no sense having this as "const char*",
57  // void* will be proper (and in all related functions)
58  const char *addr;
59  ICCDynamicObject *callback;
60  int refCount;
61 
62  bool isUsed() const {
63  return obj_type != kScValUndefined;
64  }
65 
66  ManagedObject() : obj_type(kScValUndefined), handle(0), addr(nullptr),
67  callback(nullptr), refCount(0) {
68  }
69  ManagedObject(ScriptValueType theType, int32_t theHandle,
70  const char *theAddr, ICCDynamicObject *theCallback)
71  : obj_type(theType), handle(theHandle), addr(theAddr),
72  callback(theCallback), refCount(0) {
73  }
74  };
75 
76  int objectCreationCounter; // used to do garbage collection every so often
77 
78  int32_t nextHandle{}; // TODO: manage nextHandle's going over INT32_MAX !
79  std::queue<int32_t> available_ids;
82 
83  void Init(int32_t theHandle, const char *theAddress, ICCDynamicObject *theCallback, ScriptValueType objType);
84  int Remove(ManagedObject &o, bool force = false);
85 
86  void RunGarbageCollection();
87 
88 public:
89 
90  int32_t AddRef(int32_t handle);
91  int CheckDispose(int32_t handle);
92  int32_t SubRef(int32_t handle);
93  int32_t AddressToHandle(const char *addr);
94  const char *HandleToAddress(int32_t handle);
95  ScriptValueType HandleToAddressAndManager(int32_t handle, void *&object, ICCDynamicObject *&manager);
96  int RemoveObject(const char *address);
97  void RunGarbageCollectionIfAppropriate();
98  int AddObject(const char *address, ICCDynamicObject *callback, bool plugin_object);
99  int AddUnserializedObject(const char *address, ICCDynamicObject *callback, bool plugin_object, int handle);
100  void WriteToDisk(Shared::Stream *out);
101  int ReadFromDisk(Shared::Stream *in, ICCObjectReader *reader);
102  void reset();
104 
105  const char *disableDisposeForObject{ nullptr };
106 };
107 
108 // Extreme(!!) verbosity managed memory pool log
109 #if DEBUG_MANAGED_OBJECTS
110 #define ManagedObjectLog(...) Debug::Printf(kDbgGroup_ManObj, kDbgMsg_Debug, __VA_ARGS__)
111 #else
112 #define ManagedObjectLog(...)
113 #endif
114 
115 } // namespace AGS3
116 
117 #endif
Definition: achievements_tables.h:27
Definition: cc_dynamic_object.h:97
Definition: map.h:204
Definition: managed_object_pool.h:50
Definition: managed_object_pool.h:43
Definition: queue.h:42
Definition: cc_dynamic_object.h:51
Definition: stream.h:52
Definition: ags.h:40