2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "Interpreter.h"
36 #include <sys/types.h>
43 Interpreter::Interpreter(const char* fileName)
45 m_fd = open(fileName, O_RDWR, S_IRUSR | S_IWUSR);
47 fprintf(stderr, "failed to open\n");
52 m_opCount = buf.st_size / sizeof(Op);
53 assert(m_opCount * sizeof(Op) == buf.st_size);
57 std::vector<Op> ops(1024);
58 size_t remaining = m_opCount * sizeof(Op);
60 size_t bytes = std::min(remaining, ops.size() * sizeof(Op));
62 read(m_fd, ops.data(), bytes);
64 size_t opCount = bytes / sizeof(Op);
65 for (size_t i = 0; i < opCount; ++i) {
67 if (op.slot > maxSlot)
72 m_objects.resize(maxSlot + 1);
75 Interpreter::~Interpreter()
77 int result = close(m_fd);
79 fprintf(stderr, "failed to close\n");
82 void Interpreter::run()
84 std::vector<Op> ops(1024);
85 lseek(m_fd, 0, SEEK_SET);
86 size_t remaining = m_opCount * sizeof(Op);
88 size_t bytes = std::min(remaining, ops.size() * sizeof(Op));
90 read(m_fd, ops.data(), bytes);
92 size_t opCount = bytes / sizeof(Op);
93 for (size_t i = 0; i < opCount; ++i) {
97 m_objects[op.slot] = { mbmalloc(op.size), op.size };
98 assert(m_objects[op.slot].object);
99 bzero(m_objects[op.slot].object, op.size);
103 assert(m_objects[op.slot].object);
104 assert(m_objects[op.slot].size);
105 mbfree(m_objects[op.slot].object, m_objects[op.slot].size);
106 m_objects[op.slot] = { 0, 0 };
110 fprintf(stderr, "bad opcode: %d\n", op.opcode);
118 // A recording might not free all of its allocations.
119 for (size_t i = 0; i < m_objects.size(); ++i) {
120 if (!m_objects[i].object)
122 mbfree(m_objects[i].object, m_objects[i].size);
123 m_objects[i] = { 0, 0 };