From: Nicholas Nethercote Date: Sat, 14 May 2005 21:28:43 +0000 (+0000) Subject: Modularised vg_hashtable.c as m_hashtable. X-Git-Tag: svn/VALGRIND_3_0_0~608 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=357303aebfa984629b4e6bc31cf194675168eff8;p=thirdparty%2Fvalgrind.git Modularised vg_hashtable.c as m_hashtable. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3710 --- diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c index a21cb8512e..0bf049918b 100644 --- a/cachegrind/cg_main.c +++ b/cachegrind/cg_main.c @@ -30,6 +30,7 @@ */ #include "tool.h" +#include "pub_tool_hashtable.h" #include "pub_tool_mallocfree.h" #include "pub_tool_tooliface.h" diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am index 8c51c7ea23..a0b1596cc8 100644 --- a/coregrind/Makefile.am +++ b/coregrind/Makefile.am @@ -45,6 +45,7 @@ noinst_HEADERS = \ pub_core_dispatch_asm.h \ pub_core_errormgr.h \ pub_core_execontext.h \ + pub_core_hashtable.h \ pub_core_mallocfree.h \ pub_core_replacemalloc.h\ pub_core_sigframe.h \ @@ -76,6 +77,7 @@ stage2_SOURCES = \ m_debuglog.c \ m_errormgr.c \ m_execontext.c \ + m_hashtable.c \ m_mallocfree.c \ m_skiplist.c \ m_stacktrace.c \ @@ -85,7 +87,6 @@ stage2_SOURCES = \ ume.c \ \ vg_scheduler.c \ - vg_hashtable.c \ vg_main.c \ vg_messages.c \ vg_mylibc.c \ diff --git a/coregrind/vg_hashtable.c b/coregrind/m_hashtable.c similarity index 96% rename from coregrind/vg_hashtable.c rename to coregrind/m_hashtable.c index 7f070df7a6..9e19dd99bb 100644 --- a/coregrind/vg_hashtable.c +++ b/coregrind/m_hashtable.c @@ -1,6 +1,6 @@ /*--------------------------------------------------------------------*/ -/*--- A separately chained hash table. vg_hashtable.c ---*/ +/*--- A separately chained hash table. m_hashtable.c ---*/ /*--------------------------------------------------------------------*/ /* @@ -29,6 +29,7 @@ */ #include "core.h" +#include "pub_core_hashtable.h" /*--------------------------------------------------------------------*/ /*--- Declarations ---*/ @@ -176,5 +177,5 @@ void VG_(HT_destruct)(VgHashTable table) } /*--------------------------------------------------------------------*/ -/*--- end vg_hashtable.c ---*/ +/*--- end ---*/ /*--------------------------------------------------------------------*/ diff --git a/coregrind/pub_core_hashtable.h b/coregrind/pub_core_hashtable.h new file mode 100644 index 0000000000..46ede3ce36 --- /dev/null +++ b/coregrind/pub_core_hashtable.h @@ -0,0 +1,48 @@ + +/*--------------------------------------------------------------------*/ +/*--- A separately-chained hash table. pub_core_hashtable.h ---*/ +/*--------------------------------------------------------------------*/ + +/* + This file is part of Valgrind, a dynamic binary instrumentation + framework. + + Copyright (C) 2000-2005 Julian Seward + jseward@acm.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_CORE_HASHTABLE_H +#define __PUB_CORE_HASHTABLE_H + +//-------------------------------------------------------------------- +// PURPOSE: A generic data structure with fairly fast lookup for not too +// many elements, eg. up to a few thousand. +//-------------------------------------------------------------------- + +#include "pub_tool_hashtable.h" + +// No core-only exports; everything in this module is visible to both +// the core and tools. + +#endif // __PUB_CORE_HASHTABLE_H + +/*--------------------------------------------------------------------*/ +/*--- end ---*/ +/*--------------------------------------------------------------------*/ diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c index 67caa2e7fd..314afb35d0 100644 --- a/helgrind/hg_main.c +++ b/helgrind/hg_main.c @@ -30,6 +30,7 @@ */ #include "tool.h" +#include "pub_tool_hashtable.h" #include "pub_tool_mallocfree.h" #include "pub_tool_replacemalloc.h" #include "pub_tool_tooliface.h" diff --git a/include/Makefile.am b/include/Makefile.am index 622e8fb4fa..9119c44893 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -14,6 +14,7 @@ incinc_HEADERS = \ tool_asm.h \ pub_tool_errormgr.h \ pub_tool_execontext.h \ + pub_tool_hashtable.h \ pub_tool_mallocfree.h \ pub_tool_replacemalloc.h\ pub_tool_skiplist.h \ diff --git a/include/pub_tool_hashtable.h b/include/pub_tool_hashtable.h new file mode 100644 index 0000000000..d53f0d860d --- /dev/null +++ b/include/pub_tool_hashtable.h @@ -0,0 +1,98 @@ + +/*--------------------------------------------------------------------*/ +/*--- A hash table implementation. pub_tool_hashtable.h ---*/ +/*--------------------------------------------------------------------*/ + +/* + This file is part of Valgrind, a dynamic binary instrumentation + framework. + + Copyright (C) 2000-2005 Julian Seward + jseward@acm.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 type: +// - Table is fixed-size. +// - 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 +// I'd like. + +typedef + struct _VgHashNode { + struct _VgHashNode * next; + UWord key; + } + VgHashNode; + +typedef + VgHashNode** + VgHashTable; + +/* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed + * with VG_(free)(). */ +extern VgHashTable VG_(HT_construct) ( void ); + +/* 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, VgHashNode* node ); + +/* Looks up a node in the hash table. Also returns the address of the + previous node's `next' pointer which allows it to be removed from the + list later without having to look it up again. */ +extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UWord key, + /*OUT*/VgHashNode*** next_ptr ); + +/* Allocates an array of pointers to all the shadow chunks of malloc'd + blocks. Must be freed with VG_(free)(). */ +extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows ); + +/* Returns first node that matches predicate `p', or NULL if none do. + Extra arguments can be implicitly passed to `p' using `d' which is an + opaque pointer passed to `p' each time it is called. */ +extern VgHashNode* VG_(HT_first_match) ( VgHashTable t, + Bool (*p)(VgHashNode*, void*), + void* d ); + +/* Applies a function f() once to each node. Again, `d' can be used + to pass extra information to the function. */ +extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, + void (*f)(VgHashNode*, void*), + void* d ); + +/* Destroy a table. */ +extern void VG_(HT_destruct) ( VgHashTable t ); + + +#endif // __PUB_TOOL_HASHTABLE_H + +/*--------------------------------------------------------------------*/ +/*--- end ---*/ +/*--------------------------------------------------------------------*/ diff --git a/include/tool.h b/include/tool.h index 98afc8d177..0af508d10f 100644 --- a/include/tool.h +++ b/include/tool.h @@ -578,62 +578,6 @@ typedef extern VgSectKind VG_(seg_sect_kind)(Addr); -/*====================================================================*/ -/*=== Generic hash table ===*/ -/*====================================================================*/ - -/* 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. */ -typedef - struct _VgHashNode { - struct _VgHashNode * next; - UWord key; - } - VgHashNode; - -typedef - VgHashNode** - VgHashTable; - -/* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed - * with VG_(free)(). */ -extern VgHashTable VG_(HT_construct) ( void ); - -/* 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, VgHashNode* node ); - -/* Looks up a node in the hash table. Also returns the address of the - previous node's `next' pointer which allows it to be removed from the - list later without having to look it up again. */ -extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UWord key, - /*OUT*/VgHashNode*** next_ptr ); - -/* Allocates an array of pointers to all the shadow chunks of malloc'd - blocks. Must be freed with VG_(free)(). */ -extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows ); - -/* Returns first node that matches predicate `p', or NULL if none do. - Extra arguments can be implicitly passed to `p' using `d' which is an - opaque pointer passed to `p' each time it is called. */ -extern VgHashNode* VG_(HT_first_match) ( VgHashTable t, - Bool (*p)(VgHashNode*, void*), - void* d ); - -/* Applies a function f() once to each node. Again, `d' can be used - to pass extra information to the function. */ -extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, - void (*f)(VgHashNode*, void*), - void* d ); - -/* Destroy a table. */ -extern void VG_(HT_destruct) ( VgHashTable t ); - - /*====================================================================*/ /*=== Functions for shadow registers ===*/ /*====================================================================*/ diff --git a/massif/ms_main.c b/massif/ms_main.c index 55be3c2ac0..6c1fc67044 100644 --- a/massif/ms_main.c +++ b/massif/ms_main.c @@ -35,6 +35,7 @@ // structures below for more info on how things work. #include "tool.h" +#include "pub_tool_hashtable.h" #include "pub_tool_mallocfree.h" #include "pub_tool_replacemalloc.h" #include "pub_tool_stacktrace.h" diff --git a/memcheck/mac_shared.h b/memcheck/mac_shared.h index 186021d4dc..9a56bc39fd 100644 --- a/memcheck/mac_shared.h +++ b/memcheck/mac_shared.h @@ -37,6 +37,7 @@ #define __MAC_SHARED_H #include "tool.h" +#include "pub_tool_hashtable.h" #include "pub_tool_mallocfree.h" #include "pub_tool_replacemalloc.h" #include "pub_tool_tooliface.h"