-
Notifications
You must be signed in to change notification settings - Fork 54
/
ValueIndex.h
80 lines (60 loc) · 2.77 KB
/
ValueIndex.h
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_recordreplay_ValueIndex_h
#define mozilla_recordreplay_ValueIndex_h
#include "mozilla/Types.h"
#include <unordered_map>
namespace mozilla {
namespace recordreplay {
// ValueIndexes are a bidirectional map between arbitrary pointers and indexes.
// These are used while recording and replaying to handle the general issue
// that pointer values are not preserved during replay: recording a pointer and
// replaying its bits later will not yield a pointer to the same heap value,
// but rather a pointer to garbage that must not be dereferenced.
//
// When entries are added to a ValueIndex at consistent points between
// recording and replaying, then the resulting indexes will be consistent, and
// that index can be recorded and later replayed and used to find the
// replay-specific pointer value corresponding to the pointer used at that
// point in the recording. Entries can be removed from the ValueIndex at
// different points in the recording and replay without affecting the indexes
// that will be generated later.
//
// This is a helper class that is used in various places to help record/replay
// pointers to heap data.
class ValueIndex {
public:
ValueIndex() : mIndexCount(0) {}
typedef std::unordered_map<const void*, size_t> ValueToIndexMap;
// Add a new entry to the map.
size_t Insert(const void* aValue);
// Remove an entry from the map, unless there is no entry for aValue.
void Remove(const void* aValue);
// Get the index for an entry in the map. The entry must exist in the map.
size_t GetIndex(const void* aValue);
// Get the index for an entry in the map if there is one, otherwise return
// false.
bool MaybeGetIndex(const void* aValue, size_t* aIndex);
// Return whether there is an entry for aValue.
bool Contains(const void* aValue);
// Get the value associated with an index. The index must exist in the map.
const void* GetValue(size_t aIndex);
// Whether the map is empty.
bool IsEmpty();
// Raw read-only access to the map contents.
const ValueToIndexMap& GetValueToIndexMap();
private:
typedef std::unordered_map<size_t, const void*> IndexToValueMap;
// Map from pointer values to indexes.
ValueToIndexMap mValueToIndex;
// Map from indexes to pointer values.
IndexToValueMap mIndexToValue;
// The total number of entries that have ever been added to this map.
size_t mIndexCount;
};
} // namespace recordreplay
} // namespace mozilla
#endif // mozilla_recordreplay_ValueIndex_h