1//
2// Copyright (c) Microsoft. All rights reserved.
3// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4//
5
6#include "simpletimer.h"
7
8// Class to implement method context hive reading and iterating.
9
10class MethodContextIterator
11{
12public:
13 MethodContextIterator(bool progressReport = false)
14 : m_hFile(INVALID_HANDLE_VALUE)
15 , m_fileSize(0)
16 , m_methodContextNumber(0)
17 , m_mc(nullptr)
18 , m_indexCount(-1)
19 , m_index(0)
20 , m_indexes(nullptr)
21 , m_progressReport(progressReport)
22 , m_timer(nullptr)
23 {
24 if (m_progressReport)
25 {
26 m_timer = new SimpleTimer();
27 }
28 }
29
30 MethodContextIterator(const int indexCount, const int* indexes, bool progressReport = false)
31 : m_hFile(INVALID_HANDLE_VALUE)
32 , m_fileSize(0)
33 , m_methodContextNumber(0)
34 , m_mc(nullptr)
35 , m_indexCount(indexCount)
36 , m_index(0)
37 , m_indexes(indexes)
38 , m_progressReport(progressReport)
39 , m_timer(nullptr)
40 {
41 if (m_progressReport)
42 {
43 m_timer = new SimpleTimer();
44 }
45 }
46
47 ~MethodContextIterator()
48 {
49 Destroy();
50 }
51
52 bool Initialize(const char* fileName);
53
54 bool Destroy();
55
56 bool MoveNext();
57
58 // The iterator class owns the memory returned by Current(); the caller should not delete it.
59 MethodContext* Current()
60 {
61 return m_mc;
62 }
63
64 // In this case, we are giving ownership of the MethodContext* to the caller. So, null out m_mc
65 // before we return, so we don't attempt to delete it in this class.
66 MethodContext* CurrentTakeOwnership()
67 {
68 MethodContext* ret = m_mc;
69 m_mc = nullptr;
70 return ret;
71 }
72
73 // Return the file position offset of the current method context.
74 __int64 CurrentPos()
75 {
76 return m_pos.QuadPart;
77 }
78
79 int MethodContextNumber()
80 {
81 return m_methodContextNumber;
82 }
83
84private:
85 HANDLE m_hFile;
86 int64_t m_fileSize;
87 int m_methodContextNumber;
88 MethodContext* m_mc;
89 LARGE_INTEGER m_pos;
90
91 // If m_indexCount==-1, use all method contexts. Otherwise, m_indexCount is the number of elements in the
92 // m_indexes array, which contains a sorted set of method context indexes to return. In this case, m_index
93 // is the index of the current element in m_indexes.
94 const int m_indexCount;
95 int m_index;
96 const int* m_indexes;
97
98 // Should we log a progress report as we are loading the method contexts?
99 // The timer is only used when m_progressReport==true.
100 bool m_progressReport;
101 SimpleTimer* m_timer;
102};