1 | /* |
2 | * Copyright (C) 2014 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #ifndef Interpreter_h |
27 | #define Interpreter_h |
28 | |
29 | #include <condition_variable> |
30 | #include <mutex> |
31 | #include <thread> |
32 | #include <vector> |
33 | |
34 | class Interpreter { |
35 | public: |
36 | Interpreter(const char* fileName, bool shouldFreeAllObjects = true, bool useThreadId = false); |
37 | ~Interpreter(); |
38 | |
39 | void run(); |
40 | void detailedReport(); |
41 | |
42 | private: |
43 | typedef unsigned short ThreadId; // 0 is the main thread |
44 | typedef unsigned short Log2Alignment; // log2(alignment) or ~0 for non power of 2. |
45 | enum Opcode { op_malloc, op_free, op_realloc, op_align_malloc }; |
46 | struct Op { Opcode opcode; ThreadId threadId; Log2Alignment alignLog2; size_t slot; size_t size; }; |
47 | struct Record { void* object; size_t size; }; |
48 | |
49 | class Thread |
50 | { |
51 | public: |
52 | Thread(Interpreter*, ThreadId); |
53 | ~Thread(); |
54 | |
55 | void runThread(); |
56 | |
57 | void waitToRun(); |
58 | void switchTo(); |
59 | void stop(); |
60 | |
61 | bool isMainThread() { return m_threadId == 0; } |
62 | |
63 | private: |
64 | ThreadId m_threadId; |
65 | Interpreter* m_myInterpreter; |
66 | std::condition_variable m_shouldRun; |
67 | std::thread m_thread; |
68 | }; |
69 | |
70 | bool readOps(); |
71 | void doOnSameThread(ThreadId); |
72 | void switchToThread(ThreadId); |
73 | |
74 | void doMallocOp(Op, ThreadId); |
75 | |
76 | bool m_shouldFreeAllObjects; |
77 | bool m_useThreadId; |
78 | int m_fd; |
79 | size_t m_opCount; |
80 | size_t m_remaining; |
81 | size_t m_opsCursor; |
82 | size_t m_opsInBuffer; |
83 | ThreadId m_currentThreadId; |
84 | std::vector<Op> m_ops; |
85 | std::mutex m_threadMutex; |
86 | std::condition_variable m_shouldRun; |
87 | std::vector<Thread*> m_threads; |
88 | std::vector<Record> m_objects; |
89 | }; |
90 | |
91 | #endif // Interpreter_h |
92 | |