Hi,
here is updated patch.  Bootstrapped/regtested x86_64-linux, OK?

Honza

        * ggc-page.c (ggc_collect): Call memory_block_pool::trim.
        * memory-block.cc (memory_block_pool::clear_free_list): Rename to ...
        (memory_block_pool::reduce_free_list): ... this one.
        (memory_block_pool::trim): New static function.
        * memory-block.h (memory_block_pool::freelist_size): New constant
        (memory_block_pool::clear_free_list): Rename to ...
        (memory_block_pool::reduce_free_list): ... this one.
        (memory_block_pool::trim): Declare.
        
        * lto.c (lto_wpa_write_files): Call memory_block_pool::trim.
Index: ggc-page.c
===================================================================
--- ggc-page.c  (revision 278570)
+++ ggc-page.c  (working copy)
@@ -2186,6 +2186,9 @@ ggc_collect (void)
   float allocated_last_gc =
     MAX (G.allocated_last_gc, (size_t)param_ggc_min_heapsize * 1024);
 
+  /* It is also good time to get memory block pool into limits.  */
+  memory_block_pool::trim ();
+
   float min_expand = allocated_last_gc * param_ggc_min_expand / 100;
   if (G.allocated < allocated_last_gc + min_expand && !ggc_force_collect)
     return;
Index: lto/lto.c
===================================================================
--- lto/lto.c   (revision 278570)
+++ lto/lto.c   (working copy)
@@ -387,6 +387,7 @@ lto_wpa_write_files (void)
       temp_priority.safe_push (part->insns);
       temp_filenames.safe_push (xstrdup (temp_filename));
     }
+  memory_block_pool::trim (0);
 
   for (int set = 0; set < MAX (lto_parallelism, 1); set++)
     {
Index: memory-block.cc
===================================================================
--- memory-block.cc     (revision 278570)
+++ memory-block.cc     (working copy)
@@ -28,15 +28,30 @@ memory_block_pool memory_block_pool::ins
 
 memory_block_pool::memory_block_pool () : m_blocks (NULL) {}
 
-/* Return all blocks from free list to the OS.  */
+/* Reduce free list to NUM blocks and return remaining to malloc.  */
 void
-memory_block_pool::clear_free_list ()
+memory_block_pool::reduce_free_list (int num)
 {
-  while (m_blocks)
+  block_list **blocks = &m_blocks;
+
+  /* First skip NUM blocks.  */
+
+  for (;num > 0 && *blocks; num--)
+    blocks = &(*blocks)->m_next;
+
+  if (!*blocks)
+    return;
+
+  /* And free the remainder of them.  */
+
+  block_list *to_free = *blocks;
+  *blocks = NULL;
+
+  while (to_free)
     {
-      block_list *next = m_blocks->m_next;
-      XDELETEVEC (m_blocks);
-      m_blocks = next;

+      block_list *next = to_free->m_next;
+      XDELETEVEC (to_free);
+      to_free = next;
     }
 }
 
@@ -62,3 +77,10 @@ mempool_obstack_chunk_free (void *chunk)
   else
     XDELETEVEC (chunk);
 }
+
+/* Return allocated memory back to malloc (and to system).  */
+void
+memory_block_pool::trim (int num)
+{
+  instance.reduce_free_list (num);
+}
Index: memory-block.h
===================================================================
--- memory-block.h      (revision 278570)
+++ memory-block.h      (working copy)
@@ -28,12 +28,15 @@ class memory_block_pool
 public:
   /* Blocks have fixed size.  This is necessary for sharing.  */
   static const size_t block_size = 64 * 1024;
+  /* Number of blocks we keep in the freelists.  */
+  static const size_t freelist_size = 1024 * 1024 / block_size;
 
   memory_block_pool ();
 
   static inline void *allocate () ATTRIBUTE_MALLOC;
   static inline void release (void *);
-  void clear_free_list ();
+  static void trim (int nblocks = freelist_size);
+  void reduce_free_list (int);
 
 private:
   /* memory_block_pool singleton instance, defined in memory-block.cc.  */

Reply via email to