GCC Wikia
Advertisement

このページを編集する際は,編集に関する方針に従ってください.[]

概要[]

引数[]

実装[]

ggc-none.c[]

55 void *
56 ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL)
57 {
58   return xrealloc (x, size);
59 }

ggc-common.c[]

143 /* Resize a block of memory, possibly re-allocating it.  */
144 void *
145 ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL)
146 {
147   void *r;
148   size_t old_size;
149 
150   if (x == NULL)
151     return ggc_alloc_stat (size PASS_MEM_STAT);
152 
153   old_size = ggc_get_size (x);
154 
155   if (size <= old_size)
156     {
157       /* Mark the unwanted memory as unaccessible.  We also need to make
158          the "new" size accessible, since ggc_get_size returns the size of
159          the pool, not the size of the individually allocated object, the
160          size which was previously made accessible.  Unfortunately, we
161          don't know that previously allocated size.  Without that
162          knowledge we have to lose some initialization-tracking for the
163          old parts of the object.  An alternative is to mark the whole
164          old_size as reachable, but that would lose tracking of writes
165          after the end of the object (by small offsets).  Discard the
166          handle to avoid handle leak.  */
167       VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x + size,
168                                                 old_size - size));
169       VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x, size));
170       return x;
171     }
172 
173   r = ggc_alloc_stat (size PASS_MEM_STAT);
174 
175   /* Since ggc_get_size returns the size of the pool, not the size of the
176      individually allocated object, we'd access parts of the old object
177      that were marked invalid with the memcpy below.  We lose a bit of the
178      initialization-tracking since some of it may be uninitialized.  */
179   VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x, old_size));
180 
181   memcpy (r, x, old_size);
182 
183   /* The old object is not supposed to be used anymore.  */
184   ggc_free (x);
185 
186   return r;
187 }



リンク元

Advertisement