blob: 9253bc0944dfa7bf7c9b9d6c30d1c73295ceeed4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ScratchBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/ScratchBuffer.h"
15#include "clang/Basic/SourceManager.h"
16#include "llvm/Support/MemoryBuffer.h"
Chris Lattner87cf5ac2008-03-10 17:04:53 +000017#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
20// ScratchBufSize - The size of each chunk of scratch memory. Slightly less
21//than a page, almost certainly enough for anything. :)
22static const unsigned ScratchBufSize = 4060;
23
24ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
25 // Set BytesUsed so that the first call to getToken will require an alloc.
26 BytesUsed = ScratchBufSize;
Reid Spencer5f016e22007-07-11 17:01:13 +000027}
28
29/// getToken - Splat the specified text into a temporary MemoryBuffer and
30/// return a SourceLocation that refers to the token. This is just like the
31/// method below, but returns a location that indicates the physloc of the
32/// token.
Chris Lattner47246be2009-01-26 19:29:26 +000033SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
34 const char *&DestPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000035 if (BytesUsed+Len > ScratchBufSize)
36 AllocScratchBuffer(Len);
37
Chris Lattner47246be2009-01-26 19:29:26 +000038 // Return a pointer to the character data.
39 DestPtr = CurBuffer+BytesUsed;
40
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Copy the token data into the buffer.
42 memcpy(CurBuffer+BytesUsed, Buf, Len);
43
44 // Remember that we used these bytes.
45 BytesUsed += Len;
46
Chris Lattner2b2453a2009-01-17 06:22:33 +000047 return BufferStartLoc.getFileLocWithOffset(BytesUsed-Len);
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
51 // Only pay attention to the requested length if it is larger than our default
52 // page size. If it is, we allocate an entire chunk for it. This is to
53 // support gigantic tokens, which almost certainly won't happen. :)
54 if (RequestLen < ScratchBufSize)
55 RequestLen = ScratchBufSize;
56
57 llvm::MemoryBuffer *Buf =
58 llvm::MemoryBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
Chris Lattner2b2453a2009-01-17 06:22:33 +000059 FileID FID = SourceMgr.createFileIDForMemBuffer(Buf);
60 BufferStartLoc = SourceMgr.getLocForStartOfFile(FID);
Reid Spencer5f016e22007-07-11 17:01:13 +000061 CurBuffer = const_cast<char*>(Buf->getBufferStart());
62 BytesUsed = 0;
63}