@@ -102,7 +102,7 @@ namespace PYGPU_PACKAGE
102102 container_t m_container;
103103 typedef typename container_t ::value_type bin_pair_t ;
104104
105- std::unique_ptr <Allocator> m_allocator;
105+ std::shared_ptr <Allocator> m_allocator;
106106
107107 // A held block is one that's been released by the application, but that
108108 // we are keeping around to dish out again.
@@ -125,8 +125,8 @@ namespace PYGPU_PACKAGE
125125 unsigned m_leading_bits_in_bin_id;
126126
127127 public:
128- memory_pool (Allocator const & alloc=Allocator() , unsigned leading_bits_in_bin_id=4 )
129- : m_allocator(alloc.copy() ),
128+ memory_pool (std::shared_ptr< Allocator> alloc, unsigned leading_bits_in_bin_id=4 )
129+ : m_allocator(alloc),
130130 m_held_blocks (0 ), m_active_blocks(0 ),
131131 m_managed_bytes(0 ), m_active_bytes(0 ),
132132 m_stop_holding(false ),
@@ -233,7 +233,8 @@ namespace PYGPU_PACKAGE
233233 std::cout
234234 << " [pool] allocation of size " << size << " served from bin " << bin_nr
235235 << " which contained " << bin.size () << " entries" << std::endl;
236- return pop_block_from_bin (bin, size);
236+ return m_allocator->hand_out_existing_block (
237+ pop_block_from_bin (bin, size));
237238 }
238239
239240 size_type alloc_sz = alloc_size (bin_nr);
@@ -256,7 +257,8 @@ namespace PYGPU_PACKAGE
256257
257258 m_allocator->try_release_blocks ();
258259 if (bin.size ())
259- return pop_block_from_bin (bin, size);
260+ return m_allocator->hand_out_existing_block (
261+ pop_block_from_bin (bin, size));
260262
261263 if (m_trace)
262264 std::cout << " [pool] allocation still OOM after GC" << std::endl;
@@ -282,7 +284,7 @@ namespace PYGPU_PACKAGE
282284 " failed to free memory for allocation" );
283285 }
284286
285- void free (pointer_type p, size_type size)
287+ void free (pointer_type && p, size_type size)
286288 {
287289 --m_active_blocks;
288290 m_active_bytes -= size;
@@ -291,7 +293,7 @@ namespace PYGPU_PACKAGE
291293 if (!m_stop_holding)
292294 {
293295 inc_held_blocks ();
294- get_bin (bin_nr).push_back (p );
296+ get_bin (bin_nr).push_back (std::move (p) );
295297
296298 if (m_trace)
297299 std::cout << " [pool] block of size " << size << " returned to bin "
@@ -300,7 +302,7 @@ namespace PYGPU_PACKAGE
300302 }
301303 else
302304 {
303- m_allocator->free (p );
305+ m_allocator->free (std::move (p) );
304306 m_managed_bytes -= alloc_size (bin_nr);
305307 }
306308 }
@@ -313,7 +315,7 @@ namespace PYGPU_PACKAGE
313315
314316 while (bin.size ())
315317 {
316- m_allocator->free (bin.back ());
318+ m_allocator->free (std::move ( bin.back () ));
317319 m_managed_bytes -= alloc_size (bin_pair.first );
318320 bin.pop_back ();
319321
@@ -353,7 +355,7 @@ namespace PYGPU_PACKAGE
353355
354356 if (bin.size ())
355357 {
356- m_allocator->free (bin.back ());
358+ m_allocator->free (std::move ( bin.back () ));
357359 m_managed_bytes -= alloc_size (bin_pair.first );
358360 bin.pop_back ();
359361
@@ -379,7 +381,7 @@ namespace PYGPU_PACKAGE
379381
380382 pointer_type pop_block_from_bin (bin_t &bin, size_type size)
381383 {
382- pointer_type result = bin.back ();
384+ pointer_type result ( std::move ( bin.back ()) );
383385 bin.pop_back ();
384386
385387 dec_held_blocks ();
@@ -399,7 +401,7 @@ namespace PYGPU_PACKAGE
399401 typedef typename Pool::pointer_type pointer_type;
400402 typedef typename Pool::size_type size_type;
401403
402- private :
404+ protected :
403405 PYGPU_SHARED_PTR<pool_type> m_pool;
404406
405407 pointer_type m_ptr;
@@ -421,7 +423,7 @@ namespace PYGPU_PACKAGE
421423 {
422424 if (m_valid)
423425 {
424- m_pool->free (m_ptr, m_size);
426+ m_pool->free (std::move ( m_ptr) , m_size);
425427 m_valid = false ;
426428 }
427429 else
@@ -435,16 +437,8 @@ namespace PYGPU_PACKAGE
435437#endif
436438 );
437439 }
438-
439- pointer_type ptr () const
440- { return m_ptr; }
441-
442- size_type size () const
443- { return m_size; }
444440 };
445441}
446442
447443
448-
449-
450444#endif
0 commit comments