2012-06-11 13:57:03 +01:00
|
|
|
#ifndef BITSET_H
|
|
|
|
#define BITSET_H
|
|
|
|
|
|
|
|
#include "util.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-05-18 13:24:35 +01:00
|
|
|
#include <inttypes.h>
|
2012-05-17 20:14:22 +01:00
|
|
|
#include <string.h>
|
2012-05-20 14:38:46 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
/*
|
|
|
|
* Make the bitfield words 'opaque' to prevent code
|
|
|
|
* poking at the bits directly without using these
|
|
|
|
* accessors/macros
|
|
|
|
*/
|
|
|
|
typedef uint64_t bitfield_word_t;
|
|
|
|
typedef bitfield_word_t * bitfield_p;
|
|
|
|
|
|
|
|
#define BITFIELD_WORD_SIZE sizeof(bitfield_word_t)
|
|
|
|
#define BITS_PER_WORD (BITFIELD_WORD_SIZE * 8)
|
|
|
|
|
|
|
|
#define BIT_MASK(_idx) \
|
|
|
|
(1LL << ((_idx) & (BITS_PER_WORD - 1)))
|
|
|
|
#define BIT_WORD(_b, _idx) \
|
|
|
|
((bitfield_word_t*)(_b))[(_idx) / BITS_PER_WORD]
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
/* Calculates the number of words needed to store _bytes number of bytes
|
|
|
|
* this is added to accommodate code that wants to use bytes sizes
|
|
|
|
*/
|
|
|
|
#define BIT_WORDS_FOR_SIZE(_bytes) \
|
2014-02-27 12:57:09 +00:00
|
|
|
((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE)
|
2014-02-26 15:19:03 +00:00
|
|
|
|
|
|
|
/** Return the bit value ''idx'' in array ''b'' */
|
|
|
|
static inline int bit_get(bitfield_p b, uint64_t idx) {
|
|
|
|
return (BIT_WORD(b, idx) >> (idx & (BITS_PER_WORD-1))) & 1;
|
|
|
|
}
|
2012-05-18 13:24:35 +01:00
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Return 1 if the bit at ''idx'' in array ''b'' is set */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline int bit_is_set(bitfield_p b, uint64_t idx) {
|
|
|
|
return bit_get(b, idx);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Return 1 if the bit at ''idx'' in array ''b'' is clear */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline int bit_is_clear(bitfield_p b, uint64_t idx) {
|
|
|
|
return !bit_get(b, idx);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Tests whether the bit at ''idx'' in array ''b'' has value ''value'' */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value) {
|
|
|
|
return bit_get(b, idx) == !!value;
|
2012-05-18 13:24:35 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Sets the bit ''idx'' in array ''b'' */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline void bit_set(bitfield_p b, uint64_t idx) {
|
|
|
|
BIT_WORD(b, idx) |= BIT_MASK(idx);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Clears the bit ''idx'' in array ''b'' */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline void bit_clear(bitfield_p b, uint64_t idx) {
|
|
|
|
BIT_WORD(b, idx) &= ~BIT_MASK(idx);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Sets ''len'' bits in array ''b'' starting at offset ''from'' */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
|
2013-07-24 11:19:52 +01:00
|
|
|
{
|
2014-02-26 15:19:03 +00:00
|
|
|
for ( ; (from % BITS_PER_WORD) != 0 && len > 0 ; len-- ) {
|
2013-07-24 11:19:52 +01:00
|
|
|
bit_set( b, from++ );
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
if (len >= BITS_PER_WORD) {
|
|
|
|
memset(&BIT_WORD(b, from), 0xff, len / 8 );
|
2013-07-24 11:19:52 +01:00
|
|
|
from += len;
|
2014-02-26 15:19:03 +00:00
|
|
|
len = len % BITS_PER_WORD;
|
2013-07-24 11:19:52 +01:00
|
|
|
from -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; len > 0 ; len-- ) {
|
|
|
|
bit_set( b, from++ );
|
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Clears ''len'' bits in array ''b'' starting at offset ''from'' */
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline void bit_clear_range(bitfield_p b, uint64_t from, uint64_t len)
|
2013-07-24 11:19:52 +01:00
|
|
|
{
|
2014-02-26 15:19:03 +00:00
|
|
|
for ( ; (from % BITS_PER_WORD) != 0 && len > 0 ; len-- ) {
|
2013-07-24 11:19:52 +01:00
|
|
|
bit_clear( b, from++ );
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
if (len >= BITS_PER_WORD) {
|
|
|
|
memset(&BIT_WORD(b, from), 0, len / 8 );
|
2013-07-24 11:19:52 +01:00
|
|
|
from += len;
|
2014-02-26 15:19:03 +00:00
|
|
|
len = len % BITS_PER_WORD;
|
2013-07-24 11:19:52 +01:00
|
|
|
from -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; len > 0 ; len-- ) {
|
|
|
|
bit_clear( b, from++ );
|
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:22:23 +01:00
|
|
|
/** Counts the number of contiguous bits in array ''b'', starting at ''from''
|
2012-05-29 04:03:28 +01:00
|
|
|
* up to a maximum number of bits ''len''. Returns the number of contiguous
|
2013-08-09 16:49:38 +01:00
|
|
|
* bits that are the same as the first one specified. If ''run_is_set'' is
|
|
|
|
* non-NULL, the value of that bit is placed into it.
|
2012-05-29 04:03:28 +01:00
|
|
|
*/
|
2014-02-26 15:19:03 +00:00
|
|
|
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from, uint64_t len, int *run_is_set) {
|
2013-07-24 12:03:24 +01:00
|
|
|
uint64_t count = 0;
|
2014-02-26 15:19:03 +00:00
|
|
|
int first_value = bit_get(b, from);
|
|
|
|
bitfield_word_t word_match = first_value ? -1 : 0;
|
2012-05-18 13:24:35 +01:00
|
|
|
|
2013-08-09 16:49:38 +01:00
|
|
|
if ( run_is_set != NULL ) {
|
|
|
|
*run_is_set = first_value;
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
for ( ; ((from + count) % BITS_PER_WORD) != 0 && len > 0; len--) {
|
|
|
|
if (bit_has_value(b, from + count, first_value)) {
|
2012-05-18 13:24:35 +01:00
|
|
|
count++;
|
2013-07-24 12:03:24 +01:00
|
|
|
} else {
|
2012-05-18 13:24:35 +01:00
|
|
|
return count;
|
2013-07-24 12:03:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:19:03 +00:00
|
|
|
for ( ; len >= BITS_PER_WORD ; len -= BITS_PER_WORD ) {
|
|
|
|
if (BIT_WORD(b, from + count) == word_match) {
|
|
|
|
count += BITS_PER_WORD;
|
2013-07-24 12:03:24 +01:00
|
|
|
} else {
|
2012-05-18 13:24:35 +01:00
|
|
|
break;
|
2013-07-24 12:03:24 +01:00
|
|
|
}
|
2012-05-18 13:24:35 +01:00
|
|
|
}
|
2013-07-24 12:03:24 +01:00
|
|
|
|
|
|
|
for ( ; len > 0; len-- ) {
|
2014-02-26 15:19:03 +00:00
|
|
|
if ( bit_has_value(b, from + count, first_value) ) {
|
2013-07-23 17:22:23 +01:00
|
|
|
count++;
|
2013-07-24 12:03:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-18 13:24:35 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:30:50 +01:00
|
|
|
enum bitset_stream_events {
|
|
|
|
BITSET_STREAM_UNSET = 0,
|
|
|
|
BITSET_STREAM_SET = 1,
|
|
|
|
BITSET_STREAM_ON = 2,
|
|
|
|
BITSET_STREAM_OFF = 3
|
|
|
|
};
|
2013-09-24 15:27:17 +01:00
|
|
|
#define BITSET_STREAM_EVENTS_ENUM_SIZE 4
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
struct bitset_stream_entry {
|
|
|
|
enum bitset_stream_events event;
|
|
|
|
uint64_t from;
|
|
|
|
uint64_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Limit the stream size to 1MB for now.
|
|
|
|
*
|
|
|
|
* If this is too small, it'll cause requests to stall as the migration lags
|
|
|
|
* behind the changes made by those requests.
|
|
|
|
*/
|
|
|
|
#define BITSET_STREAM_SIZE ( ( 1024 * 1024 ) / sizeof( struct bitset_stream_entry ) )
|
|
|
|
|
|
|
|
struct bitset_stream {
|
|
|
|
struct bitset_stream_entry entries[BITSET_STREAM_SIZE];
|
|
|
|
int in;
|
|
|
|
int out;
|
|
|
|
int size;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond_not_full;
|
|
|
|
pthread_cond_t cond_not_empty;
|
2013-09-24 15:27:17 +01:00
|
|
|
uint64_t queued_bytes[BITSET_STREAM_EVENTS_ENUM_SIZE];
|
2013-09-12 12:30:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-23 17:22:23 +01:00
|
|
|
/** An application of a bitset - a bitset mapping represents a file of ''size''
|
2012-05-29 04:03:28 +01:00
|
|
|
* broken down into ''resolution''-sized chunks. The bit set is assumed to
|
2012-10-07 21:55:01 +01:00
|
|
|
* represent one bit per chunk. We also bundle a lock so that the set can be
|
|
|
|
* written reliably by multiple threads.
|
2012-05-29 04:03:28 +01:00
|
|
|
*/
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset {
|
2012-10-07 21:55:01 +01:00
|
|
|
pthread_mutex_t lock;
|
2012-05-20 14:38:46 +01:00
|
|
|
uint64_t size;
|
|
|
|
int resolution;
|
2013-09-12 12:30:50 +01:00
|
|
|
struct bitset_stream *stream;
|
|
|
|
int stream_enabled;
|
2014-02-26 15:19:03 +00:00
|
|
|
bitfield_word_t bits[];
|
2012-05-20 14:38:46 +01:00
|
|
|
};
|
|
|
|
|
2013-09-23 16:58:40 +01:00
|
|
|
/** Allocate a bitset for a file of the given size, and chunks of the
|
2012-05-29 04:03:28 +01:00
|
|
|
* given resolution.
|
|
|
|
*/
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline struct bitset *bitset_alloc( uint64_t size, int resolution )
|
2012-05-20 14:38:46 +01:00
|
|
|
{
|
2014-02-26 15:19:03 +00:00
|
|
|
// calculate a size to allocate that is a multiple of the size of the
|
|
|
|
// bitfield word
|
|
|
|
size_t bitfield_size =
|
2014-02-27 12:57:09 +00:00
|
|
|
BIT_WORDS_FOR_SIZE((( size + resolution - 1 ) / resolution)) * sizeof( bitfield_word_t );
|
2015-05-13 09:25:09 +01:00
|
|
|
struct bitset *bitset = xmalloc(sizeof( struct bitset ) + ( bitfield_size / 8 ) );
|
2014-02-26 15:19:03 +00:00
|
|
|
|
2012-05-20 14:38:46 +01:00
|
|
|
bitset->size = size;
|
|
|
|
bitset->resolution = resolution;
|
2012-10-07 21:55:01 +01:00
|
|
|
/* don't actually need to call pthread_mutex_destroy '*/
|
|
|
|
pthread_mutex_init(&bitset->lock, NULL);
|
2013-09-12 12:30:50 +01:00
|
|
|
bitset->stream = xmalloc( sizeof( struct bitset_stream ) );
|
|
|
|
pthread_mutex_init( &bitset->stream->mutex, NULL );
|
|
|
|
|
|
|
|
/* Technically don't need to call pthread_cond_destroy either */
|
|
|
|
pthread_cond_init( &bitset->stream->cond_not_full, NULL );
|
|
|
|
pthread_cond_init( &bitset->stream->cond_not_empty, NULL );
|
|
|
|
|
2012-05-20 14:38:46 +01:00
|
|
|
return bitset;
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline void bitset_free( struct bitset * set )
|
2013-09-11 14:41:59 +01:00
|
|
|
{
|
|
|
|
/* TODO: free our mutex... */
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
free( set->stream );
|
|
|
|
set->stream = NULL;
|
|
|
|
|
2013-09-11 14:41:59 +01:00
|
|
|
free( set );
|
|
|
|
}
|
|
|
|
|
2012-05-20 14:38:46 +01:00
|
|
|
#define INT_FIRST_AND_LAST \
|
2013-07-24 10:34:22 +01:00
|
|
|
uint64_t first = from/set->resolution, \
|
2013-07-24 17:42:08 +01:00
|
|
|
last = ((from+len)-1)/set->resolution, \
|
|
|
|
bitlen = (last-first)+1
|
2012-05-20 14:38:46 +01:00
|
|
|
|
2012-10-07 21:55:01 +01:00
|
|
|
#define BITSET_LOCK \
|
|
|
|
FATAL_IF_NEGATIVE(pthread_mutex_lock(&set->lock), "Error locking bitset")
|
|
|
|
|
|
|
|
#define BITSET_UNLOCK \
|
|
|
|
FATAL_IF_NEGATIVE(pthread_mutex_unlock(&set->lock), "Error unlocking bitset")
|
|
|
|
|
|
|
|
|
2013-09-12 12:30:50 +01:00
|
|
|
static inline void bitset_stream_enqueue(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-09-12 12:30:50 +01:00
|
|
|
enum bitset_stream_events event,
|
|
|
|
uint64_t from,
|
|
|
|
uint64_t len
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct bitset_stream * stream = set->stream;
|
|
|
|
|
|
|
|
pthread_mutex_lock( &stream->mutex );
|
|
|
|
|
|
|
|
while ( stream->size == BITSET_STREAM_SIZE ) {
|
|
|
|
pthread_cond_wait( &stream->cond_not_full, &stream->mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->entries[stream->in].event = event;
|
|
|
|
stream->entries[stream->in].from = from;
|
|
|
|
stream->entries[stream->in].len = len;
|
2013-09-24 15:27:17 +01:00
|
|
|
stream->queued_bytes[event] += len;
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
stream->size++;
|
|
|
|
stream->in++;
|
|
|
|
stream->in %= BITSET_STREAM_SIZE;
|
|
|
|
|
|
|
|
pthread_mutex_unlock( & stream->mutex );
|
2013-09-24 15:28:58 +01:00
|
|
|
pthread_cond_signal( &stream->cond_not_empty );
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitset_stream_dequeue(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-09-12 12:30:50 +01:00
|
|
|
struct bitset_stream_entry * out
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct bitset_stream * stream = set->stream;
|
2013-09-24 15:27:17 +01:00
|
|
|
struct bitset_stream_entry * dequeued;
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock( &stream->mutex );
|
|
|
|
|
|
|
|
while ( stream->size == 0 ) {
|
|
|
|
pthread_cond_wait( &stream->cond_not_empty, &stream->mutex );
|
|
|
|
}
|
|
|
|
|
2013-09-24 15:27:17 +01:00
|
|
|
dequeued = &stream->entries[stream->out];
|
|
|
|
|
2013-09-12 12:30:50 +01:00
|
|
|
if ( out != NULL ) {
|
2013-09-24 15:27:17 +01:00
|
|
|
out->event = dequeued->event;
|
|
|
|
out->from = dequeued->from;
|
|
|
|
out->len = dequeued->len;
|
2013-09-12 12:30:50 +01:00
|
|
|
}
|
|
|
|
|
2013-09-24 15:27:17 +01:00
|
|
|
stream->queued_bytes[dequeued->event] -= dequeued->len;
|
2013-09-12 12:30:50 +01:00
|
|
|
stream->size--;
|
|
|
|
stream->out++;
|
|
|
|
stream->out %= BITSET_STREAM_SIZE;
|
|
|
|
|
|
|
|
pthread_mutex_unlock( &stream->mutex );
|
2013-09-24 15:28:58 +01:00
|
|
|
pthread_cond_signal( &stream->cond_not_full );
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-23 17:15:56 +01:00
|
|
|
static inline size_t bitset_stream_size( struct bitset * set )
|
2013-09-12 16:54:42 +01:00
|
|
|
{
|
2013-09-23 17:15:56 +01:00
|
|
|
size_t size;
|
2013-09-12 16:54:42 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock( &set->stream->mutex );
|
|
|
|
size = set->stream->size;
|
|
|
|
pthread_mutex_unlock( &set->stream->mutex );
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t bitset_stream_queued_bytes(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-09-12 16:54:42 +01:00
|
|
|
enum bitset_stream_events event
|
|
|
|
)
|
|
|
|
{
|
2013-09-24 15:27:17 +01:00
|
|
|
uint64_t total;
|
2013-09-12 16:54:42 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock( &set->stream->mutex );
|
2013-09-24 15:27:17 +01:00
|
|
|
total = set->stream->queued_bytes[event];
|
2013-09-12 16:54:42 +01:00
|
|
|
pthread_mutex_unlock( &set->stream->mutex );
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2013-09-23 17:10:14 +01:00
|
|
|
static inline void bitset_enable_stream( struct bitset * set )
|
2013-09-12 12:30:50 +01:00
|
|
|
{
|
|
|
|
BITSET_LOCK;
|
|
|
|
set->stream_enabled = 1;
|
|
|
|
bitset_stream_enqueue( set, BITSET_STREAM_ON, 0, set->size );
|
|
|
|
BITSET_UNLOCK;
|
|
|
|
}
|
|
|
|
|
2013-09-23 17:10:14 +01:00
|
|
|
static inline void bitset_disable_stream( struct bitset * set )
|
2013-09-12 12:30:50 +01:00
|
|
|
{
|
|
|
|
BITSET_LOCK;
|
|
|
|
bitset_stream_enqueue( set, BITSET_STREAM_OFF, 0, set->size );
|
|
|
|
set->stream_enabled = 0;
|
|
|
|
BITSET_UNLOCK;
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Set the bits in a bitset which correspond to the given bytes in the larger
|
|
|
|
* file.
|
|
|
|
*/
|
2012-05-20 14:38:46 +01:00
|
|
|
static inline void bitset_set_range(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-07-23 17:22:23 +01:00
|
|
|
uint64_t from,
|
2012-05-20 14:38:46 +01:00
|
|
|
uint64_t len)
|
|
|
|
{
|
|
|
|
INT_FIRST_AND_LAST;
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_LOCK;
|
2012-05-20 14:38:46 +01:00
|
|
|
bit_set_range(set->bits, first, bitlen);
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
if ( set->stream_enabled ) {
|
|
|
|
bitset_stream_enqueue( set, BITSET_STREAM_SET, from, len );
|
|
|
|
}
|
|
|
|
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_UNLOCK;
|
2012-05-20 14:38:46 +01:00
|
|
|
}
|
|
|
|
|
2012-06-22 10:05:41 +01:00
|
|
|
|
|
|
|
/** Set every bit in the bitset. */
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline void bitset_set( struct bitset * set )
|
2012-06-22 10:05:41 +01:00
|
|
|
{
|
|
|
|
bitset_set_range(set, 0, set->size);
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:22:23 +01:00
|
|
|
/** Clear the bits in a bitset which correspond to the given bytes in the
|
2012-05-29 04:03:28 +01:00
|
|
|
* larger file.
|
|
|
|
*/
|
2012-05-20 14:38:46 +01:00
|
|
|
static inline void bitset_clear_range(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-07-23 17:22:23 +01:00
|
|
|
uint64_t from,
|
2012-05-20 14:38:46 +01:00
|
|
|
uint64_t len)
|
|
|
|
{
|
|
|
|
INT_FIRST_AND_LAST;
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_LOCK;
|
2012-05-20 14:38:46 +01:00
|
|
|
bit_clear_range(set->bits, first, bitlen);
|
2013-09-12 12:30:50 +01:00
|
|
|
|
|
|
|
if ( set->stream_enabled ) {
|
|
|
|
bitset_stream_enqueue( set, BITSET_STREAM_UNSET, from, len );
|
|
|
|
}
|
|
|
|
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_UNLOCK;
|
2012-05-20 14:38:46 +01:00
|
|
|
}
|
|
|
|
|
2012-06-22 10:05:41 +01:00
|
|
|
|
|
|
|
/** Clear every bit in the bitset. */
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline void bitset_clear( struct bitset * set )
|
2012-06-22 10:05:41 +01:00
|
|
|
{
|
|
|
|
bitset_clear_range(set, 0, set->size);
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:49:38 +01:00
|
|
|
/** As per bitset_run_count but also tells you whether the run it found was set
|
|
|
|
* or unset, atomically.
|
2012-05-29 04:03:28 +01:00
|
|
|
*/
|
2013-08-09 16:49:38 +01:00
|
|
|
static inline uint64_t bitset_run_count_ex(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-07-23 17:22:23 +01:00
|
|
|
uint64_t from,
|
2013-08-09 16:49:38 +01:00
|
|
|
uint64_t len,
|
|
|
|
int* run_is_set
|
|
|
|
)
|
2012-05-20 14:38:46 +01:00
|
|
|
{
|
2013-07-24 10:34:22 +01:00
|
|
|
uint64_t run;
|
|
|
|
|
|
|
|
/* Clip our requests to the end of the bitset, avoiding uint underflow. */
|
|
|
|
if ( from > set->size ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = ( len + from ) > set->size ? ( set->size - from ) : len;
|
|
|
|
|
2012-05-20 14:38:46 +01:00
|
|
|
INT_FIRST_AND_LAST;
|
2013-07-24 17:42:08 +01:00
|
|
|
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_LOCK;
|
2013-08-09 16:49:38 +01:00
|
|
|
run = bit_run_count(set->bits, first, bitlen, run_is_set) * set->resolution;
|
2013-07-24 17:42:08 +01:00
|
|
|
run -= (from % set->resolution);
|
2012-10-07 21:55:01 +01:00
|
|
|
BITSET_UNLOCK;
|
2013-07-24 17:42:08 +01:00
|
|
|
|
2012-10-07 21:55:01 +01:00
|
|
|
return run;
|
2012-05-20 14:38:46 +01:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:49:38 +01:00
|
|
|
/** Counts the number of contiguous bytes that are represented as a run in
|
|
|
|
* the bit field.
|
|
|
|
*/
|
|
|
|
static inline uint64_t bitset_run_count(
|
2013-09-23 16:58:40 +01:00
|
|
|
struct bitset * set,
|
2013-08-09 16:49:38 +01:00
|
|
|
uint64_t from,
|
|
|
|
uint64_t len)
|
|
|
|
{
|
|
|
|
return bitset_run_count_ex( set, from, len, NULL );
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Tests whether the bit field is clear for the given file offset.
|
|
|
|
*/
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline int bitset_is_clear_at( struct bitset * set, uint64_t at )
|
2012-05-20 14:38:46 +01:00
|
|
|
{
|
|
|
|
return bit_is_clear(set->bits, at/set->resolution);
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Tests whether the bit field is set for the given file offset.
|
|
|
|
*/
|
2013-09-23 16:58:40 +01:00
|
|
|
static inline int bitset_is_set_at( struct bitset * set, uint64_t at )
|
2012-05-20 14:38:46 +01:00
|
|
|
{
|
|
|
|
return bit_is_set(set->bits, at/set->resolution);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
#endif
|
|
|
|
|