Merge in the COMPVBITS branch to the trunk.  This is a big change to
Memcheck, replacing the 9-bits-per-byte shadow memory representation to a
2-bits-per-byte representation (with possibly a little more on the side) by
taking advantage of the fact that extremely few memory bytes are partially
defined.

For the SPEC2k benchmarks with "test" inputs, this speeds up Memcheck by a
(geometric mean) factor of 1.20, and reduces the size of shadow memory by a
(geometric mean) factor of 4.26.

At the same time, Addrcheck is removed.  It hadn't worked for quite some
time, and with these improvements in Memcheck its raisons-d'etre have
shrivelled so much that it's not worth the effort to keep around.  Hooray!

Nb: this code hasn't been tested on PPC.  If things go wrong, look first in
the fast stack-handling functions (eg. mc_new_mem_stack_160,
MC_(helperc_MAKE_STACK_UNINIT)).


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@5791 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/memcheck/mc_include.h b/memcheck/mc_include.h
index 4de9f52..4541749 100644
--- a/memcheck/mc_include.h
+++ b/memcheck/mc_include.h
@@ -29,27 +29,236 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-/* Note: this header should contain declarations that are for use by
-   Memcheck only -- declarations shared with Addrcheck go in mac_shared.h.
-*/
-
 #ifndef __MC_INCLUDE_H
 #define __MC_INCLUDE_H
 
-#include "mac_shared.h"
-
 #define MC_(str)    VGAPPEND(vgMemCheck_,str)
 
 /*------------------------------------------------------------*/
-/*--- Command line options                                 ---*/
+/*--- Tracking the heap                                    ---*/
 /*------------------------------------------------------------*/
 
-/* There are no memcheck-specific ones, only mac-specific
-   ones (those shared by both memcheck and addrcheck). */
+/* We want at least a 16B redzone on client heap blocks for Memcheck */
+#define MC_MALLOC_REDZONE_SZB    16
+
+/* For malloc()/new/new[] vs. free()/delete/delete[] mismatch checking. */
+typedef
+   enum {
+      MC_AllocMalloc = 0,
+      MC_AllocNew    = 1,
+      MC_AllocNewVec = 2,
+      MC_AllocCustom = 3
+   }
+   MC_AllocKind;
+   
+/* Nb: first two fields must match core's VgHashNode. */
+typedef
+   struct _MC_Chunk {
+      struct _MC_Chunk* next;
+      Addr         data;            // ptr to actual block
+      SizeT        size : (sizeof(UWord)*8)-2; // size requested; 30 or 62 bits
+      MC_AllocKind allockind : 2;   // which wrapper did the allocation
+      ExeContext*  where;           // where it was allocated
+   }
+   MC_Chunk;
+
+/* Memory pool.  Nb: first two fields must match core's VgHashNode. */
+typedef
+   struct _MC_Mempool {
+      struct _MC_Mempool* next;
+      Addr          pool;           // pool identifier
+      SizeT         rzB;            // pool red-zone size
+      Bool          is_zeroed;      // allocations from this pool are zeroed
+      VgHashTable   chunks;         // chunks associated with this pool
+   }
+   MC_Mempool;
+
+
+extern void* MC_(new_block)  ( ThreadId tid,
+                               Addr p, SizeT size, SizeT align, UInt rzB,
+                               Bool is_zeroed, MC_AllocKind kind,
+                               VgHashTable table);
+extern void MC_(handle_free) ( ThreadId tid,
+                                Addr p, UInt rzB, MC_AllocKind kind );
+
+extern void MC_(create_mempool)  ( Addr pool, UInt rzB, Bool is_zeroed );
+extern void MC_(destroy_mempool) ( Addr pool );
+extern void MC_(mempool_alloc)   ( ThreadId tid, Addr pool,
+                                   Addr addr, SizeT size );
+extern void MC_(mempool_free)    ( Addr pool, Addr addr );
+
+extern MC_Chunk* MC_(get_freed_list_head)( void );
+
+/* For tracking malloc'd blocks */
+extern VgHashTable MC_(malloc_list);
+
+/* For tracking memory pools. */
+extern VgHashTable MC_(mempool_list);
+
+/* Shadow memory functions */
+extern Bool MC_(check_noaccess)( Addr a, SizeT len, Addr* bad_addr );
+extern void MC_(make_noaccess) ( Addr a, SizeT len );
+extern void MC_(make_writable) ( Addr a, SizeT len );
+extern void MC_(make_readable) ( Addr a, SizeT len );
+extern void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len );
+
+extern void MC_(print_malloc_stats) ( void );
+
+extern void* MC_(malloc)               ( ThreadId tid, SizeT n );
+extern void* MC_(__builtin_new)        ( ThreadId tid, SizeT n );
+extern void* MC_(__builtin_vec_new)    ( ThreadId tid, SizeT n );
+extern void* MC_(memalign)             ( ThreadId tid, SizeT align, SizeT n );
+extern void* MC_(calloc)               ( ThreadId tid, SizeT nmemb, SizeT size1 );
+extern void  MC_(free)                 ( ThreadId tid, void* p );
+extern void  MC_(__builtin_delete)     ( ThreadId tid, void* p );
+extern void  MC_(__builtin_vec_delete) ( ThreadId tid, void* p );
+extern void* MC_(realloc)              ( ThreadId tid, void* p, SizeT new_size );
 
 
 /*------------------------------------------------------------*/
-/*--- Functions                                            ---*/
+/*--- Errors and suppressions                              ---*/
+/*------------------------------------------------------------*/
+
+/* Extra info for overlap errors */
+typedef
+   struct {
+      Addr src;
+      Addr dst;
+      Int  len;   // -1 if unused
+   }
+   OverlapExtra;
+
+extern void MC_(record_free_error)            ( ThreadId tid, Addr a ); 
+extern void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a );
+extern void MC_(record_freemismatch_error)    ( ThreadId tid, Addr a,
+                                                MC_Chunk* mc );
+extern Bool MC_(record_leak_error)            ( ThreadId tid, 
+                                                void* leak_extra,
+                                                ExeContext* where,
+                                                Bool print_record );
+
+/*------------------------------------------------------------*/
+/*--- Profiling of memory events                           ---*/
+/*------------------------------------------------------------*/
+
+/* Define to collect detailed performance info. */
+/* #define MC_PROFILE_MEMORY */
+
+#ifdef MC_PROFILE_MEMORY
+#  define N_PROF_EVENTS 500
+
+extern UInt   MC_(event_ctr)[N_PROF_EVENTS];
+extern HChar* MC_(event_ctr_name)[N_PROF_EVENTS];
+
+#  define PROF_EVENT(ev, name)                                \
+   do { tl_assert((ev) >= 0 && (ev) < N_PROF_EVENTS);         \
+        /* crude and inaccurate check to ensure the same */   \
+        /* event isn't being used with > 1 name */            \
+        if (MC_(event_ctr_name)[ev])                         \
+           tl_assert(name == MC_(event_ctr_name)[ev]);       \
+        MC_(event_ctr)[ev]++;                                \
+        MC_(event_ctr_name)[ev] = (name);                    \
+   } while (False);
+
+#else
+
+#  define PROF_EVENT(ev, name) /* */
+
+#endif   /* MC_PROFILE_MEMORY */
+
+
+/*------------------------------------------------------------*/
+/*--- V and A bits (Victoria & Albert ?)                   ---*/
+/*------------------------------------------------------------*/
+
+/* The number of entries in the primary map can be altered.  However
+   we hardwire the assumption that each secondary map covers precisely
+   64k of address space. */
+#define SM_SIZE 65536            /* DO NOT CHANGE */
+#define SM_MASK (SM_SIZE-1)      /* DO NOT CHANGE */
+
+#define V_BIT_DEFINED         0
+#define V_BIT_UNDEFINED       1
+
+#define V_BITS8_DEFINED       0
+#define V_BITS8_UNDEFINED     0xFF
+
+#define V_BITS16_DEFINED      0
+#define V_BITS16_UNDEFINED    0xFFFF
+
+#define V_BITS32_DEFINED      0
+#define V_BITS32_UNDEFINED    0xFFFFFFFF
+
+#define V_BITS64_DEFINED      0ULL
+#define V_BITS64_UNDEFINED    0xFFFFFFFFFFFFFFFFULL
+
+
+/*------------------------------------------------------------*/
+/*--- Leak checking                                        ---*/
+/*------------------------------------------------------------*/
+
+/* For VALGRIND_COUNT_LEAKS client request */
+extern SizeT MC_(bytes_leaked);
+extern SizeT MC_(bytes_indirect);
+extern SizeT MC_(bytes_dubious);
+extern SizeT MC_(bytes_reachable);
+extern SizeT MC_(bytes_suppressed);
+
+/* For leak checking */
+extern void MC_(pp_LeakError)(void* extra);
+                           
+typedef
+   enum {
+      LC_Off,
+      LC_Summary,
+      LC_Full,
+   }
+   LeakCheckMode;
+
+extern void MC_(do_detect_memory_leaks) (
+          ThreadId tid, LeakCheckMode mode,
+          Bool (*is_within_valid_secondary) ( Addr ),
+          Bool (*is_valid_aligned_word)     ( Addr )
+       );
+
+/*------------------------------------------------------------*/
+/*--- Command line options + defaults                      ---*/
+/*------------------------------------------------------------*/
+
+/* Allow loads from partially-valid addresses?  default: YES */
+extern Bool MC_(clo_partial_loads_ok);
+
+/* Max volume of the freed blocks queue. */
+extern Int MC_(clo_freelist_vol);
+
+/* Do leak check at exit?  default: NO */
+extern LeakCheckMode MC_(clo_leak_check);
+
+/* How closely should we compare ExeContexts in leak records? default: 2 */
+extern VgRes MC_(clo_leak_resolution);
+
+/* In leak check, show reachable-but-not-freed blocks?  default: NO */
+extern Bool MC_(clo_show_reachable);
+
+/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
+ * default: NO */
+extern Bool MC_(clo_workaround_gcc296_bugs);
+
+/* Do undefined value checking? "No" gives Addrcheck-style behaviour, ie.
+ * faster but fewer errors found.  Note that although Addrcheck had 1 bit
+ * per byte overhead vs the old Memcheck's 9 bits per byte, with this mode
+ * and compressed V bits, no memory is saved with this mode -- it's still
+ * 2 bits per byte overhead.  This is a little wasteful -- it could be done
+ * with 1 bit per byte -- but lets us reuse the many shadow memory access
+ * functions.  Note also that in this mode the secondary V bit table is
+ * never used.
+ *
+ * default: YES */
+extern Bool MC_(clo_undef_value_errors);
+
+
+/*------------------------------------------------------------*/
+/*--- Instrumentation                                      ---*/
 /*------------------------------------------------------------*/
 
 /* Functions defined in mc_main.c */
@@ -59,21 +268,21 @@
 extern void MC_(helperc_value_check1_fail) ( void );
 extern void MC_(helperc_value_check0_fail) ( void );
 
-extern VG_REGPARM(1) void MC_(helperc_STOREV8be) ( Addr, ULong );
-extern VG_REGPARM(1) void MC_(helperc_STOREV8le) ( Addr, ULong );
-extern VG_REGPARM(2) void MC_(helperc_STOREV4be) ( Addr, UWord );
-extern VG_REGPARM(2) void MC_(helperc_STOREV4le) ( Addr, UWord );
-extern VG_REGPARM(2) void MC_(helperc_STOREV2be) ( Addr, UWord );
-extern VG_REGPARM(2) void MC_(helperc_STOREV2le) ( Addr, UWord );
-extern VG_REGPARM(2) void MC_(helperc_STOREV1)   ( Addr, UWord );
+extern VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr, ULong );
+extern VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr, ULong );
+extern VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr, UWord );
+extern VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr, UWord );
+extern VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr, UWord );
+extern VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr, UWord );
+extern VG_REGPARM(2) void MC_(helperc_STOREV8)   ( Addr, UWord );
 
-extern VG_REGPARM(1) ULong MC_(helperc_LOADV8be) ( Addr );
-extern VG_REGPARM(1) ULong MC_(helperc_LOADV8le) ( Addr );
-extern VG_REGPARM(1) UWord MC_(helperc_LOADV4be) ( Addr );
-extern VG_REGPARM(1) UWord MC_(helperc_LOADV4le) ( Addr );
-extern VG_REGPARM(1) UWord MC_(helperc_LOADV2be) ( Addr );
-extern VG_REGPARM(1) UWord MC_(helperc_LOADV2le) ( Addr );
-extern VG_REGPARM(1) UWord MC_(helperc_LOADV1)   ( Addr );
+extern VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr );
+extern VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr );
+extern VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr );
+extern VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr );
+extern VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr );
+extern VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr );
+extern VG_REGPARM(1) UWord MC_(helperc_LOADV8)    ( Addr );
 
 extern void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len );