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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [common/] [autobuffer.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/*
2
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 */
16
 
17
#include <autobuffer.h>
18
#include <api_core.h>
19
#include <cstdio>
20
#include <cstring>  // memcpy definition
21
 
22
namespace debugger {
23
 
24
AutoBuffer::AutoBuffer() {
25
    buf_ = NULL;
26
    buf_len_ = 0;
27
    buf_size_ = 0;
28
}
29
 
30
AutoBuffer::~AutoBuffer() {
31
    if (buf_) {
32
        delete [] buf_;
33
    }
34
}
35
 
36
void AutoBuffer::write_bin(const char *p, int sz) {
37
    if (buf_len_ + sz >= buf_size_) {
38
        if (buf_size_ == 0) {
39
            buf_size_ = 1024;
40
            buf_ = new char[buf_size_];
41
        } else {
42
            while (buf_len_ + sz >= buf_size_) {
43
                buf_size_ <<= 1;
44
            }
45
            char *t1 = new char[buf_size_];
46
            memcpy(t1, buf_, buf_len_);
47
            delete [] buf_;
48
            buf_ = t1;
49
        }
50
    }
51
    memcpy(&buf_[buf_len_], p, sz);
52
    buf_len_ += sz;
53
    buf_[buf_len_] = '\0';
54
}
55
 
56
void AutoBuffer::write_string(const char s) {
57
    write_bin(&s, 1);
58
}
59
 
60
void AutoBuffer::write_string(const char *s) {
61
    write_bin(s, static_cast<int>(strlen(s)));
62
}
63
 
64
void AutoBuffer::write_uint64(uint64_t v) {
65
    char tmp[128];
66
    int sz = RISCV_sprintf(tmp, sizeof(tmp), "0x%" RV_PRI64 "x", v);
67
    write_bin(tmp, sz);
68
}
69
 
70
void AutoBuffer::write_byte(uint8_t v) {
71
    char tmp[8];
72
    int sz = RISCV_sprintf(tmp, sizeof(tmp), "0x%02X", v);
73
    write_bin(tmp, sz);
74
}
75
 
76
}  // namespace debugger

powered by: WebSVN 2.1.0

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