Coverage Report

Created: 2024-11-18 10:37

/root/doris/be/src/util/byte_buffer.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <string.h>
21
22
#include <cstddef>
23
#include <memory>
24
25
#include "common/logging.h"
26
27
namespace doris {
28
29
struct ByteBuffer;
30
using ByteBufferPtr = std::shared_ptr<ByteBuffer>;
31
32
struct ByteBuffer {
33
9
    static ByteBufferPtr allocate(size_t size) {
34
9
        ByteBufferPtr ptr(new ByteBuffer(size));
35
9
        return ptr;
36
9
    }
37
38
9
    ~ByteBuffer() { delete[] ptr; }
39
40
9
    void put_bytes(const char* data, size_t size) {
41
9
        memcpy(ptr + pos, data, size);
42
9
        pos += size;
43
9
    }
44
45
0
    void get_bytes(char* data, size_t size) {
46
0
        memcpy(data, ptr + pos, size);
47
0
        pos += size;
48
0
        DCHECK(pos <= limit);
49
0
    }
50
51
9
    void flip() {
52
9
        limit = pos;
53
9
        pos = 0;
54
9
    }
55
56
15
    size_t remaining() const { return limit - pos; }
57
0
    bool has_remaining() const { return limit > pos; }
58
59
    char* const ptr;
60
    size_t pos;
61
    size_t limit;
62
    size_t capacity;
63
64
private:
65
    ByteBuffer(size_t capacity_)
66
9
            : ptr(new char[capacity_]), pos(0), limit(capacity_), capacity(capacity_) {}
67
};
68
69
} // namespace doris