]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Changed m_hashtable.c to allow the size of the hash table to be specified
authorNicholas Nethercote <njn@valgrind.org>
Sun, 3 Jul 2005 17:53:11 +0000 (17:53 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Sun, 3 Jul 2005 17:53:11 +0000 (17:53 +0000)
when it is created.  Fortunately this didn't affect code outside this
module except for the calls to VG_(HT_construct)().

As a result, we save some memory because not all tables have to be as big
as the ones needed for malloc/free tracking.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4100

cachegrind/cg_main.c
coregrind/m_hashtable.c
helgrind/hg_main.c
include/pub_tool_hashtable.h
massif/ms_main.c
memcheck/mac_malloc_wrappers.c
memcheck/mac_shared.c

index 06dd4c9f428e1d1cf863527655e1161e938ff66f..d9f4c9f3284b405e9b9e93390883cbb019fff009 100644 (file)
@@ -118,7 +118,7 @@ struct _fileCC {
 static fileCC *CC_table[N_FILE_ENTRIES];
 
 //------------------------------------------------------------
-// Primary data structre #2: Instr-info table
+// Primary data structure #2: Instr-info table
 // - Holds the cached info about each instr that is used for simulation.
 // - table(BB_start_addr, list(instr_info))
 // - For each BB, each instr_info in the list holds info about the
@@ -1160,11 +1160,11 @@ static void cg_pre_clo_init(void)
    VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
                 base_dir, VG_(getpid)());
 
-   instr_info_table = VG_(HT_construct)();
+   instr_info_table = VG_(HT_construct)( 4999 );   // prime, biggish
 }
 
 VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
 
 /*--------------------------------------------------------------------*/
-/*--- end                                                cg_main.c ---*/
+/*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
index 4e6a59924b53db28a95c3f89731c3b09b2aa9bc0..9c925fb1ae27931cbf3b53862641c44279a4ab9b 100644 (file)
@@ -1,6 +1,6 @@
 
 /*--------------------------------------------------------------------*/
-/*--- A separately chained hash table.               m_hashtable.c ---*/
+/*--- A separately-chained hash table.               m_hashtable.c ---*/
 /*--------------------------------------------------------------------*/
 
 /*
 /*--- Declarations                                                 ---*/
 /*--------------------------------------------------------------------*/
 
-/* Holds malloc'd but not freed blocks.  Static, so zero-inited by default. */
-
-//#define VG_N_CHAINS 80021 /*4999*/ /* a prime number */
-
 #define CHAIN_NO(key,tbl) (((UWord)(key)) % tbl->n_chains)
 
-struct {
+struct _VgHashTable {
    UInt        n_chains;     // should be prime
    VgHashNode* chains[0];
-}
-_VgHashTable;
-
-typedef (struct _VgHashTable *) VgHashTable;
+};
 
 /*--------------------------------------------------------------------*/
 /*--- Functions                                                    ---*/
@@ -58,9 +51,10 @@ typedef (struct _VgHashTable *) VgHashTable;
 VgHashTable VG_(HT_construct)(UInt n_chains)
 {
    /* Initialises to zero, ie. all entries NULL */
-   VgHashTable* table = 
-      VG_(calloc)(sizeof(struct _VgHashTable) + n_chains*sizeof(VgHashNode*));
+   SizeT sz = sizeof(struct _VgHashTable) + n_chains*sizeof(VgHashNode*);
+   VgHashTable table = VG_(calloc)(1, sz);
    table->n_chains = n_chains;
+   return table;
 }
 
 Int VG_(HT_count_nodes) ( VgHashTable table )
@@ -75,12 +69,13 @@ Int VG_(HT_count_nodes) ( VgHashTable table )
    return n;
 }
 
-/* Puts a new, heap allocated VgHashNode, into the malloclist. */
+/* Puts a new, heap allocated VgHashNode, into the VgHashTable.  Prepends
+   the node to the appropriate chain. */
 void VG_(HT_add_node) ( VgHashTable table, VgHashNode* node )
 {
-   UInt chain   = CHAIN_NO(node->key);
-   node->next   = table->chains[chain];
-   table[chain] = node;
+   UInt chain           = CHAIN_NO(node->key, table);
+   node->next           = table->chains[chain];
+   table->chains[chain] = node;
 }
 
 /* Looks up a VgHashNode in the table.  Also returns the address of
@@ -92,7 +87,7 @@ VgHashNode* VG_(HT_get_node) ( VgHashTable table, UWord key,
    VgHashNode *prev, *curr;
    Int       chain;
 
-   chain = CHAIN_NO(key);
+   chain = CHAIN_NO(key, table);
 
    prev = NULL;
    curr = table->chains[chain];
@@ -106,7 +101,7 @@ VgHashNode* VG_(HT_get_node) ( VgHashTable table, UWord key,
    }
 
    if (NULL == prev)
-      *next_ptr = & (table->chain[chain]);
+      *next_ptr = & (table->chains[chain]);
    else
       *next_ptr = & (prev->next);
 
index 2b7bac80d25199efb825eb0339b161f6be1bc9d3..889d1d8aabbcc4c8b356c545b30fb80df49a75d4 100644 (file)
@@ -3446,7 +3446,7 @@ static void hg_pre_clo_init(void)
    }
 
    init_shadow_memory();
-   hg_malloc_list = VG_(HT_construct)();
+   hg_malloc_list = VG_(HT_construct)( 80021 );    // prime, big
 }
 
 /* Uses a 1:1 mapping */
index d53f0d860d2dda768cfcd166f01d82709b8cc36f..6aa7efae55ad0c2f21b450eb1c3eded8db66d857 100644 (file)
@@ -36,8 +36,7 @@
    as the first two fields match the sizes of these two fields.  Requires
    a bit of casting by the tool. */
 
-// Problems with this type:
-// - Table is fixed-size.  
+// Problems with this data structure:
 // - Separate chaining gives bad cache behaviour.  Hash tables with linear
 //   probing give better cache behaviour.
 // - It's not very abstract, eg. deleting nodes exposes more internals than
@@ -50,13 +49,11 @@ typedef
    }
    VgHashNode;
 
-typedef
-   VgHashNode**
-   VgHashTable;
+typedef struct _VgHashTable * VgHashTable;
 
 /* Make a new table.  Allocates the memory with VG_(calloc)(), so can be freed
* with VG_(free)(). */
-extern VgHashTable VG_(HT_construct) ( void );
  with VG_(free)().  n_chains should be prime. */
+extern VgHashTable VG_(HT_construct) ( UInt n_chains );
 
 /* Count the number of nodes in a table. */
 extern Int VG_(HT_count_nodes) ( VgHashTable table );
index 2877d14b0ff161ea57fd0edebba38b169ddeff9d..f436d0f6251392cafd9fa3376eaf34700fb7a747 100644 (file)
@@ -1830,7 +1830,7 @@ static void ms_pre_clo_init()
    VG_(register_profile_event)(VgpPrintXPts,      "print-XPts");
 
    // HP_Chunks
-   malloc_list  = VG_(HT_construct)();
+   malloc_list  = VG_(HT_construct)( 80021 );   // prime, big
 
    // Dummy node at top of the context structure.
    alloc_xpt = new_XPt(0, NULL, /*is_bottom*/False);
index ae7635997e049180a3a7527da452c64684bbafc1..003c6fa79bc308fb11cdfa319d404802e7d338a8 100644 (file)
@@ -441,7 +441,7 @@ void MAC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed)
    mp->pool      = pool;
    mp->rzB       = rzB;
    mp->is_zeroed = is_zeroed;
-   mp->chunks    = VG_(HT_construct)();
+   mp->chunks    = VG_(HT_construct)( 3001 );  // prime, not so big
 
    /* Paranoia ... ensure this area is off-limits to the client, so
       the mp->data field isn't visible to the leak checker.  If memory
index 6c56ca2442436ec97b847b6da23db3669c335b8d..28e85794ffd2f094a9903551f547565123e7ce3c 100644 (file)
@@ -899,8 +899,8 @@ static void done_prof_mem ( void ) { }
 
 void MAC_(common_pre_clo_init)(void)
 {
-   MAC_(malloc_list) = VG_(HT_construct)();
-   MAC_(mempool_list) = VG_(HT_construct)();
+   MAC_(malloc_list)  = VG_(HT_construct)( 80021 );   // prime, big
+   MAC_(mempool_list) = VG_(HT_construct)( 1009  );   // prime, not so big
    init_prof_mem();
 }