diff --git a/vvp/schedule.cc b/vvp/schedule.cc index 1928f85a9..73abf2eed 100644 --- a/vvp/schedule.cc +++ b/vvp/schedule.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2013 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2015 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +# include "config.h" # include "schedule.h" # include "vthread.h" # include "vpi_priv.h" @@ -27,8 +28,11 @@ # include # include # include - # include +#ifdef CHECK_WITH_VALGRIND +# include "vvp_cleanup.h" +# include "ivl_alloc.h" +#endif unsigned long count_assign_events = 0; unsigned long count_gen_events = 0; @@ -1137,4 +1141,21 @@ void schedule_simulate(void) // Execute post-simulation callbacks vpiPostsim(); +#ifdef CHECK_WITH_VALGRIND + schedule_delete(); +#endif } + +#ifdef CHECK_WITH_VALGRIND +void schedule_delete(void) +{ + vthread_event_heap.delete_pool(); + assign4_heap.delete_pool(); + assign8_heap.delete_pool(); + assignr_heap.delete_pool(); + array_w_heap.delete_pool(); + array_r_w_heap.delete_pool(); + generic_event_heap.delete_pool(); + event_time_heap.delete_pool(); +} +#endif diff --git a/vvp/slab.h b/vvp/slab.h index 1541b6969..e19930702 100644 --- a/vvp/slab.h +++ b/vvp/slab.h @@ -1,7 +1,7 @@ #ifndef IVL_slab_H #define IVL_slab_H /* - * Copyright (c) 2008-2014 Picture Elements, Inc. + * Copyright (c) 2008-2015 Picture Elements, Inc. * Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it @@ -21,6 +21,8 @@ */ +# include "config.h" + template class slab_t { union item_cell_u { @@ -33,12 +35,22 @@ template class slab_t { void* alloc_slab(); void free_slab(void*); +#ifdef CHECK_WITH_VALGRIND + // If we have allocated memory then we need to delete it to make + // valgrind happy. + void delete_pool(void); +#endif unsigned long pool; private: item_cell_u*heap_; item_cell_u initial_chunk_[CHUNK_COUNT]; +#ifdef CHECK_WITH_VALGRIND + // Each slab needs a pointer to the allocated space. + item_cell_u**slab_pool; + unsigned slab_pool_count; +#endif }; template @@ -50,6 +62,11 @@ slab_t::slab_t() initial_chunk_[idx].next = initial_chunk_+idx+1; initial_chunk_[CHUNK_COUNT-1].next = 0; +#ifdef CHECK_WITH_VALGRIND + // Initially we have no allocated space. + slab_pool = NULL; + slab_pool_count = 0; +#endif } template @@ -57,6 +74,12 @@ inline void* slab_t::alloc_slab() { if (heap_ == 0) { item_cell_u*chunk = new item_cell_u[CHUNK_COUNT]; +#ifdef CHECK_WITH_VALGRIND + slab_pool_count += 1; + slab_pool = (item_cell_u **) realloc(slab_pool, + slab_pool_count*sizeof(item_cell_u **)); + slab_pool[slab_pool_count-1] = chunk; +#endif for (unsigned idx = 0 ; idx < CHUNK_COUNT ; idx += 1) { chunk[idx].next = heap_; heap_ = chunk+idx; @@ -77,4 +100,18 @@ inline void slab_t::free_slab(void*ptr) heap_ = cur; } +#ifdef CHECK_WITH_VALGRIND +template +inline void slab_t::delete_pool(void) +{ + for (unsigned idx = 0; idx < slab_pool_count; idx += 1) { + delete [] slab_pool[idx]; + } + free(slab_pool); + slab_pool = NULL; + slab_pool_count = 0; +} +#endif + + #endif /* IVL_slab_H */ diff --git a/vvp/vvp_cleanup.h b/vvp/vvp_cleanup.h index 47cf6c479..99785ca00 100644 --- a/vvp/vvp_cleanup.h +++ b/vvp/vvp_cleanup.h @@ -1,7 +1,7 @@ #ifndef IVL_vvp_cleanup_H #define IVL_vvp_cleanup_H /* - * Copyright (c) 2009-2014 Cary R. (cygcary@yahoo.com) + * Copyright (c) 2009-2015 Cary R. (cygcary@yahoo.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -33,6 +33,7 @@ extern void vpi_mcd_delete(void); extern void load_module_delete(void); extern void modpath_delete(void); extern void root_table_delete(void); +extern void schedule_delete(void); extern void signal_pool_delete(void); extern void simulator_cb_delete(void); extern void udp_defns_delete(void);