@@ -38,31 +38,29 @@ void EHScopeStack::Cleanup::anchor() {}
3838char *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}
0 commit comments