summaryrefslogtreecommitdiff
path: root/Runtime/Serialize/Remapper.h
blob: 6e1debc455cd3aa646d045a2965df5f150183260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef REMAPPER_H
#define REMAPPER_H

#include <limits>
#include "Runtime/Utilities/MemoryPool.h"
#include "Configuration/UnityConfigure.h"

class Remapper
{
	public:
#if ENABLE_CUSTOM_ALLOCATORS_FOR_STDMAP
	MemoryPool      m_SerializedObjectIdentifierPool;
	typedef std::map<SerializedObjectIdentifier, SInt32, std::less<SerializedObjectIdentifier>, memory_pool_explicit<std::pair<const SerializedObjectIdentifier, SInt32> > > FileToHeapIDMap;
	typedef std::map<SInt32, SerializedObjectIdentifier, std::less<SInt32>, memory_pool_explicit<std::pair<const SInt32, SerializedObjectIdentifier> > > HeapIDToFileMap;
#else
	typedef std::map<SerializedObjectIdentifier, SInt32, std::less<SerializedObjectIdentifier> > FileToHeapIDMap;
	typedef std::map<SInt32, SerializedObjectIdentifier, std::less<SInt32> > HeapIDToFileMap;
#endif

	typedef FileToHeapIDMap::iterator FileToHeapIDIterator;
	typedef HeapIDToFileMap::iterator HeapIDToFileIterator;

	FileToHeapIDMap	m_FileToHeapID;
	HeapIDToFileMap	m_HeapIDToFile;
	#if UNITY_EDITOR
	vector_set<SInt32> m_LoadingSceneInstanceIDs;
	#endif
	

	
	// Instance ID's are simply allocated in an increasing index
	int 											m_HighestMemoryID;
	
	// When loading scenes we can fast path because objects are not kept persistent / unloaded / loaded again etc.
	// So we just preallocate a bunch of id's and use those without going through a lot of table lookups.
	int                                             m_ActivePreallocatedIDBase;
	int                                             m_ActivePreallocatedIDEnd;
	int                                             m_ActivePreallocatedPathID;

	Remapper () 
// map node contains 3 pointers (left, right, parent)

#if ENABLE_CUSTOM_ALLOCATORS_FOR_STDMAP
: 	m_SerializedObjectIdentifierPool( false, "Remapper pool", sizeof (SerializedObjectIdentifier) + sizeof(SInt32)*2 + sizeof(void*)*3, 16 * 1024)
,	m_FileToHeapID (std::less<SerializedObjectIdentifier> (), m_SerializedObjectIdentifierPool)
,	m_HeapIDToFile (std::less<SInt32>  (), m_SerializedObjectIdentifierPool)
#endif
	{ m_HighestMemoryID = 0; m_ActivePreallocatedIDBase = 0; m_ActivePreallocatedIDEnd = 0; m_ActivePreallocatedPathID = -1;  }
	
	
	void PreallocateIDs (LocalIdentifierInFileType highestFileID, int pathID)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		AssertIf(pathID == -1);
		m_HighestMemoryID += 2;
		m_ActivePreallocatedIDBase = m_HighestMemoryID;
		m_HighestMemoryID  += highestFileID * 2;
		m_ActivePreallocatedIDEnd = m_HighestMemoryID;
		//printf_console("Preallocating %d .. %d\n", m_ActivePreallocatedIDBase, m_ActivePreallocatedIDEnd);
		m_ActivePreallocatedPathID = pathID;
	}

	void ClearPreallocateIDs ()
	{
		AssertIf(m_ActivePreallocatedPathID == -1);
		m_ActivePreallocatedIDBase = 0;
		m_ActivePreallocatedIDEnd = 0;
		m_ActivePreallocatedPathID = -1;
	}

	void Remove (int memoryID)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		
		HeapIDToFileIterator i = m_HeapIDToFile.find (memoryID);
		if (i == m_HeapIDToFile.end ())
			return;

		FileToHeapIDIterator j = m_FileToHeapID.find (i->second);
		AssertIf (j == m_FileToHeapID.end ());
		SerializedObjectIdentifier bug = j->first;
		
		m_HeapIDToFile.erase (i);
		m_FileToHeapID.erase (j);
		AssertIf (m_FileToHeapID.find (bug) != m_FileToHeapID.end ());
	}

	void RemoveCompletePathID (int serializedFileIndex, vector<SInt32>& objects)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);

		SerializedObjectIdentifier proxy;
		proxy.serializedFileIndex = serializedFileIndex;
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::min();
		
		FileToHeapIDIterator begin = m_FileToHeapID.lower_bound (proxy);
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::max();
		FileToHeapIDIterator end = m_FileToHeapID.upper_bound (proxy);
		for (FileToHeapIDIterator i=begin;i != end;i++)
		{
			ErrorIf(i->first.serializedFileIndex != serializedFileIndex);
			m_HeapIDToFile.erase (m_HeapIDToFile.find(i->second));
			objects.push_back(i->second);
		}
		m_FileToHeapID.erase(begin, end);
	}
	
	bool IsSceneID (int memoryID)
	{
#if UNITY_EDITOR
		return m_LoadingSceneInstanceIDs.count(memoryID) != 0;
#endif		
		return IsPreallocatedID (memoryID);
	}
	
	bool IsPreallocatedID (int memoryID)
	{
		return m_ActivePreallocatedPathID != -1 && memoryID >= m_ActivePreallocatedIDBase && memoryID <= m_ActivePreallocatedIDEnd;
	}
	
	bool InstanceIDToSerializedObjectIdentifier (int instanceID, SerializedObjectIdentifier& identifier)
	{
		if (IsPreallocatedID(instanceID))
		{
			identifier.serializedFileIndex = m_ActivePreallocatedPathID;
			identifier.localIdentifierInFile = (instanceID - m_ActivePreallocatedIDBase) / 2;
			return true;
		}
		
		HeapIDToFileIterator i = m_HeapIDToFile.find (instanceID);
		if (i == m_HeapIDToFile.end ())
		{
			identifier.serializedFileIndex = -1;
			identifier.localIdentifierInFile = 0;
			return false;
		}
		identifier = i->second;
		
		#if LOCAL_IDENTIFIER_IN_FILE_SIZE != 32	
		-- fix this, should we use UInt32 for localIdentifierInFile?
			USInt64 debugLocalIdentifier = identifier.localIdentifierInFile;
		AssertIf(debugLocalIdentifier >= (1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE) || debugLocalIdentifier <= -(1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE));
		#endif
		
		return true;
	}

	
	int GetSerializedFileIndex (int memoryID)
	{
		SerializedObjectIdentifier identifier;
		InstanceIDToSerializedObjectIdentifier (memoryID, identifier);
		return identifier.serializedFileIndex;
	}
	
	bool IsSetup (const SerializedObjectIdentifier& identifier)
	{
		return m_FileToHeapID.find (identifier) != m_FileToHeapID.end ();
	}

	bool IsHeapIDSetup (int memoryID)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		return m_HeapIDToFile.count(memoryID);
	}

	int GetOrGenerateMemoryID (const SerializedObjectIdentifier& identifier)
	{
		if (identifier.serializedFileIndex == -1)
			return 0;
		
		if (m_ActivePreallocatedPathID != -1 && m_ActivePreallocatedPathID == identifier.serializedFileIndex)
		{
			return identifier.localIdentifierInFile * 2 + m_ActivePreallocatedIDBase;
		}
		
		#if LOCAL_IDENTIFIER_IN_FILE_SIZE != 32	
		-- fix this, should we use UInt32 for localIdentifierInFile?
		USInt64 debugLocalIdentifier = identifier.localIdentifierInFile;
		AssertIf(debugLocalIdentifier >= (1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE) || debugLocalIdentifier <= -(1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE));
		#endif
		std::pair<FileToHeapIDIterator, bool> inserted = m_FileToHeapID.insert (std::make_pair (identifier, 0));
		if (inserted.second)
		{
			int memoryID = 0;
			
			m_HighestMemoryID += 2;
			memoryID = m_HighestMemoryID;
		
			inserted.first->second = memoryID;

			AssertIf (m_HeapIDToFile.find (memoryID) != m_HeapIDToFile.end ());
			m_HeapIDToFile.insert (std::make_pair (memoryID, identifier));
			
			return memoryID;
		}
		else
			return inserted.first->second;
	}
	
	void SetupRemapping (int memoryID, const SerializedObjectIdentifier& identifier)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		#if LOCAL_IDENTIFIER_IN_FILE_SIZE != 32	
		-- fix this, should we use UInt32 for localIdentifierInFile?
			USInt64 debugLocalIdentifier = identifier.localIdentifierInFile;
		AssertIf(debugLocalIdentifier >= (1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE) || debugLocalIdentifier <= -(1ULL << LOCAL_IDENTIFIER_IN_FILE_SIZE));
		#endif
		
		if (m_HeapIDToFile.find (memoryID) != m_HeapIDToFile.end ())
		{
			m_FileToHeapID.erase(m_HeapIDToFile.find (memoryID)->second);
			m_HeapIDToFile.erase(memoryID);
		}

		if (m_FileToHeapID.find (identifier) != m_FileToHeapID.end ())
		{
			m_HeapIDToFile.erase(m_FileToHeapID.find (identifier)->second);
			m_FileToHeapID.erase(identifier);
		}

		m_HeapIDToFile[memoryID] = identifier;
		m_FileToHeapID[identifier] = memoryID;
		
/*
//		This code asserts more when something goes wrong but also in edge cases that are allowed.
		SerializedObjectIdentifier id;
		id.fileID = fileID;
		id.pathID = pathID;

		HeapIDToFileIterator inserted;
		inserted = m_HeapIDToFile.insert (std::make_pair (memoryID, id)).first;
		AssertIf (inserted->second != id);
		inserted->second = id;
				
		FileToHeapIDIterator inserted2;
		#if DEBUGMODE
		inserted2 = m_FileToHeapID.find (id);
		AssertIf (inserted2 != m_FileToHeapID.end () && inserted2->second != memoryID);
		#endif
		
		inserted2 = m_FileToHeapID.insert (std::make_pair (id, memoryID)).first;
		AssertIf (inserted2->second != memoryID);
		inserted2->second = memoryID;
*/
	}
	
	void GetAllLoadedObjectsAtPath (int pathID, set<SInt32>* objects)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		AssertIf (objects == NULL);

		SerializedObjectIdentifier proxy;
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::min ();
		proxy.serializedFileIndex = pathID;
		FileToHeapIDIterator begin = m_FileToHeapID.lower_bound (proxy);
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::max ();
		FileToHeapIDIterator end = m_FileToHeapID.upper_bound (proxy);
		
		for (FileToHeapIDIterator i=begin;i != end;++i)
		{
			int instanceID = i->second;
			Object* o = Object::IDToPointer (instanceID);
			if (o)
				objects->insert (instanceID);
		}
	}

	void GetAllPersistentObjectsAtPath (int pathID, set<SInt32>* objects)
	{
		AssertIf(m_ActivePreallocatedPathID != -1);
		AssertIf (objects == NULL);
		
		SerializedObjectIdentifier proxy;
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::min ();
		proxy.serializedFileIndex = pathID;
		FileToHeapIDIterator begin = m_FileToHeapID.lower_bound (proxy);
		proxy.localIdentifierInFile = std::numeric_limits<LocalIdentifierInFileType>::max ();
		FileToHeapIDIterator end = m_FileToHeapID.upper_bound (proxy);
		
		for (FileToHeapIDIterator i=begin;i != end;++i)
		{
			int instanceID = i->second;
			objects->insert(instanceID);
		}
	}

	int GetHighestInUseHeapID ()
	{
		if (!m_HeapIDToFile.empty())
			return m_HeapIDToFile.rbegin ()->first;
		else
			return 0;	
	}
};

#endif