Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Make SmallHashTable more widely usable
Browse files Browse the repository at this point in the history
 - Add default key infos for int/unsigned
 - Call compGetMem through a forward-declared wrapper to avoid circular
   dependency problems if Compiler itself (or one of its dependencies)
   uses a SmallHashTable
  • Loading branch information
JosephTremoulet committed May 11, 2017
1 parent 11dea52 commit cda51e4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/jit/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4104,6 +4104,13 @@ inline void* Compiler::compGetMem(size_t sz, CompMemKind cmk)

#endif

// Wrapper for Compiler::compGetMem that can be forward-declared for use in template
// types which Compiler depends on but which need to allocate heap memory.
inline void* compGetMem(Compiler* comp, size_t sz)
{
return comp->compGetMem(sz);
}

/*****************************************************************************
*
* A common memory allocation for arrays of structures involves the
Expand Down
48 changes: 46 additions & 2 deletions src/jit/smallhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#ifndef _SMALLHASHTABLE_H_
#define _SMALLHASHTABLE_H_

// Since compiler depends on valuenum which depends on smallhash, forward declare
// a wrapper for comp->compGetMem here (implemented in compiler.hpp) that can be used below.
class Compiler;
void* compGetMem(Compiler* comp, size_t sz);

// genLog2 is defined in compiler.hpp
unsigned genLog2(unsigned value);

//------------------------------------------------------------------------
// HashTableInfo: a concept that provides equality and hashing methods for
// a particular key type. Used by HashTableBase and its
Expand Down Expand Up @@ -37,6 +45,42 @@ struct HashTableInfo<TKey*>
}
};

//------------------------------------------------------------------------
// HashTableInfo<int>: specialized version of HashTableInfo for int-
// typed keys.
template <>
struct HashTableInfo<int>
{
static bool Equals(int x, int y)
{
return x == y;
}

static unsigned GetHashCode(int key)
{
// Cast and return the key
return static_cast<unsigned>(key);
}
};

//------------------------------------------------------------------------
// HashTableInfo<unsigned>: specialized version of HashTableInfo for unsigned-
// typed keys.
template <>
struct HashTableInfo<unsigned>
{
static bool Equals(unsigned x, unsigned y)
{
return x == y;
}

static unsigned GetHashCode(unsigned key)
{
// Return the key itself
return key;
}
};

//------------------------------------------------------------------------
// HashTableBase: base type for HashTable and SmallHashTable. This class
// provides the vast majority of the implementation. The
Expand Down Expand Up @@ -261,7 +305,7 @@ class HashTableBase
size_t allocSize = sizeof(Bucket) * newNumBuckets;
assert((sizeof(Bucket) * m_numBuckets) < allocSize);

auto* newBuckets = reinterpret_cast<Bucket*>(m_compiler->compGetMem(allocSize));
auto* newBuckets = reinterpret_cast<Bucket*>(compGetMem(m_compiler, allocSize));
memset(newBuckets, 0, allocSize);

for (unsigned currentIndex = 0; currentIndex < m_numBuckets; currentIndex++)
Expand Down Expand Up @@ -558,7 +602,7 @@ class HashTable final : public HashTableBase<TKey, TValue, TKeyInfo>
HashTable(Compiler* compiler, unsigned initialSize)
: TBase(compiler,
reinterpret_cast<typename TBase::Bucket*>(
compiler->compGetMem(RoundUp(initialSize) * sizeof(typename TBase::Bucket))),
compGetMem(compiler, RoundUp(initialSize) * sizeof(typename TBase::Bucket))),
RoundUp(initialSize))
{
}
Expand Down

0 comments on commit cda51e4

Please sign in to comment.