Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VDB-5087: bitmagick patch #27

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
695 changes: 502 additions & 193 deletions tools/tax/src/bm/bm.h

Large diffs are not rendered by default.

627 changes: 410 additions & 217 deletions tools/tax/src/bm/bmaggregator.h

Large diffs are not rendered by default.

36 changes: 0 additions & 36 deletions tools/tax/src/bm/bmalgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,43 +281,7 @@ int for_each_bit_range(const BV& bv,
#undef BM_SCANNER_OP


/// functor-adaptor for C-style callbacks
///
/// @internal
///
template <class VCBT, class size_type>
struct bit_visitor_callback_adaptor
{
typedef VCBT bit_visitor_callback_type;

bit_visitor_callback_adaptor(void* h, bit_visitor_callback_type cb_func)
: handle_(h), func_(cb_func)
{}

int add_bits(size_type offset, const unsigned char* bits, unsigned size)
{
for (unsigned i = 0; i < size; ++i)
{
int ret = func_(handle_, offset + bits[i]);
if (ret < 0)
return ret;
}
return 0;
}
int add_range(size_type offset, size_type size)
{
for (size_type i = 0; i < size; ++i)
{
int ret = func_(handle_, offset + i);
if (ret < 0)
return ret;
}
return 0;
}

void* handle_;
bit_visitor_callback_type func_;
};


/// Functor for bit-copy (for testing)
Expand Down
66 changes: 65 additions & 1 deletion tools/tax/src/bm/bmalgo_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ distance_metric operation2metric(set_operation op) BMNOEXCEPT
\brief Distance metric descriptor, holds metric code and result.
\sa distance_operation
*/

struct distance_metric_descriptor
{
#ifdef BM64ADDR
Expand Down Expand Up @@ -113,6 +112,71 @@ struct distance_metric_descriptor
}
};

/// functor-adaptor for C-style callbacks
///
/// @internal
///
template <class VCBT, class size_type>
struct bit_visitor_callback_adaptor
{
typedef VCBT bit_visitor_callback_type;

bit_visitor_callback_adaptor(void* h, bit_visitor_callback_type cb_func)
: handle_(h), func_(cb_func)
{}

int add_bits(size_type offset, const unsigned char* bits, unsigned size)
{
for (unsigned i = 0; i < size; ++i)
{
int ret = func_(handle_, offset + bits[i]);
if (ret < 0)
return ret;
}
return 0;
}
int add_range(size_type offset, size_type size)
{
for (size_type i = 0; i < size; ++i)
{
int ret = func_(handle_, offset + i);
if (ret < 0)
return ret;
}
return 0;
}

void* handle_;
bit_visitor_callback_type func_;
};

/// functor-adaptor for back-inserter
///
/// @internal
///
template <class BII, class size_type>
struct bit_visitor_back_inserter_adaptor
{

bit_visitor_back_inserter_adaptor(BII bi)
: bi_(bi)
{}

int add_bits(size_type offset, const unsigned char* bits, unsigned size)
{
for (unsigned i = 0; i < size; ++i)
*bi_ = offset + bits[i];
return 0;
}
int add_range(size_type offset, size_type size)
{
for (size_type i = 0; i < size; ++i)
*bi_ = offset + i;
return 0;
}

BII bi_;
};


/*!
Expand Down
50 changes: 42 additions & 8 deletions tools/tax/src/bm/bmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,16 @@ class ptr_allocator
*/
static void* allocate(size_t n, const void *)
{
void* ptr = ::malloc(n * sizeof(void*));
void* ptr;
#if defined(BM_ALLOC_ALIGN)
#ifdef _MSC_VER
ptr = (bm::word_t*) ::_aligned_malloc(n * sizeof(void*), BM_ALLOC_ALIGN);
#else
ptr = (bm::word_t*) ::_mm_malloc(n * sizeof(void*), BM_ALLOC_ALIGN);
#endif
#else
ptr = (bm::word_t*) ::malloc(n * sizeof(void*));
#endif
if (!ptr)
throw std::bad_alloc();
return ptr;
Expand All @@ -127,7 +136,15 @@ class ptr_allocator
*/
static void deallocate(void* p, size_t) BMNOEXCEPT
{
#ifdef BM_ALLOC_ALIGN
# ifdef _MSC_VER
::_aligned_free(p);
#else
::_mm_free(p);
# endif
#else
::free(p);
#endif
}
};

Expand Down Expand Up @@ -175,6 +192,11 @@ class pointer_pool_array
return 0;
return pool_ptr_[--size_];
}

/// return stack size
///
unsigned size() const BMNOEXCEPT { return size_; }

private:
void allocate_pool(size_t pool_size)
{
Expand All @@ -190,7 +212,7 @@ class pointer_pool_array
}
private:
void** pool_ptr_; ///< array of pointers in the pool
unsigned size_; ///< current size
unsigned size_; ///< current size
};

/**
Expand All @@ -206,10 +228,10 @@ class alloc_pool
public:

alloc_pool() {}
~alloc_pool()
{
free_pools();
}
~alloc_pool() { free_pools(); }

void set_block_limit(size_t limit) BMNOEXCEPT
{ block_limit_ = limit; }

bm::word_t* alloc_bit_block()
{
Expand All @@ -222,6 +244,14 @@ class alloc_pool
void free_bit_block(bm::word_t* block) BMNOEXCEPT
{
BM_ASSERT(IS_VALID_ADDR(block));
if (block_limit_) // soft limit set
{
if (block_pool_.size() >= block_limit_)
{
block_alloc_.deallocate(block, bm::set_block_size);
return;
}
}
if (!block_pool_.push(block))
block_alloc_.deallocate(block, bm::set_block_size);
}
Expand All @@ -237,9 +267,14 @@ class alloc_pool
} while (block);
}

/// return stack size
///
unsigned size() const BMNOEXCEPT { return block_pool_.size(); }

protected:
pointer_pool_array block_pool_;
BA block_alloc_;
size_t block_limit_ = 0; ///< soft limit for the pool of blocks
};


Expand Down Expand Up @@ -322,7 +357,7 @@ class mem_alloc

/*! @brief Frees bit block allocated by alloc_bit_block.
*/
void free_bit_block(bm::word_t* block, unsigned alloc_factor = 1) BMNOEXCEPT
void free_bit_block(bm::word_t* block, size_t alloc_factor = 1) BMNOEXCEPT
{
BM_ASSERT(IS_VALID_ADDR(block));
if (alloc_pool_p_ && alloc_factor == 1)
Expand Down Expand Up @@ -443,7 +478,6 @@ void aligned_free(void* ptr) BMNOEXCEPT



#undef BM_ALLOC_ALIGN

} // namespace bm

Expand Down
Loading