This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Add valgrind support to alloc-pool.c
- From: Kenneth Zadeck <zadeck at naturalbridge dot com>
- To: Andrew Pinski <pinskia at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>, "Berlin, Daniel" <dberlin at dberlin dot org>
- Date: Mon, 04 Dec 2006 08:13:01 -0500
- Subject: Re: Add valgrind support to alloc-pool.c
- References: <1165179879.30345.76.camel@celery.andrew.com>
Andrew,
Thanks, this works nicely. If danny (who originally wrote alloc pool)
does not have any comments, will you check it into the dataflow branch?
Kenny
Andrew Pinski wrote:
> Here is the patch which I promised, I will be sending it out to the list
> once I do a bootstrap/test with it but I tested it on compiling
> insn-attrtab.i at -O2 which should be a good test.
>
> Thanks,
> Andrew Pinski
>
>
> ------------------------------------------------------------------------
>
> Index: alloc-pool.c
> ===================================================================
> --- alloc-pool.c (revision 119245)
> +++ alloc-pool.c (working copy)
> @@ -25,6 +25,19 @@ Software Foundation, 51 Franklin Street,
> #include "alloc-pool.h"
> #include "hashtab.h"
>
> +#ifdef ENABLE_VALGRIND_CHECKING
> +# ifdef HAVE_VALGRIND_MEMCHECK_H
> +# include <valgrind/memcheck.h>
> +# elif defined HAVE_MEMCHECK_H
> +# include <memcheck.h>
> +# else
> +# include <valgrind.h>
> +# endif
> +#else
> +/* Avoid #ifdef:s when we can help it. */
> +#define VALGRIND_DISCARD(x)
> +#endif
> +
> #define align_eight(x) (((x+7) >> 3) << 3)
>
> /* The internal allocation object. */
> @@ -224,6 +237,7 @@ pool_alloc (alloc_pool pool)
> {
> alloc_pool_list header;
> char *block;
> + int size;
> #ifdef GATHER_STATISTICS
> struct alloc_pool_descriptor *desc = alloc_pool_descriptor (pool->name);
>
> @@ -231,6 +245,7 @@ pool_alloc (alloc_pool pool)
> #endif
>
> gcc_assert (pool);
> + size = pool->elt_size - offsetof (allocation_object, u.data);
>
> /* If there are no more free elements, make some more!. */
> if (!pool->free_list)
> @@ -261,6 +276,7 @@ pool_alloc (alloc_pool pool)
> #endif
> header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block);
> header->next = pool->free_list;
> + VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (header,size));
> pool->free_list = header;
> }
> /* Also update the number of elements we have free/allocated, and
> @@ -272,6 +288,7 @@ pool_alloc (alloc_pool pool)
>
> /* Pull the first free element from the free list, and return it. */
> header = pool->free_list;
> + VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (header, sizeof(*header)));
> pool->free_list = header->next;
> pool->elts_free--;
>
> @@ -279,6 +296,7 @@ pool_alloc (alloc_pool pool)
> /* Set the ID for element. */
> ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
> #endif
> + VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (header, size));
>
> return ((void *) header);
> }
> @@ -288,11 +306,13 @@ void
> pool_free (alloc_pool pool, void *ptr)
> {
> alloc_pool_list header;
> + int size;
> + size = pool->elt_size - offsetof (allocation_object, u.data);
>
> gcc_assert (ptr);
>
> #ifdef ENABLE_CHECKING
> - memset (ptr, 0xaf, pool->elt_size - offsetof (allocation_object, u.data));
> + memset (ptr, 0xaf, size);
>
> /* Check whether the PTR was allocated from POOL. */
> gcc_assert (pool->id == ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id);
> @@ -307,6 +327,7 @@ pool_free (alloc_pool pool, void *ptr)
> header = (alloc_pool_list) ptr;
> header->next = pool->free_list;
> pool->free_list = header;
> + VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (ptr, size));
> pool->elts_free++;
> }
> /* Output per-alloc_pool statistics. */
>