Files
swift-mirror/include/swift/Syntax/SyntaxArena.h
Alex Hoppen 66374a14ea [libSyntax] Make RawSyntax nodes hold a strong reference to their arena
This allows an elegant design in which we can still allocate RawSyntax
nodes using a bump allocator but are able to automatically free that
buffer once the last RawSyntax node within that buffer is freed.

This also resolves a memory leak of RawSyntax nodes that was caused by
ParserUnit not freeing its underlying ASTContext.
2018-08-24 08:39:54 -07:00

46 lines
1.3 KiB
C++

//===--- SyntaxArena.h - Syntax Tree Memory Allocation ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines SyntaxArena that is Memory manager for Syntax nodes.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SYNTAX_SYNTAXARENA_H
#define SWIFT_SYNTAX_SYNTAXARENA_H
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Support/Allocator.h"
namespace swift {
namespace syntax {
/// Memory manager for Syntax nodes.
class SyntaxArena : public llvm::ThreadSafeRefCountedBase<SyntaxArena> {
SyntaxArena(const SyntaxArena &) = delete;
void operator=(const SyntaxArena &) = delete;
llvm::BumpPtrAllocator Allocator;
public:
SyntaxArena() {}
llvm::BumpPtrAllocator &getAllocator() { return Allocator; }
void *Allocate(size_t size, size_t alignment) {
return Allocator.Allocate(size, alignment);
}
};
} // namespace syntax
} // namespace swift
#endif