MemoryBuffer::~MemoryBuffer() {
if (MustDeleteBuffer)
- delete [] BufferStart;
+ free((void*)BufferStart);
}
/// initCopyOf - Initialize this source buffer with a copy of the specified
/// successfully.
void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
size_t Size = BufEnd-BufStart;
- BufferStart = new char[Size+1];
+ BufferStart = (char *)malloc((Size+1) * sizeof(char));
BufferEnd = BufferStart+Size;
memcpy(const_cast<char*>(BufferStart), BufStart, Size);
*const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
/// that is completely initialized to zeros. Note that the caller should
/// initialize the memory allocated by this method. The memory is owned by
/// the MemoryBuffer object.
-MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size,
+MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
const char *BufferName) {
- char *Buf = new char[Size+1];
+ char *Buf = (char *)malloc((Size+1) * sizeof(char));
+ if (!Buf) return 0;
Buf[Size] = 0;
MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
// The memory for this buffer is owned by the MemoryBuffer.
/// is completely initialized to zeros. Note that the caller should
/// initialize the memory allocated by this method. The memory is owned by
/// the MemoryBuffer object.
-MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
+MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size,
const char *BufferName) {
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
+ if (!SB) return 0;
memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
return SB;
}
return new MemoryBufferMMapFile(Filename, Pages, FileSize);
}
}
-
- OwningPtr<MemoryBuffer> SB;
- SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename));
+
+ MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename);
+ if (!Buf) {
+ // Failed to create a buffer.
+ if (ErrStr) *ErrStr = "could not allocate buffer";
+ ::close(FD);
+ return 0;
+ }
+
+ OwningPtr<MemoryBuffer> SB(Buf);
char *BufPtr = const_cast<char*>(SB->getBufferStart());
- unsigned BytesLeft = FileSize;
+ size_t BytesLeft = FileSize;
while (BytesLeft) {
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
if (NumRead != -1) {
MemoryBuffer *MemoryBuffer::getSTDIN() {
char Buffer[4096*4];
-
+
std::vector<char> FileData;
-
+
// Read in all of the data from stdin, we cannot mmap stdin.
sys::Program::ChangeStdinToBinary();
- while (size_t ReadBytes = fread(Buffer, sizeof(char), 4096*4, stdin))
+ size_t ReadBytes;
+ do {
+ ReadBytes = fread(Buffer, sizeof(char), sizeof(Buffer), stdin);
FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
+ } while (ReadBytes == sizeof(Buffer));
FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
size_t Size = FileData.size();