Commit cef5120a authored by Etienne Renault's avatar Etienne Renault
Browse files

bricks: add bricks for concurrent hashmap

* Makefile.am, README, bricks/brick-assert.h,
bricks/brick-bitlevel.h, bricks/brick-hash.h,
bricks/brick-hashset.h, bricks/brick-shmem.h,
bricks/brick-types.h, configure.ac,
debian/copyright, debian/libspot-dev.install,
m4/bricks.m4, tests/Makefile.am,
tests/core/.gitignore, tests/core/bricks.cc: here.
parent d7e0366d
......@@ -39,6 +39,10 @@ SUBDIRS = picosat buddy lib ltdl spot bin tests $(PYTHON_SUBDIR) $(DOC_SUBDIR) \
UTF8 = utf8/README.md utf8/utf8.h \
utf8/utf8/checked.h utf8/utf8/core.h utf8/utf8/unchecked.h
nobase_include_HEADERS= bricks/brick-assert.h bricks/brick-bitlevel.h \
bricks/brick-hash.h bricks/brick-hashset.h bricks/brick-shmem.h \
bricks/brick-types.h
DEBIAN = \
debian/changelog \
debian/changelog.in \
......
......@@ -229,6 +229,7 @@ Third party software
--------------------
buddy/ A customized version of BuDDy 2.3 (a BDD library).
bricks/ A collection of useful C++ code provided by DiVinE 3.3.2
ltdl/ Libtool's portable dlopen() wrapper library.
lib/ Gnulib's portability modules.
utf8/ Nemanja Trifunovic's utf-8 routines.
......
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
* Various assert macros based on C++ exceptions and their support code.
*/
/*
* (c) 2006-2014 Petr Ročkai <me@mornfall.net>
*/
/* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. */
#include <exception>
#include <string>
#include <sstream>
#ifdef __divine__
#include <divine.h>
#endif
#ifndef TEST
#define TEST(n) void n()
#define TEST_FAILING(n) void n()
#endif
#ifdef __divine__
#define ASSERT(x) assert( x )
#define ASSERT_PRED(p, x) assert( p( x ) )
#define ASSERT_EQ(x, y) assert( (x) == (y) )
#define ASSERT_LEQ(x, y) assert( (x) <= (y) )
#define ASSERT_NEQ(x, y) assert ( (x) != (y) )
#define ASSERT_EQ_IDX(i, x, y) assert( (x) == (y) )
#elif !defined NDEBUG
#define ASSERT(x) ::brick::_assert::assert_fn( BRICK_LOCWRAP( BRICK_LOCATION( #x ) ), x )
#define ASSERT_PRED(p, x) ::brick::_assert::assert_pred_fn( BRICK_LOCWRAP( BRICK_LOCATION( #p "( " #x " )" ) ), x, p( x ) )
#define ASSERT_EQ(x, y) ::brick::_assert::assert_eq_fn( BRICK_LOCWRAP( BRICK_LOCATION( #x " == " #y ) ), x, y )
#define ASSERT_LEQ(x, y) ::brick::_assert::assert_leq_fn( BRICK_LOCWRAP( BRICK_LOCATION( #x " <= " #y ) ), x, y )
#define ASSERT_NEQ(x, y) ::brick::_assert::assert_neq_fn( BRICK_LOCWRAP( BRICK_LOCATION( #x " != " #y ) ), x, y )
#define ASSERT_EQ_IDX(i, x, y) ::brick::_assert::assert_eq_fn( BRICK_LOCWRAP( BRICK_LOCATION_I( #x " == " #y, i ) ), x, y )
#else
#define ASSERT(x) ((void)0)
#define ASSERT_PRED(p, x) ((void)0)
#define ASSERT_EQ(x, y) ((void)0)
#define ASSERT_LEQ(x, y) ((void)0)
#define ASSERT_NEQ(x, y) ((void)0)
#define ASSERT_EQ_IDX(i, x, y) ((void)0)
#endif
/* you must #include <brick-string.h> to use ASSERT_UNREACHABLE_F */
#define ASSERT_UNREACHABLE_F(...) ::brick::_assert::assert_die_fn( BRICK_LOCATION( brick::string::fmtf(__VA_ARGS__) ) )
#define ASSERT_UNREACHABLE(x) ::brick::_assert::assert_die_fn( BRICK_LOCATION( x ) )
#define ASSERT_UNIMPLEMENTED() ::brick::_assert::assert_die_fn( BRICK_LOCATION( "not imlemented" ) )
#ifdef _MSC_VER
#define UNUSED
#define noexcept
#else
#define UNUSED __attribute__((unused))
#endif
#ifndef BRICK_ASSERT_H
#define BRICK_ASSERT_H
namespace brick {
namespace _assert {
/* discard any number of paramentets, taken as const references */
template< typename... X >
void unused( const X&... ) { }
struct Location {
const char *file;
int line, iteration;
std::string stmt;
Location( const char *f, int l, std::string st, int iter = -1 )
: file( f ), line( l ), iteration( iter ), stmt( st ) {}
};
#define BRICK_LOCATION(stmt) ::brick::_assert::Location( __FILE__, __LINE__, stmt )
#define BRICK_LOCATION_I(stmt, i) ::brick::_assert::Location( __FILE__, __LINE__, stmt, i )
// lazy location construction in C++11
#if __cplusplus >= 201103L
#define BRICK_LOCWRAP(x) [&]{ return (x); }
#define BRICK_LOCUNWRAP(x) (x)()
#else
#define BRICK_LOCWRAP(x) (x)
#define BRICK_LOCUNWRAP(x) (x)
#endif
struct AssertFailed : std::exception {
std::string str;
template< typename X >
friend inline AssertFailed &operator<<( AssertFailed &f, X x )
{
std::stringstream str;
str << x;
f.str += str.str();
return f;
}
AssertFailed( Location l )
{
(*this) << l.file << ": " << l.line;
if ( l.iteration != -1 )
(*this) << " (iteration " << l.iteration << ")";
(*this) << ": assertion `" << l.stmt << "' failed;";
}
const char *what() const noexcept { return str.c_str(); }
};
template< typename Location, typename X >
void assert_fn( Location l, X x )
{
if ( !x ) {
throw AssertFailed( BRICK_LOCUNWRAP( l ) );
}
}
inline void assert_die_fn( Location l ) __attribute__((noreturn));
inline void assert_die_fn( Location l )
{
throw AssertFailed( l );
}
template< typename Location, typename X, typename Y >
void assert_eq_fn( Location l, X x, Y y )
{
if ( !( x == y ) ) {
AssertFailed f( BRICK_LOCUNWRAP( l ) );
f << " got ["
<< x << "] != [" << y
<< "] instead";
throw f;
}
}
template< typename Location, typename X, typename Y >
void assert_leq_fn( Location l, X x, Y y )
{
if ( !( x <= y ) ) {
AssertFailed f( BRICK_LOCUNWRAP( l ) );
f << " got ["
<< x << "] > [" << y
<< "] instead";
throw f;
}
}
template< typename Location, typename X >
void assert_pred_fn( Location l, X x, bool p )
{
if ( !p ) {
AssertFailed f( BRICK_LOCUNWRAP( l ) );
f << " for " << x;
throw f;
}
}
template< typename Location, typename X, typename Y >
void assert_neq_fn( Location l, X x, Y y )
{
if ( x != y )
return;
AssertFailed f( BRICK_LOCUNWRAP( l ) );
f << " got ["
<< x << "] == [" << y << "] instead";
throw f;
}
}
}
#endif
// vim: syntax=cpp tabstop=4 shiftwidth=4 expandtab
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
* Utilities and data structures for bit-level manipulation and data packing.
*/
/*
* (c) 2013-2014 Jiří Weiser <xweiser1@fi.muni.cz>
* (c) 2013 Petr Ročkai <me@mornfall.net>
*/
/* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. */
#include <bricks/brick-assert.h>
#include <type_traits>
#ifdef __linux
#include <asm/byteorder.h>
#include <byteswap.h>
#elif !defined LITTLE_ENDIAN // if defined _WIN32
#define BYTE_ORDER 1234
#define LITTLE_ENDIAN 1234
#endif
#ifndef bswap_64
#define bswap_64 __builtin_bswap64
#endif
#include <atomic>
#include <cstring>
#ifndef BRICK_BITLEVEL_H
#define BRICK_BITLEVEL_H
namespace brick {
namespace bitlevel {
template< typename T1, typename T2 >
constexpr inline T1 align( T1 v, T2 a ) {
return (v % T1(a)) ? (v + T1(a) - (v % T1(a))) : v;
}
template< typename T1, typename T2 >
constexpr inline T1 downalign( T1 v, T2 a ) {
return v - (v % T1(a));
}
namespace compiletime {
template< typename T >
constexpr unsigned MSB( T x ) {
return x > 1 ? 1 + MSB( x >> 1 ) : 0;
}
template< typename T >
constexpr T fill( T x ) {
return x ? x | fill( x >> 1 ) : x;
}
template< typename T >
constexpr size_t sizeOf() {
return std::is_empty< T >::value ? 0 : sizeof( T );
}
}
/*
* Fills `x` by bits up to the most si significant bit.
* Comlexity is O(log n), n is sizeof(x)*8
*/
template< typename number >
static inline number fill( number x ) {
static const unsigned m = sizeof( number ) * 8;
unsigned r = 1;
if ( !x )
return 0;
while ( m != r ) {
x |= x >> r;
r <<= 1;
}
return x;
}
// get index of Most Significant Bit
// templated by argument to int, long, long long (all unsigned)
template< typename T >
static inline unsigned MSB( T x ) {
unsigned position = 0;
while ( x ) {
x >>= 1;
++position;
}
return position - 1;
}
template<>
inline unsigned MSB< unsigned int >( unsigned int x ) {
static const unsigned long bits = sizeof( unsigned int ) * 8 - 1;
return bits - __builtin_clz( x );
}
template<>
inline unsigned MSB< unsigned long >( unsigned long x ) {
static const unsigned bits = sizeof( unsigned long ) * 8 - 1;
return bits - __builtin_clzl( x );
}
template<>
inline unsigned MSB< unsigned long long >( unsigned long long x ) {
static const unsigned bits = sizeof( unsigned long long ) * 8 - 1;
return bits - __builtin_clzll( x );
}
// gets only Most Significant Bit
template< typename number >
static inline number onlyMSB( number x ) {
return number(1) << MSB( x );
}
// gets number without Most Significant Bit
template< typename number >
static inline number withoutMSB( number x ) {
return x & ~onlyMSB( x );
}
inline uint64_t bitshift( uint64_t t, int shift ) {
#if BYTE_ORDER == LITTLE_ENDIAN
return bswap_64( shift < 0 ? bswap_64( t << -shift ) : bswap_64( t >> shift ) );
#else
return shift < 0 ? ( t << -shift ) : ( t >> shift );
#endif
}
struct BitPointer {
BitPointer() : base( nullptr ), _bitoffset( 0 ) {}
template< typename T > BitPointer( T *t, int offset = 0 )
: base( static_cast< void * >( t ) ), _bitoffset( offset )
{
normalize();
}
uint32_t &word() { ASSERT( valid() ); return *static_cast< uint32_t * >( base ); }
uint64_t &dword() { ASSERT( valid() ); return *static_cast< uint64_t * >( base ); }
void normalize() {
int shift = downalign( _bitoffset, 32 );
_bitoffset -= shift;
ASSERT_EQ( shift % 8, 0 );
base = static_cast< uint32_t * >( base ) + shift / 32;
}
void shift( int bits ) { _bitoffset += bits; normalize(); }
void fromReference( BitPointer r ) { *this = r; }
int bitoffset() { return _bitoffset; }
bool valid() { return base; }
private:
void *base;
int _bitoffset;
};
inline uint64_t mask( int first, int count ) {
return bitshift(uint64_t(-1), -first) & bitshift(uint64_t(-1), (64 - first - count));
}
/*
* NB. This function will alias whatever "to" points to with an uint64_t. With
* aggressive optimisations, this might break code that passes an address of a
* variable of different type. When "to" points to a stack variable, take
* precautions to avoid breaking strict aliasing rules (the violation is not
* detected by GCC as of 4.7.3).
*/
inline void bitcopy( BitPointer from, BitPointer to, int bitcount )
{
while ( bitcount ) {
int w = std::min( 32 - from.bitoffset(), bitcount );
uint32_t fmask = mask( from.bitoffset(), w );
uint64_t tmask = mask( to.bitoffset(), w );
uint64_t bits = bitshift( from.word() & fmask, from.bitoffset() - to.bitoffset() );
ASSERT_EQ( bits & ~tmask, 0u );
ASSERT_EQ( bits & tmask, bits );
if ( to.bitoffset() + bitcount > 32 )
to.dword() = (to.dword() & ~tmask) | bits;
else
to.word() = (to.word() & ~static_cast< uint32_t >( tmask )) | static_cast< uint32_t >( bits );
from.shift( w ); to.shift( w ); bitcount -= w; // slide
}
}
template< typename T, int width = sizeof( T ) * 8 >
struct BitField
{
static const int bitwidth = width;
struct Virtual : BitPointer {
void set( T t ) { bitcopy( BitPointer( &t ), *this, bitwidth ); }
Virtual operator=( T t ) {
set( t );
return *this;
}
Virtual operator=( Virtual v ) {
set( v.get() );
return *this;
}
operator T() const { return get(); }
T get() const {
union U {
uint64_t x;
T t;
U() : t() { }
} u;
bitcopy( *this, BitPointer( &u.x ), bitwidth );
return u.t;
}
Virtual &operator++() {
T value( get() );
set( ++value );
return *this;
}
T operator++(int) {
T value( get() );
T result( value++ );
set( value );
return result;
}
Virtual &operator--() {
T value( get() );
set( --value );
return *this;
}
T operator--(int) {
T value( get() );
T result( value-- );
set( value );
return result;
}
template< typename U >
Virtual operator+=( U value ) {
T t( get() );
t += value;
set( t );
return *this;
}
template< typename U >
Virtual operator-=( U value ) {
T t( get() );
t -= value;
set( t );
return *this;
}
template< typename U >
Virtual operator*=( U value ) {
T t( get() );
t *= value;
set( t );
return *this;
}
template< typename U >
Virtual operator/=( U value ) {
T t( get() );
t /= value;
set( t );
return *this;
}
template< typename U >
Virtual operator%=( U value ) {
T t( get() );
t %= value;
set( t );
return *this;
}
};
};
struct BitLock
{
static const int bitwidth = 1;
struct Virtual : BitPointer {
using Atomic = std::atomic< uint32_t >;
Atomic &atomic() { return *reinterpret_cast< Atomic * >( &word() ); }
uint32_t bit() {
ASSERT_LEQ( bitoffset(), 31 );
return uint32_t( 1 ) << bitoffset();
}
void lock() {
uint32_t l = word();
do { l &= ~bit(); } while ( !atomic().compare_exchange_weak( l, l | bit() ) );
}
void unlock() { atomic().exchange( word() & ~bit() ); }
bool locked() { return atomic().load() & bit(); }
};
};
template< int O, typename... Args > struct BitAccess;
template< int O >
struct BitAccess< O > { static const int total = 0; };
template< int O, typename T, typename... Args >
struct BitAccess< O, T, Args... > {
static const int offset = O;
static const int width = T::bitwidth;
typedef typename T::Virtual Head;
typedef BitAccess< offset + T::bitwidth, Args... > Tail;
static const int total = width + Tail::total;
};
template< typename BA, int I >
struct _AccessAt : _AccessAt< typename BA::Tail, I - 1 > {};
template< typename BA >
struct _AccessAt< BA, 0 > { using T = BA; };
template< typename... Args >
struct _BitTuple
{
using Access = BitAccess< 0, Args... >;
static const int bitwidth = Access::total;
template< int I > using AccessAt = _AccessAt< Access, I >;
template< int I > static int offset() { return AccessAt< I >::T::offset; }
};
template< typename... Args > struct BitTuple : _BitTuple< Args... >
{
struct Virtual : BitPointer, _BitTuple< Args... > {};
char storage[ align( Virtual::bitwidth, 32 ) / 8 ];
BitTuple() { std::fill( storage, storage + sizeof( storage ), 0 ); }
operator BitPointer() { return BitPointer( storage ); }
};
template< int I, typename BT >
typename BT::template AccessAt< I >::T::Head get( BT &bt )
{
typename BT::template AccessAt< I >::T::Head t;
t.fromReference( bt );
t.shift( BT::template offset< I >() );
return t;
}
}
}
namespace brick_test {
namespace bitlevel {
using namespace ::brick::bitlevel;
struct BitTupleTest {
using U10 = BitField< unsigned, 10 >;
using T10_10 = BitTuple< U10, U10 >;
int bitcount( uint32_t word ) {
int i = 0;
while ( word ) {
if ( word & 1 )
++i;
word >>= 1;
}
return i;
}
TEST(mask) {
/* only works on little endian machines ... */
ASSERT_EQ( 0xFF00u, bitlevel::mask( 8, 8 ) );
ASSERT_EQ( 0xF000u, bitlevel::mask( 12, 4 ) );
ASSERT_EQ( 0x0F00u, bitlevel::mask( 8, 4 ) );
ASSERT_EQ( 60u, bitlevel::mask( 2, 4 ) );// 0b111100
ASSERT_EQ( 28u, bitlevel::mask( 2, 3 ) );// 0b11100
}
TEST(bitcopy) {
uint32_t a = 42, b = 11;
bitlevel::bitcopy( BitPointer( &a ), BitPointer( &b ), 32 );