OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [common/] [autobuffer.cpp] - Rev 4

Compare with Previous | Blame | View Log

/*
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
 
#include <autobuffer.h>
#include <api_core.h>
#include <cstdio>
#include <cstring>  // memcpy definition
 
namespace debugger {
 
AutoBuffer::AutoBuffer() {
    buf_ = NULL;
    buf_len_ = 0;
    buf_size_ = 0;
}
 
AutoBuffer::~AutoBuffer() {
    if (buf_) {
        delete [] buf_;
    }
}
 
void AutoBuffer::write_bin(const char *p, int sz) {
    if (buf_len_ + sz >= buf_size_) {
        if (buf_size_ == 0) {
            buf_size_ = 1024;
            buf_ = new char[buf_size_];
        } else {
            while (buf_len_ + sz >= buf_size_) {
                buf_size_ <<= 1;
            }
            char *t1 = new char[buf_size_];
            memcpy(t1, buf_, buf_len_);
            delete [] buf_;
            buf_ = t1;
        }
    }
    memcpy(&buf_[buf_len_], p, sz);
    buf_len_ += sz;
    buf_[buf_len_] = '\0';
}
 
void AutoBuffer::write_string(const char s) {
    write_bin(&s, 1);
}
 
void AutoBuffer::write_string(const char *s) {
    write_bin(s, static_cast<int>(strlen(s)));
}
 
void AutoBuffer::write_uint64(uint64_t v) {
    char tmp[128];
    int sz = RISCV_sprintf(tmp, sizeof(tmp), "0x%" RV_PRI64 "x", v);
    write_bin(tmp, sz);
}
 
void AutoBuffer::write_byte(uint8_t v) {
    char tmp[8];
    int sz = RISCV_sprintf(tmp, sizeof(tmp), "0x%02X", v);
    write_bin(tmp, sz);
}
 
}  // namespace debugger
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.