TextBuffer::enlargeBuffer(int n) Enlarge the buffer so at least at N bytes are free in the buffer. Always enlarges by a power of two. Returns -1 if insufficient memory, zero otherwise
| 122 | // Returns -1 if insufficient memory, |
| 123 | // zero otherwise |
| 124 | int |
| 125 | TextBuffer::enlargeBuffer(unsigned N) |
| 126 | { |
| 127 | unsigned addedSize = 0; |
| 128 | unsigned newSize = (currentSize ? currentSize : 1) * 2; |
| 129 | char *newSpace; |
| 130 | |
| 131 | if (spaceLeft < N) { |
| 132 | while ((newSize - currentSize) < N) { |
| 133 | newSize *= 2; |
| 134 | } |
| 135 | |
| 136 | addedSize = newSize - currentSize; |
| 137 | |
| 138 | newSpace = static_cast<char *>(ats_realloc(bufferStart, newSize)); |
| 139 | if (newSpace != nullptr) { |
| 140 | nextAdd = newSpace + static_cast<unsigned>(nextAdd - bufferStart); |
| 141 | bufferStart = newSpace; |
| 142 | spaceLeft += addedSize; |
| 143 | currentSize = newSize; |
| 144 | } else { |
| 145 | // Out of Memory, Sigh |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | // int TextBuffer::rawReadFromFile |
| 154 | // |