ftmemsim-valgrind/include/pub_tool_hashtable.h
Nicholas Nethercote fbe37d96a8 Fix bug #191182, where printing the leak checker results was really slow if
there were a lot of loss records.

The fix was:
- Avoid the O(m * n) looping over the chunks when creating the loss
  records, by putting loss records into an OSet instead of a list, which
  makes duplicate detection for each chunk an O(log n) operation instead of
  an O(n) operation.
- Avoid the looping over loss records which was used to do a poor
  man's sort, but was O(n^2).  Instead copy pointers to the loss records
  from the OSet into an array and sort it normally with VG_(ssort) (n log n,
  usually) before printing.

This approach was similar to that used in the patch Philippe attached to the
bug report.

Other changes:
- Added Philippe's test programs in the new memcheck/perf directory.  It
  used to take 57s on my machine, now it takes 1.6s.
- Cleaned up massif/perf/Makefile.am to be consistent with other Makefiles.
- Improved some comments relating to VgHashTable and OSet.
- Avoided a redundant traversal of the hash table in VG_(HT_to_array), also
  identified by Philippe..
- Made memcheck/tests/mempool's results independent of the pointer size, and
  thus was able to remove its .stderr.exp64 file.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9781
2009-05-06 06:15:55 +00:00

100 lines
3.9 KiB
C

/*--------------------------------------------------------------------*/
/*--- A hash table implementation. pub_tool_hashtable.h ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2005-2009 Nicholas Nethercote
njn@valgrind.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef __PUB_TOOL_HASHTABLE_H
#define __PUB_TOOL_HASHTABLE_H
/* Generic type for a separately-chained hash table. Via a kind of dodgy
C-as-C++ style inheritance, tools can extend the VgHashNode type, so long
as the first two fields match the sizes of these two fields. Requires
a bit of casting by the tool. */
// Problems with this data structure:
// - Separate chaining gives bad cache behaviour. Hash tables with linear
// probing give better cache behaviour.
typedef
struct _VgHashNode {
struct _VgHashNode * next;
UWord key;
}
VgHashNode;
typedef struct _VgHashTable * VgHashTable;
/* Make a new table. Allocates the memory with VG_(calloc)(), so can
be freed with VG_(free)(). The table starts small but will
periodically be expanded. This is transparent to the users of this
module. */
extern VgHashTable VG_(HT_construct) ( HChar* name );
/* Count the number of nodes in a table. */
extern Int VG_(HT_count_nodes) ( VgHashTable table );
/* Add a node to the table. */
extern void VG_(HT_add_node) ( VgHashTable t, void* node );
/* Looks up a VgHashNode in the table. Returns NULL if not found. */
extern void* VG_(HT_lookup) ( VgHashTable table, UWord key );
/* Removes a VgHashNode from the table. Returns NULL if not found. */
extern void* VG_(HT_remove) ( VgHashTable table, UWord key );
/* Allocates a suitably-sized array, copies pointers to all the hashtable
elements into it, then returns both the array and the size of it. The
array must be freed with VG_(free). */
extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_elems );
/* Reset the table's iterator to point to the first element. */
extern void VG_(HT_ResetIter) ( VgHashTable table );
/* Return the element pointed to by the iterator and move on to the
next one. Returns NULL if the last one has been passed, or if
HT_ResetIter() has not been called previously. Asserts if the
table has been modified (HT_add_node, HT_remove) since
HT_ResetIter. This guarantees that callers cannot screw up by
modifying the table whilst iterating over it (and is necessary to
make the implementation safe; specifically we must guarantee that
the table will not get resized whilst iteration is happening.
Since resizing only happens as a result of calling HT_add_node,
disallowing HT_add_node during iteration should give the required
assurance. */
extern void* VG_(HT_Next) ( VgHashTable table );
/* Destroy a table. */
extern void VG_(HT_destruct) ( VgHashTable t );
#endif // __PUB_TOOL_HASHTABLE_H
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/