// Copyright 2017, VIXL authors1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright notice,7// this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright notice,9// this list of conditions and the following disclaimer in the documentation10// and/or other materials provided with the distribution.11// * Neither the name of ARM Limited nor the names of its contributors may be12// used to endorse or promote products derived from this software without13// specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE19// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR21// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER22// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,23// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2526#include "code-buffer-vixl.h"27#include "utils-vixl.h"2829namespace vixl {3031CodeBuffer::CodeBuffer() : buffer_(nullptr), cursor_(nullptr), dirty_(false), capacity_(0)32{33}3435CodeBuffer::CodeBuffer(byte* buffer, size_t capacity)36: buffer_(reinterpret_cast<byte*>(buffer)),37cursor_(reinterpret_cast<byte*>(buffer)),38dirty_(false),39capacity_(capacity) {40VIXL_ASSERT(buffer_ != NULL);41}424344CodeBuffer::~CodeBuffer() VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION {45VIXL_ASSERT(!IsDirty());46}474849void CodeBuffer::EmitString(const char* string) {50const auto len = strlen(string) + 1;51VIXL_ASSERT(HasSpaceFor(len));52char* dst = reinterpret_cast<char*>(cursor_);53dirty_ = true;54memcpy(dst, string, len);55cursor_ = reinterpret_cast<byte*>(dst + len);56}575859void CodeBuffer::EmitData(const void* data, size_t size) {60VIXL_ASSERT(HasSpaceFor(size));61dirty_ = true;62memcpy(cursor_, data, size);63cursor_ = cursor_ + size;64}656667void CodeBuffer::UpdateData(size_t offset, const void* data, size_t size) {68dirty_ = true;69byte* dst = buffer_ + offset;70VIXL_ASSERT(dst + size <= cursor_);71memcpy(dst, data, size);72}737475void CodeBuffer::Align() {76byte* end = AlignUp(cursor_, 4);77const size_t padding_size = end - cursor_;78VIXL_ASSERT(padding_size <= 4);79EmitZeroedBytes(static_cast<int>(padding_size));80}8182void CodeBuffer::EmitZeroedBytes(int n) {83VIXL_ASSERT(HasSpaceFor(n));84dirty_ = true;85memset(cursor_, 0, n);86cursor_ += n;87}8889void CodeBuffer::Reset() {90cursor_ = buffer_;91SetClean();92}9394void CodeBuffer::Reset(byte* buffer, size_t capacity) {95buffer_ = buffer;96cursor_ = buffer;97capacity_ = capacity;98SetClean();99}100101} // namespace vixl102103104