Skip to content

Commit cf0409d

Browse files
committed
Address review feedback
1 parent 135934e commit cf0409d

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

clang/lib/CIR/CodeGen/CIRGenCleanup.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,29 @@ void EHScopeStack::Cleanup::anchor() {}
3838
char *EHScopeStack::allocate(size_t size) {
3939
size = llvm::alignTo(size, ScopeStackAlignment);
4040
if (!startOfBuffer) {
41-
unsigned capacity = 1024;
42-
while (capacity < size)
43-
capacity *= 2;
44-
startOfBuffer = new char[capacity];
45-
startOfData = endOfBuffer = startOfBuffer + capacity;
46-
} else if (static_cast<size_t>(startOfData - startOfBuffer) < size) {
47-
unsigned currentCapacity = endOfBuffer - startOfBuffer;
48-
unsigned usedCapacity = currentCapacity - (startOfData - startOfBuffer);
49-
50-
unsigned newCapacity = currentCapacity;
51-
do {
52-
newCapacity *= 2;
53-
} while (newCapacity < usedCapacity + size);
54-
55-
char *newStartOfBuffer = new char[newCapacity];
56-
char *newEndOfBuffer = newStartOfBuffer + newCapacity;
41+
unsigned capacity = llvm::PowerOf2Ceil(std::max(size, 1024ul));
42+
startOfBuffer = std::make_unique<char[]>(capacity);
43+
startOfData = endOfBuffer = startOfBuffer.get() + capacity;
44+
} else if (static_cast<size_t>(startOfData - startOfBuffer.get()) < size) {
45+
unsigned currentCapacity = endOfBuffer - startOfBuffer.get();
46+
unsigned usedCapacity =
47+
currentCapacity - (startOfData - startOfBuffer.get());
48+
unsigned requiredCapacity = usedCapacity + size;
49+
// We know from the 'else if' condition that requiredCapacity is greater
50+
// than currentCapacity.
51+
unsigned newCapacity = llvm::PowerOf2Ceil(requiredCapacity);
52+
53+
std::unique_ptr<char[]> newStartOfBuffer =
54+
std::make_unique<char[]>(newCapacity);
55+
char *newEndOfBuffer = newStartOfBuffer.get() + newCapacity;
5756
char *newStartOfData = newEndOfBuffer - usedCapacity;
5857
memcpy(newStartOfData, startOfData, usedCapacity);
59-
delete[] startOfBuffer;
60-
startOfBuffer = newStartOfBuffer;
58+
startOfBuffer.swap(newStartOfBuffer);
6159
endOfBuffer = newEndOfBuffer;
6260
startOfData = newStartOfData;
6361
}
6462

65-
assert(startOfBuffer + size <= startOfData);
63+
assert(startOfBuffer.get() + size <= startOfData);
6664
startOfData -= size;
6765
return startOfData;
6866
}

clang/lib/CIR/CodeGen/CIRGenCleanup.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#ifndef CLANG_LIB_CIR_CODEGEN_CGCLEANUP_H
15-
#define CLANG_LIB_CIR_CODEGEN_CGCLEANUP_H
14+
#ifndef CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
15+
#define CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
1616

1717
#include "EHScopeStack.h"
1818

1919
namespace clang::CIRGen {
2020

2121
/// A non-stable pointer into the scope stack.
2222
class EHScopeStack::iterator {
23-
char *ptr;
23+
char *ptr = nullptr;
2424

2525
friend class EHScopeStack;
2626
explicit iterator(char *ptr) : ptr(ptr) {}
2727

2828
public:
29-
iterator() : ptr(nullptr) {}
29+
iterator() = default;
3030

3131
EHScopeStack::Cleanup *get() const {
3232
return reinterpret_cast<EHScopeStack::Cleanup *>(ptr);
@@ -40,4 +40,4 @@ inline EHScopeStack::iterator EHScopeStack::begin() const {
4040
}
4141

4242
} // namespace clang::CIRGen
43-
#endif // CLANG_LIB_CIR_CODEGEN_CGCLEANUP_H
43+
#endif // CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H

clang/lib/CIR/CodeGen/EHScopeStack.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class EHScopeStack {
5454
friend class EHScopeStack;
5555

5656
/// Offset from startOfData to endOfBuffer.
57-
ptrdiff_t size;
57+
ptrdiff_t size = -1;
5858

59-
stable_iterator(ptrdiff_t size) : size(size) {}
59+
explicit stable_iterator(ptrdiff_t size) : size(size) {}
6060

6161
public:
6262
static stable_iterator invalid() { return stable_iterator(-1); }
63-
stable_iterator() : size(-1) {}
63+
stable_iterator() = default;
6464

6565
bool isValid() const { return size >= 0; }
6666

@@ -121,7 +121,7 @@ class EHScopeStack {
121121
/// The start of the scope-stack buffer, i.e. the allocated pointer
122122
/// for the buffer. All of these pointers are either simultaneously
123123
/// null or simultaneously valid.
124-
char *startOfBuffer = nullptr;
124+
std::unique_ptr<char[]> startOfBuffer;
125125

126126
/// The end of the buffer.
127127
char *endOfBuffer = nullptr;
@@ -143,7 +143,7 @@ class EHScopeStack {
143143

144144
public:
145145
EHScopeStack() = default;
146-
~EHScopeStack() { delete[] startOfBuffer; }
146+
~EHScopeStack() = default;
147147

148148
/// Push a lazily-created cleanup on the stack.
149149
template <class T, class... As> void pushCleanup(CleanupKind kind, As... a) {

0 commit comments

Comments
 (0)