re PR c++/14179 (out of memory while parsing array with many initializers)
authorJason Merrill <jason@redhat.com>
Mon, 16 Jan 2012 16:40:26 +0000 (11:40 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Mon, 16 Jan 2012 16:40:26 +0000 (11:40 -0500)
PR c++/14179
* vec.c (vec_gc_o_reserve_1): Use ggc_round_alloc_size.

From-SVN: r183213

gcc/ChangeLog
gcc/vec.c

index d06de3d16a91293293f453e0546592a67d27f87f..dd13d0bbc72af6e4276dd8907d9f184eed7ae9b7 100644 (file)
@@ -1,3 +1,8 @@
+2012-01-16  Jason Merrill  <jason@redhat.com>
+
+       PR c++/14179
+       * vec.c (vec_gc_o_reserve_1): Use ggc_round_alloc_size.
+
 2012-01-16  Jakub Jelinek  <jakub@redhat.com>
 
        PR bootstrap/51860
index c1d003492e3c2d802ba6713f7fdb3c463e8e8a87..783a3cfd52d5210a7eb7bc97e63a9c9e44c21f86 100644 (file)
--- a/gcc/vec.c
+++ b/gcc/vec.c
@@ -221,6 +221,7 @@ vec_gc_o_reserve_1 (void *vec, int reserve, size_t vec_offset, size_t elt_size,
 {
   struct vec_prefix *pfx = (struct vec_prefix *) vec;
   unsigned alloc = calculate_allocation (pfx, reserve, exact);
+  size_t size;
 
   if (!alloc)
     {
@@ -229,7 +230,17 @@ vec_gc_o_reserve_1 (void *vec, int reserve, size_t vec_offset, size_t elt_size,
       return NULL;
     }
 
-  vec = ggc_realloc_stat (vec, vec_offset + alloc * elt_size PASS_MEM_STAT);
+  /* Calculate the amount of space we want.  */
+  size = vec_offset + alloc * elt_size;
+  /* Ask the allocator how much space it will really give us.  */
+  size = ggc_round_alloc_size (size);
+  /* Adjust the number of slots accordingly.  */
+  alloc = (size - vec_offset) / elt_size;
+  /* And finally, recalculate the amount of space we ask for.  */
+  size = vec_offset + alloc * elt_size;
+
+  vec = ggc_realloc_stat (vec, size PASS_MEM_STAT);
+
   ((struct vec_prefix *)vec)->alloc = alloc;
   if (!pfx)
     ((struct vec_prefix *)vec)->num = 0;