From b95b3f128bd83854b7159300902a0e124d0205d4 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Sat, 26 Jan 2013 22:44:19 +0000 Subject: [PATCH] libclang: refactor CXStringPool: make it a class We are not exposing the pool or string buffers to libclang users, so no need to maintain a procedural interface. llvm-svn: 173595 --- clang/tools/libclang/CIndex.cpp | 4 +-- clang/tools/libclang/CIndexUSRs.cpp | 2 +- clang/tools/libclang/CXString.cpp | 44 +++++++++++++------------------- clang/tools/libclang/CXString.h | 38 +++++++++++++++++---------- clang/tools/libclang/CXTranslationUnit.h | 3 ++- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 949db52..9eacd97 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -66,7 +66,7 @@ CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *AU) { CXTranslationUnit D = new CXTranslationUnitImpl(); D->CIdx = CIdx; D->TheASTUnit = AU; - D->StringPool = createCXStringPool(); + D->StringPool = new CXStringPool(); D->Diagnostics = 0; D->OverridenCursorsPool = createOverridenCXCursorsPool(); D->FormatContext = 0; @@ -2820,7 +2820,7 @@ void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { return; delete cxtu::getASTUnit(CTUnit); - disposeCXStringPool(CTUnit->StringPool); + delete CTUnit->StringPool; delete static_cast(CTUnit->Diagnostics); disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); delete CTUnit->FormatContext; diff --git a/clang/tools/libclang/CIndexUSRs.cpp b/clang/tools/libclang/CIndexUSRs.cpp index 16735f4..eac74fa 100644 --- a/clang/tools/libclang/CIndexUSRs.cpp +++ b/clang/tools/libclang/CIndexUSRs.cpp @@ -835,7 +835,7 @@ CXString clang_getCursorUSR(CXCursor C) { bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data); if (Ignore) { - disposeCXStringBuf(buf); + buf->dispose(); return createCXString(""); } diff --git a/clang/tools/libclang/CXString.cpp b/clang/tools/libclang/CXString.cpp index 5866415..739646b 100644 --- a/clang/tools/libclang/CXString.cpp +++ b/clang/tools/libclang/CXString.cpp @@ -68,37 +68,29 @@ CXString cxstring::createCXString(CXStringBuf *buf) { // String pools. //===----------------------------------------------------------------------===// - -typedef std::vector CXStringPool; - -void *cxstring::createCXStringPool() { - return new CXStringPool(); +cxstring::CXStringPool::~CXStringPool() { + for (std::vector::iterator I = Pool.begin(), E = Pool.end(); + I != E; ++I) { + delete *I; + } } -void cxstring::disposeCXStringPool(void *p) { - CXStringPool *pool = static_cast(p); - if (pool) { - for (CXStringPool::iterator I = pool->begin(), E = pool->end(); - I != E; ++I) { - delete *I; - } - delete pool; - } +CXStringBuf *cxstring::CXStringPool::getCXStringBuf(CXTranslationUnit TU) { + if (Pool.empty()) + return new CXStringBuf(TU); + + CXStringBuf *Buf = Pool.back(); + Buf->Data.clear(); + Pool.pop_back(); + return Buf; } CXStringBuf *cxstring::getCXStringBuf(CXTranslationUnit TU) { - CXStringPool *pool = static_cast(TU->StringPool); - if (pool->empty()) - return new CXStringBuf(TU); - CXStringBuf *buf = pool->back(); - buf->Data.clear(); - pool->pop_back(); - return buf; + return TU->StringPool->getCXStringBuf(TU); } -void cxstring::disposeCXStringBuf(CXStringBuf *buf) { - if (buf) - static_cast(buf->TU->StringPool)->push_back(buf); +void cxstring::CXStringBuf::dispose() { + TU->StringPool->Pool.push_back(this); } bool cxstring::isManagedByPool(CXString str) { @@ -126,8 +118,8 @@ void clang_disposeString(CXString string) { free(const_cast(string.data)); break; case CXS_StringBuf: - disposeCXStringBuf(static_cast( - const_cast(string.data))); + static_cast( + const_cast(string.data))->dispose(); break; } } diff --git a/clang/tools/libclang/CXString.h b/clang/tools/libclang/CXString.h index 73d94f6..3753ed4 100644 --- a/clang/tools/libclang/CXString.h +++ b/clang/tools/libclang/CXString.h @@ -18,15 +18,12 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" +#include namespace clang { namespace cxstring { - -struct CXStringBuf { - SmallString<128> Data; - CXTranslationUnit TU; - CXStringBuf(CXTranslationUnit tu) : TU(tu) {} -}; + +struct CXStringBuf; /// \brief Create a CXString object from a C string. CXString createCXString(const char *String, bool DupString = false); @@ -37,15 +34,30 @@ CXString createCXString(StringRef String, bool DupString = true); /// \brief Create a CXString object that is backed by a string buffer. CXString createCXString(CXStringBuf *buf); -/// \brief Create an opaque string pool used for fast geneneration of strings. -void *createCXStringPool(); +/// \brief A string pool used for fast allocation/deallocation of strings. +class CXStringPool { +public: + ~CXStringPool(); + + CXStringBuf *getCXStringBuf(CXTranslationUnit TU); + +private: + std::vector Pool; + + friend struct CXStringBuf; +}; + +struct CXStringBuf { + SmallString<128> Data; + CXTranslationUnit TU; + + CXStringBuf(CXTranslationUnit TU) : TU(TU) {} + + /// \brief Return this buffer to the pool. + void dispose(); +}; -/// \brief Dispose of a string pool. -void disposeCXStringPool(void *pool); - CXStringBuf *getCXStringBuf(CXTranslationUnit TU); - -void disposeCXStringBuf(CXStringBuf *buf); /// \brief Returns true if the CXString data is managed by a pool. bool isManagedByPool(CXString str); diff --git a/clang/tools/libclang/CXTranslationUnit.h b/clang/tools/libclang/CXTranslationUnit.h index c25056a..699b74a 100644 --- a/clang/tools/libclang/CXTranslationUnit.h +++ b/clang/tools/libclang/CXTranslationUnit.h @@ -15,6 +15,7 @@ #define LLVM_CLANG_CXTRANSLATIONUNIT_H #include "clang-c/Index.h" +#include "CXString.h" namespace clang { class ASTUnit; @@ -25,7 +26,7 @@ namespace clang { struct CXTranslationUnitImpl { clang::CIndexer *CIdx; clang::ASTUnit *TheASTUnit; - void *StringPool; + clang::cxstring::CXStringPool *StringPool; void *Diagnostics; void *OverridenCursorsPool; clang::SimpleFormatContext *FormatContext; -- 2.7.4