Handle-ize BytecodeEmitter.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #include "BytecodeEmitter.h"
25 BytecodeEmitter::BytecodeEmitter()
26 : buffer_(inline_buffer_),
28 buffer_end_(buffer_pos_ + ARRAY_LENGTH(inline_buffer_)),
36 BytecodeEmitter::growBuffer(size_t nbytes)
39 buffer_pos_ = buffer_;
43 size_t newSize = size_t((buffer_end_ - buffer_) * 2);
44 if (newSize >= MAX_LENGTH) {
46 buffer_pos_ = buffer_;
51 if (buffer_ == inline_buffer_)
52 newBuffer = (bytecode *)malloc(nbytes);
54 newBuffer = (bytecode *)realloc(buffer_, newSize);
56 // If we can't allocate a buffer, reset the buffer to the start and set
57 // a flag to indicate out-of-memory.
60 buffer_pos_ = buffer_;
64 buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
66 buffer_end_ = buffer_ + newSize;