Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions src/parser/cxx/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,14 @@

#include <cxx/cxx_fwd.h>

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <memory_resource>
#include <new>
#include <vector>

namespace cxx {

class Arena {
struct Block {
static const std::size_t SIZE = 16 * 1024;

char* data;
char* ptr;

Block(const Block&) = delete;
auto operator=(const Block&) -> Block& = delete;

Block() { ptr = data = static_cast<char*>(std::malloc(SIZE)); }

~Block() {
if (data) std::free(data);
}
};

std::vector<Block*> blocks;

class Arena : public std::pmr::monotonic_buffer_resource {
public:
Arena(const Arena& other) = delete;
auto operator=(const Arena& other) -> Arena& = delete;
Arena() = default;
~Arena() { reset(); }

void reset() {
for (auto&& block : blocks) delete block;
blocks.clear();
}

auto allocate(std::size_t size) noexcept -> void* {
if (!blocks.empty()) {
constexpr auto align = alignof(std::max_align_t) - 1;

auto block = blocks.back();
block->ptr = (char*)((std::intptr_t(block->ptr) + align) & ~align);
void* addr = block->ptr;
block->ptr += size;
if (block->ptr < block->data + Block::SIZE) return addr;
}
blocks.push_back(::new Block());
return allocate(size);
}
using monotonic_buffer_resource::monotonic_buffer_resource;
};

struct Managed {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/cxx/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2966,7 +2966,7 @@ void Preprocessor::setOnWillIncludeHeader(
d->willIncludeHeader_ = std::move(willIncludeHeader);
}

void Preprocessor::squeeze() { d->pool_.reset(); }
void Preprocessor::squeeze() { d->pool_.release(); }

auto Preprocessor::sourceFileName(uint32_t sourceFileId) const
-> const std::string & {
Expand Down