[cfe-commits] r173595 - libclang: refactor CXStringPool: make it a class

Dmitri Gribenko gribozavr at gmail.com
Sat Jan 26 14:44:20 PST 2013


Author: gribozavr
Date: Sat Jan 26 16:44:19 2013
New Revision: 173595

URL: http://llvm.org/viewvc/llvm-project?rev=173595&view=rev
Log:
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.

Modified:
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/CIndexUSRs.cpp
    cfe/trunk/tools/libclang/CXString.cpp
    cfe/trunk/tools/libclang/CXString.h
    cfe/trunk/tools/libclang/CXTranslationUnit.h

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=173595&r1=173594&r2=173595&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Sat Jan 26 16:44:19 2013
@@ -66,7 +66,7 @@ CXTranslationUnit cxtu::MakeCXTranslatio
   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(CXTran
       return;
 
     delete cxtu::getASTUnit(CTUnit);
-    disposeCXStringPool(CTUnit->StringPool);
+    delete CTUnit->StringPool;
     delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
     disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
     delete CTUnit->FormatContext;

Modified: cfe/trunk/tools/libclang/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexUSRs.cpp?rev=173595&r1=173594&r2=173595&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexUSRs.cpp Sat Jan 26 16:44:19 2013
@@ -835,7 +835,7 @@ CXString clang_getCursorUSR(CXCursor C) 
 
     bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
     if (Ignore) {
-      disposeCXStringBuf(buf);
+      buf->dispose();
       return createCXString("");
     }
 

Modified: cfe/trunk/tools/libclang/CXString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXString.cpp?rev=173595&r1=173594&r2=173595&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXString.cpp (original)
+++ cfe/trunk/tools/libclang/CXString.cpp Sat Jan 26 16:44:19 2013
@@ -68,37 +68,29 @@ CXString cxstring::createCXString(CXStri
 // String pools.
 //===----------------------------------------------------------------------===//
 
-  
-typedef std::vector<CXStringBuf *> CXStringPool;
-
-void *cxstring::createCXStringPool() {
-  return new CXStringPool();
+cxstring::CXStringPool::~CXStringPool() {
+  for (std::vector<CXStringBuf *>::iterator I = Pool.begin(), E = Pool.end();
+       I != E; ++I) {
+    delete *I;
+  }
 }
 
-void cxstring::disposeCXStringPool(void *p) {
-  CXStringPool *pool = static_cast<CXStringPool*>(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<CXStringPool*>(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<CXStringPool*>(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<void *>(string.data));
       break;
     case CXS_StringBuf:
-      disposeCXStringBuf(static_cast<CXStringBuf *>(
-                             const_cast<void *>(string.data)));
+      static_cast<CXStringBuf *>(
+          const_cast<void *>(string.data))->dispose();
       break;
   }
 }

Modified: cfe/trunk/tools/libclang/CXString.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXString.h?rev=173595&r1=173594&r2=173595&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXString.h (original)
+++ cfe/trunk/tools/libclang/CXString.h Sat Jan 26 16:44:19 2013
@@ -18,15 +18,12 @@
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
+#include <vector>
 
 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
 /// \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<CXStringBuf *> 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);

Modified: cfe/trunk/tools/libclang/CXTranslationUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXTranslationUnit.h?rev=173595&r1=173594&r2=173595&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXTranslationUnit.h (original)
+++ cfe/trunk/tools/libclang/CXTranslationUnit.h Sat Jan 26 16:44:19 2013
@@ -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;





More information about the cfe-commits mailing list