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_script_object.h" // IScriptObject
32
33namespace AGS3 {
34
35namespace AGS {
36namespace Shared {
37class Stream;
38} // namespace Shared
39} // namespace AGS
40
41using namespace AGS; // FIXME later
42
44 uint operator()(void *v) const {
45 return static_cast<uint>(reinterpret_cast<uintptr>(v));
46 }
47};
48
49
50struct ManagedObjectPool final {
51private:
52 // TODO: find out if we can make handle size_t
53 struct ManagedObject {
54 ScriptValueType obj_type;
55 int32_t handle;
56 void *addr;
57 IScriptObject *callback;
58 int refCount;
59
60 bool isUsed() const {
61 return obj_type != kScValUndefined;
62 }
63
64 ManagedObject() : obj_type(kScValUndefined), handle(0), addr(nullptr),
65 callback(nullptr), refCount(0) {}
66 ManagedObject(ScriptValueType theType, int32_t theHandle,
67 void *theAddr, IScriptObject *theCallback)
68 : obj_type(theType), handle(theHandle), addr(theAddr),
69 callback(theCallback), refCount(0) {
70 }
71 };
72
73 int objectCreationCounter; // used to do garbage collection every so often
74
75 int32_t nextHandle{}; // TODO: manage nextHandle's going over INT32_MAX !
76 std::queue<int32_t> available_ids;
79
80 int Add(int handle, void *address, IScriptObject *callback, ScriptValueType obj_type);
81 int Remove(ManagedObject &o, bool force = false);
82 void RunGarbageCollection();
83
84public:
85
86 int32_t AddRef(int32_t handle);
87 int CheckDispose(int32_t handle);
88 int32_t SubRef(int32_t handle);
89 int32_t AddressToHandle(void *addr);
90 void *HandleToAddress(int32_t handle);
91 ScriptValueType HandleToAddressAndManager(int32_t handle, void *&object, IScriptObject *&manager);
92 int RemoveObject(void *address);
93 void RunGarbageCollectionIfAppropriate();
94 int AddObject(void *address, IScriptObject *callback, ScriptValueType obj_type);
95 int AddUnserializedObject(void *address, IScriptObject *callback, ScriptValueType obj_type, int handle);
96 void WriteToDisk(Shared::Stream *out);
97 int ReadFromDisk(Shared::Stream *in, ICCObjectCollectionReader *reader);
98 void reset();
100
101 void *disableDisposeForObject{ nullptr };
102};
103
104// Extreme(!!) verbosity managed memory pool log
105#if DEBUG_MANAGED_OBJECTS
106#define ManagedObjectLog(...) Debug::Printf(kDbgGroup_ManObj, kDbgMsg_Debug, __VA_ARGS__)
107#else
108#define ManagedObjectLog(...)
109#endif
110
111} // namespace AGS3
112
113#endif
Definition: stream.h:52
Definition: queue.h:42
Definition: map.h:204
Definition: cc_script_object.h:113
Definition: cc_script_object.h:60
Definition: managed_object_pool.h:50
Definition: managed_object_pool.h:43