Coverage Report

Created: 2026-03-15 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/butil/arena.cpp
Line
Count
Source
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
// Date: Fri Jun  5 18:25:40 CST 2015
19
20
#include <stdlib.h>
21
#include <algorithm>
22
#include "butil/arena.h"
23
24
namespace butil {
25
26
ArenaOptions::ArenaOptions()
27
2
    : initial_block_size(64)
28
2
    , max_block_size(8192)
29
2
{}
30
31
Arena::Arena(const ArenaOptions& options)
32
2
    : _cur_block(NULL)
33
2
    , _isolated_blocks(NULL)
34
2
    , _block_size(options.initial_block_size)
35
2
    , _options(options) {
36
2
}
37
38
0
Arena::~Arena() {
39
0
    while (_cur_block != NULL) {
40
0
        Block* const saved_next = _cur_block->next;
41
0
        free(_cur_block);
42
0
        _cur_block = saved_next;
43
0
    }
44
0
    while (_isolated_blocks != NULL) {
45
0
        Block* const saved_next = _isolated_blocks->next;
46
0
        free(_isolated_blocks);
47
0
        _isolated_blocks = saved_next;
48
0
    }
49
0
}
50
51
0
void Arena::swap(Arena& other) {
52
0
    std::swap(_cur_block, other._cur_block);
53
0
    std::swap(_isolated_blocks, other._isolated_blocks);
54
0
    std::swap(_block_size, other._block_size);
55
0
    const ArenaOptions tmp = _options;
56
0
    _options = other._options;
57
0
    other._options = tmp;
58
0
}
59
60
0
void Arena::clear() {
61
    // TODO(gejun): Reuse memory
62
0
    Arena a;
63
0
    swap(a);
64
0
}
65
66
0
void* Arena::allocate_new_block(size_t n) {
67
0
    Block* b = (Block*)malloc(offsetof(Block, data) + n);
68
0
    b->next = _isolated_blocks;
69
0
    b->alloc_size = n;
70
0
    b->size = n;
71
0
    _isolated_blocks = b;
72
0
    return b->data;
73
0
}
74
75
0
void* Arena::allocate_in_other_blocks(size_t n) {
76
0
    if (n > _block_size / 4) { // put outlier on separate blocks.
77
0
        return allocate_new_block(n);
78
0
    }
79
    // Waste the left space. At most 1/4 of allocated spaces are wasted.
80
81
    // Grow the block size gradually.
82
0
    if (_cur_block != NULL) {
83
0
        _block_size = std::min(2 * _block_size, _options.max_block_size);
84
0
    }
85
0
    size_t new_size = _block_size;
86
0
    if (new_size < n) {
87
0
        new_size = n;
88
0
    }
89
0
    Block* b = (Block*)malloc(offsetof(Block, data) + new_size);
90
0
    if (NULL == b) {
91
0
        return NULL;
92
0
    }
93
0
    b->next = NULL;
94
0
    b->alloc_size = n;
95
0
    b->size = new_size;
96
0
    if (_cur_block) {
97
0
        _cur_block->next = _isolated_blocks;
98
0
        _isolated_blocks = _cur_block;
99
0
    }
100
0
    _cur_block = b;
101
0
    return b->data;
102
0
}
103
104
}  // namespace butil