Hi

Attached is another diff for a PdfRefCountedBuffer pool allocator. It works a lot better than mine, but relies on Boost. It's written to be entirely optional, so if Boost isn't available we just fall back to the default operator new() transparently.

Thoughts?

--
Craig Ringer
Index: src/PdfRefCountedBuffer.cpp
===================================================================
--- src/PdfRefCountedBuffer.cpp (revision 731)
+++ src/PdfRefCountedBuffer.cpp (working copy)
@@ -22,6 +22,9 @@

 namespace PoDoFo {

+#if defined(HAVE_BOOST)
+boost::fast_pool_allocator<PdfRefCountedBuffer::TRefCountedBuffer> 
PdfRefCountedBuffer::TRefCountedBuffer::m_pool;
+#endif

 #define STREAM_SIZE_INCREASE 1024

@@ -48,6 +51,7 @@
     if( m_pBuffer->m_bOnHeap && m_pBuffer->m_bPossesion )
         free( m_pBuffer->m_pHeapBuffer );
     delete m_pBuffer;
+    m_pBuffer = 0;
 }

 void PdfRefCountedBuffer::ReallyDetach( long lExtraLen )
@@ -241,5 +245,4 @@
     return false;
 }

-
 };
Index: src/PdfRefCountedBuffer.h
===================================================================
--- src/PdfRefCountedBuffer.h   (revision 731)
+++ src/PdfRefCountedBuffer.h   (working copy)
@@ -23,6 +23,11 @@

 #include "PdfDefines.h"

+#if defined(HAVE_BOOST)
+#include <boost/pool/singleton_pool.hpp>
+#include <boost/pool/pool_alloc.hpp>
+#endif
+
 namespace PoDoFo {

 /**
@@ -189,6 +194,22 @@
         bool  m_bPossesion;
         // Are we using the heap-allocated buffer in place of our small 
internal one?
         bool  m_bOnHeap;
+
+#if defined(HAVE_BOOST)
+        // Note that the memory allocated here is NOT released until program 
exit or explicit
+        // deallocation of the pool. You must call:
+        // boost::singleton_pool<boost::fast_pool_allocator_tag, 
sizeof(TRefCountedBuffer)>::release_memory()
+        // to force that.
+        static boost::fast_pool_allocator<TRefCountedBuffer> m_pool;
+        void* operator new(size_t /*size*/)
+        {
+            return m_pool.allocate(1);
+        }
+        PODOFO_NOTHROW void operator delete(void* p)
+        {
+            m_pool.deallocate( static_cast<TRefCountedBuffer*>(p), 1);
+        }
+#endif
     };

     TRefCountedBuffer* m_pBuffer;
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Podofo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to