[PATCH] rewrite of the allocator, removed unneccesary includes of memorypool.h

Floris Ruijter flo.ruijt at hotmail.com
Wed Feb 23 23:03:06 UTC 2011


---
 languages/cpp/cppparsejob.cpp              |    1 +
 languages/cpp/parser/ast.h                 |    8 ---
 languages/cpp/parser/control.h             |    1 -
 languages/cpp/parser/listnode.h            |    5 +-
 languages/cpp/parser/parser.cpp            |   10 ++++
 languages/cpp/parser/rxx_allocator.h       |   78 ++++++++++++++++++++--------
 languages/cpp/parser/tests/test_parser.cpp |    1 +
 7 files changed, 72 insertions(+), 32 deletions(-)

diff --git a/languages/cpp/cppparsejob.cpp b/languages/cpp/cppparsejob.cpp
index f4819f2..7278af7 100644
--- a/languages/cpp/cppparsejob.cpp
+++ b/languages/cpp/cppparsejob.cpp
@@ -43,6 +43,7 @@
 #include "parser/parser.h"
 #include "parser/control.h"
 #include "parser/dumptree.h"
+#include "parser/memorypool.h"
 #include <language/duchain/duchain.h>
 #include <language/duchain/duchainpointer.h>
 #include <language/duchain/duchainlock.h>
diff --git a/languages/cpp/parser/ast.h b/languages/cpp/parser/ast.h
index a736a71..eff2c90 100644
--- a/languages/cpp/parser/ast.h
+++ b/languages/cpp/parser/ast.h
@@ -20,7 +20,6 @@
 #ifndef AST_H
 #define AST_H
 
-#include "memorypool.h"
 #include "listnode.h"
 
 #define DECLARE_AST_NODE(k) \
@@ -1144,13 +1143,6 @@ public:
   uint modifier;
 };
 
-template <class _Tp>
-_Tp *CreateNode(pool *memory_pool)
-{
-  _Tp *node = reinterpret_cast<_Tp*>(memory_pool->allocate(sizeof(_Tp)));
-  node->kind = _Tp::__node_kind;
-  return node;
-}
 
 template <class _Tp>
 _Tp ast_cast(AST *item)
diff --git a/languages/cpp/parser/control.h b/languages/cpp/parser/control.h
index 0b06248..8965585 100644
--- a/languages/cpp/parser/control.h
+++ b/languages/cpp/parser/control.h
@@ -20,7 +20,6 @@
 #define CONTROL_H
 
 #include "symbol.h"
-#include "memorypool.h"
 #include <language/interfaces/iproblem.h>
 #include <cppparserexport.h>
 
diff --git a/languages/cpp/parser/listnode.h b/languages/cpp/parser/listnode.h
index d1eda36..89e5688 100644
--- a/languages/cpp/parser/listnode.h
+++ b/languages/cpp/parser/listnode.h
@@ -19,7 +19,6 @@
 #ifndef LISTNODE_H
 #define LISTNODE_H
 
-#include "memorypool.h"
 #include <kdebug.h>
 
 /**
@@ -35,6 +34,7 @@ public:
   int index;
   mutable const ListNode<Tp> *next;
 
+  template<class pool>
   static ListNode *create(const Tp &element, pool *p)
   {
     ListNode<Tp> *node = new (p->allocate(sizeof(ListNode))) ListNode();
@@ -45,6 +45,7 @@ public:
     return node;
   }
 
+  template<class pool>
   static ListNode *create(const ListNode *n1, const Tp &element, pool *p)
   {
     ListNode<Tp> *n2 = ListNode::create(element, p);
@@ -84,7 +85,7 @@ public:
   }
 };
 
-template <class Tp>
+template <class Tp,class pool>
 inline const ListNode<Tp> *snoc(const ListNode<Tp> *list,
 				const Tp &element, pool *p)
 {
diff --git a/languages/cpp/parser/parser.cpp b/languages/cpp/parser/parser.cpp
index e969785..89df4f9 100644
--- a/languages/cpp/parser/parser.cpp
+++ b/languages/cpp/parser/parser.cpp
@@ -26,6 +26,7 @@
 #include "control.h"
 #include "parsesession.h"
 #include "commentformatter.h"
+#include "memorypool.h"
 
 #include <cstdlib>
 #include <iostream>
@@ -63,6 +64,15 @@
       (_node)->end_token = end; \
   } while (0)
 
+template <class _Tp>
+inline _Tp *CreateNode(pool *memory_pool)
+{
+  _Tp *node = reinterpret_cast<_Tp*>(memory_pool->allocate(sizeof(_Tp)));
+  node->kind = _Tp::__node_kind;
+  return node;
+}
+
+
 void Parser::addComment( CommentAST* ast, const Comment& comment ) {
   if( comment ) {
 /*    kDebug() << "Adding but leaving comment" << session->token_stream->token(comment.token()).symbol();*/
diff --git a/languages/cpp/parser/rxx_allocator.h b/languages/cpp/parser/rxx_allocator.h
index f0159e9..55295c3 100644
--- a/languages/cpp/parser/rxx_allocator.h
+++ b/languages/cpp/parser/rxx_allocator.h
@@ -23,6 +23,8 @@
 #include <cstdlib>
 #include <string.h>
 #include <memory>
+#include <KDebug>
+#include <QMutex>
 
 /**The allocator which uses fixed size blocks for allocation of its elements.
 Block size is currently 64k, allocated space is not reclaimed,
@@ -43,8 +45,17 @@ public:
   typedef std::ptrdiff_t difference_type;
 
   static const size_type max_block_count = size_type(-1);
-  static const size_type _S_block_size = 1 << 16; // 64K
+  static const size_type _S_block_size = 1 << 15; // 32K
+  static const size_type _S_null_size  = 1<<10; // 1K
 
+
+  struct block{
+    block *next;
+    char data[_S_block_size-sizeof(block*)];
+  };
+
+  static block* allocated_blocks;
+  static QMutex chain_lock;
   rxx_allocator() {
     init();
   }
@@ -54,10 +65,12 @@ public:
   }
 
   ~rxx_allocator() {
-    for (size_type index = 0; index < _M_block_index + 1; ++index)
-      delete[] _M_storage[index];
-
-    ::free(_M_storage);
+    if(_M_current_block){
+      chain_lock.lock();
+      _M_current_block->next=allocated_blocks;
+      allocated_blocks=_M_first_block;
+      chain_lock.unlock();
+    }
   }
 
   pointer address(reference __val) { return &__val; }
@@ -71,22 +84,42 @@ public:
     const size_type bytes = __n * sizeof(_Tp);
 
     if (_M_current_block == 0
-	|| _S_block_size < _M_current_index + bytes)
+        || (sizeof(_M_current_block->data)) < ( _M_current_index + bytes))
       {
-	++_M_block_index;
-
-	_M_storage = reinterpret_cast<char**>
-	  (::realloc(_M_storage, sizeof(char*) * (1 + _M_block_index)));
+        block *new_block=0;
+        if(chain_lock.tryLock()){
+          if(allocated_blocks){
+            new_block=allocated_blocks;
+            allocated_blocks=allocated_blocks->next;
+          }
+          chain_lock.unlock();
+        }
+        
+        if(!new_block){
+          new_block = new block;
+        }
+
+        ::memset(new_block->data, 0, _S_null_size - sizeof(block*) );
+        if(_M_first_block){
+          _M_current_block->next=new_block;
+        } else {
+          _M_first_block = new_block;
+        }
+
+        _M_current_block=new_block;
+        _M_current_index = 0;
+        _M_nulled_index=_S_null_size;
+      }
 
-	_M_current_block = _M_storage[_M_block_index] = reinterpret_cast<char*>
-	  (new char[_S_block_size]);
+    if( ( _M_nulled_index - sizeof(block*) ) < (_M_current_index + bytes) ){
+      ::memset( (char*)_M_current_block + _M_nulled_index, 0, _S_null_size);
+      _M_nulled_index+=_S_null_size;
+      Q_ASSERT(_M_nulled_index <= _S_block_size );
+    }
 
-	::memset(_M_current_block, 0, _S_block_size);
-	_M_current_index = 0;
-      }
 
     pointer p = reinterpret_cast<pointer>
-      (_M_current_block + _M_current_index);
+      (_M_current_block->data + _M_current_index);
 
     _M_current_index += bytes;
 
@@ -109,20 +142,23 @@ private:
 
   void init()
   {
-    _M_block_index = max_block_count;
     _M_current_index = 0;
-    _M_storage = 0;
     _M_current_block = 0;
+    _M_first_block = 0;
   }
 
   template <class _Tp1> rxx_allocator(const rxx_allocator<_Tp1> &__o) {}
 
 private:
-  size_type _M_block_index;
   size_type _M_current_index;
-  char *_M_current_block;
-  char **_M_storage;
+  size_type _M_nulled_index;
+  block *_M_current_block;
+  block *_M_first_block;
 };
+template<class T>
+typename rxx_allocator<T>::block *rxx_allocator<T>::allocated_blocks = 0;
+
+template<class T> QMutex rxx_allocator<T>::chain_lock;
 
 #endif // RXX_ALLOCATOR_H
 
diff --git a/languages/cpp/parser/tests/test_parser.cpp b/languages/cpp/parser/tests/test_parser.cpp
index a723582..dee3132 100644
--- a/languages/cpp/parser/tests/test_parser.cpp
+++ b/languages/cpp/parser/tests/test_parser.cpp
@@ -11,6 +11,7 @@
 #include "tokens.h"
 #include "parsesession.h"
 #include "commentformatter.h"
+#include "memorypool.h"
 
 #include "testconfig.h"
 
-- 
1.7.2.3


--=-gdACqmlNvU1tq2uxEbK5--





More information about the KDevelop-devel mailing list