Coverage Report

Created: 2026-11-21 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/pagespeed/kernel/base/arena.h
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 * 
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 * 
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
#ifndef PAGESPEED_KERNEL_BASE_ARENA_H_
21
#define PAGESPEED_KERNEL_BASE_ARENA_H_
22
23
#include <cstddef>
24
#include <vector>
25
26
#include "base/logging.h"
27
#include "pagespeed/kernel/base/basictypes.h"
28
29
namespace net_instaweb {
30
31
// This template keeps a packed set of objects inheriting from the same base
32
// type (which must have a virtual destructor) where all of the
33
// objects in the same arena are expected to be destroyed at once.
34
template <typename T>
35
class Arena {
36
 public:
37
  // All allocations we make will be aligned to this. We will also reserve
38
  // this much room for our work area, as it keeps things simple.
39
  static const size_t kAlign = 8;
40
41
50.9k
  Arena() { InitEmpty(); }
42
43
50.9k
  ~Arena() { CHECK(chunks_.empty()); }
44
45
20.3M
  void* Allocate(size_t size) {
46
20.3M
    size += kAlign;  // Need room to link the next object.
47
20.3M
    size = ExpandToAlign(size);
48
49
20.3M
    DCHECK(sizeof(void*) <= kAlign);
50
20.3M
    DCHECK(size < Chunk::kSize);
51
52
20.3M
    if (chunk_end_ == nullptr || next_alloc_ + size > chunk_end_) {
53
123k
      AddChunk();
54
123k
    }
55
56
20.3M
    char* base = next_alloc_;
57
58
    // Update the links -- the previous object should point to our
59
    // chunk's base, our base should point to NULL, and last_link_
60
    // should point to our base
61
20.3M
    char** our_last_link_field = reinterpret_cast<char**>(base);
62
20.3M
    *last_link_ = base;
63
20.3M
    *our_last_link_field = nullptr;
64
20.3M
    last_link_ = our_last_link_field;
65
66
20.3M
    next_alloc_ += size;
67
68
20.3M
    char* out = base + kAlign;
69
    // Warning: the following line is very type-sensitive and can't
70
    // easily be turned into a DCHECK_EQ as lint would like.
71
20.3M
    DCHECK((reinterpret_cast<uintptr_t>(out) & (kAlign - 1)) == 0);
72
20.3M
    return out;
73
20.3M
  }
74
75
  // Cleans up all the objects in the arena. You must call this explicitly.
76
  void DestroyObjects();
77
78
  // Rounds block size up to 8; we always align to it, even on 32-bit.
79
20.3M
  static size_t ExpandToAlign(size_t in) {
80
20.3M
    return (in + kAlign - 1) & ~(kAlign - 1);
81
20.3M
  }
82
83
 private:
84
  struct Chunk {
85
    // Representation: the arena is a vector of fixed-size (kSize, 8k) chunks;
86
    // we allocate objects from the end of the most recently created chunk until
87
    // an allocation doesn't fit (in which case we make a fresh chunk)
88
    //
89
    // Each chunk is independently organized into a singly-linked list, where
90
    // we precede each object with a pointer to the next allocated block, e.g.:
91
    //
92
    //                  /-----------------|
93
    //                  |                \|/
94
    // -----------------|---------------|----|-----------|
95
    // |   | object 1 | | | object 2    | NU | object 3  |
96
    // | | |          |   |             | LL |           |
97
    // |_|_|----------|---|-------------|----|-----------|
98
    //   |              ^
99
    //   \--------------/
100
    // We need this because objects may have different sizes, and we'll need
101
    // to find everyone to call their destructor
102
    static const size_t kSize = 8192;
103
    char buf[kSize];
104
  };
105
106
  // Adds in a new chunk and initializes all the fields below to refer to it
107
  void AddChunk();
108
109
  // Sets up all the pointers below to denote us being empty.
110
  void InitEmpty();
111
112
  // First free byte of the current chunk
113
  char* next_alloc_;
114
115
  // The point where to link in a new object we allocate.
116
  // We need this so that the last object in a chunk has
117
  // a null 'next' link.
118
  char** last_link_;
119
120
  // First address after the last byte of the currently active chunk
121
  char* chunk_end_;
122
123
  // Scratch location in case we want a write to go nowhere.
124
  // Does not point anywhere, just bitbuckets writes
125
  char* scratch_;
126
127
  std::vector<Chunk*> chunks_;
128
};
129
130
template <typename T>
131
123k
void Arena<T>::AddChunk() {
132
123k
  Chunk* chunk = new Chunk();
133
123k
  chunks_.push_back(chunk);
134
123k
  next_alloc_ = chunk->buf;
135
123k
  chunk_end_ = next_alloc_ + Chunk::kSize;
136
123k
  last_link_ = &scratch_;
137
123k
}
138
139
template <typename T>
140
101k
void Arena<T>::DestroyObjects() {
141
225k
  for (int i = 0; i < static_cast<int>(chunks_.size()); ++i) {
142
    // Walk through objects in this chunk.
143
123k
    char* base = chunks_[i]->buf;
144
20.5M
    while (base != nullptr) {
145
20.3M
      reinterpret_cast<T*>(base + kAlign)->~T();
146
20.3M
      base = *reinterpret_cast<char**>(base);
147
20.3M
    }
148
123k
    delete chunks_[i];
149
123k
  }
150
101k
  chunks_.clear();
151
101k
  InitEmpty();
152
101k
}
153
154
template <typename T>
155
152k
void Arena<T>::InitEmpty() {
156
  // The way this is initialized ensures that the next call to allocate
157
  // will call AddChunk(). Doing it this way rather than calling NewChunk()
158
  // from here establishes the invariant that all the chunks are non-empty,
159
  // which helps in DestroyObjects()
160
152k
  next_alloc_ = nullptr;
161
152k
  last_link_ = nullptr;
162
152k
  chunk_end_ = nullptr;
163
152k
}
164
165
}  // namespace  net_instaweb
166
167
#endif  // PAGESPEED_KERNEL_BASE_ARENA_H_